runservice: convert RunConfigTask.Depends to a map

This commit is contained in:
Simone Gotti 2019-04-12 17:04:07 +02:00
parent 991fcc59de
commit 5165984030
5 changed files with 107 additions and 184 deletions

View File

@ -197,8 +197,8 @@ func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, pipelineName s
for _, rct := range rcts {
cpe := cp.Elements[rct.Name]
depends := make([]*rstypes.RunConfigTaskDepend, len(cpe.Depends))
for id, d := range cpe.Depends {
depends := make(map[string]*rstypes.RunConfigTaskDepend, len(cpe.Depends))
for _, d := range cpe.Depends {
conditions := make([]rstypes.RunConfigTaskDependCondition, len(d.Conditions))
// when no conditions are defined default to on_success
if len(d.Conditions) == 0 {
@ -217,7 +217,7 @@ func GenRunConfigTasks(uuid util.UUIDGenerator, c *config.Config, pipelineName s
}
drct := getRunConfigTaskByName(rcts, d.ElementName)
depends[id] = &rstypes.RunConfigTaskDepend{
depends[drct.ID] = &rstypes.RunConfigTaskDepend{
TaskID: drct.ID,
Conditions: conditions,
}
@ -331,13 +331,7 @@ func GenTasksLevels(rcts map[string]*rstypes.RunConfigTask) error {
func GetParents(rcts map[string]*rstypes.RunConfigTask, task *rstypes.RunConfigTask) []*rstypes.RunConfigTask {
parents := []*rstypes.RunConfigTask{}
for _, t := range rcts {
isParent := false
for _, d := range task.Depends {
if d.TaskID == t.ID {
isParent = true
}
}
if isParent {
if _, ok := task.Depends[t.ID]; ok {
parents = append(parents, t)
}
}
@ -387,5 +381,4 @@ func genValue(val config.Value, variables map[string]string) string {
default:
panic(fmt.Errorf("wrong value type: %q", val.Value))
}
return ""
}

View File

@ -33,7 +33,7 @@ func TestGenTasksLevels(t *testing.T) {
type task struct {
ID string
Level int
Depends []*rstypes.RunConfigTaskDepend
Depends map[string]*rstypes.RunConfigTaskDepend
}
tests := []struct {
name string
@ -89,10 +89,8 @@ func TestGenTasksLevels(t *testing.T) {
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -104,10 +102,8 @@ func TestGenTasksLevels(t *testing.T) {
{
ID: "2",
Level: 1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -118,19 +114,15 @@ func TestGenTasksLevels(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -142,28 +134,22 @@ func TestGenTasksLevels(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -175,28 +161,22 @@ func TestGenTasksLevels(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
},
@ -243,7 +223,7 @@ func TestGetAllParents(t *testing.T) {
type task struct {
ID string
Level int
Depends []*rstypes.RunConfigTaskDepend
Depends map[string]*rstypes.RunConfigTaskDepend
}
tests := []struct {
name string
@ -285,10 +265,8 @@ func TestGetAllParents(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -306,10 +284,8 @@ func TestGetAllParents(t *testing.T) {
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -324,31 +300,23 @@ func TestGetAllParents(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "4",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"4": &rstypes.RunConfigTaskDepend{TaskID: "4"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "5",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"5": &rstypes.RunConfigTaskDepend{TaskID: "5"},
},
},
{
@ -374,19 +342,15 @@ func TestGetAllParents(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -401,28 +365,22 @@ func TestGetAllParents(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -438,28 +396,22 @@ func TestGetAllParents(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
},
@ -502,7 +454,7 @@ func TestCheckRunConfig(t *testing.T) {
type task struct {
ID string
Level int
Depends []*rstypes.RunConfigTaskDepend
Depends map[string]*rstypes.RunConfigTaskDepend
}
tests := []struct {
name string
@ -541,10 +493,8 @@ func TestCheckRunConfig(t *testing.T) {
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -555,19 +505,15 @@ func TestCheckRunConfig(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -584,28 +530,22 @@ func TestCheckRunConfig(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "1",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"1": &rstypes.RunConfigTaskDepend{TaskID: "1"},
},
},
},
@ -623,28 +563,22 @@ func TestCheckRunConfig(t *testing.T) {
{
ID: "1",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
{
ID: "2",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "3",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"3": &rstypes.RunConfigTaskDepend{TaskID: "3"},
},
},
{
ID: "3",
Level: -1,
Depends: []*rstypes.RunConfigTaskDepend{
&rstypes.RunConfigTaskDepend{
TaskID: "2",
},
Depends: map[string]*rstypes.RunConfigTaskDepend{
"2": &rstypes.RunConfigTaskDepend{TaskID: "2"},
},
},
},
@ -795,7 +729,7 @@ func TestGenRunConfig(t *testing.T) {
out: map[string]*rstypes.RunConfigTask{
uuid.New("element01").String(): &rstypes.RunConfigTask{
ID: uuid.New("element01").String(),
Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{},
Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{},
Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"),
Containers: []*rstypes.Container{
{
@ -879,7 +813,7 @@ func TestGenRunConfig(t *testing.T) {
out: map[string]*rstypes.RunConfigTask{
uuid.New("element01").String(): &rstypes.RunConfigTask{
ID: uuid.New("element01").String(),
Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{},
Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{},
Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"),
Containers: []*rstypes.Container{
{
@ -959,7 +893,7 @@ func TestGenRunConfig(t *testing.T) {
out: map[string]*rstypes.RunConfigTask{
uuid.New("element01").String(): &rstypes.RunConfigTask{
ID: uuid.New("element01").String(),
Name: "element01", Depends: []*rstypes.RunConfigTaskDepend{},
Name: "element01", Depends: map[string]*rstypes.RunConfigTaskDepend{},
Runtime: &rstypes.Runtime{Type: rstypes.RuntimeType("pod"),
Containers: []*rstypes.Container{
{

View File

@ -71,7 +71,7 @@ type RunResponseTask struct {
Name string `json:"name"`
Status rstypes.RunTaskStatus `json:"status"`
Level int `json:"level"`
Depends []*rstypes.RunConfigTaskDepend `json:"depends"`
Depends map[string]*rstypes.RunConfigTaskDepend `json:"depends"`
WaitingApproval bool `json:"waiting_approval"`
Approved bool `json:"approved"`

View File

@ -30,7 +30,7 @@ func TestAdvanceRunTasks(t *testing.T) {
"task01": &types.RunConfigTask{
ID: "task01",
Name: "task01",
Depends: []*types.RunConfigTaskDepend{},
Depends: map[string]*types.RunConfigTaskDepend{},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
},
@ -41,10 +41,8 @@ func TestAdvanceRunTasks(t *testing.T) {
"task02": &types.RunConfigTask{
ID: "task02",
Name: "task02",
Depends: []*types.RunConfigTaskDepend{
&types.RunConfigTaskDepend{
TaskID: "task01",
},
Depends: map[string]*types.RunConfigTaskDepend{
"task01": &types.RunConfigTaskDepend{TaskID: "task01"},
},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
@ -56,7 +54,7 @@ func TestAdvanceRunTasks(t *testing.T) {
"task03": &types.RunConfigTask{
ID: "task03",
Name: "task03",
Depends: []*types.RunConfigTaskDepend{},
Depends: map[string]*types.RunConfigTaskDepend{},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
},
@ -77,9 +75,9 @@ func TestAdvanceRunTasks(t *testing.T) {
"task05": &types.RunConfigTask{
ID: "task05",
Name: "task05",
Depends: []*types.RunConfigTaskDepend{
&types.RunConfigTaskDepend{TaskID: "task03"},
&types.RunConfigTaskDepend{TaskID: "task04"},
Depends: map[string]*types.RunConfigTaskDepend{
"task03": &types.RunConfigTaskDepend{TaskID: "task03"},
"task04": &types.RunConfigTaskDepend{TaskID: "task04"},
},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
@ -215,7 +213,7 @@ func TestGetTasksToRun(t *testing.T) {
"task01": &types.RunConfigTask{
ID: "task01",
Name: "task01",
Depends: []*types.RunConfigTaskDepend{},
Depends: map[string]*types.RunConfigTaskDepend{},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
},
@ -226,10 +224,8 @@ func TestGetTasksToRun(t *testing.T) {
"task02": &types.RunConfigTask{
ID: "task02",
Name: "task02",
Depends: []*types.RunConfigTaskDepend{
&types.RunConfigTaskDepend{
TaskID: "task01",
},
Depends: map[string]*types.RunConfigTaskDepend{
"task01": &types.RunConfigTaskDepend{TaskID: "task01"},
},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
@ -241,7 +237,7 @@ func TestGetTasksToRun(t *testing.T) {
"task03": &types.RunConfigTask{
ID: "task03",
Name: "task03",
Depends: []*types.RunConfigTaskDepend{},
Depends: map[string]*types.RunConfigTaskDepend{},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},
},
@ -262,9 +258,9 @@ func TestGetTasksToRun(t *testing.T) {
"task05": &types.RunConfigTask{
ID: "task05",
Name: "task05",
Depends: []*types.RunConfigTaskDepend{
&types.RunConfigTaskDepend{TaskID: "task03"},
&types.RunConfigTaskDepend{TaskID: "task04"},
Depends: map[string]*types.RunConfigTaskDepend{
"task03": &types.RunConfigTaskDepend{TaskID: "task03"},
"task04": &types.RunConfigTaskDepend{TaskID: "task04"},
},
Runtime: &types.Runtime{Type: types.RuntimeType("pod"),
Containers: []*types.Container{{Image: "image01"}},

View File

@ -319,7 +319,7 @@ type RunConfigTask struct {
Level int `json:"level,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Depends []*RunConfigTaskDepend `json:"depends"`
Depends map[string]*RunConfigTaskDepend `json:"depends"`
Runtime *Runtime `json:"runtime,omitempty"`
Environment map[string]string `json:"environment,omitempty"`
WorkingDir string `json:"working_dir,omitempty"`