-
Notifications
You must be signed in to change notification settings - Fork 15
/
analyze_map_test.go
263 lines (254 loc) · 6.23 KB
/
analyze_map_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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package crossplane
import (
"testing"
"github.com/stretchr/testify/require"
)
//nolint:funlen,exhaustruct
func TestAnalyzeMapBody(t *testing.T) {
t.Parallel()
testcases := map[string]struct {
mapDirective string
parameter *Directive
term string
wantErr *ParseError
}{
"valid map": {
mapDirective: "map",
parameter: &Directive{
Directive: "default",
Args: []string{"0"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"valid map volatile parameter": {
mapDirective: "map",
parameter: &Directive{
Directive: "volatile",
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid map volatile parameter": {
mapDirective: "map",
parameter: &Directive{
Directive: "volatile",
Args: []string{"1"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "map"},
},
"valid map hostnames parameter": {
mapDirective: "map",
parameter: &Directive{
Directive: "hostnames",
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid map hostnames parameter": {
mapDirective: "map",
parameter: &Directive{
Directive: "hostnames",
Args: []string{"foo"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "map"},
},
"valid geo proxy_recursive parameter": {
mapDirective: "geo",
parameter: &Directive{
Directive: "proxy_recursive",
Line: 5,
Block: Directives{},
},
term: ";",
},
"valid types": {
mapDirective: "types",
parameter: &Directive{
Directive: "text/html",
Args: []string{"html htm shtml"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid types with special parameter": {
mapDirective: "types",
parameter: &Directive{
Directive: "hostnames",
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "types"},
},
"invalid geo proxy_recursive parameter": {
mapDirective: "geo",
parameter: &Directive{
Directive: "proxy_recursive",
Args: []string{"1"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "geo"},
},
"valid geo ranges parameter": {
mapDirective: "geo",
parameter: &Directive{
Directive: "ranges",
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid geo ranges parameter": {
mapDirective: "geo",
parameter: &Directive{
Directive: "ranges",
Args: []string{"0", "0", "0"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "geo"},
},
"invalid number of parameters in map": {
mapDirective: "map",
parameter: &Directive{
Directive: "default",
Args: []string{"0", "0"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "map"},
},
"valid split_clients": {
mapDirective: "split_clients",
parameter: &Directive{
Directive: "0.5%",
Args: []string{"google.com"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid split_clients": {
mapDirective: "split_clients",
parameter: &Directive{
Directive: "0.5%",
Args: []string{"google.com", "testme"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "split_clients"},
},
"valid geoip2": {
mapDirective: "geoip2",
parameter: &Directive{
Directive: "$geoip2_data_continent_code",
Args: []string{"continent", "code"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid geoip2": {
mapDirective: "geoip2",
parameter: &Directive{
Directive: "$geoip2_data_city_name",
Args: []string{},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "geoip2"},
},
"valid geoip2 auto_reload": {
mapDirective: "geoip2",
parameter: &Directive{
Directive: "auto_reload",
Args: []string{"5m"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid geoip2 auto_reload": {
mapDirective: "geoip2",
parameter: &Directive{
Directive: "auto_reload",
Args: []string{"5m", "10m"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "geoip2"},
},
"valid otel_exporter": {
mapDirective: "otel_exporter",
parameter: &Directive{
Directive: "endpoint",
Args: []string{"localhost:4317"},
Line: 5,
Block: Directives{},
},
term: ";",
},
"invalid otel_exporter": {
mapDirective: "otel_exporter",
parameter: &Directive{
Directive: "endpoint",
Args: []string{"localhost:4317", "localhost:1234"},
Line: 5,
Block: Directives{},
},
term: ";",
wantErr: &ParseError{What: "invalid number of parameters", BlockCtx: "otel_exporter"},
},
"missing semicolon": {
mapDirective: "map",
parameter: &Directive{
Directive: "default",
Args: []string{"0", "0"},
Line: 5,
Block: Directives{},
},
term: "}",
wantErr: &ParseError{What: `unexpected "}"`, BlockCtx: "map"},
},
}
for name, tc := range testcases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
err := analyzeMapBody("nginx.conf", tc.parameter, tc.term, tc.mapDirective)
if tc.wantErr == nil {
require.NoError(t, err)
return
}
require.Error(t, err)
var perr *ParseError
require.ErrorAs(t, err, &perr)
require.Equal(t, tc.wantErr.What, perr.What)
require.Equal(t, tc.wantErr.BlockCtx, perr.BlockCtx)
})
}
}