Debian Codegraph @

cmd/config/internal/commands/grep_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package commands_test
5
6 import (
7 "bytes"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "sigs.k8s.io/kustomize/cmd/config/internal/commands"
16 "sigs.k8s.io/kustomize/kyaml/copyutil"
17 "sigs.k8s.io/kustomize/kyaml/filesys"
18 )
19
20 // TestGrepCommand_files verifies grep reads the files and filters them
21 func TestGrepCommand_files(t *testing.T) {
22 d := t.TempDir()
23
24 err := os.WriteFile(filepath.Join(d, "f1.yaml"), []byte(`
25 kind: Deployment
26 metadata:
27 labels:
28 app: nginx2
29 name: foo
30 annotations:
31 app: nginx2
32 spec:
33 replicas: 1
34 ---
35 kind: Service
36 metadata:
37 name: foo
38 annotations:
39 app: nginx
40 spec:
41 selector:
42 app: nginx
43 `), 0600)
44 if !assert.NoError(t, err) {
45 return
46 }
47 err = os.WriteFile(filepath.Join(d, "f2.yaml"), []byte(`kind: Deployment
48 metadata:
49 labels:
50 app: nginx
51 name: bar
52 annotations:
53 app: nginx
54 spec:
55 replicas: 3
56 `), 0600)
57 if !assert.NoError(t, err) {
58 return
59 }
60
61 // fmt the files
62 b := &bytes.Buffer{}
63 r := commands.GetGrepRunner("")
64 r.Command.SetArgs([]string{"metadata.name=foo", d})
65 r.Command.SetOut(b)
66 if !assert.NoError(t, r.Command.Execute()) {
67 return
68 }
69
70 if !assert.Contains(t, b.String(), `kind: Deployment
71 metadata:
72 labels:
73 app: nginx2
74 name: foo
75 annotations:
76 app: nginx2
77 config.kubernetes.io/index: '0'
78 config.kubernetes.io/path: 'f1.yaml'
79 internal.config.kubernetes.io/index: '0'
80 internal.config.kubernetes.io/path: 'f1.yaml'
81 spec:
82 replicas: 1
83 ---
84 kind: Service
85 metadata:
86 name: foo
87 annotations:
88 app: nginx
89 config.kubernetes.io/index: '1'
90 config.kubernetes.io/path: 'f1.yaml'
91 internal.config.kubernetes.io/index: '1'
92 internal.config.kubernetes.io/path: 'f1.yaml'
93 spec:
94 selector:
95 app: nginx
96 `) {
97 return
98 }
99 }
100
101 // TestCmd_stdin verifies the grep command reads stdin if no files are provided
102 func TestGrepCmd_stdin(t *testing.T) {
103 // fmt the files
104 b := &bytes.Buffer{}
105 r := commands.GetGrepRunner("")
106 r.Command.SetArgs([]string{"metadata.name=foo"})
107 r.Command.SetOut(b)
108 r.Command.SetIn(bytes.NewBufferString(`
109 kind: Deployment
110 metadata:
111 labels:
112 app: nginx2
113 name: foo
114 annotations:
115 app: nginx2
116 spec:
117 replicas: 1
118 ---
119 kind: Service
120 metadata:
121 name: foo
122 annotations:
123 app: nginx
124 spec:
125 selector:
126 app: nginx
127 ---
128 kind: Deployment
129 metadata:
130 labels:
131 app: nginx
132 name: bar
133 annotations:
134 app: nginx
135 spec:
136 replicas: 3
137 `))
138 if !assert.NoError(t, r.Command.Execute()) {
139 return
140 }
141
142 if !assert.Contains(t, b.String(), `kind: Deployment
143 metadata:
144 labels:
145 app: nginx2
146 name: foo
147 annotations:
148 app: nginx2
149 config.kubernetes.io/index: '0'
150 internal.config.kubernetes.io/index: '0'
151 spec:
152 replicas: 1
153 ---
154 kind: Service
155 metadata:
156 name: foo
157 annotations:
158 app: nginx
159 config.kubernetes.io/index: '1'
160 internal.config.kubernetes.io/index: '1'
161 spec:
162 selector:
163 app: nginx
164 `) {
165 return
166 }
167 }
168
169 // TestGrepCmd_errInputs verifies the grep command errors on invalid matches
170 func TestGrepCmd_errInputs(t *testing.T) {
171 b := &bytes.Buffer{}
172 r := commands.GetGrepRunner("")
173 r.Command.SetArgs([]string{"metadata.name=foo=bar"})
174 r.Command.SetOut(b)
175 r.Command.SetIn(bytes.NewBufferString(`
176 kind: Deployment
177 metadata:
178 labels:
179 app: nginx2
180 name: foo
181 annotations:
182 app: nginx2
183 spec:
184 replicas: 1
185 `))
186 err := r.Command.Execute()
187 if !assert.Error(t, err) {
188 return
189 }
190 assert.Contains(t, err.Error(), "ambiguous match")
191
192 // fmt the files
193 b = &bytes.Buffer{}
194 r = commands.GetGrepRunner("")
195 r.Command.SetArgs([]string{"spec.template.spec.containers[a[b=c].image=foo"})
196 r.Command.SetOut(b)
197 r.Command.SetIn(bytes.NewBufferString(`
198 kind: Deployment
199 metadata:
200 labels:
201 app: nginx2
202 name: foo
203 annotations:
204 app: nginx2
205 spec:
206 replicas: 1
207 `))
208 err = r.Command.Execute()
209 if !assert.Error(t, err) {
210 return
211 }
212 assert.Contains(t, err.Error(), "unrecognized path element:")
213 }
214
215 // TestGrepCommand_escapeDots verifies the grep command correctly escapes '\.' in inputs
216 func TestGrepCommand_escapeDots(t *testing.T) {
217 // fmt the files
218 b := &bytes.Buffer{}
219 r := commands.GetGrepRunner("")
220 r.Command.SetArgs([]string{"spec.template.spec.containers[name=nginx].image=nginx:1\\.7\\.9",
221 "--annotate=false"})
222 r.Command.SetOut(b)
223 r.Command.SetIn(bytes.NewBufferString(`
224 kind: Deployment
225 metadata:
226 labels:
227 app: nginx1.8
228 name: nginx1.8
229 annotations:
230 app: nginx1.8
231 spec:
232 replicas: 1
233 template:
234 spec:
235 containers:
236 - name: nginx
237 image: nginx:1.8.1
238 ---
239 kind: Deployment
240 metadata:
241 labels:
242 app: nginx1.7
243 name: nginx1.7
244 annotations:
245 app: nginx1.7
246 spec:
247 replicas: 1
248 template:
249 spec:
250 containers:
251 - name: nginx
252 image: nginx:1.7.9
253 `))
254 err := r.Command.Execute()
255 if !assert.NoError(t, err) {
256 return
257 }
258 if !assert.Contains(t, b.String(), `kind: Deployment
259 metadata:
260 labels:
261 app: nginx1.7
262 name: nginx1.7
263 annotations:
264 app: nginx1.7
265 spec:
266 replicas: 1
267 template:
268 spec:
269 containers:
270 - name: nginx
271 image: nginx:1.7.9
272 `) {
273 return
274 }
275 }
276
277 func TestGrepSubPackages(t *testing.T) {
278 var tests = []struct {
279 name string
280 dataset string
281 packagePath string
282 args []string
283 expected string
284 }{
285 {
286 name: "grep-recurse-subpackages",
287 dataset: "dataset-without-setters",
288 args: []string{"kind=Deployment"},
289 expected: `
290 # Copyright 2019 The Kubernetes Authors.
291 # SPDX-License-Identifier: Apache-2.0
292
293 apiVersion: apps/v1
294 kind: Deployment
295 metadata:
296 namespace: myspace
297 name: mysql-deployment
298 annotations:
299 config.kubernetes.io/index: '0'
300 config.kubernetes.io/path: 'deployment.yaml'
301 internal.config.kubernetes.io/index: '0'
302 internal.config.kubernetes.io/path: 'deployment.yaml'
303 spec:
304 replicas: 3
305 template:
306 spec:
307 containers:
308 - name: mysql
309 image: mysql:1.7.9
310 ---
311 # Copyright 2019 The Kubernetes Authors.
312 # SPDX-License-Identifier: Apache-2.0
313
314 apiVersion: apps/v1
315 kind: Deployment
316 metadata:
317 namespace: myspace
318 name: storage-deployment
319 annotations:
320 config.kubernetes.io/index: '0'
321 config.kubernetes.io/path: 'deployment.yaml'
322 internal.config.kubernetes.io/index: '0'
323 internal.config.kubernetes.io/path: 'deployment.yaml'
324 spec:
325 replicas: 4
326 template:
327 spec:
328 containers:
329 - name: storage
330 image: storage:1.7.7
331 `,
332 },
333 {
334 name: "grep-top-level-pkg-no-recurse-subpackages",
335 dataset: "dataset-without-setters",
336 args: []string{"kind=Deployment", "-R=false"},
337 packagePath: "mysql",
338 expected: `# Copyright 2019 The Kubernetes Authors.
339 # SPDX-License-Identifier: Apache-2.0
340
341 apiVersion: apps/v1
342 kind: Deployment
343 metadata:
344 namespace: myspace
345 name: mysql-deployment
346 annotations:
347 config.kubernetes.io/index: '0'
348 config.kubernetes.io/path: 'deployment.yaml'
349 internal.config.kubernetes.io/index: '0'
350 internal.config.kubernetes.io/path: 'deployment.yaml'
351 spec:
352 replicas: 3
353 template:
354 spec:
355 containers:
356 - name: mysql
357 image: mysql:1.7.9
358 `,
359 },
360 {
361 name: "grep-nested-pkg-no-recurse-subpackages",
362 dataset: "dataset-without-setters",
363 packagePath: "mysql/storage",
364 args: []string{"kind=Deployment", "-R=false"},
365 expected: `# Copyright 2019 The Kubernetes Authors.
366 # SPDX-License-Identifier: Apache-2.0
367
368 apiVersion: apps/v1
369 kind: Deployment
370 metadata:
371 namespace: myspace
372 name: storage-deployment
373 annotations:
374 config.kubernetes.io/index: '0'
375 config.kubernetes.io/path: 'deployment.yaml'
376 internal.config.kubernetes.io/index: '0'
377 internal.config.kubernetes.io/path: 'deployment.yaml'
378 spec:
379 replicas: 4
380 template:
381 spec:
382 containers:
383 - name: storage
384 image: storage:1.7.7
385 `,
386 },
387 {
388 name: "grep-recurse-subpackages-no-result",
389 dataset: "dataset-without-setters",
390 args: []string{"kind=ConfigMap"},
391 expected: `
392
393 `,
394 },
395 }
396 for i := range tests {
397 test := tests[i]
398 t.Run(test.name, func(t *testing.T) {
399 sourceDir := filepath.Join("test", "testdata", test.dataset)
400 baseDir := t.TempDir()
401 require.NoError(t, copyutil.CopyDir(filesys.MakeFsOnDisk(), sourceDir, baseDir))
402 runner := commands.GetGrepRunner("")
403 actual := &bytes.Buffer{}
404 runner.Command.SetOut(actual)
405 runner.Command.SetArgs(append(test.args, filepath.Join(baseDir, test.packagePath)))
406 err := runner.Command.Execute()
407 if !assert.NoError(t, err) {
408 t.FailNow()
409 }
410
411 // normalize path format for windows
412 actualNormalized := strings.ReplaceAll(
413 strings.ReplaceAll(actual.String(), "\\", "/"),
414 "//", "/")
415
416 expected := strings.ReplaceAll(test.expected, "${baseDir}", baseDir)
417 expectedNormalized := strings.ReplaceAll(expected, "\\", "/")
418 if !assert.Equal(t, expectedNormalized, actualNormalized) {
419 t.FailNow()
420 }
421 })
422 }
423 }
424
425 // TestGrepCmd_noQuery verifies the grep command errors when QUERY argument is missing
426 func TestGrepCmd_noQuery(t *testing.T) {
427 b := &bytes.Buffer{}
428 r := commands.GetGrepRunner("")
429 // No QUERY argument
430 r.Command.SetArgs([]string{})
431 r.Command.SetOut(b)
432
433 err := r.Command.Execute()
434 if assert.Error(t, err) {
435 assert.Contains(t, err.Error(), "missing required argument: QUERY")
436 }
437 }