Skip to content

Commit

Permalink
fix: determine dynamic status correctly in the execution's expressions (
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Oct 1, 2024
1 parent bfb3940 commit 6d220ef
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 11 deletions.
20 changes: 9 additions & 11 deletions cmd/testworkflow-init/data/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var StateMachine = expressions.NewMachine().
RegisterAccessor(func(name string) (interface{}, bool) {
if name == "status" {
currentStatus := GetState().CurrentStatus
expr, err := expressions.EvalExpression(currentStatus, RefStatusMachine, AliasMachine)
expr, err := expressions.EvalExpression(currentStatus, RefNotFailedMachine, AliasMachine)
if err != nil {
output.ExitErrorf(CodeInternal, "current status is invalid: %s: %v\n", currentStatus, err.Error())
}
Expand Down Expand Up @@ -126,20 +126,18 @@ var RefSuccessMachine = expressions.NewMachine().
return *s.Status == StepStatusPassed || *s.Status == StepStatusSkipped, true
})

var RefStatusMachine = expressions.NewMachine().
var RefNotFailedMachine = expressions.NewMachine().
RegisterAccessor(func(ref string) (interface{}, bool) {
status := GetState().GetStep(ref).Status
if status == nil {
return nil, false
s := GetState().GetStep(ref)
if s.Status == nil && s.Result != "" {
exp, err := expressions.Compile(s.Result)
if err == nil {
return exp, true
}
}
return string(*status), true
return s.Status == nil || *s.Status == StepStatusPassed || *s.Status == StepStatusSkipped, true
})

func Template(tpl string, m ...expressions.Machine) (string, error) {
m = append(m, AliasMachine, GetBaseTestWorkflowMachine())
return expressions.EvalTemplate(tpl, m...)
}

func Expression(expr string, m ...expressions.Machine) (expressions.StaticValue, error) {
m = append(m, AliasMachine, GetBaseTestWorkflowMachine())
return expressions.EvalExpression(expr, m...)
Expand Down
69 changes: 69 additions & 0 deletions pkg/testworkflows/testworkflowprocessor/presets/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,3 +1174,72 @@ func TestProcessNamedGroupWithSkippedSteps(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, want, res.LiteActions())
}

func TestProcess_ConditionAlways(t *testing.T) {
wf := &testworkflowsv1.TestWorkflow{
Spec: testworkflowsv1.TestWorkflowSpec{
Steps: []testworkflowsv1.Step{
{StepOperations: testworkflowsv1.StepOperations{Shell: "test-command-1"}},
{
StepMeta: testworkflowsv1.StepMeta{Condition: "always"},
StepOperations: testworkflowsv1.StepOperations{
Run: &testworkflowsv1.StepRun{
ContainerConfig: testworkflowsv1.ContainerConfig{
Env: []corev1.EnvVar{
{Name: "result", Value: "{{passed}}"},
},
},
Shell: common.Ptr("echo $result"),
},
},
},
},
},
}

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

want := lite.NewLiteActionGroups().
Append(func(list lite.LiteActionList) lite.LiteActionList {
return list.
// configure
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).

// initialize
Start("").
CurrentStatus("true").
Start(constants.RootOperationName).
CurrentStatus(constants.RootOperationName).

// start first container
MutateContainer(lite.LiteContainerConfig{
Command: cmd("/.tktw-bin/sh"),
Args: cmdShell("test-command-1"),
}).
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("echo $result"),
}).
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())
}
71 changes: 71 additions & 0 deletions pkg/testworkflows/testworkflowresolver/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"

testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1"
"github.com/kubeshop/testkube/internal/common"
)

var (
Expand Down Expand Up @@ -602,3 +603,73 @@ func TestApplyTemplatesConfigOverflow(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, want, wf)
}

func TestApplyTemplates_ConditionAlways(t *testing.T) {
tpls := map[string]testworkflowsv1.TestWorkflowTemplate{
"example": {
Spec: testworkflowsv1.TestWorkflowTemplateSpec{
TestWorkflowSpecBase: testworkflowsv1.TestWorkflowSpecBase{
Config: map[string]testworkflowsv1.ParameterSchema{
"result": {
Type: testworkflowsv1.ParameterTypeString,
Default: &intstr.IntOrString{Type: intstr.String, StrVal: ""},
},
},
},
Steps: []testworkflowsv1.IndependentStep{
{
StepMeta: testworkflowsv1.StepMeta{Condition: "always"},
StepOperations: testworkflowsv1.StepOperations{
Run: &testworkflowsv1.StepRun{
ContainerConfig: testworkflowsv1.ContainerConfig{
Env: []corev1.EnvVar{
{Name: "result", Value: "{{ config.result }}"},
},
},
Shell: common.Ptr("echo $result"),
},
},
},
},
},
},
}
wf := &testworkflowsv1.TestWorkflow{
Spec: testworkflowsv1.TestWorkflowSpec{
Steps: []testworkflowsv1.Step{
{StepOperations: testworkflowsv1.StepOperations{Shell: "exit 0"}},
{Template: &testworkflowsv1.TemplateRef{
Name: "example",
Config: map[string]intstr.IntOrString{
"result": {Type: intstr.String, StrVal: "{{ passed }}"},
},
}},
},
},
}
err := ApplyTemplates(wf, tpls, nil)

want := &testworkflowsv1.TestWorkflow{
Spec: testworkflowsv1.TestWorkflowSpec{
Steps: []testworkflowsv1.Step{
{StepOperations: testworkflowsv1.StepOperations{Shell: "exit 0"}},
{
StepMeta: testworkflowsv1.StepMeta{Condition: "always"},
StepOperations: testworkflowsv1.StepOperations{
Run: &testworkflowsv1.StepRun{
ContainerConfig: testworkflowsv1.ContainerConfig{
Env: []corev1.EnvVar{
{Name: "result", Value: "{{passed}}"},
},
},
Shell: common.Ptr("echo $result"),
},
},
},
},
},
}

assert.NoError(t, err)
assert.Equal(t, want, wf)
}

0 comments on commit 6d220ef

Please sign in to comment.