@@ -6,110 +6,273 @@ import (
6
6
"text/template"
7
7
)
8
8
9
- func TestWithPathRegex_MatchesCaseSensitiveModifier (t * testing.T ) {
9
+ func TestMakeLocationPath_WithRegexCaseSensitiveModifier (t * testing.T ) {
10
10
t .Parallel ()
11
11
12
12
want := "~ \" ^/coffee/[A-Z0-9]{3}\" "
13
- got := makeLocationPath ("/coffee/[A-Z0-9]{3}" , map [string ]string {"nginx.org/path-regex" : "case_sensitive" })
13
+ got := makeLocationPath (
14
+ & Location {Path : "/coffee/[A-Z0-9]{3}" },
15
+ map [string ]string {"nginx.org/path-regex" : "case_sensitive" },
16
+ )
14
17
if got != want {
15
18
t .Errorf ("got: %s, want: %s" , got , want )
16
19
}
17
20
}
18
21
19
- func TestWithPathRegex_MatchesCaseInsensitiveModifier (t * testing.T ) {
22
+ func TestMakeLocationPath_WithRegexCaseInsensitiveModifier (t * testing.T ) {
20
23
t .Parallel ()
21
24
22
25
want := "~* \" ^/coffee/[A-Z0-9]{3}\" "
23
- got := makeLocationPath ("/coffee/[A-Z0-9]{3}" , map [string ]string {"nginx.org/path-regex" : "case_insensitive" })
26
+ got := makeLocationPath (
27
+ & Location {Path : "/coffee/[A-Z0-9]{3}" },
28
+ map [string ]string {"nginx.org/path-regex" : "case_insensitive" },
29
+ )
24
30
if got != want {
25
31
t .Errorf ("got: %s, want: %s" , got , want )
26
32
}
27
33
}
28
34
29
- func TestWithPathReqex_MatchesExactModifier (t * testing.T ) {
35
+ func TestMakeLocationPath_WithRegexExactModifier (t * testing.T ) {
30
36
t .Parallel ()
31
37
32
38
want := "= \" /coffee\" "
33
- got := makeLocationPath ("/coffee" , map [string ]string {"nginx.org/path-regex" : "exact" })
39
+ got := makeLocationPath (
40
+ & Location {Path : "/coffee" },
41
+ map [string ]string {"nginx.org/path-regex" : "exact" },
42
+ )
34
43
if got != want {
35
44
t .Errorf ("got: %s, want: %s" , got , want )
36
45
}
37
46
}
38
47
39
- func TestWithPathReqex_DoesNotMatchModifier (t * testing.T ) {
48
+ func TestMakeLocationPath_WithBogusRegexModifier (t * testing.T ) {
40
49
t .Parallel ()
41
50
42
51
want := "/coffee"
43
- got := makeLocationPath ("/coffee" , map [string ]string {"nginx.org/path-regex" : "bogus" })
52
+ got := makeLocationPath (
53
+ & Location {Path : "/coffee" },
54
+ map [string ]string {"nginx.org/path-regex" : "bogus" },
55
+ )
44
56
if got != want {
45
57
t .Errorf ("got: %s, want: %s" , got , want )
46
58
}
47
59
}
48
60
49
- func TestWithPathReqex_DoesNotMatchEmptyModifier (t * testing.T ) {
61
+ func TestMakeLocationPath_WithEmptyRegexModifier (t * testing.T ) {
50
62
t .Parallel ()
51
63
52
64
want := "/coffee"
53
- got := makeLocationPath ("/coffee" , map [string ]string {"nginx.org/path-regex" : "" })
65
+ got := makeLocationPath (
66
+ & Location {Path : "/coffee" },
67
+ map [string ]string {"nginx.org/path-regex" : "" },
68
+ )
54
69
if got != want {
55
70
t .Errorf ("got: %s, want: %s" , got , want )
56
71
}
57
72
}
58
73
59
- func TestWithPathReqex_DoesNotMatchBogusAnnotationName (t * testing.T ) {
74
+ func TestMakeLocationPath_WithBogusAnnotationName (t * testing.T ) {
60
75
t .Parallel ()
61
76
62
77
want := "/coffee"
63
- got := makeLocationPath ("/coffee" , map [string ]string {"nginx.org/bogus-annotation" : "" })
78
+ got := makeLocationPath (
79
+ & Location {Path : "/coffee" },
80
+ map [string ]string {"nginx.org/bogus-annotation" : "" },
81
+ )
64
82
if got != want {
65
83
t .Errorf ("got: %s, want: %s" , got , want )
66
84
}
67
85
}
68
86
69
- func TestSplitHelperFunction (t * testing.T ) {
87
+ func TestMakeLocationPath_ForIngressWithoutPathRegex (t * testing.T ) {
70
88
t .Parallel ()
71
- const tpl = `{{range $n := split . ","}}{{$n}} {{end}}`
72
89
73
- tmpl , err := template .New ("testTemplate" ).Funcs (helperFunctions ).Parse (tpl )
74
- if err != nil {
75
- t .Fatalf ("Failed to parse template: %v" , err )
90
+ want := "/coffee"
91
+ got := makeLocationPath (
92
+ & Location {Path : "/coffee" },
93
+ map [string ]string {},
94
+ )
95
+ if got != want {
96
+ t .Errorf ("got %q, want %q" , got , want )
97
+ }
98
+ }
99
+
100
+ func TestMakeLocationPath_ForIngressWithPathRegexCaseSensitive (t * testing.T ) {
101
+ t .Parallel ()
102
+
103
+ want := "~ \" ^/coffee\" "
104
+ got := makeLocationPath (
105
+ & Location {Path : "/coffee" },
106
+ map [string ]string {
107
+ "nginx.org/path-regex" : "case_sensitive" ,
108
+ },
109
+ )
110
+ if got != want {
111
+ t .Errorf ("got %q, want %q" , got , want )
112
+ }
113
+ }
114
+
115
+ func TestMakeLocationPath_ForIngressWithPathRegexSetOnMinion (t * testing.T ) {
116
+ t .Parallel ()
117
+
118
+ want := "~ \" ^/coffee\" "
119
+ got := makeLocationPath (
120
+ & Location {
121
+ Path : "/coffee" ,
122
+ MinionIngress : & Ingress {
123
+ Name : "cafe-ingress-coffee-minion" ,
124
+ Namespace : "default" ,
125
+ Annotations : map [string ]string {
126
+ "nginx.org/mergeable-ingress-type" : "minion" ,
127
+ "nginx.org/path-regex" : "case_sensitive" ,
128
+ },
129
+ },
130
+ },
131
+ map [string ]string {
132
+ "nginx.org/mergeable-ingress-type" : "master" ,
133
+ },
134
+ )
135
+
136
+ if got != want {
137
+ t .Errorf ("got %q, want %q" , got , want )
138
+ }
139
+ }
140
+
141
+ func TestMakeLocationPath_ForIngressWithPathRegexSetOnMaster (t * testing.T ) {
142
+ t .Parallel ()
143
+
144
+ want := "~ \" ^/coffee\" "
145
+ got := makeLocationPath (
146
+ & Location {
147
+ Path : "/coffee" ,
148
+ MinionIngress : & Ingress {
149
+ Name : "cafe-ingress-coffee-minion" ,
150
+ Namespace : "default" ,
151
+ Annotations : map [string ]string {
152
+ "nginx.org/mergeable-ingress-type" : "minion" ,
153
+ },
154
+ },
155
+ },
156
+ map [string ]string {
157
+ "nginx.org/mergeable-ingress-type" : "master" ,
158
+ "nginx.org/path-regex" : "case_sensitive" ,
159
+ },
160
+ )
161
+
162
+ if got != want {
163
+ t .Errorf ("got %q, want %q" , got , want )
164
+ }
165
+ }
166
+
167
+ func TestMakeLocationPath_SetOnMinionTakesPrecedenceOverMaster (t * testing.T ) {
168
+ t .Parallel ()
169
+
170
+ want := "= \" /coffee\" "
171
+ got := makeLocationPath (
172
+ & Location {
173
+ Path : "/coffee" ,
174
+ MinionIngress : & Ingress {
175
+ Name : "cafe-ingress-coffee-minion" ,
176
+ Namespace : "default" ,
177
+ Annotations : map [string ]string {
178
+ "nginx.org/mergeable-ingress-type" : "minion" ,
179
+ "nginx.org/path-regex" : "exact" ,
180
+ },
181
+ },
182
+ },
183
+ map [string ]string {
184
+ "nginx.org/mergeable-ingress-type" : "master" ,
185
+ "nginx.org/path-regex" : "case_sensitive" ,
186
+ },
187
+ )
188
+
189
+ if got != want {
190
+ t .Errorf ("got %q, want %q" , got , want )
76
191
}
192
+ }
193
+
194
+ func TestMakeLocationPath_PathRegexSetOnMaster (t * testing.T ) {
195
+ t .Parallel ()
196
+
197
+ want := "= \" /coffee\" "
198
+ got := makeLocationPath (
199
+ & Location {
200
+ Path : "/coffee" ,
201
+ MinionIngress : & Ingress {
202
+ Name : "cafe-ingress-coffee-minion" ,
203
+ Namespace : "default" ,
204
+ Annotations : map [string ]string {
205
+ "nginx.org/mergeable-ingress-type" : "minion" ,
206
+ },
207
+ },
208
+ },
209
+ map [string ]string {
210
+ "nginx.org/mergeable-ingress-type" : "master" ,
211
+ "nginx.org/path-regex" : "exact" ,
212
+ },
213
+ )
77
214
215
+ if got != want {
216
+ t .Errorf ("got %q, want %q" , got , want )
217
+ }
218
+ }
219
+
220
+ func TestSplitInputString (t * testing.T ) {
221
+ t .Parallel ()
222
+
223
+ tmpl := newSplitTemplate (t )
78
224
var buf bytes.Buffer
79
225
80
226
input := "foo,bar"
81
227
expected := "foo bar "
82
228
83
- err = tmpl .Execute (& buf , input )
229
+ err : = tmpl .Execute (& buf , input )
84
230
if err != nil {
85
231
t .Fatalf ("Failed to execute the template %v" , err )
86
232
}
87
-
88
233
if buf .String () != expected {
89
- t .Fatalf ("Template generated wrong config, got %v but expected %v." , buf .String (), expected )
234
+ t .Errorf ("Template generated wrong config, got %v but expected %v." , buf .String (), expected )
90
235
}
91
236
}
92
237
93
- func TestTrimHelperFunction (t * testing.T ) {
238
+ func TestTrimWhiteSpaceFromInputString (t * testing.T ) {
94
239
t .Parallel ()
95
- const tpl = `{{trim .}}`
96
240
97
- tmpl , err := template .New ("testTemplate" ).Funcs (helperFunctions ).Parse (tpl )
98
- if err != nil {
99
- t .Fatalf ("Failed to parse template: %v" , err )
241
+ tmpl := newTrimTemplate (t )
242
+ inputs := []string {
243
+ " foobar " ,
244
+ "foobar " ,
245
+ " foobar" ,
246
+ "foobar" ,
100
247
}
101
-
102
- var buf bytes.Buffer
103
-
104
- input := " foobar "
105
248
expected := "foobar"
106
249
107
- err = tmpl .Execute (& buf , input )
250
+ for _ , i := range inputs {
251
+ var buf bytes.Buffer
252
+ err := tmpl .Execute (& buf , i )
253
+ if err != nil {
254
+ t .Fatalf ("Failed to execute the template %v" , err )
255
+ }
256
+ if buf .String () != expected {
257
+ t .Errorf ("Template generated wrong config, got %v but expected %v." , buf .String (), expected )
258
+ }
259
+ }
260
+ }
261
+
262
+ func newSplitTemplate (t * testing.T ) * template.Template {
263
+ t .Helper ()
264
+ tmpl , err := template .New ("testTemplate" ).Funcs (helperFunctions ).Parse (`{{range $n := split . ","}}{{$n}} {{end}}` )
108
265
if err != nil {
109
- t .Fatalf ("Failed to execute the template %v" , err )
266
+ t .Fatalf ("Failed to parse template: %v" , err )
110
267
}
268
+ return tmpl
269
+ }
111
270
112
- if buf .String () != expected {
113
- t .Fatalf ("Template generated wrong config, got %v but expected %v." , buf .String (), expected )
271
+ func newTrimTemplate (t * testing.T ) * template.Template {
272
+ t .Helper ()
273
+ tmpl , err := template .New ("testTemplate" ).Funcs (helperFunctions ).Parse (`{{trim .}}` )
274
+ if err != nil {
275
+ t .Fatalf ("Failed to parse template: %v" , err )
114
276
}
277
+ return tmpl
115
278
}
0 commit comments