Skip to content

Commit

Permalink
fix: merge general-use steps properly (#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Nov 15, 2024
1 parent 39fbcdc commit b56671f
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
64 changes: 64 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/presets/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b56671f

Please sign in to comment.