courgette/internal/runner/runner_job_test.go

182 lines
5.4 KiB
Go

package runner
import (
"context"
driver "courgette/internal/driver"
workflow "courgette/internal/workflow"
"errors"
"fmt"
"testing"
)
func TestJobErrorsWorkflowErrorsIfJobContinueOnErrorUndefined(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
steps:
- run: exit 1
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
mockDriver.WithNthMockedCall("Exec", 1, driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1})
task := runner.RunWorkflow(*workflow)
if !task.Failed() {
t.Error("Expected task to fail, but it succeeded.")
}
}
func TestJobErrorsWorkflowErrorsIfJobContinueOnErrorFalsy(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
continue-on-error: false
steps:
- run: exit 1
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
mockDriver.WithNthMockedCall("Exec", 1, driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1})
task := runner.RunWorkflow(*workflow)
if !task.Failed() {
t.Error("Expected task to fail, but it succeeded.")
}
}
func TestJobSkipsErrorWorkflowSucceedsIfContinueOnErrorTruthy(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
continue-on-error: true
steps:
- run: exit 1
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", fmt.Sprintf("%#v", map[string]string{}))
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
task := runner.RunWorkflow(*workflow)
if task.Failed() {
t.Error("Expected task to succeed, but it failed.")
}
}
func TestJobStepSkipsErrorIfContinueOnErrorTruthy(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
steps:
- run: exit 1
continue-on-error: true
- run: echo "Continued!"
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", fmt.Sprintf("%#v", map[string]string{}))
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
runErr := runner.RunJobInContainer("testUri", "testContainer", jobContext)
if runErr != nil {
t.Errorf("Did not expect error, got %+v", runErr)
}
execCallCount := len(mockDriver.Calls["Exec"])
if execCallCount != 2 {
t.Errorf("Expected 2 calls to Exec, got %d", execCallCount)
}
}
func TestJobStepExitsOnErrorIfContinueOnErrorFalsy(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
steps:
- run: exit 1
continue-on-error: false
- run: echo "Continued!"
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", ".", fmt.Sprintf("%#v", map[string]string{}))
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
runErr := runner.RunJobInContainer("testUri", "testContainer", jobContext)
if runErr == nil {
t.Error("Expected error, got nil")
}
execCallCount := len(mockDriver.Calls["Exec"])
if execCallCount != 1 {
t.Errorf("Expected 1 calls to Exec, got %d", execCallCount)
}
}
func TestJobStepExitsOnErrorIfContinueOnErrorUndefined(t *testing.T) {
workflowSample := `
jobs:
jobA:
runs-on: test
steps:
- run: exit 1
- run: echo "Continued!"
`
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
mockDriver := driver.NewMockDriver()
mockDriver.WithMockedCall("Exec", driver.CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", ".", fmt.Sprintf("%#v", map[string]string{}))
runner := NewRunner(&mockDriver, map[string]string{"test": "test"}, t.TempDir())
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
runErr := runner.RunJobInContainer("testUri", "testContainer", jobContext)
if runErr == nil {
t.Error("Expect error, got nil")
}
execCallCount := len(mockDriver.Calls["Exec"])
if execCallCount != 1 {
t.Errorf("Expected 1 calls to Exec, got %d", execCallCount)
}
}