-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathcommands_test.go
711 lines (659 loc) · 18.8 KB
/
commands_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package main
import (
"errors"
"io"
"os"
"testing"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/types"
"github.com/nginx/nginx-gateway-fabric/internal/mode/static/config"
)
type flagTestCase struct {
name string
expectedErrPrefix string
args []string
wantErr bool
}
func testFlag(t *testing.T, cmd *cobra.Command, test flagTestCase) {
t.Helper()
g := NewWithT(t)
// discard any output generated by cobra
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
// override RunE to avoid executing the command
cmd.RunE = func(_ *cobra.Command, _ []string) error {
return nil
}
cmd.SetArgs(test.args)
err := cmd.Execute()
if test.wantErr {
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(HavePrefix(test.expectedErrPrefix))
} else {
g.Expect(err).NotTo(HaveOccurred())
}
}
func TestRootCmd(t *testing.T) {
t.Parallel()
testCase := flagTestCase{
name: "no flags",
args: nil,
wantErr: false,
}
testFlag(t, createRootCommand(), testCase)
}
func TestCommonFlagsValidation(t *testing.T) {
t.Parallel()
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway",
"--gatewayclass=nginx",
},
wantErr: false,
},
{
name: "gateway-ctlr-name is not set",
args: []string{
"--gatewayclass=nginx",
},
wantErr: true,
expectedErrPrefix: `required flag(s) "gateway-ctlr-name" not set`,
},
{
name: "gateway-ctlr-name is set to empty string",
args: []string{
"--gateway-ctlr-name=",
"--gatewayclass=nginx",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--gateway-ctlr-name" flag: must be set`,
},
{
name: "gateway-ctlr-name is invalid",
args: []string{
"--gateway-ctlr-name=nginx-gateway",
"--gatewayclass=nginx",
},
wantErr: true,
expectedErrPrefix: `invalid argument "nginx-gateway" for "--gateway-ctlr-name" flag: invalid format; ` +
"must be DOMAIN/PATH",
},
{
name: "gatewayclass is not set",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway",
},
wantErr: true,
expectedErrPrefix: `required flag(s) "gatewayclass" not set`,
},
{
name: "gatewayclass is set to empty string",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway",
"--gatewayclass=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--gatewayclass" flag: must be set`,
},
{
name: "gatewayclass is invalid",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway",
"--gatewayclass=@",
},
wantErr: true,
expectedErrPrefix: `invalid argument "@" for "--gatewayclass" flag: invalid format`,
},
}
for _, test := range tests {
t.Run(test.name+"_static_mode", func(t *testing.T) {
t.Parallel()
testFlag(t, createStaticModeCommand(), test)
})
t.Run(test.name+"_provisioner_mode", func(t *testing.T) {
t.Parallel()
testFlag(t, createProvisionerModeCommand(), test)
})
}
}
func TestStaticModeCmdFlagValidation(t *testing.T) {
t.Parallel()
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway", // common and required flag
"--gatewayclass=nginx", // common and required flag
"--gateway=nginx-gateway/nginx",
"--config=nginx-gateway-config",
"--service=nginx-gateway",
"--update-gatewayclass-status=true",
"--metrics-port=9114",
"--metrics-disable",
"--metrics-secure-serving",
"--health-port=8081",
"--health-disable",
"--leader-election-lock-name=my-lock",
"--leader-election-disable=false",
"--nginx-plus",
"--usage-report-secret=my-secret",
"--usage-report-endpoint=example.com",
"--usage-report-resolver=resolver.com",
"--usage-report-ca-secret=ca-secret",
"--usage-report-client-ssl-secret=client-secret",
"--snippets-filters",
},
wantErr: false,
},
{
name: "valid flags, non-required not set",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway", // common and required flag
"--gatewayclass=nginx", // common and required flag,
},
wantErr: false,
},
{
name: "gateway is set to empty string",
args: []string{
"--gateway=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--gateway" flag: must be set`,
},
{
name: "gateway is invalid",
args: []string{
"--gateway=nginx-gateway", // no namespace
},
wantErr: true,
expectedErrPrefix: `invalid argument "nginx-gateway" for "--gateway" flag: invalid format; ` +
"must be NAMESPACE/NAME",
},
{
name: "config is set to empty string",
args: []string{
"--config=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "-c, --config" flag: must be set`,
},
{
name: "config is set to invalid string",
args: []string{
"--config=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "-c, --config" flag: invalid format`,
},
{
name: "service is set to empty string",
args: []string{
"--service=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--service" flag: must be set`,
},
{
name: "service is set to invalid string",
args: []string{
"--service=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--service" flag: invalid format`,
},
{
name: "update-gatewayclass-status is set to empty string",
args: []string{
"--update-gatewayclass-status=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--update-gatewayclass-status" flag: strconv.ParseBool`,
},
{
name: "update-gatewayclass-status is invalid",
args: []string{
"--update-gatewayclass-status=invalid", // not a boolean
},
wantErr: true,
expectedErrPrefix: `invalid argument "invalid" for "--update-gatewayclass-status" flag: strconv.ParseBool`,
},
{
name: "metrics-port is invalid type",
args: []string{
"--metrics-port=invalid", // not an int
},
wantErr: true,
expectedErrPrefix: `invalid argument "invalid" for "--metrics-port" flag: failed to parse int value:` +
` strconv.ParseInt: parsing "invalid": invalid syntax`,
},
{
name: "metrics-port is outside of range",
args: []string{
"--metrics-port=999", // outside of range
},
wantErr: true,
expectedErrPrefix: `invalid argument "999" for "--metrics-port" flag:` +
` port outside of valid port range [1024 - 65535]: 999`,
},
{
name: "metrics-disable is not a bool",
args: []string{
"--metrics-disable=999", // not a bool
},
wantErr: true,
expectedErrPrefix: `invalid argument "999" for "--metrics-disable" flag: strconv.ParseBool:` +
` parsing "999": invalid syntax`,
},
{
name: "metrics-secure-serving is not a bool",
args: []string{
"--metrics-secure-serving=999", // not a bool
},
wantErr: true,
expectedErrPrefix: `invalid argument "999" for "--metrics-secure-serving" flag: strconv.ParseBool:` +
` parsing "999": invalid syntax`,
},
{
name: "health-port is invalid type",
args: []string{
"--health-port=invalid", // not an int
},
wantErr: true,
expectedErrPrefix: `invalid argument "invalid" for "--health-port" flag: failed to parse int value:` +
` strconv.ParseInt: parsing "invalid": invalid syntax`,
},
{
name: "health-port is outside of range",
args: []string{
"--health-port=999", // outside of range
},
wantErr: true,
expectedErrPrefix: `invalid argument "999" for "--health-port" flag:` +
` port outside of valid port range [1024 - 65535]: 999`,
},
{
name: "health-disable is not a bool",
args: []string{
"--health-disable=999", // not a bool
},
wantErr: true,
expectedErrPrefix: `invalid argument "999" for "--health-disable" flag: strconv.ParseBool:` +
` parsing "999": invalid syntax`,
},
{
name: "leader-election-lock-name is set to invalid string",
args: []string{
"--leader-election-lock-name=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--leader-election-lock-name" flag: invalid format`,
},
{
name: "leader-election-disable is set to empty string",
args: []string{
"--leader-election-disable=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--leader-election-disable" flag: strconv.ParseBool`,
},
{
name: "usage-report-secret is set to empty string",
args: []string{
"--usage-report-secret=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--usage-report-secret" flag: must be set`,
},
{
name: "usage-report-secret is invalid",
args: []string{
"--usage-report-secret=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--usage-report-secret" flag: invalid format: `,
},
{
name: "usage-report-endpoint is set to empty string",
args: []string{
"--usage-report-endpoint=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--usage-report-endpoint" flag: must be set`,
},
{
name: "usage-report-endpoint is an invalid endpoint",
args: []string{
"--usage-report-endpoint=$*(invalid)",
},
wantErr: true,
expectedErrPrefix: `invalid argument "$*(invalid)" for "--usage-report-endpoint" flag: ` +
`"$*(invalid)" must be a domain name or IP address with optional port`,
},
{
name: "usage-report-resolver is set to empty string",
args: []string{
"--usage-report-resolver=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--usage-report-resolver" flag: must be set`,
},
{
name: "usage-report-resolver is an invalid endpoint",
args: []string{
"--usage-report-resolver=$*(invalid)",
},
wantErr: true,
expectedErrPrefix: `invalid argument "$*(invalid)" for "--usage-report-resolver" flag: ` +
`"$*(invalid)" must be a domain name or IP address with optional port`,
},
{
name: "usage-report-ca-secret is set to empty string",
args: []string{
"--usage-report-ca-secret=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--usage-report-ca-secret" flag: must be set`,
},
{
name: "usage-report-ca-secret is invalid",
args: []string{
"--usage-report-ca-secret=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--usage-report-ca-secret" flag: invalid format: `,
},
{
name: "usage-report-client-ssl-secret is set to empty string",
args: []string{
"--usage-report-client-ssl-secret=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--usage-report-client-ssl-secret" flag: must be set`,
},
{
name: "usage-report-client-ssl-secret is invalid",
args: []string{
"--usage-report-client-ssl-secret=!@#$",
},
wantErr: true,
expectedErrPrefix: `invalid argument "!@#$" for "--usage-report-client-ssl-secret" flag: invalid format: `,
},
{
name: "snippets-filters is not a bool",
expectedErrPrefix: `invalid argument "not-a-bool" for "--snippets-filters" flag: strconv.ParseBool:` +
` parsing "not-a-bool": invalid syntax`,
args: []string{
"--snippets-filters=not-a-bool",
},
wantErr: true,
},
}
// common flags validation is tested separately
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cmd := createStaticModeCommand()
testFlag(t, cmd, test)
})
}
}
func TestProvisionerModeCmdFlagValidation(t *testing.T) {
t.Parallel()
testCase := flagTestCase{
name: "valid flags",
args: []string{
"--gateway-ctlr-name=gateway.nginx.org/nginx-gateway", // common and required flag
"--gatewayclass=nginx", // common and required flag
},
wantErr: false,
}
// common flags validation is tested separately
testFlag(t, createProvisionerModeCommand(), testCase)
}
func TestSleepCmdFlagValidation(t *testing.T) {
t.Parallel()
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--duration=1s",
},
wantErr: false,
},
{
name: "omitted flags",
args: nil,
wantErr: false,
},
{
name: "duration is set to empty string",
args: []string{
"--duration=",
},
wantErr: true,
expectedErrPrefix: `invalid argument "" for "--duration" flag: time: invalid duration ""`,
},
{
name: "duration is invalid",
args: []string{
"--duration=invalid",
},
wantErr: true,
expectedErrPrefix: `invalid argument "invalid" for "--duration" flag: time: invalid duration "invalid"`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cmd := createSleepCommand()
testFlag(t, cmd, test)
})
}
}
func TestInitializeCmdFlagValidation(t *testing.T) {
t.Parallel()
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--source=/my/file",
"--destination=dest/file",
"--nginx-plus",
},
wantErr: false,
},
{
name: "omitted flags",
args: nil,
wantErr: false,
},
{
name: "source set without destination",
args: []string{
"--source=/my/file",
},
wantErr: true,
expectedErrPrefix: "if any flags in the group [source destination] are set they must all be set; " +
"missing [destination]",
},
{
name: "destination set without source",
args: []string{
"--destination=/dest/file",
},
wantErr: true,
expectedErrPrefix: "if any flags in the group [source destination] are set they must all be set; " +
"missing [source]",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
cmd := createInitializeCommand()
testFlag(t, cmd, test)
})
}
}
func TestParseFlags(t *testing.T) {
t.Parallel()
g := NewWithT(t)
flagSet := pflag.NewFlagSet("flagSet", 0)
// set SortFlags to false for testing purposes so when parseFlags loops over the flagSet it
// goes off of primordial order.
flagSet.SortFlags = false
var boolFlagTrue bool
flagSet.BoolVar(
&boolFlagTrue,
"boolFlagTrue",
true,
"boolean true test flag",
)
var boolFlagFalse bool
flagSet.BoolVar(
&boolFlagFalse,
"boolFlagFalse",
false,
"boolean false test flag",
)
customIntFlagDefault := intValidatingValue{
validator: validatePort,
value: 8080,
}
flagSet.Var(
&customIntFlagDefault,
"customIntFlagDefault",
"default custom int test flag",
)
customIntFlagUserDefined := intValidatingValue{
validator: validatePort,
value: 8080,
}
flagSet.Var(
&customIntFlagUserDefined,
"customIntFlagUserDefined",
"user defined custom int test flag",
)
err := flagSet.Set("customIntFlagUserDefined", "8081")
g.Expect(err).To(Not(HaveOccurred()))
customStringFlagDefault := stringValidatingValue{
validator: validateResourceName,
value: "default-custom-string-test-flag",
}
flagSet.Var(
&customStringFlagDefault,
"customStringFlagDefault",
"default custom string test flag",
)
customStringFlagUserDefined := stringValidatingValue{
validator: validateResourceName,
value: "user-defined-custom-string-test-flag",
}
flagSet.Var(
&customStringFlagUserDefined,
"customStringFlagUserDefined",
"user defined custom string test flag",
)
err = flagSet.Set("customStringFlagUserDefined", "changed-test-flag-value")
g.Expect(err).To(Not(HaveOccurred()))
customStringFlagNoDefaultValueUnset := namespacedNameValue{
value: types.NamespacedName{},
}
flagSet.Var(
&customStringFlagNoDefaultValueUnset,
"customStringFlagNoDefaultValueUnset",
"no default value custom string test flag",
)
customStringFlagNoDefaultValueUserDefined := namespacedNameValue{
value: types.NamespacedName{},
}
flagSet.Var(
&customStringFlagNoDefaultValueUserDefined,
"customStringFlagNoDefaultValueUserDefined",
"no default value but with user defined namespacedName test flag",
)
userDefinedNamespacedName := types.NamespacedName{
Namespace: "changed-namespace",
Name: "changed-name",
}
err = flagSet.Set("customStringFlagNoDefaultValueUserDefined", userDefinedNamespacedName.String())
g.Expect(err).To(Not(HaveOccurred()))
expectedKeys := []string{
"boolFlagTrue",
"boolFlagFalse",
"customIntFlagDefault",
"customIntFlagUserDefined",
"customStringFlagDefault",
"customStringFlagUserDefined",
"customStringFlagNoDefaultValueUnset",
"customStringFlagNoDefaultValueUserDefined",
}
expectedValues := []string{
"true",
"false",
"default",
"user-defined",
"default",
"user-defined",
"default",
"user-defined",
}
flagKeys, flagValues := parseFlags(flagSet)
g.Expect(flagKeys).Should(Equal(expectedKeys))
g.Expect(flagValues).Should(Equal(expectedValues))
}
func TestGetBuildInfo(t *testing.T) {
t.Parallel()
g := NewWithT(t)
commitHash, commitTime, dirtyBuild := getBuildInfo()
g.Expect(commitHash).To(Not(BeEmpty()))
g.Expect(commitTime).To(Not(BeEmpty()))
g.Expect(dirtyBuild).To(Not(BeEmpty()))
g.Expect(commitHash).To(Not(Equal("unknown")))
g.Expect(commitTime).To(Not(Equal("unknown")))
g.Expect(dirtyBuild).To(Not(Equal("unknown")))
}
func TestCreateGatewayPodConfig(t *testing.T) {
t.Parallel()
g := NewWithT(t)
// Order matters here
// We start with all env vars set
g.Expect(os.Setenv("POD_IP", "10.0.0.0")).To(Succeed())
g.Expect(os.Setenv("POD_UID", "1234")).To(Succeed())
g.Expect(os.Setenv("POD_NAMESPACE", "default")).To(Succeed())
g.Expect(os.Setenv("POD_NAME", "my-pod")).To(Succeed())
expCfg := config.GatewayPodConfig{
PodIP: "10.0.0.0",
ServiceName: "svc",
Namespace: "default",
Name: "my-pod",
UID: "1234",
}
cfg, err := createGatewayPodConfig("svc")
g.Expect(err).To(Not(HaveOccurred()))
g.Expect(cfg).To(Equal(expCfg))
// unset name
g.Expect(os.Unsetenv("POD_NAME")).To(Succeed())
cfg, err = createGatewayPodConfig("svc")
g.Expect(err).To(MatchError(errors.New("environment variable POD_NAME not set")))
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
// unset namespace
g.Expect(os.Unsetenv("POD_NAMESPACE")).To(Succeed())
cfg, err = createGatewayPodConfig("svc")
g.Expect(err).To(MatchError(errors.New("environment variable POD_NAMESPACE not set")))
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
// unset pod UID
g.Expect(os.Unsetenv("POD_UID")).To(Succeed())
cfg, err = createGatewayPodConfig("svc")
g.Expect(err).To(MatchError(errors.New("environment variable POD_UID not set")))
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
// unset IP
g.Expect(os.Unsetenv("POD_IP")).To(Succeed())
cfg, err = createGatewayPodConfig("svc")
g.Expect(err).To(MatchError(errors.New("environment variable POD_IP not set")))
g.Expect(cfg).To(Equal(config.GatewayPodConfig{}))
}