60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package runner
|
|
|
|
import (
|
|
"context"
|
|
logger "courgette/internal/logging"
|
|
workflow "courgette/internal/workflow"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func init() {
|
|
logger.ConfigureLogger()
|
|
}
|
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithMockedCall("Exec", 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", ".", map[string]string{})
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected error, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorIfCommandExitCodeNonzero(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithMockedCall("Exec", 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", ".", map[string]string{})
|
|
|
|
if err == nil {
|
|
t.Errorf("Expected error, got nil.")
|
|
}
|
|
}
|
|
|
|
func TestRunJobInContainerSchedulesStoppingContainers(t *testing.T) {
|
|
mockDriver := NewMockDriver()
|
|
mockDriver.WithMockedCall("Exec", CommandResult{ExitCode: 1, Error: nil}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
|
|
|
|
runner := NewRunner(&mockDriver, map[string]string{})
|
|
|
|
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()))
|
|
}
|
|
}
|