Debian Codegraph @

cmd/config/internal/commands/run_test.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3
4 package commands
5
6 import (
7 "io"
8 "os"
9 "strings"
10 "testing"
11
12 "github.com/spf13/cobra"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 "sigs.k8s.io/kustomize/kyaml/runfn"
16 )
17
18 // TestRunFnCommand_preRunE verifies that preRunE correctly parses the commandline
19 // flags and arguments into the RunFns structure to be executed.
20 func TestRunFnCommand_preRunE(t *testing.T) {
21 wd, err := os.Getwd()
22 require.NoError(t, err)
23 tests := []struct {
24 name string
25 args []string
26 expected string
27 expectedStruct *runfn.RunFns
28 err string
29 path string
30 input io.Reader
31 output io.Writer
32 functionPaths []string
33 network bool
34 mount []string
35 }{
36 {
37 name: "config map",
38 args: []string{"run", "dir", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
39 path: "dir",
40 expected: `
41 metadata:
42 name: function-input
43 annotations:
44 config.kubernetes.io/function: |
45 container: {image: 'foo:bar'}
46 data: {a: b, c: d, e: f}
47 kind: ConfigMap
48 apiVersion: v1
49 `,
50 },
51 {
52 name: "config map stdin / stdout",
53 args: []string{"run", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
54 input: os.Stdin,
55 output: os.Stdout,
56 expected: `
57 metadata:
58 name: function-input
59 annotations:
60 config.kubernetes.io/function: |
61 container: {image: 'foo:bar'}
62 data: {a: b, c: d, e: f}
63 kind: ConfigMap
64 apiVersion: v1
65 `,
66 },
67 {
68 name: "config map dry-run",
69 args: []string{"run", "dir", "--image", "foo:bar", "--dry-run", "--", "a=b", "c=d", "e=f"},
70 output: os.Stdout,
71 path: "dir",
72 expected: `
73 metadata:
74 name: function-input
75 annotations:
76 config.kubernetes.io/function: |
77 container: {image: 'foo:bar'}
78 data: {a: b, c: d, e: f}
79 kind: ConfigMap
80 apiVersion: v1
81 `,
82 },
83 {
84 name: "config map no args",
85 args: []string{"run", "dir", "--image", "foo:bar"},
86 path: "dir",
87 expected: `
88 metadata:
89 name: function-input
90 annotations:
91 config.kubernetes.io/function: |
92 container: {image: 'foo:bar'}
93 data: {}
94 kind: ConfigMap
95 apiVersion: v1
96 `,
97 },
98 {
99 name: "network enabled",
100 args: []string{"run", "dir", "--image", "foo:bar", "--network"},
101 path: "dir",
102 network: true,
103 expected: `
104 metadata:
105 name: function-input
106 annotations:
107 config.kubernetes.io/function: |
108 container: {image: 'foo:bar', network: true}
109 data: {}
110 kind: ConfigMap
111 apiVersion: v1
112 `,
113 },
114 {
115 name: "with network name",
116 args: []string{"run", "dir", "--image", "foo:bar", "--network"},
117 path: "dir",
118 network: true,
119 expected: `
120 metadata:
121 name: function-input
122 annotations:
123 config.kubernetes.io/function: |
124 container: {image: 'foo:bar', network: true}
125 data: {}
126 kind: ConfigMap
127 apiVersion: v1
128 `,
129 },
130 {
131 name: "custom kind",
132 args: []string{"run", "dir", "--image", "foo:bar", "--", "Foo", "g=h"},
133 path: "dir",
134 expected: `
135 metadata:
136 name: function-input
137 annotations:
138 config.kubernetes.io/function: |
139 container: {image: 'foo:bar'}
140 data: {g: h}
141 kind: Foo
142 apiVersion: v1
143 `,
144 },
145 {
146 name: "custom kind '=' in data",
147 args: []string{"run", "dir", "--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
148 path: "dir",
149 expected: `
150 metadata:
151 name: function-input
152 annotations:
153 config.kubernetes.io/function: |
154 container: {image: 'foo:bar'}
155 data: {g: h, i: j=k}
156 kind: Foo
157 apiVersion: v1
158 `,
159 },
160 {
161 name: "function paths",
162 args: []string{"run", "dir", "--fn-path", "path1", "--fn-path", "path2"},
163 path: "dir",
164 functionPaths: []string{"path1", "path2"},
165 },
166 {
167 name: "custom kind with function paths",
168 args: []string{
169 "run", "dir", "--fn-path", "path", "--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
170 path: "dir",
171 functionPaths: []string{"path"},
172 expected: `
173 metadata:
174 name: function-input
175 annotations:
176 config.kubernetes.io/function: |
177 container: {image: 'foo:bar'}
178 data: {g: h, i: j=k}
179 kind: Foo
180 apiVersion: v1
181 `,
182 },
183 {
184 name: "custom kind with storage mounts",
185 args: []string{
186 "run", "dir", "--mount", "type=bind,src=/mount/path,dst=/local/",
187 "--mount", "type=volume,src=myvol,dst=/local/",
188 "--mount", "type=tmpfs,dst=/local/",
189 "--image", "foo:bar", "--", "Foo", "g=h", "i=j=k"},
190 path: "dir",
191 mount: []string{"type=bind,src=/mount/path,dst=/local/", "type=volume,src=myvol,dst=/local/", "type=tmpfs,dst=/local/"},
192 expected: `
193 metadata:
194 name: function-input
195 annotations:
196 config.kubernetes.io/function: |
197 container: {image: 'foo:bar'}
198 data: {g: h, i: j=k}
199 kind: Foo
200 apiVersion: v1
201 `,
202 },
203 {
204 name: "results_dir",
205 args: []string{"run", "dir", "--results-dir", "foo/", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
206 path: "dir",
207 expectedStruct: &runfn.RunFns{
208 Path: "dir",
209 ResultsDir: "foo/",
210 Env: []string{},
211 WorkingDir: wd,
212 },
213 expected: `
214 metadata:
215 name: function-input
216 annotations:
217 config.kubernetes.io/function: |
218 container: {image: 'foo:bar'}
219 data: {a: b, c: d, e: f}
220 kind: ConfigMap
221 apiVersion: v1
222 `,
223 },
224 {
225 name: "config map multi args",
226 args: []string{"run", "dir", "dir2", "--image", "foo:bar", "--", "a=b", "c=d", "e=f"},
227 err: "0 or 1 arguments supported",
228 },
229 {
230 name: "config map not image",
231 args: []string{"run", "dir", "--", "a=b", "c=d", "e=f"},
232 err: "must specify --image",
233 },
234 {
235 name: "config map bad data",
236 args: []string{"run", "dir", "--image", "foo:bar", "--", "a=b", "c", "e=f"},
237 err: "must have keys and values separated by",
238 },
239 {
240 name: "log steps",
241 args: []string{"run", "dir", "--log-steps"},
242 path: "dir",
243 expectedStruct: &runfn.RunFns{
244 Path: "dir",
245 LogSteps: true,
246 Env: []string{},
247 WorkingDir: wd,
248 },
249 },
250 {
251 name: "envs",
252 args: []string{"run", "dir", "--env", "FOO=BAR", "-e", "BAR"},
253 path: "dir",
254 expectedStruct: &runfn.RunFns{
255 Path: "dir",
256 Env: []string{"FOO=BAR", "BAR"},
257 WorkingDir: wd,
258 },
259 },
260 {
261 name: "as current user",
262 args: []string{"run", "dir", "--as-current-user"},
263 path: "dir",
264 expectedStruct: &runfn.RunFns{
265 Path: "dir",
266 AsCurrentUser: true,
267 Env: []string{},
268 WorkingDir: wd,
269 },
270 },
271 }
272
273 for i := range tests {
274 tt := tests[i]
275 t.Run(tt.name, func(t *testing.T) {
276 r := GetRunFnRunner("kustomize")
277 // Don't run the actual command
278 r.Command.Run = nil
279 r.Command.RunE = func(cmd *cobra.Command, args []string) error { return nil }
280 r.Command.SilenceErrors = true
281 r.Command.SilenceUsage = true
282
283 // hack due to https://github.com/spf13/cobra/issues/42
284 root := &cobra.Command{Use: "root"}
285 root.AddCommand(r.Command)
286 root.SetArgs(tt.args)
287
288 // error case
289 err := r.Command.Execute()
290 if tt.err != "" {
291 if !assert.Error(t, err) {
292 t.FailNow()
293 }
294 if !assert.Contains(t, err.Error(), tt.err) {
295 t.FailNow()
296 }
297 // don't check anything else in error case
298 return
299 }
300
301 // non-error case
302 if !assert.NoError(t, err) {
303 t.FailNow()
304 }
305
306 // check if Input was set
307 if !assert.Equal(t, tt.input, r.RunFns.Input) {
308 t.FailNow()
309 }
310
311 // check if Output was set
312 if !assert.Equal(t, tt.output, r.RunFns.Output) {
313 t.FailNow()
314 }
315
316 // check if Path was set
317 if !assert.Equal(t, tt.path, r.RunFns.Path) {
318 t.FailNow()
319 }
320
321 // check if Network was set
322 if tt.network {
323 if !assert.Equal(t, tt.network, r.RunFns.Network) {
324 t.FailNow()
325 }
326 } else {
327 if !assert.Equal(t, false, r.RunFns.Network) {
328 t.FailNow()
329 }
330 }
331
332 // check if FunctionPaths were set
333 if tt.functionPaths == nil {
334 // make Equal work against flag default
335 tt.functionPaths = []string{}
336 }
337 if !assert.Equal(t, tt.functionPaths, r.RunFns.FunctionPaths) {
338 t.FailNow()
339 }
340
341 if !assert.Equal(t, r.RunFns, r.RunFns) {
342 t.FailNow()
343 }
344
345 if !assert.Equal(t, toStorageMounts(tt.mount), r.RunFns.StorageMounts) {
346 t.FailNow()
347 }
348
349 // check if Functions were set
350 if tt.expected != "" {
351 if !assert.Len(t, r.RunFns.Functions, 1) {
352 t.FailNow()
353 }
354 actual := strings.TrimSpace(r.RunFns.Functions[0].MustString())
355 if !assert.Equal(t, strings.TrimSpace(tt.expected), actual) {
356 t.FailNow()
357 }
358 }
359
360 if tt.expectedStruct != nil {
361 r.RunFns.Functions = nil
362 tt.expectedStruct.FunctionPaths = tt.functionPaths
363 if !assert.Equal(t, *tt.expectedStruct, r.RunFns) {
364 t.FailNow()
365 }
366 }
367 })
368 }
369 }