180 lines
5 KiB
Go
180 lines
5 KiB
Go
package runner
|
|
|
|
import (
|
|
"context"
|
|
workflow "courgette/internal/workflow"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestJobErrorsWorkflowErrorsIfJobContinueOnErrorUndefined(t *testing.T) {
|
|
workflowSample := `
|
|
jobs:
|
|
jobA:
|
|
runs-on: test
|
|
steps:
|
|
- run: exit 1
|
|
`
|
|
|
|
workflow, _ := workflow.FromYamlBytes([]byte(workflowSample))
|
|
mockDriver := NewMockDriver()
|
|
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
|
|
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
|
|
|
|
mockDriver.WithNthMockedCall("Exec", 1, 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 := NewMockDriver()
|
|
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
jobContext := context.WithValue(context.Background(), "workflow", *workflow)
|
|
jobContext = context.WithValue(jobContext, "currentJob", workflow.Jobs["jobA"])
|
|
|
|
mockDriver.WithNthMockedCall("Exec", 1, 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 := NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1")
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
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 := NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1")
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
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 := NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", ".")
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
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 := NewMockDriver()
|
|
|
|
mockDriver.WithMockedCall("Exec", CommandResult{Error: errors.New("exit 1!"), ExitCode: 1}, "testContainer", "exit 1", ".")
|
|
runner := NewRunner(&mockDriver, map[string]string{"test": "test"})
|
|
|
|
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)
|
|
}
|
|
}
|