103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package runner
|
|
|
|
import (
|
|
"context"
|
|
workflow "courgette/internal/workflow"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
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)
|
|
}
|
|
}
|