2019-04-11 15:23:59 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-07 21:56:10 +00:00
|
|
|
package runservice
|
2019-04-11 15:23:59 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2019-06-12 16:12:37 +00:00
|
|
|
"time"
|
2019-04-11 15:23:59 +00:00
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/common"
|
|
|
|
"agola.io/agola/internal/services/runservice/types"
|
2019-04-11 15:23:59 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAdvanceRunTasks(t *testing.T) {
|
|
|
|
// a global run config for all tests
|
|
|
|
rc := &types.RunConfig{
|
|
|
|
Tasks: map[string]*types.RunConfigTask{
|
|
|
|
"task01": &types.RunConfigTask{
|
|
|
|
ID: "task01",
|
|
|
|
Name: "task01",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task02": &types.RunConfigTask{
|
|
|
|
ID: "task02",
|
|
|
|
Name: "task02",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{
|
2019-04-12 14:46:04 +00:00
|
|
|
"task01": &types.RunConfigTaskDepend{TaskID: "task01", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
2019-04-11 15:23:59 +00:00
|
|
|
},
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task03": &types.RunConfigTask{
|
|
|
|
ID: "task03",
|
|
|
|
Name: "task03",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task04": &types.RunConfigTask{
|
|
|
|
ID: "task04",
|
|
|
|
Name: "task04",
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task05": &types.RunConfigTask{
|
|
|
|
ID: "task05",
|
|
|
|
Name: "task05",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{
|
2019-04-12 14:46:04 +00:00
|
|
|
"task03": &types.RunConfigTaskDepend{TaskID: "task03", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
|
|
|
"task04": &types.RunConfigTaskDepend{TaskID: "task04", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
2019-04-11 15:23:59 +00:00
|
|
|
},
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-17 16:00:34 +00:00
|
|
|
// initial run that matched the runconfig:
|
|
|
|
// * the run is in phase running with result unknown
|
|
|
|
// * all tasks are not started or skipped
|
2019-04-11 15:23:59 +00:00
|
|
|
// (if the runconfig task as Skip == true). This must match the status
|
|
|
|
// generated by command.genRun()
|
|
|
|
run := &types.Run{
|
2019-04-17 16:00:34 +00:00
|
|
|
Phase: types.RunPhaseRunning,
|
|
|
|
Result: types.RunResultUnknown,
|
2019-04-12 15:45:38 +00:00
|
|
|
Tasks: map[string]*types.RunTask{
|
2019-04-11 15:23:59 +00:00
|
|
|
"task01": &types.RunTask{
|
|
|
|
ID: "task01",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task02": &types.RunTask{
|
|
|
|
ID: "task02",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task03": &types.RunTask{
|
|
|
|
ID: "task03",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task04": &types.RunTask{
|
|
|
|
ID: "task04",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task05": &types.RunTask{
|
|
|
|
ID: "task05",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2019-04-17 16:00:34 +00:00
|
|
|
name string
|
|
|
|
rc *types.RunConfig
|
|
|
|
r *types.Run
|
|
|
|
activeExecutorTasks []*types.ExecutorTask
|
|
|
|
out *types.Run
|
|
|
|
err error
|
2019-04-11 15:23:59 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test top level task not started",
|
|
|
|
rc: rc,
|
|
|
|
r: run.DeepCopy(),
|
|
|
|
out: run.DeepCopy(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task status set to skipped when parent status is skipped",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task01"].Skip = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task01"].Status = types.RunTaskStatusSkipped
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task01"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task02"].Status = types.RunTaskStatusSkipped
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task status set to skipped when all parent status is skipped",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
rc.Tasks["task04"].Skip = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSkipped
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusSkipped
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
2019-04-12 14:46:04 +00:00
|
|
|
name: "test task set to skipped when only some parents status is skipped",
|
2019-04-11 15:23:59 +00:00
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusSkipped
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task set to skipped when one of the parents doesn't match default conditions (on_success)",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusSkipped
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task set to skipped when one of the parents doesn't match custom conditions",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
rc.Tasks["task05"].Depends["task03"].Conditions = []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnFailure}
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusSkipped
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task set to not skipped when one of the parent is skipped and task condition is on_skipped",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
rc.Tasks["task05"].Depends["task03"].Conditions = []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSkipped}
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task not set to waiting approval when task is skipped",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
rc.Tasks["task05"].NeedsApproval = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusSkipped
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test task set to waiting approval when all the parents are finished and task is not skipped",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task03"].Skip = true
|
|
|
|
rc.Tasks["task05"].NeedsApproval = true
|
|
|
|
rc.Tasks["task05"].Depends["task03"].Conditions = []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSkipped}
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
2019-04-12 14:46:04 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].WaitingApproval = true
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
2019-04-17 16:00:34 +00:00
|
|
|
{
|
|
|
|
name: "cancel all root not started tasks when run has a result set",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
|
|
|
run.Result = types.RunResultSuccess
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
|
|
|
run.Result = types.RunResultSuccess
|
|
|
|
run.Tasks["task01"].Status = types.RunTaskStatusCancelled
|
|
|
|
run.Tasks["task02"].Status = types.RunTaskStatusNotStarted
|
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusCancelled
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusNotStarted
|
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cancel all root not started tasks when run has a result set (task01 is already scheduled)",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
|
|
|
run.Result = types.RunResultSuccess
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
activeExecutorTasks: []*types.ExecutorTask{
|
|
|
|
&types.ExecutorTask{ID: "task01"},
|
|
|
|
},
|
|
|
|
out: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
|
|
|
run.Result = types.RunResultSuccess
|
|
|
|
run.Tasks["task01"].Status = types.RunTaskStatusNotStarted
|
|
|
|
run.Tasks["task02"].Status = types.RunTaskStatusNotStarted
|
|
|
|
run.Tasks["task03"].Status = types.RunTaskStatusCancelled
|
|
|
|
run.Tasks["task04"].Status = types.RunTaskStatusSuccess
|
|
|
|
run.Tasks["task05"].Status = types.RunTaskStatusNotStarted
|
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
},
|
2019-04-11 15:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2019-04-17 16:00:34 +00:00
|
|
|
r, err := advanceRunTasks(ctx, tt.r, tt.rc, tt.activeExecutorTasks)
|
2019-04-17 16:06:31 +00:00
|
|
|
if err != nil {
|
2019-04-11 15:23:59 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2019-04-17 16:06:31 +00:00
|
|
|
if diff := cmp.Diff(tt.out, r); diff != "" {
|
2019-04-11 15:23:59 +00:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTasksToRun(t *testing.T) {
|
|
|
|
// a global run config for all tests
|
|
|
|
rc := &types.RunConfig{
|
|
|
|
Tasks: map[string]*types.RunConfigTask{
|
|
|
|
"task01": &types.RunConfigTask{
|
|
|
|
ID: "task01",
|
|
|
|
Name: "task01",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task02": &types.RunConfigTask{
|
|
|
|
ID: "task02",
|
|
|
|
Name: "task02",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{
|
2019-04-12 14:46:04 +00:00
|
|
|
"task01": &types.RunConfigTaskDepend{TaskID: "task01", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
2019-04-11 15:23:59 +00:00
|
|
|
},
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task03": &types.RunConfigTask{
|
|
|
|
ID: "task03",
|
|
|
|
Name: "task03",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task04": &types.RunConfigTask{
|
|
|
|
ID: "task04",
|
|
|
|
Name: "task04",
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
"task05": &types.RunConfigTask{
|
|
|
|
ID: "task05",
|
|
|
|
Name: "task05",
|
2019-04-12 15:04:07 +00:00
|
|
|
Depends: map[string]*types.RunConfigTaskDepend{
|
2019-04-12 14:46:04 +00:00
|
|
|
"task03": &types.RunConfigTaskDepend{TaskID: "task03", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
|
|
|
"task04": &types.RunConfigTaskDepend{TaskID: "task04", Conditions: []types.RunConfigTaskDependCondition{types.RunConfigTaskDependConditionOnSuccess}},
|
2019-04-11 15:23:59 +00:00
|
|
|
},
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Containers: []*types.Container{{Image: "image01"}},
|
|
|
|
},
|
|
|
|
Environment: map[string]string{},
|
2019-06-08 14:30:36 +00:00
|
|
|
Steps: types.Steps{},
|
2019-04-11 15:23:59 +00:00
|
|
|
Skip: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// initial run that matched the runconfig, all tasks are not started or skipped
|
|
|
|
// (if the runconfig task as Skip == true). This must match the status
|
|
|
|
// generated by command.genRun()
|
|
|
|
run := &types.Run{
|
2019-04-12 15:45:38 +00:00
|
|
|
Tasks: map[string]*types.RunTask{
|
2019-04-11 15:23:59 +00:00
|
|
|
"task01": &types.RunTask{
|
|
|
|
ID: "task01",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task02": &types.RunTask{
|
|
|
|
ID: "task02",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task03": &types.RunTask{
|
|
|
|
ID: "task03",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task04": &types.RunTask{
|
|
|
|
ID: "task04",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
"task05": &types.RunTask{
|
|
|
|
ID: "task05",
|
|
|
|
Status: types.RunTaskStatusNotStarted,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
rc *types.RunConfig
|
|
|
|
r *types.Run
|
|
|
|
out []string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test run top level tasks",
|
|
|
|
rc: rc,
|
|
|
|
r: run.DeepCopy(),
|
|
|
|
out: []string{"task01", "task03", "task04"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test don't run skipped tasks",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task01"].Skip = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task01"].Status = types.RunTaskStatusSkipped
|
|
|
|
run.Tasks["task02"].Status = types.RunTaskStatusSkipped
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: []string{"task03", "task04"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test don't run if needs approval but not approved",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task01"].NeedsApproval = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: run.DeepCopy(),
|
|
|
|
out: []string{"task03", "task04"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test run if needs approval and approved",
|
|
|
|
rc: func() *types.RunConfig {
|
|
|
|
rc := rc.DeepCopy()
|
|
|
|
rc.Tasks["task01"].NeedsApproval = true
|
|
|
|
return rc
|
|
|
|
}(),
|
|
|
|
r: func() *types.Run {
|
|
|
|
run := run.DeepCopy()
|
2019-04-12 15:45:38 +00:00
|
|
|
run.Tasks["task01"].Approved = true
|
2019-04-11 15:23:59 +00:00
|
|
|
return run
|
|
|
|
}(),
|
|
|
|
out: []string{"task01", "task03", "task04"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
tasks, err := getTasksToRun(ctx, tt.r, tt.rc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
outTasks := []string{}
|
|
|
|
for _, t := range tasks {
|
|
|
|
outTasks = append(outTasks, t.ID)
|
|
|
|
}
|
|
|
|
sort.Sort(sort.StringSlice(tt.out))
|
|
|
|
sort.Sort(sort.StringSlice(outTasks))
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.out, outTasks); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 16:12:37 +00:00
|
|
|
|
|
|
|
func TestChooseExecutor(t *testing.T) {
|
|
|
|
executorOK := &types.Executor{
|
|
|
|
ID: "executorOK",
|
|
|
|
Archs: []common.Arch{common.ArchAMD64},
|
|
|
|
ActiveTasksLimit: 2,
|
|
|
|
ActiveTasks: 0,
|
|
|
|
LastStatusUpdateTime: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
executorNoFreeTaskSlots := func() *types.Executor {
|
|
|
|
e := executorOK.DeepCopy()
|
|
|
|
e.ID = "executorNoFreeTasksSlots"
|
|
|
|
e.ActiveTasks = 2
|
|
|
|
return e
|
|
|
|
}()
|
|
|
|
|
|
|
|
executorNotAlive := func() *types.Executor {
|
|
|
|
e := executorOK.DeepCopy()
|
|
|
|
e.ID = "executorNotAlive"
|
|
|
|
e.LastStatusUpdateTime = time.Now().Add(-120 * time.Second)
|
|
|
|
return e
|
|
|
|
}()
|
|
|
|
|
|
|
|
executorOKMultipleArchs := func() *types.Executor {
|
|
|
|
e := executorOK.DeepCopy()
|
|
|
|
e.ID = "executorOKMultipleArchs"
|
|
|
|
e.Archs = []common.Arch{common.ArchAMD64, common.ArchARM64}
|
|
|
|
return e
|
|
|
|
}()
|
|
|
|
|
2019-06-13 16:32:56 +00:00
|
|
|
executorOKAllowsPriviledContainers := func() *types.Executor {
|
|
|
|
e := executorOK.DeepCopy()
|
|
|
|
e.ID = "executorOKAllowsPrivilegedContainers"
|
|
|
|
e.AllowPrivilegedContainers = true
|
|
|
|
return e
|
|
|
|
}()
|
|
|
|
|
2019-06-12 16:12:37 +00:00
|
|
|
// Only primary and the required variables for this test are set
|
|
|
|
rct := &types.RunConfigTask{
|
|
|
|
ID: "task01",
|
|
|
|
Name: "task01",
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Arch: common.ArchAMD64,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:32:56 +00:00
|
|
|
rctWithPrivilegedContainers := &types.RunConfigTask{
|
|
|
|
ID: "task01",
|
|
|
|
Name: "task01",
|
|
|
|
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
|
|
|
|
Arch: common.ArchAMD64,
|
|
|
|
Containers: []*types.Container{
|
|
|
|
{
|
|
|
|
Privileged: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-12 16:12:37 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
executors []*types.Executor
|
|
|
|
rct *types.RunConfigTask
|
|
|
|
out *types.Executor
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test single executor ok",
|
|
|
|
executors: []*types.Executor{executorOK},
|
|
|
|
// Only primary and the required variables for this test are set
|
|
|
|
rct: rct,
|
|
|
|
out: executorOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test single executor without free task slots",
|
|
|
|
executors: []*types.Executor{executorNoFreeTaskSlots},
|
|
|
|
// Only primary and the required variables for this test are set
|
|
|
|
rct: rct,
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test single executor not alive",
|
|
|
|
executors: []*types.Executor{executorNotAlive},
|
|
|
|
rct: rct,
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test single executor with different arch",
|
|
|
|
executors: func() []*types.Executor {
|
|
|
|
e := executorOK.DeepCopy()
|
|
|
|
e.Archs = []common.Arch{common.ArchARM64}
|
|
|
|
return []*types.Executor{e}
|
|
|
|
}(),
|
|
|
|
rct: rct,
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test single executor with multiple archs and one matches the task required arch",
|
|
|
|
executors: []*types.Executor{executorOKMultipleArchs},
|
|
|
|
rct: rct,
|
|
|
|
out: executorOKMultipleArchs,
|
|
|
|
},
|
2019-06-13 16:32:56 +00:00
|
|
|
{
|
|
|
|
name: "test single executor without allowed privileged container but privileged containers are required",
|
|
|
|
executors: []*types.Executor{executorOK},
|
|
|
|
rct: rctWithPrivilegedContainers,
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test single executor with allowed privileged container and privileged containers are required",
|
|
|
|
executors: []*types.Executor{executorOKAllowsPriviledContainers},
|
|
|
|
rct: rctWithPrivilegedContainers,
|
|
|
|
out: executorOKAllowsPriviledContainers,
|
|
|
|
},
|
2019-06-12 16:12:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
e := chooseExecutor(tt.executors, tt.rct)
|
|
|
|
if e == nil && tt.out == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e == nil && tt.out != nil {
|
|
|
|
t.Fatalf("expected executor with id: %s, go no executor selected", tt.out.ID)
|
|
|
|
}
|
|
|
|
if e != nil && tt.out == nil {
|
|
|
|
t.Fatalf("expected no executor selected, got executor with id: %s", e.ID)
|
|
|
|
}
|
|
|
|
if e != tt.out {
|
|
|
|
t.Fatalf("wrong executor ID, expected %s, got: %s", tt.out.ID, e.ID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|