Skip to content

Commit

Permalink
fix: correctly handle consecutive condition: always steps (#5785)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Aug 28, 2024
1 parent 3286ac4 commit 0a7b9f6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
56 changes: 56 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/action/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,59 @@ func TestProcess_IgnoreExecutionOfStaticSkip_PauseGroup(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, want, got)
}

func TestProcess_ConsecutiveAlways(t *testing.T) {
// Build the structure
root := stage.NewGroupStage("init", false)
step1 := stage.NewContainerStage("step1", stage.NewContainer().SetImage("image:1.2.3").SetCommand("a", "b"))
step1.SetCondition("always")
root.Add(step1)
step2 := stage.NewContainerStage("step2", stage.NewContainer().SetImage("image:3.2.1").SetCommand("c", "d"))
step2.SetCondition("always")
root.Add(step2)

// Build the expectations
want := actiontypes.NewActionList().
// Declare stage conditions
Declare("init", "true").
Declare("step1", "true", "init").
Declare("step2", "true", "init").

// Declare group resolutions
Result("init", "step1&&step2").
Result("", "init").

// Initialize
Start("").
CurrentStatus("true").
Start("init").

// Run the step 1
CurrentStatus("init").
MutateContainer("step1", testworkflowsv1.ContainerConfig{
Image: "image:1.2.3",
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
End("step1").

// Run the step 2
CurrentStatus("step1&&init").
MutateContainer("step2", testworkflowsv1.ContainerConfig{
Image: "image:3.2.1",
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
End("step2").

// Finish
End("init").
End("")

// Assert
got, err := Process(root, nil)
assert.NoError(t, err)
assert.Equal(t, want, got)
}
58 changes: 58 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/presets/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,64 @@ func TestProcessShell(t *testing.T) {
assert.Equal(t, want, res.LiteActions())
}

func TestProcessConsecutiveAlways(t *testing.T) {
wf := &testworkflowsv1.TestWorkflow{
Spec: testworkflowsv1.TestWorkflowSpec{
TestWorkflowSpecBase: testworkflowsv1.TestWorkflowSpecBase{
System: &testworkflowsv1.TestWorkflowSystem{
IsolatedContainers: common.Ptr(true),
},
},
Steps: []testworkflowsv1.Step{
{StepOperations: testworkflowsv1.StepOperations{Run: &testworkflowsv1.StepRun{Shell: common.Ptr("shell-test")}}},
{StepMeta: testworkflowsv1.StepMeta{Condition: "always"}, StepOperations: testworkflowsv1.StepOperations{Run: &testworkflowsv1.StepRun{Shell: common.Ptr("shell-test")}}},
},
},
}

res, err := proc.Bundle(context.Background(), wf, execMachine)
sig := res.Signature

want := lite.NewLiteActionGroups().
Append(func(list lite.LiteActionList) lite.LiteActionList {
return list.
Setup(false, false, false).
Declare(constants.RootOperationName, "true").
Declare(sig[0].Ref(), "true", constants.RootOperationName).
Declare(sig[1].Ref(), "true", constants.RootOperationName).
Result(constants.RootOperationName, and(sig[0].Ref(), sig[1].Ref())).
Result("", constants.RootOperationName).
Start("").
CurrentStatus("true").
Start(constants.RootOperationName).
CurrentStatus(constants.RootOperationName)
}).Append(func(list lite.LiteActionList) lite.LiteActionList {
return list.
MutateContainer(lite.LiteContainerConfig{
Command: cmd("/.tktw-bin/sh"),
Args: cmdShell("shell-test"),
}).
Start(sig[0].Ref()).
Execute(sig[0].Ref(), false).
End(sig[0].Ref()).
CurrentStatus(and(sig[0].Ref(), constants.RootOperationName))
}).Append(func(list lite.LiteActionList) lite.LiteActionList {
return list.
MutateContainer(lite.LiteContainerConfig{
Command: cmd("/.tktw-bin/sh"),
Args: cmdShell("shell-test"),
}).
Start(sig[1].Ref()).
Execute(sig[1].Ref(), false).
End(sig[1].Ref()).
End(constants.RootOperationName).
End("")
})

assert.NoError(t, err)
assert.Equal(t, want, res.LiteActions())
}

func TestProcessNestedCondition(t *testing.T) {
wf := &testworkflowsv1.TestWorkflow{
Spec: testworkflowsv1.TestWorkflowSpec{
Expand Down
7 changes: 6 additions & 1 deletion pkg/testworkflows/testworkflowprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (p *processor) process(layer Intermediate, container stage.Container, step
if err != nil {
return nil, err
}
self.Add(stage)
if stage != nil {
if step.Condition != "" {
stage.SetCondition(step.Condition)
}
self.Add(stage)
}
}

// Add virtual pause step in case no other is there
Expand Down

0 comments on commit 0a7b9f6

Please sign in to comment.