1
2
3
4 package replicacount
5
6 import (
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 filtertest_test "sigs.k8s.io/kustomize/api/testutils/filtertest"
12 "sigs.k8s.io/kustomize/api/types"
13 "sigs.k8s.io/kustomize/kyaml/yaml"
14 )
15
16 func TestFilter(t *testing.T) {
17 mutationTrackerStub := filtertest_test.MutationTrackerStub{}
18 testCases := map[string]struct {
19 input string
20 expected string
21 filter Filter
22 mutationTracker func(key, value, tag string, node *yaml.RNode)
23 expectedSetValueArgs []filtertest_test.SetValueArg
24 }{
25 "update field": {
26 input: `
27 apiVersion: apps/v1
28 kind: Deployment
29 metadata:
30 name: dep
31 spec:
32 replicas: 5
33 `,
34 expected: `
35 apiVersion: apps/v1
36 kind: Deployment
37 metadata:
38 name: dep
39 spec:
40 replicas: 42
41 `,
42 filter: Filter{
43 Replica: types.Replica{
44 Name: "dep",
45 Count: 42,
46 },
47 FieldSpec: types.FieldSpec{Path: "spec/replicas"},
48 },
49 },
50 "add field": {
51 input: `
52 apiVersion: custom/v1
53 kind: Custom
54 metadata:
55 name: cus
56 spec:
57 template:
58 other: something
59 `,
60 expected: `
61 apiVersion: custom/v1
62 kind: Custom
63 metadata:
64 name: cus
65 spec:
66 template:
67 other: something
68 replicas: 42
69 `,
70 filter: Filter{
71 Replica: types.Replica{
72 Name: "cus",
73 Count: 42,
74 },
75 FieldSpec: types.FieldSpec{
76 Path: "spec/template/replicas",
77 CreateIfNotPresent: true,
78 },
79 },
80 },
81
82 "add_field_null": {
83 input: `
84 apiVersion: custom/v1
85 kind: Custom
86 metadata:
87 name: cus
88 spec:
89 template:
90 other: something
91 replicas: null
92 `,
93 expected: `
94 apiVersion: custom/v1
95 kind: Custom
96 metadata:
97 name: cus
98 spec:
99 template:
100 other: something
101 replicas: 42
102 `,
103 filter: Filter{
104 Replica: types.Replica{
105 Name: "cus",
106 Count: 42,
107 },
108 FieldSpec: types.FieldSpec{
109 Path: "spec/template/replicas",
110 CreateIfNotPresent: true,
111 },
112 },
113 },
114 "no update if CreateIfNotPresent is false": {
115 input: `
116 apiVersion: custom/v1
117 kind: Custom
118 metadata:
119 name: cus
120 spec:
121 template:
122 other: something
123 `,
124 expected: `
125 apiVersion: custom/v1
126 kind: Custom
127 metadata:
128 name: cus
129 spec:
130 template:
131 other: something
132 `,
133 filter: Filter{
134 Replica: types.Replica{
135 Name: "cus",
136 Count: 42,
137 },
138 FieldSpec: types.FieldSpec{
139 Path: "spec/template/replicas",
140 },
141 },
142 },
143 "update multiple fields": {
144 input: `
145 apiVersion: custom/v1
146 kind: Custom
147 metadata:
148 name: cus
149 spec:
150 template:
151 replicas: 5
152 `,
153 expected: `
154 apiVersion: custom/v1
155 kind: Custom
156 metadata:
157 name: cus
158 spec:
159 template:
160 replicas: 42
161 `,
162 filter: Filter{
163 Replica: types.Replica{
164 Name: "cus",
165 Count: 42,
166 },
167 FieldSpec: types.FieldSpec{Path: "spec/template/replicas"},
168 },
169 },
170 "mutation tracker": {
171 input: `
172 apiVersion: apps/v1
173 kind: Deployment
174 metadata:
175 name: dep
176 spec:
177 replicas: 5
178 `,
179 expected: `
180 apiVersion: apps/v1
181 kind: Deployment
182 metadata:
183 name: dep
184 spec:
185 replicas: 42
186 `,
187 filter: Filter{
188 Replica: types.Replica{
189 Name: "dep",
190 Count: 42,
191 },
192 FieldSpec: types.FieldSpec{Path: "spec/replicas"},
193 },
194 mutationTracker: mutationTrackerStub.MutationTracker,
195 expectedSetValueArgs: []filtertest_test.SetValueArg{
196 {
197 Value: "42",
198 Tag: "!!int",
199 NodePath: []string{"spec", "replicas"},
200 },
201 },
202 },
203 }
204
205 for tn, tc := range testCases {
206 mutationTrackerStub.Reset()
207 tc.filter.WithMutationTracker(tc.mutationTracker)
208 t.Run(tn, func(t *testing.T) {
209 if !assert.Equal(t,
210 strings.TrimSpace(tc.expected),
211 strings.TrimSpace(
212 filtertest_test.RunFilter(t, tc.input, tc.filter))) {
213 t.FailNow()
214 }
215 assert.Equal(t, tc.expectedSetValueArgs, mutationTrackerStub.SetValueArgs())
216 })
217 }
218 }
219
View as plain text