115 lines
2.7 KiB
Go
115 lines
2.7 KiB
Go
package runner
|
|
|
|
import (
|
|
logger "courgette/internal/logging"
|
|
workflow "courgette/internal/workflow"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
type MockCall struct {
|
|
fname string
|
|
args []string
|
|
}
|
|
|
|
type MockDriver struct {
|
|
calls map[string][]MockCall
|
|
mockResult *CommandResult
|
|
}
|
|
|
|
func init() {
|
|
logger.ConfigureLogger()
|
|
}
|
|
|
|
func NewMockDriver() MockDriver {
|
|
return MockDriver{
|
|
calls: map[string][]MockCall{},
|
|
}
|
|
}
|
|
|
|
func (d *MockDriver) WithCommandResult(c *CommandResult) {
|
|
d.mockResult = c
|
|
}
|
|
|
|
func (d *MockDriver) Pull(uri string) error {
|
|
if _, init := d.calls["Pull"]; !init {
|
|
d.calls["Pull"] = []MockCall{}
|
|
}
|
|
|
|
d.calls["Pull"] = append(d.calls["Pull"], MockCall{fname: "Pull", args: []string{uri}})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *MockDriver) Start(uri string, containerName string) error {
|
|
if _, init := d.calls["Start"]; !init {
|
|
d.calls["Start"] = []MockCall{}
|
|
}
|
|
d.calls["Start"] = append(d.calls["Start"], MockCall{fname: "Start", args: []string{uri, containerName}})
|
|
return nil
|
|
}
|
|
|
|
func (d *MockDriver) Stop(uri string) error {
|
|
if _, init := d.calls["Stop"]; !init {
|
|
d.calls["Stop"] = []MockCall{}
|
|
}
|
|
d.calls["Stop"] = append(d.calls["Stop"], MockCall{fname: "Stop", args: []string{uri}})
|
|
return nil
|
|
|
|
}
|
|
|
|
func (d *MockDriver) Exec(containerName string, command string) CommandResult {
|
|
if _, init := d.calls["Exec"]; !init {
|
|
d.calls["Exec"] = []MockCall{}
|
|
}
|
|
d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: []string{containerName, command}})
|
|
|
|
if d.mockResult != nil {
|
|
return *d.mockResult
|
|
}
|
|
|
|
return CommandResult{}
|
|
}
|
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithCommandResult(&CommandResult{ExitCode: 0, Error: errors.New("test")})
|
|
|
|
runner := Runner{
|
|
Driver: &mockDriver,
|
|
}
|
|
|
|
err := runner.RunCommandInContainer("test-container", "test-command")
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected error, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorIfCommandExitCodeNonzero(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithCommandResult(&CommandResult{ExitCode: 1, Error: nil})
|
|
|
|
runner := Runner{
|
|
Driver: &mockDriver,
|
|
}
|
|
|
|
err := runner.RunCommandInContainer("test-container", "test-command")
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected error, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestRunJobInContainerSchedulesStoppingContainers(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithCommandResult(&CommandResult{ExitCode: 1, Error: nil})
|
|
|
|
runner := NewRunner(&mockDriver, map[string]string{})
|
|
|
|
runner.RunJobInContainer("uri", "container", workflow.Job{})
|
|
|
|
if len(runner.deferred.GetAllTasks()) != 1 {
|
|
t.Errorf("Expected 1 deferred task, found %d instead.", len(runner.deferred.GetAllTasks()))
|
|
}
|
|
}
|