Skip to content

Commit

Permalink
fix(api): mutex as-code (#4565)
Browse files Browse the repository at this point in the history
* fix(api): mutex as-code

close #4551

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Aug 29, 2019
1 parent 145226e commit 68ea2c5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
8 changes: 8 additions & 0 deletions sdk/exportentities/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Workflow struct {
Hooks map[string][]HookEntry `json:"hooks,omitempty" yaml:"hooks,omitempty" jsonschema_description:"Workflow hooks list."`
// this will be filled for simple workflows
DependsOn []string `json:"depends_on,omitempty" yaml:"depends_on,omitempty" jsonschema_description:"Names of the parent nodes, can be pipelines, forks or joins."`
OneAtATime *bool `json:"one_at_a_time,omitempty" yaml:"one_at_a_time,omitempty" jsonschema_description:"Set to true if you want to limit the execution of this node to one at a time."`
Conditions *sdk.WorkflowNodeConditions `json:"conditions,omitempty" yaml:"conditions,omitempty" jsonschema_description:"Conditions to run this node.\nhttps://ovh.github.io/cds/docs/concepts/workflow/run-conditions."`
When []string `json:"when,omitempty" yaml:"when,omitempty" jsonschema_description:"Set manual and status condition (ex: 'success')."` //This is used only for manual and success condition
PipelineName string `json:"pipeline,omitempty" yaml:"pipeline,omitempty" jsonschema_description:"The name of a pipeline used for pipeline node."`
Expand Down Expand Up @@ -317,6 +318,7 @@ func NewWorkflow(w sdk.Workflow, opts ...WorkflowOptions) (Workflow, error) {
exportedWorkflow.EnvironmentName = entry.EnvironmentName
exportedWorkflow.ProjectIntegrationName = entry.ProjectIntegrationName
exportedWorkflow.DependsOn = entry.DependsOn
exportedWorkflow.OneAtATime = entry.OneAtATime
if entry.Conditions != nil && (len(entry.Conditions.PlainConditions) > 0 || entry.Conditions.LuaScript != "") {
exportedWorkflow.When = entry.When
exportedWorkflow.Conditions = entry.Conditions
Expand Down Expand Up @@ -405,6 +407,7 @@ func (w Workflow) Entries() map[string]NodeEntry {
When: w.When,
Payload: w.Payload,
Parameters: w.Parameters,
OneAtATime: w.OneAtATime,
}
return map[string]NodeEntry{
w.PipelineName: singleEntry,
Expand Down Expand Up @@ -578,6 +581,10 @@ func (w Workflow) GetWorkflow() (*sdk.Workflow, error) {
}

func (e *NodeEntry) getNode(name string, w *sdk.Workflow) (*sdk.Node, error) {
var mutex bool
if e.OneAtATime != nil && *e.OneAtATime {
mutex = true
}
node := &sdk.Node{
Name: name,
Ref: name,
Expand All @@ -587,6 +594,7 @@ func (e *NodeEntry) getNode(name string, w *sdk.Workflow) (*sdk.Node, error) {
ApplicationName: e.ApplicationName,
EnvironmentName: e.EnvironmentName,
ProjectIntegrationName: e.ProjectIntegrationName,
Mutex: mutex,
},
}

Expand Down
54 changes: 53 additions & 1 deletion sdk/exportentities/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestWorkflow_checkValidity(t *testing.T) {
ProjectIntegrationName string
PipelineHooks []exportentities.HookEntry
Permissions map[string]int
OneAtATime *bool
}
tests := []struct {
name string
Expand Down Expand Up @@ -193,6 +194,7 @@ func TestWorkflow_checkValidity(t *testing.T) {
ProjectIntegrationName: tt.fields.ProjectIntegrationName,
PipelineHooks: tt.fields.PipelineHooks,
Permissions: tt.fields.Permissions,
OneAtATime: tt.fields.OneAtATime,
}
if err := w.CheckValidity(); (err != nil) != tt.wantErr {
t.Errorf("Workflow.checkValidity() error = %v, wantErr %v", err, tt.wantErr)
Expand All @@ -202,6 +204,7 @@ func TestWorkflow_checkValidity(t *testing.T) {
}

func TestWorkflow_GetWorkflow(t *testing.T) {
strue := true
type fields struct {
Name string
Description string
Expand All @@ -218,13 +221,36 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
PipelineHooks []exportentities.HookEntry
Permissions map[string]int
HistoryLength int64
OneAtATime *bool
}
tsts := []struct {
name string
fields fields
want sdk.Workflow
wantErr bool
}{
// pipeline
{
name: "Simple workflow with mutex should not raise an error",
fields: fields{
PipelineName: "pipeline",
OneAtATime: &strue,
},
wantErr: false,
want: sdk.Workflow{
HistoryLength: sdk.DefaultHistoryLength,
WorkflowData: &sdk.WorkflowData{
Node: sdk.Node{
Name: "pipeline",
Type: "pipeline",
Context: &sdk.NodeContext{
PipelineName: "pipeline",
Mutex: true,
},
},
},
},
},
// pipeline
{
name: "Simple workflow should not raise an error",
Expand Down Expand Up @@ -275,7 +301,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
// root(pipeline-root) -> child(pipeline-child)
{
name: "Complexe workflow without joins should not raise an error",
name: "Complexe workflow without joins and mutex should not raise an error",
fields: fields{
Workflow: map[string]exportentities.NodeEntry{
"root": {
Expand Down Expand Up @@ -694,6 +720,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
PipelineHooks: tt.fields.PipelineHooks,
Permissions: tt.fields.Permissions,
HistoryLength: &tt.fields.HistoryLength,
OneAtATime: tt.fields.OneAtATime,
}
got, err := w.GetWorkflow()
if (err != nil) != tt.wantErr {
Expand Down Expand Up @@ -1036,6 +1063,31 @@ workflow:
- aa_2
when:
- manual
`,
},
{
name: "Workflow with mutex on root",
yaml: `name: mymutex
version: v1.0
one_at_a_time: true
pipeline: env
`,
},
{
name: "Workflow with mutex pipeline child",
yaml: `name: mymutex
version: v1.0
workflow:
env:
pipeline: env
one_at_a_time: true
env_2:
depends_on:
- env
when:
- success
pipeline: env
one_at_a_time: true
`,
},
}
Expand Down

0 comments on commit 68ea2c5

Please sign in to comment.