2024-08-01 22:43:18 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2024-08-15 23:13:33 +00:00
|
|
|
"context"
|
2024-08-23 03:11:00 +00:00
|
|
|
driver "courgette/internal/driver"
|
2024-08-03 17:47:51 +00:00
|
|
|
logger "courgette/internal/logging"
|
2024-08-02 23:49:56 +00:00
|
|
|
workflow "courgette/internal/workflow"
|
2024-08-02 20:38:18 +00:00
|
|
|
"errors"
|
2024-08-21 03:35:49 +00:00
|
|
|
"fmt"
|
2024-08-01 22:43:18 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-08-03 17:47:51 +00:00
|
|
|
func init() {
|
|
|
|
logger.ConfigureLogger()
|
|
|
|
}
|
|
|
|
|
2024-08-02 20:38:18 +00:00
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) {
|
2024-08-23 03:11:00 +00:00
|
|
|
mockDriver := driver.NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 0, Error: errors.New("test")}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
|
2024-08-02 20:38:18 +00:00
|
|
|
|
|
|
|
runner := Runner{
|
|
|
|
Driver: &mockDriver,
|
|
|
|
}
|
|
|
|
|
2024-08-23 03:38:32 +00:00
|
|
|
err := runner.RunCommandInContainer("test-container", "test-command", driver.NewCommandOptions())
|
2024-08-02 20:38:18 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error, got nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunnerRunCommandInContainerReturnsErrorIfCommandExitCodeNonzero(t *testing.T) {
|
2024-08-23 03:11:00 +00:00
|
|
|
mockDriver := driver.NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 1, Error: nil}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
|
2024-08-02 20:38:18 +00:00
|
|
|
|
|
|
|
runner := Runner{
|
|
|
|
Driver: &mockDriver,
|
|
|
|
}
|
|
|
|
|
2024-08-23 03:38:32 +00:00
|
|
|
err := runner.RunCommandInContainer("test-container", "test-command", driver.NewCommandOptions())
|
2024-08-02 20:38:18 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error, got nil.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 23:30:37 +00:00
|
|
|
func TestRunJobInContainerSchedulesStoppingContainers(t *testing.T) {
|
2024-08-23 03:11:00 +00:00
|
|
|
mockDriver := driver.NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", driver.CommandResult{ExitCode: 1, Error: nil}, "test-container", "test-command", ".", fmt.Sprintf("%#v", map[string]string{}))
|
2024-08-02 20:38:18 +00:00
|
|
|
|
2024-08-31 02:20:56 +00:00
|
|
|
runner := NewRunner(&mockDriver, map[string]string{}, t.TempDir())
|
2024-08-02 20:38:18 +00:00
|
|
|
|
2024-08-15 23:13:33 +00:00
|
|
|
jobCtx := context.WithValue(context.Background(), "currentJob", workflow.Job{})
|
|
|
|
jobCtx = context.WithValue(jobCtx, "workflow", workflow.Workflow{})
|
|
|
|
|
|
|
|
runner.RunJobInContainer("uri", "container", jobCtx)
|
2024-08-02 20:38:18 +00:00
|
|
|
|
2024-09-03 02:55:29 +00:00
|
|
|
if len(runner.Deferred.GetAllTasks()) != 1 {
|
|
|
|
t.Errorf("Expected 1 deferred task, found %d instead.", len(runner.Deferred.GetAllTasks()))
|
2024-08-02 20:38:18 +00:00
|
|
|
}
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|