forked from nginx/nginx-gateway-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands_test.go
169 lines (158 loc) · 4.06 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
package main
import (
"io"
"testing"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
)
type flagTestCase struct {
name string
expectedErrPrefix string
args []string
wantErr bool
}
func testFlag(t *testing.T, cmd *cobra.Command, test flagTestCase) {
g := NewGomegaWithT(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(cmd *cobra.Command, args []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 TestRootCmdFlagValidation(t *testing.T) {
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--gateway-ctlr-name=k8s-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=k8s-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=k8s-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=k8s-gateway.nginx.org/nginx-gateway",
"--gatewayclass=@",
},
wantErr: true,
expectedErrPrefix: `invalid argument "@" for "--gatewayclass" flag: invalid format`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
rootCmd := createRootCommand()
testFlag(t, rootCmd, test)
})
}
}
func TestStaticModeCmdFlagValidation(t *testing.T) {
tests := []flagTestCase{
{
name: "valid flags",
args: []string{
"--gateway=nginx-gateway/nginx",
"--update-gatewayclass-status=true",
},
wantErr: false,
},
{
name: "valid flags, not set",
args: nil,
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: "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`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd := createStaticModeCommand()
testFlag(t, cmd, test)
})
}
}