From f1215fe3da6dbf65f36142d1b8808adda5e47831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 23 Mar 2022 10:24:54 +0100 Subject: [PATCH] Fix sequence iterator looping condition and improper state --- automation/automation/iterators.go | 5 +- automation/automation/loop_handler.go | 3 +- .../iterator_sequence_complex_test.go | 51 +++++++++++++++++++ tests/workflows/iterator_sequence_test.go | 51 +++++++++++++++++++ .../testdata/iterator_sequence/workflow.yaml | 29 +++++++++++ .../iterator_sequence_complex/workflow.yaml | 30 +++++++++++ 6 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 tests/workflows/iterator_sequence_complex_test.go create mode 100644 tests/workflows/iterator_sequence_test.go create mode 100644 tests/workflows/testdata/iterator_sequence/workflow.yaml create mode 100644 tests/workflows/testdata/iterator_sequence_complex/workflow.yaml diff --git a/automation/automation/iterators.go b/automation/automation/iterators.go index 02573427b..d75448b8b 100644 --- a/automation/automation/iterators.go +++ b/automation/automation/iterators.go @@ -3,6 +3,7 @@ package automation import ( "bufio" "context" + . "github.com/cortezaproject/corteza-server/pkg/expr" ) @@ -18,7 +19,7 @@ func (i *sequenceIterator) More(context.Context, *Vars) (bool, error) { } func (i *sequenceIterator) more() bool { - return i.counter*(i.cStep/i.cStep) < i.cLast*(i.cStep/i.cStep) + return i.counter < i.cLast } func (i *sequenceIterator) Start(context.Context, *Vars) error { return nil } @@ -27,8 +28,8 @@ func (i *sequenceIterator) Next(context.Context, *Vars) (*Vars, error) { out := &Vars{} out.Set("counter", i.counter) out.Set("isFirst", i.counter == i.cFirst) - out.Set("isLast", !i.more()) i.counter = i.counter + i.cStep + out.Set("isLast", !i.more()) return out, nil } diff --git a/automation/automation/loop_handler.go b/automation/automation/loop_handler.go index 614593209..6c31efab0 100644 --- a/automation/automation/loop_handler.go +++ b/automation/automation/loop_handler.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "fmt" + "github.com/cortezaproject/corteza-server/pkg/expr" "github.com/cortezaproject/corteza-server/pkg/wfexec" ) @@ -43,7 +44,7 @@ func (h loopHandler) sequence(_ context.Context, args *loopSequenceArgs) (wfexec } i := &sequenceIterator{ - counter: 0, + counter: args.First, cFirst: args.First, cLast: args.Last, cStep: args.Step, diff --git a/tests/workflows/iterator_sequence_complex_test.go b/tests/workflows/iterator_sequence_complex_test.go new file mode 100644 index 000000000..949187818 --- /dev/null +++ b/tests/workflows/iterator_sequence_complex_test.go @@ -0,0 +1,51 @@ +package workflows + +import ( + "context" + "testing" + + autTypes "github.com/cortezaproject/corteza-server/automation/types" + "github.com/cortezaproject/corteza-server/pkg/wfexec" + "github.com/spf13/cast" + "github.com/stretchr/testify/require" +) + +func Test_iterator_sequence_complex(t *testing.T) { + wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize + defer func() { + wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize + }() + + var ( + ctx = bypassRBAC(context.Background()) + req = require.New(t) + ) + + req.NoError(defStore.TruncateComposeRecords(ctx, nil)) + req.NoError(defStore.TruncateComposeModules(ctx)) + req.NoError(defStore.TruncateComposeNamespaces(ctx)) + + loadNewScenario(ctx, t) + + var ( + _, trace = mustExecWorkflow(ctx, t, "testing", autTypes.WorkflowExecParams{}) + ) + + req.Len(trace, 7) + + frame := trace[1] + req.True(frame.Scope.GetValue()["first"].Get().(bool)) + req.False(frame.Scope.GetValue()["last"].Get().(bool)) + aux := frame.Scope.GetValue()["i"].Get() + i, err := cast.ToIntE(aux) + req.NoError(err) + req.Equal(2, i) + + frame = trace[len(trace)-1] + req.False(frame.Scope.GetValue()["first"].Get().(bool)) + req.True(frame.Scope.GetValue()["last"].Get().(bool)) + aux = frame.Scope.GetValue()["i"].Get() + i, err = cast.ToIntE(aux) + req.NoError(err) + req.Equal(4, i) +} diff --git a/tests/workflows/iterator_sequence_test.go b/tests/workflows/iterator_sequence_test.go new file mode 100644 index 000000000..79b393232 --- /dev/null +++ b/tests/workflows/iterator_sequence_test.go @@ -0,0 +1,51 @@ +package workflows + +import ( + "context" + "testing" + + autTypes "github.com/cortezaproject/corteza-server/automation/types" + "github.com/cortezaproject/corteza-server/pkg/wfexec" + "github.com/spf13/cast" + "github.com/stretchr/testify/require" +) + +func Test_iterator_sequence(t *testing.T) { + wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize + defer func() { + wfexec.MaxIteratorBufferSize = wfexec.DefaultMaxIteratorBufferSize + }() + + var ( + ctx = bypassRBAC(context.Background()) + req = require.New(t) + ) + + req.NoError(defStore.TruncateComposeRecords(ctx, nil)) + req.NoError(defStore.TruncateComposeModules(ctx)) + req.NoError(defStore.TruncateComposeNamespaces(ctx)) + + loadNewScenario(ctx, t) + + var ( + _, trace = mustExecWorkflow(ctx, t, "testing", autTypes.WorkflowExecParams{}) + ) + + req.Len(trace, 9) + + frame := trace[1] + req.True(frame.Scope.GetValue()["first"].Get().(bool)) + req.False(frame.Scope.GetValue()["last"].Get().(bool)) + aux := frame.Scope.GetValue()["i"].Get() + i, err := cast.ToIntE(aux) + req.NoError(err) + req.Equal(0, i) + + frame = trace[len(trace)-1] + req.False(frame.Scope.GetValue()["first"].Get().(bool)) + req.True(frame.Scope.GetValue()["last"].Get().(bool)) + aux = frame.Scope.GetValue()["i"].Get() + i, err = cast.ToIntE(aux) + req.NoError(err) + req.Equal(2, i) +} diff --git a/tests/workflows/testdata/iterator_sequence/workflow.yaml b/tests/workflows/testdata/iterator_sequence/workflow.yaml new file mode 100644 index 000000000..ccf7b0f95 --- /dev/null +++ b/tests/workflows/testdata/iterator_sequence/workflow.yaml @@ -0,0 +1,29 @@ +workflows: + testing: + enabled: true + trace: true + triggers: + - enabled: true + stepID: 10 + + steps: + - stepID: 10 + kind: iterator + ref: loopSequence + arguments: + - { target: "last", expr: "3", type: "Integer"} + - { target: "step", expr: "1", type: "Integer"} + results: + - { target: "i", expr: "counter" } + - { target: "first", expr: "isFirst" } + - { target: "last", expr: "isLast" } + + - stepID: 11 + kind: continue + + - stepID: 12 + kind: termination + + paths: + - { parentID: 10, childID: 11 } + - { parentID: 10, childID: 12 } diff --git a/tests/workflows/testdata/iterator_sequence_complex/workflow.yaml b/tests/workflows/testdata/iterator_sequence_complex/workflow.yaml new file mode 100644 index 000000000..3718a6a70 --- /dev/null +++ b/tests/workflows/testdata/iterator_sequence_complex/workflow.yaml @@ -0,0 +1,30 @@ +workflows: + testing: + enabled: true + trace: true + triggers: + - enabled: true + stepID: 10 + + steps: + - stepID: 10 + kind: iterator + ref: loopSequence + arguments: + - { target: "first", expr: "2", type: "Integer"} + - { target: "last", expr: "5", type: "Integer"} + - { target: "step", expr: "2", type: "Integer"} + results: + - { target: "i", expr: "counter" } + - { target: "first", expr: "isFirst" } + - { target: "last", expr: "isLast" } + + - stepID: 11 + kind: continue + + - stepID: 12 + kind: termination + + paths: + - { parentID: 10, childID: 11 } + - { parentID: 10, childID: 12 }