2023-05-24 12:45:19 +00:00
|
|
|
package bundle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSeqMutator(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
seqMutator := Seq(m1, m2, m3)
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, seqMutator)
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-05-24 12:45:19 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, m3.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqWithDeferredMutator(t *testing.T) {
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
m4 := &testMutator{}
|
|
|
|
seqMutator := Seq(m1, Defer(m2, m3), m4)
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, seqMutator)
|
|
|
|
assert.NoError(t, diags.Error())
|
2023-05-24 12:45:19 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, m3.applyCalled)
|
|
|
|
assert.Equal(t, 1, m4.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqWithErrorAndDeferredMutator(t *testing.T) {
|
|
|
|
errorMut := &mutatorWithError{errorMsg: "error msg"}
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
seqMutator := Seq(errorMut, Defer(m1, m2), m3)
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, seqMutator)
|
|
|
|
assert.Error(t, diags.Error())
|
2023-05-24 12:45:19 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, errorMut.applyCalled)
|
|
|
|
assert.Equal(t, 0, m1.applyCalled)
|
|
|
|
assert.Equal(t, 0, m2.applyCalled)
|
|
|
|
assert.Equal(t, 0, m3.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqWithErrorInsideDeferredMutator(t *testing.T) {
|
|
|
|
errorMut := &mutatorWithError{errorMsg: "error msg"}
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
seqMutator := Seq(m1, Defer(errorMut, m2), m3)
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, seqMutator)
|
|
|
|
assert.Error(t, diags.Error())
|
2023-05-24 12:45:19 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, errorMut.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 0, m3.applyCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeqWithErrorInsideFinallyStage(t *testing.T) {
|
|
|
|
errorMut := &mutatorWithError{errorMsg: "error msg"}
|
|
|
|
m1 := &testMutator{}
|
|
|
|
m2 := &testMutator{}
|
|
|
|
m3 := &testMutator{}
|
|
|
|
seqMutator := Seq(m1, Defer(m2, errorMut), m3)
|
|
|
|
|
2023-11-15 14:03:36 +00:00
|
|
|
b := &Bundle{}
|
2024-03-25 14:18:47 +00:00
|
|
|
diags := Apply(context.Background(), b, seqMutator)
|
|
|
|
assert.Error(t, diags.Error())
|
2023-05-24 12:45:19 +00:00
|
|
|
|
|
|
|
assert.Equal(t, 1, m1.applyCalled)
|
|
|
|
assert.Equal(t, 1, m2.applyCalled)
|
|
|
|
assert.Equal(t, 1, errorMut.applyCalled)
|
|
|
|
assert.Equal(t, 0, m3.applyCalled)
|
|
|
|
}
|