courgette/internal/runner/runner_flow_test.go

62 lines
1.9 KiB
Go
Raw Normal View History

package runner
import (
"context"
driver "courgette/internal/driver"
logger "courgette/internal/logging"
2024-08-02 23:49:56 +00:00
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{}))
2024-08-31 02:20:56 +00:00
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()))
}
}