diff --git a/pkg/testworkflows/testworkflowprocessor/action/containerize.go b/pkg/testworkflows/testworkflowprocessor/action/containerize.go index fd04d8bfe4..b0d631b64d 100644 --- a/pkg/testworkflows/testworkflowprocessor/action/containerize.go +++ b/pkg/testworkflows/testworkflowprocessor/action/containerize.go @@ -39,11 +39,15 @@ func CreateContainer(groupId int, defaultContainer stage2.Container, actions []a // Find the highest priority container configuration var bestContainerConfig *actiontypes.Action var bestIsToolkit = false + var bestIsDefaultImage = true for i := range containerConfigs { if executable[containerConfigs[i].Container.Ref] { - if bestContainerConfig == nil || bestIsToolkit { + image := containerConfigs[i].Container.Config.Image + isDefaultImage := image == "" || image == constants.DefaultInitImage || image == constants.DefaultToolkitImage + if bestContainerConfig == nil || bestIsToolkit || (bestIsDefaultImage && !isDefaultImage) { bestContainerConfig = containerConfigs[i] bestIsToolkit = toolkit[bestContainerConfig.Container.Ref] + bestIsDefaultImage = isDefaultImage } } } diff --git a/pkg/testworkflows/testworkflowprocessor/action/containerize_test.go b/pkg/testworkflows/testworkflowprocessor/action/containerize_test.go new file mode 100644 index 0000000000..90e37219d5 --- /dev/null +++ b/pkg/testworkflows/testworkflowprocessor/action/containerize_test.go @@ -0,0 +1,61 @@ +package action + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + 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" +) + +func TestCreateContainer_MergingDefaultStepBefore(t *testing.T) { + input := actiontypes.NewActionList(). + MutateContainer("ref1", testworkflowsv1.ContainerConfig{ + Command: common.Ptr([]string{"sleep"}), + }). + Start("ref1"). + Execute("ref1", false, true). + End("ref1"). + CurrentStatus("ref1"). + MutateContainer("ref2", testworkflowsv1.ContainerConfig{ + Image: "custom-image:1.2.3", + Command: common.Ptr([]string{"my-test"}), + }). + Start("ref2"). + Execute("ref2", false, false). + End("ref2"). + End(constants.RootOperationName). + End("") + result, _, err := CreateContainer(1, stage.NewContainer(), input, false) + + assert.NoError(t, err) + assert.Equal(t, result.Image, "custom-image:1.2.3") +} + +func TestCreateContainer_MergingDefaultStepAfter(t *testing.T) { + input := actiontypes.NewActionList(). + MutateContainer("ref1", testworkflowsv1.ContainerConfig{ + Image: "custom-image:1.2.3", + Command: common.Ptr([]string{"my-test"}), + }). + Start("ref1"). + Execute("ref1", false, false). + End("ref1"). + CurrentStatus("ref1"). + MutateContainer("ref2", testworkflowsv1.ContainerConfig{ + Command: common.Ptr([]string{"sleep"}), + }). + Start("ref2"). + Execute("ref2", false, true). + End("ref2"). + End(constants.RootOperationName). + End("") + result, _, err := CreateContainer(1, stage.NewContainer(), input, false) + + assert.NoError(t, err) + assert.Equal(t, result.Image, "custom-image:1.2.3") +} diff --git a/pkg/testworkflows/testworkflowprocessor/presets/processor_test.go b/pkg/testworkflows/testworkflowprocessor/presets/processor_test.go index b7f842085d..18b503e3e7 100644 --- a/pkg/testworkflows/testworkflowprocessor/presets/processor_test.go +++ b/pkg/testworkflows/testworkflowprocessor/presets/processor_test.go @@ -1334,3 +1334,67 @@ func TestProcess_PureShellAtTheEnd(t *testing.T) { assert.NoError(t, err) assert.Equal(t, want, res.LiteActions()) } + +func TestProcess_MergingActions(t *testing.T) { + wf := &testworkflowsv1.TestWorkflow{ + Spec: testworkflowsv1.TestWorkflowSpec{ + Steps: []testworkflowsv1.Step{ + { + StepOperations: testworkflowsv1.StepOperations{Delay: "1s"}, + }, + { + StepDefaults: testworkflowsv1.StepDefaults{Container: &testworkflowsv1.ContainerConfig{ + Image: "custom-image:1.2.3", + }}, + StepOperations: testworkflowsv1.StepOperations{Shell: "test-command"}, + }, + }, + }, + } + + res, err := proc.Bundle(context.Background(), wf, testworkflowprocessor.BundleOptions{Config: testConfig}) + sig := res.Signature + + wantActions := lite.NewLiteActionGroups(). + Append(func(list lite.LiteActionList) lite.LiteActionList { + return list. + // configure + Setup(true, false, true). + Declare(constants.RootOperationName, "true"). + Declare(sig[0].Ref(), "true", constants.RootOperationName). + Declare(sig[1].Ref(), sig[0].Ref(), constants.RootOperationName). + Result(constants.RootOperationName, and(sig[0].Ref(), sig[1].Ref())). + Result("", constants.RootOperationName). + + // initialize + Start(""). + CurrentStatus("true"). + Start(constants.RootOperationName). + CurrentStatus(constants.RootOperationName) + }). + Append(func(list lite.LiteActionList) lite.LiteActionList { + return list. + // start first container + MutateContainer(lite.LiteContainerConfig{ + Command: cmd("sleep"), + Args: cmd("1"), + }). + Start(sig[0].Ref()). + Execute(sig[0].Ref(), false, true). + End(sig[0].Ref()). + CurrentStatus(and(sig[0].Ref(), constants.RootOperationName)). + MutateContainer(lite.LiteContainerConfig{ + Command: cmd("/.tktw/bin/sh"), + Args: cmdShell("test-command"), + }). + Start(sig[1].Ref()). + Execute(sig[1].Ref(), false, false). + End(sig[1].Ref()). + End(constants.RootOperationName). + End("") + }) + + assert.NoError(t, err) + assert.Equal(t, wantActions, res.LiteActions()) + assert.Equal(t, res.Job.Spec.Template.Spec.Containers[0].Image, "custom-image:1.2.3") +}