diff --git a/api/filters/annotations/annotations_test.go b/api/filters/annotations/annotations_test.go index 39fd8e2e2..d52cd673a 100644 --- a/api/filters/annotations/annotations_test.go +++ b/api/filters/annotations/annotations_test.go @@ -16,32 +16,15 @@ import ( var annosFs = builtinconfig.MakeDefaultConfig().CommonAnnotations -type setEntryArg struct { - Key string - Value string - Tag string - NodePath []string -} - -var setEntryArgs []setEntryArg - -func setEntryCallbackStub(key, value, tag string, node *yaml.RNode) { - setEntryArgs = append(setEntryArgs, setEntryArg{ - Key: key, - Value: value, - Tag: tag, - NodePath: node.FieldPath(), - }) -} - func TestAnnotations_Filter(t *testing.T) { + mutationTrackStub := filtertest_test.MutationTrackerStub{} testCases := map[string]struct { input string expectedOutput string filter Filter fsslice types.FsSlice setEntryCallback func(key, value, tag string, node *yaml.RNode) - expectedSetEntryArgs []setEntryArg + expectedSetEntryArgs []filtertest_test.SetValueArg }{ "add": { input: ` @@ -261,14 +244,14 @@ spec: "b": "b1", }, }, - setEntryCallback: setEntryCallbackStub, + setEntryCallback: mutationTrackStub.MutationTracker, fsslice: []types.FieldSpec{ { Path: "spec/template/metadata/annotations", CreateIfNotPresent: true, }, }, - expectedSetEntryArgs: []setEntryArg{ + expectedSetEntryArgs: []filtertest_test.SetValueArg{ { Key: "a", Value: "a1", @@ -298,7 +281,7 @@ spec: } for tn, tc := range testCases { - setEntryArgs = nil + mutationTrackStub.Reset() t.Run(tn, func(t *testing.T) { filter := tc.filter filter.WithMutationTracker(tc.setEntryCallback) @@ -308,7 +291,7 @@ spec: strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, filter))) { t.FailNow() } - if !assert.Equal(t, tc.expectedSetEntryArgs, setEntryArgs) { + if !assert.Equal(t, tc.expectedSetEntryArgs, mutationTrackStub.SetValueArgs()) { t.FailNow() } }) diff --git a/api/filters/imagetag/imagetag_test.go b/api/filters/imagetag/imagetag_test.go index a62f95a9f..d1f0bc1de 100644 --- a/api/filters/imagetag/imagetag_test.go +++ b/api/filters/imagetag/imagetag_test.go @@ -13,32 +13,15 @@ import ( "sigs.k8s.io/kustomize/kyaml/yaml" ) -type setValueArg struct { - Key string - Value string - Tag string - PrevValue string -} - -var setValueArgs []setValueArg - -func setValueCallbackStub(key, value, tag string, node *yaml.RNode) { - setValueArgs = append(setValueArgs, setValueArg{ - Key: key, - Value: value, - Tag: tag, - PrevValue: node.YNode().Value, - }) -} - func TestImageTagUpdater_Filter(t *testing.T) { + mutationTrackerStub := filtertest.MutationTrackerStub{} testCases := map[string]struct { input string expectedOutput string filter Filter fsSlice types.FsSlice setValueCallback func(key, value, tag string, node *yaml.RNode) - expectedSetValueArgs []setValueArg + expectedSetValueArgs []filtertest.SetValueArg }{ "ignore CustomResourceDefinition": { input: ` @@ -747,30 +730,30 @@ spec: Path: "spec/template/spec/initContainers[]/image", }, }, - setValueCallback: setValueCallbackStub, - expectedSetValueArgs: []setValueArg{ + setValueCallback: mutationTrackerStub.MutationTracker, + expectedSetValueArgs: []filtertest.SetValueArg{ { - Value: "busybox:v3", - PrevValue: "nginx:1.7.9", + Value: "busybox:v3", + NodePath: []string{"spec", "template", "spec", "containers", "image"}, }, { - Value: "busybox:v3", - PrevValue: "nginx:latest", + Value: "busybox:v3", + NodePath: []string{"spec", "template", "spec", "containers", "image"}, }, { - Value: "busybox:v3", - PrevValue: "nginx", + Value: "busybox:v3", + NodePath: []string{"spec", "template", "spec", "initContainers", "image"}, }, { - Value: "busybox:v3", - PrevValue: "nginx@sha256:111111111111111111", + Value: "busybox:v3", + NodePath: []string{"spec", "template", "spec", "initContainers", "image"}, }, }, }, } for tn, tc := range testCases { - setValueArgs = nil + mutationTrackerStub.Reset() t.Run(tn, func(t *testing.T) { filter := tc.filter filter.WithMutationTracker(tc.setValueCallback) @@ -780,7 +763,7 @@ spec: strings.TrimSpace(filtertest.RunFilter(t, tc.input, filter))) { t.FailNow() } - assert.Equal(t, tc.expectedSetValueArgs, setValueArgs) + assert.Equal(t, tc.expectedSetValueArgs, mutationTrackerStub.SetValueArgs()) }) } } diff --git a/api/filters/labels/labels_test.go b/api/filters/labels/labels_test.go index 45f4eff4e..0cdb0d126 100644 --- a/api/filters/labels/labels_test.go +++ b/api/filters/labels/labels_test.go @@ -14,31 +14,14 @@ import ( "sigs.k8s.io/kustomize/kyaml/yaml" ) -type setEntryArg struct { - Key string - Value string - Tag string - NodePath []string -} - -var setEntryArgs []setEntryArg - -func setEntryCallbackStub(key, value, tag string, node *yaml.RNode) { - setEntryArgs = append(setEntryArgs, setEntryArg{ - Key: key, - Value: value, - Tag: tag, - NodePath: node.FieldPath(), - }) -} - func TestLabels_Filter(t *testing.T) { + mutationTrackerStub := filtertest_test.MutationTrackerStub{} testCases := map[string]struct { input string expectedOutput string filter Filter setEntryCallback func(key, value, tag string, node *yaml.RNode) - expectedSetEntryArgs []setEntryArg + expectedSetEntryArgs []filtertest_test.SetValueArg }{ "add": { input: ` @@ -458,8 +441,8 @@ a: }, }, }, - setEntryCallback: setEntryCallbackStub, - expectedSetEntryArgs: []setEntryArg{ + setEntryCallback: mutationTrackerStub.MutationTracker, + expectedSetEntryArgs: []filtertest_test.SetValueArg{ { Key: "mage", Value: "yennefer", @@ -477,7 +460,7 @@ a: } for tn, tc := range testCases { - setEntryArgs = nil + mutationTrackerStub.Reset() t.Run(tn, func(t *testing.T) { tc.filter.WithMutationTracker(tc.setEntryCallback) if !assert.Equal(t, @@ -485,7 +468,7 @@ a: strings.TrimSpace(filtertest_test.RunFilter(t, tc.input, tc.filter))) { t.FailNow() } - if !assert.Equal(t, tc.expectedSetEntryArgs, setEntryArgs) { + if !assert.Equal(t, tc.expectedSetEntryArgs, mutationTrackerStub.SetValueArgs()) { t.FailNow() } }) diff --git a/api/testutils/filtertest/runfilter.go b/api/testutils/filtertest/runfilter.go index 2cf775311..95f8d26c3 100644 --- a/api/testutils/filtertest/runfilter.go +++ b/api/testutils/filtertest/runfilter.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/yaml" ) func run(input string, f kio.Filter) (string, error) { @@ -46,3 +47,32 @@ func RunFilterE(t *testing.T, input string, f kio.Filter) (string, error) { } return output, nil } + +type SetValueArg struct { + Key string + Value string + Tag string + NodePath []string +} + +// MutationTrackerStub to help stub a mutation tracker for kio.TrackableFilter +type MutationTrackerStub struct { + setValueArgs []SetValueArg +} + +func (mts *MutationTrackerStub) MutationTracker(key, value, tag string, node *yaml.RNode) { + mts.setValueArgs = append(mts.setValueArgs, SetValueArg{ + Key: key, + Value: value, + Tag: tag, + NodePath: node.FieldPath(), + }) +} + +func (mts *MutationTrackerStub) SetValueArgs() []SetValueArg { + return mts.setValueArgs +} + +func (mts *MutationTrackerStub) Reset() { + mts.setValueArgs = nil +}