Skip to content

Commit

Permalink
fix: allow run condition on joins (#5071)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Mar 24, 2020
1 parent 064f428 commit 4feb0fe
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
11 changes: 10 additions & 1 deletion engine/api/workflow/process_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func processAllJoins(ctx context.Context, db gorp.SqlExecutor, store cache.Store

//now checks if all sources have been completed
var ok = true

nodeRunIDs := []int64{}
sourcesParams := map[string]string{}
for _, nodeRun := range sources {
Expand All @@ -122,11 +123,19 @@ func processAllJoins(ctx context.Context, db gorp.SqlExecutor, store cache.Store
break
}

if !sdk.StatusIsTerminated(nodeRun.Status) || nodeRun.Status == sdk.StatusFail || nodeRun.Status == sdk.StatusNeverBuilt || nodeRun.Status == sdk.StatusStopped {
if !sdk.StatusIsTerminated(nodeRun.Status) {
ok = false
break
}

// If there is no conditions on join, keep default condition ( only continue on success )
if j.Context == nil || (len(j.Context.Conditions.PlainConditions) == 0 && j.Context.Conditions.LuaScript == "") {
if nodeRun.Status == sdk.StatusFail || nodeRun.Status == sdk.StatusNeverBuilt || nodeRun.Status == sdk.StatusStopped {
ok = false
break
}
}

nodeRunIDs = append(nodeRunIDs, nodeRun.ID)
//Merge build parameters from all sources
sourcesParams = sdk.ParametersMapMerge(sourcesParams, sdk.ParametersToMap(nodeRun.BuildParameters))
Expand Down
143 changes: 143 additions & 0 deletions engine/api/workflow/process_start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package workflow_test

import (
"context"
"github.com/ovh/cds/engine/api/authentication"
"github.com/ovh/cds/engine/api/bootstrap"
"github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/engine/api/test/assets"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/sdk"
"github.com/stretchr/testify/require"
"testing"
)

func TestProcessJoinDefaultCondition(t *testing.T) {
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
u, _ := assets.InsertAdminUser(t, db)
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)

key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key)

// Test data
wr := &sdk.WorkflowRun{
ProjectID: proj.ID,
WorkflowNodeRuns: map[int64][]sdk.WorkflowNodeRun{},
Workflow: sdk.Workflow{
Name: "myworkflow",
ProjectKey: key,
ProjectID: proj.ID,
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
ID: 1,
Name: "myfork",
},
Joins: []sdk.Node{
{
Name: "myjoin",
ID: 666,
JoinContext: []sdk.NodeJoin{
{
ParentID: 1,
},
},
},
},
},
},
}

// Insert workflow
require.NoError(t, workflow.Insert(context.TODO(), db, cache, *proj, &wr.Workflow))

// Create run
wrr, err := workflow.CreateRun(db, &wr.Workflow, nil, u)
require.NoError(t, err)
wr.ID = wrr.ID
wr.WorkflowID = wr.Workflow.ID
require.NoError(t, workflow.UpdateWorkflowRun(context.TODO(), db, wr))

// Start workflow
_, err = workflow.StartWorkflowRun(context.TODO(), db, cache, *proj, wr, &sdk.WorkflowRunPostHandlerOption{Manual: &sdk.WorkflowNodeRunManual{}}, consumer, nil)
require.NoError(t, err)

wrUpdated, err := workflow.LoadRun(context.TODO(), db, proj.Key, wr.Workflow.Name, wr.Number, workflow.LoadRunOptions{})
require.NoError(t, err)

// Fork and Join has been run
require.Equal(t, 2, len(wrUpdated.WorkflowNodeRuns))
require.Equal(t, sdk.StatusSuccess, wrUpdated.WorkflowNodeRuns[wr.Workflow.WorkflowData.Joins[0].ID][0].Status)
}

func TestProcessJoinCustomCondition(t *testing.T) {
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
u, _ := assets.InsertAdminUser(t, db)
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)

key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key)

// Test data
wr := &sdk.WorkflowRun{
ProjectID: proj.ID,
WorkflowNodeRuns: map[int64][]sdk.WorkflowNodeRun{},
Workflow: sdk.Workflow{
Name: "myworkflow",
ProjectKey: key,
ProjectID: proj.ID,
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
ID: 1,
Name: "myfork",
},
Joins: []sdk.Node{
{
Name: "myjoin",
ID: 666,
JoinContext: []sdk.NodeJoin{
{
ParentID: 1,
},
},
Context: &sdk.NodeContext{
Conditions: sdk.WorkflowNodeConditions{
PlainConditions: []sdk.WorkflowNodeCondition{
{
Variable: "cds.status",
Operator: "eq",
Value: sdk.StatusFail,
},
},
},
},
},
},
},
},
}

// Insert workflow
require.NoError(t, workflow.Insert(context.TODO(), db, cache, *proj, &wr.Workflow))

// Create run
wrr, err := workflow.CreateRun(db, &wr.Workflow, nil, u)
require.NoError(t, err)
wr.ID = wrr.ID
wr.WorkflowID = wr.Workflow.ID

require.NoError(t, workflow.UpdateWorkflowRun(context.TODO(), db, wr))

// Start run
_, err = workflow.StartWorkflowRun(context.TODO(), db, cache, *proj, wr, &sdk.WorkflowRunPostHandlerOption{Manual: &sdk.WorkflowNodeRunManual{}}, consumer, nil)
require.NoError(t, err)

wrUpdated, err := workflow.LoadRun(context.TODO(), db, proj.Key, wr.Workflow.Name, wr.Number, workflow.LoadRunOptions{})
require.NoError(t, err)

// Only fork has run
require.Equal(t, 1, len(wrUpdated.WorkflowNodeRuns))
require.Equal(t, sdk.StatusSuccess, wrUpdated.WorkflowNodeRuns[wr.Workflow.WorkflowData.Node.ID][0].Status)
}

0 comments on commit 4feb0fe

Please sign in to comment.