Skip to content

Commit

Permalink
fix: handle container optimization for default images when pure shell…
Browse files Browse the repository at this point in the history
… script is run at last
  • Loading branch information
rangoo94 committed Oct 22, 2024
1 parent 068d044 commit 974154e
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (a LiteActionList) Result(ref, expression string) LiteActionList {
return append(a, LiteAction{Result: &ActionResult{Ref: ref, Value: expression}})
}

func (a LiteActionList) Execute(ref string, negative bool) LiteActionList {
return append(a, LiteAction{Execute: &ActionExecute{Ref: ref, Negative: negative}})
func (a LiteActionList) Execute(ref string, negative, pure bool) LiteActionList {
return append(a, LiteAction{Execute: &ActionExecute{Ref: ref, Negative: negative, Pure: pure}})
}

func (a LiteActionList) MutateContainer(config LiteContainerConfig) LiteActionList {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,23 @@ func (a ActionList) Result(ref, expression string) ActionList {
return append(a, Action{Result: &lite.ActionResult{Ref: ref, Value: expression}})
}

func (a ActionList) Execute(ref string, negative bool) ActionList {
return append(a, Action{Execute: &lite.ActionExecute{Ref: ref, Negative: negative}})
func (a ActionList) Execute(ref string, negative, pure bool) ActionList {
return append(a, Action{Execute: &lite.ActionExecute{Ref: ref, Negative: negative, Pure: pure}})
}

func (a ActionList) MutateContainer(ref string, config testworkflowsv1.ContainerConfig) ActionList {
return append(a, Action{Container: &ActionContainer{Ref: ref, Config: config}})
}

func (a ActionList) Image() string {
for i := range a {
if a[i].Container != nil {
return a[i].Container.Config.Image
}
}
return ""
}

type ActionGroups []ActionList

func (a ActionGroups) Append(fn func(list ActionList) ActionList) ActionGroups {
Expand Down
10 changes: 10 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/action/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,15 @@ merging:
i++
}

// Fix the auto-merged internal images
for i := range groups {
image := groups[i].Image()
// Re-use /.tktw/bin/sh when the internal image has been merged into different container
if image != constants.DefaultToolkitImage && image != constants.DefaultInitImage {
groups[i] = groups[i].RewireCommandDirectory(constants.DefaultInitImage, "/.tktw-bin", constants.InternalBinPath)
groups[i] = groups[i].RewireCommandDirectory(constants.DefaultToolkitImage, "/.tktw-bin", constants.InternalBinPath)
}
}

return groups
}
4 changes: 2 additions & 2 deletions pkg/testworkflows/testworkflowprocessor/action/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestGroup_Basic(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").
CurrentStatus("init").

Expand All @@ -43,7 +43,7 @@ func TestGroup_Basic(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step3").
Execute("step3", false).
Execute("step3", false, false).
End("step3").
End("init").
End("")
Expand Down
98 changes: 78 additions & 20 deletions pkg/testworkflows/testworkflowprocessor/action/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/action/actiontypes"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/constants"
"github.com/kubeshop/testkube/pkg/testworkflows/testworkflowprocessor/stage"
)

Expand Down Expand Up @@ -40,7 +41,7 @@ func TestProcess_BasicSteps(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -50,7 +51,7 @@ func TestProcess_BasicSteps(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestProcess_Grouping(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Start the group 1
Expand All @@ -114,7 +115,7 @@ func TestProcess_Grouping(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Run the step 2
Expand All @@ -124,7 +125,7 @@ func TestProcess_Grouping(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step3").
Execute("step3", false).
Execute("step3", false, false).
End("step3").

// End the group 1
Expand All @@ -137,7 +138,7 @@ func TestProcess_Grouping(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step4").
Execute("step4", false).
Execute("step4", false, false).
End("step4").

// Finish
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestProcess_Pause(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -194,7 +195,7 @@ func TestProcess_Pause(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -238,7 +239,7 @@ func TestProcess_NegativeStep(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", true).
Execute("step1", true, false).
End("step1").

// Run the step 2
Expand All @@ -248,7 +249,7 @@ func TestProcess_NegativeStep(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -291,7 +292,7 @@ func TestProcess_NegativeGroup(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -301,7 +302,7 @@ func TestProcess_NegativeGroup(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -345,7 +346,7 @@ func TestProcess_OptionalStep(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -355,7 +356,7 @@ func TestProcess_OptionalStep(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -404,7 +405,7 @@ func TestProcess_OptionalGroup(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -414,7 +415,7 @@ func TestProcess_OptionalGroup(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -464,7 +465,7 @@ func TestProcess_IgnoreExecutionOfStaticSkip(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -604,7 +605,7 @@ func TestProcess_IgnoreExecutionOfStaticSkip_PauseGroup(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

// Finish
Expand Down Expand Up @@ -650,7 +651,7 @@ func TestProcess_ConsecutiveAlways(t *testing.T) {
Command: common.Ptr([]string{"a", "b"}),
}).
Start("step1").
Execute("step1", false).
Execute("step1", false, false).
End("step1").

// Run the step 2
Expand All @@ -660,7 +661,64 @@ func TestProcess_ConsecutiveAlways(t *testing.T) {
Command: common.Ptr([]string{"c", "d"}),
}).
Start("step2").
Execute("step2", false).
Execute("step2", false, false).
End("step2").

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

// Assert
got, err := Process(root, nil)
assert.NoError(t, err)
assert.Equal(t, want, got)
}

func TestProcess_PureShellAtTheEnd(t *testing.T) {
// Build the structure
root := stage.NewGroupStage("init", false)
step1 := stage.NewContainerStage("step1", stage.NewContainer().SetImage("image:1.2.3").SetCommand(constants.DefaultShellPath))
step1.SetPure(true)
step1.SetCondition("always")
root.Add(step1)
step2 := stage.NewContainerStage("step2", stage.NewContainer().SetCommand(constants.DefaultShellPath))
step2.SetPure(true)
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{constants.DefaultShellPath}),
}).
Start("step1").
Execute("step1", false, true).
End("step1").

// Run the step 2
CurrentStatus("step1&&init").
MutateContainer("step2", testworkflowsv1.ContainerConfig{
Command: common.Ptr([]string{constants.DefaultShellPath}),
}).
Start("step2").
Execute("step2", false, true).
End("step2").

// Finish
Expand Down
Loading

0 comments on commit 974154e

Please sign in to comment.