Skip to content

Commit

Permalink
test: add testcases to ensure job containers are started/stopped
Browse files Browse the repository at this point in the history
This commit adds tests to ensure that the executors of `startContainer`,
`stopContainer`, `interpolateOutputs` and `closeContainer` are always
called in the correct order.
  • Loading branch information
ZauberNerd committed Feb 9, 2022
1 parent 98cbcd4 commit 5bba913
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
1 change: 1 addition & 0 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func newJobExecutor(info jobInfo) common.Executor {
return nil
})
}

steps = append(steps, func(ctx context.Context) error {
err := info.stopContainer()(ctx)
if err != nil {
Expand Down
92 changes: 70 additions & 22 deletions pkg/runner/job_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,74 +62,122 @@ func (jpm *jobInfoMock) result(result string) {

func TestNewJobExecutor(t *testing.T) {
table := []struct {
name string
steps []*model.Step
result string
hasError bool
name string
steps []*model.Step
executedSteps []string
result string
hasError bool
}{
{
"zeroSteps",
[]*model.Step{},
"success",
false,
name: "zeroSteps",
steps: []*model.Step{},
executedSteps: []string{
"startContainer",
"stopContainer",
"interpolateOutputs",
"closeContainer",
},
result: "success",
hasError: false,
},
{
"stepWithoutPrePost",
[]*model.Step{{
name: "stepWithoutPrePost",
steps: []*model.Step{{
ID: "1",
}},
"success",
false,
executedSteps: []string{
"startContainer",
"step1",
"stopContainer",
"interpolateOutputs",
"closeContainer",
},
result: "success",
hasError: false,
},
{
"stepWithFailure",
[]*model.Step{{
name: "stepWithFailure",
steps: []*model.Step{{
ID: "1",
}},
"failure",
true,
executedSteps: []string{
"startContainer",
"step1",
"stopContainer",
"interpolateOutputs",
"closeContainer",
},
result: "failure",
hasError: true,
},
{
name: "multipleSteps",
steps: []*model.Step{{
ID: "1",
}, {
ID: "2",
}},
executedSteps: []string{
"startContainer",
"step1",
"step2",
"stopContainer",
"interpolateOutputs",
"closeContainer",
},
result: "success",
hasError: false,
},
}

for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
ctx := common.WithJobErrorContainer(context.Background())
jpm := &jobInfoMock{}
executorOrder := make([]string, 0)

jpm.On("startContainer").Return(func(ctx context.Context) error {
executorOrder = append(executorOrder, "startContainer")
return nil
})

jpm.On("steps").Return(tt.steps)

for _, stepMock := range tt.steps {
jpm.On("newStepExecutor", stepMock).Return(func(ctx context.Context) error {
if tt.hasError {
return fmt.Errorf("error")
}
return nil
})
func(stepMock *model.Step) {
jpm.On("newStepExecutor", stepMock).Return(func(ctx context.Context) error {
executorOrder = append(executorOrder, "step"+stepMock.ID)
if tt.hasError {
return fmt.Errorf("error")
}
return nil
})
}(stepMock)
}

jpm.On("interpolateOutputs").Return(func(ctx context.Context) error {
executorOrder = append(executorOrder, "interpolateOutputs")
return nil
})

jpm.On("matrix").Return(map[string]interface{}{})

jpm.On("stopContainer").Return(func(ctx context.Context) error {
executorOrder = append(executorOrder, "stopContainer")
return nil
})

jpm.On("result", tt.result)

jpm.On("closeContainer").Return(func(ctx context.Context) error {
executorOrder = append(executorOrder, "closeContainer")
return nil
})

executor := newJobExecutor(jpm)
err := executor(ctx)
assert.Nil(t, err)
assert.Equal(t, tt.executedSteps, executorOrder)
})
}
}

0 comments on commit 5bba913

Please sign in to comment.