courgette/internal/runner/runner_flow_test.go

61 lines
1.9 KiB
Go

package runner
import (
"context"
driver "courgette/internal/driver"
logger "courgette/internal/logging"
workflow "courgette/internal/workflow"
"errors"
"fmt"
"testing"
)
func init() {
logger.ConfigureLogger()
}
func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) {
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 0, Error: errors.New("test")}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
runner := Runner{
Driver: &mockDriver,
}
err := runner.RunCommandInContainer("test-container", "test-command", driver.NewCommandOptions())
if err == nil {
t.Errorf("Expected error, got nil.")
}
}
func TestRunnerRunCommandInContainerReturnsErrorIfCommandExitCodeNonzero(t *testing.T) {
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 1, Error: nil}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
runner := Runner{
Driver: &mockDriver,
}
err := runner.RunCommandInContainer("test-container", "test-command", driver.NewCommandOptions())
if err == nil {
t.Errorf("Expected error, got nil.")
}
}
func TestRunJobInContainerSchedulesStoppingContainers(t *testing.T) {
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 1, Error: nil}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
runner := NewRunner(&mockDriver, map[string]string{}, t.TempDir())
jobCtx := context.WithValue(context.Background(), "currentJob", workflow.Job{})
jobCtx = context.WithValue(jobCtx, "workflow", workflow.Workflow{})
runner.RunJobInContainer("uri", "container", jobCtx)
if len(runner.Deferred.GetAllTasks()) != 1 {
t.Errorf("Expected 1 deferred task, found %d instead.", len(runner.Deferred.GetAllTasks()))
}
}