-
Notifications
You must be signed in to change notification settings - Fork 5
/
listener_test.go
373 lines (299 loc) · 11.6 KB
/
listener_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
// Copyright 2017 Yahoo Holdings Inc.
// Licensed under the terms of the 3-Clause BSD License.
package main
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os/user"
"testing"
"github.com/yahoo/k8s-ingress-claim/pkg/provider"
"github.com/stretchr/testify/assert"
admv1beta1 "k8s.io/api/admission/v1beta1"
authenticationv1 "k8s.io/api/authentication/v1"
"k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/cache"
)
var (
templateIngress = &v1beta1.Ingress{
ObjectMeta: v1.ObjectMeta{
Name: "test-ingress",
Namespace: "test-namespace",
Annotations: map[string]string{
string(provider.DefaultDomain): "app-domain-test.company.com",
string(provider.Aliases): "app-domain-default.company.com, " +
"app-domain-alias.company.com",
string(provider.Ports): "80",
},
},
Spec: v1beta1.IngressSpec{
Backend: &v1beta1.IngressBackend{
ServiceName: "test-svc",
ServicePort: intstr.FromInt(80),
},
},
}
templateAdmReview = admv1beta1.AdmissionReview{
Request: &admv1beta1.AdmissionRequest{
Resource: v1.GroupVersionResource{
Group: "extensions",
Version: "v1beta1",
Resource: "ingresses",
},
Kind: v1.GroupVersionKind{
Kind: "Ingress",
},
Object: runtime.RawExtension{
Raw: []byte("{}"),
},
Name: "test-ingress",
Namespace: "test-namespace",
Operation: "CREATE",
UserInfo: authenticationv1.UserInfo{
Username: (func() string {
user, err := user.Current()
if err != nil {
panic(err)
}
return user.Name
})(),
},
},
Response: &admv1beta1.AdmissionResponse{},
}
)
func setIngressOnAdmissionReview(testAdmReview *admv1beta1.AdmissionReview, testIngress *v1beta1.Ingress) {
ing := new(bytes.Buffer)
err := json.NewEncoder(ing).Encode(testIngress)
if err != nil {
panic(err.Error())
}
testAdmReview.Request.Object.Raw = ing.Bytes()
}
func getAdmissionReview(rw *httptest.ResponseRecorder) *admv1beta1.AdmissionReview {
admReview := &admv1beta1.AdmissionReview{
Response: &admv1beta1.AdmissionResponse{},
Request: &admv1beta1.AdmissionRequest{},
}
err := json.NewDecoder(rw.Result().Body).Decode(admReview)
if err != nil {
panic(err.Error())
}
return admReview
}
func constructPostBody(admReview *admv1beta1.AdmissionReview) io.Reader {
body := new(bytes.Buffer)
err := json.NewEncoder(body).Encode(admReview)
if err != nil {
panic(err.Error())
}
return body
}
func TestAllowedWriteResponse(t *testing.T) {
rw := httptest.NewRecorder()
review := &admv1beta1.AdmissionReview{
Request: &admv1beta1.AdmissionRequest{},
Response: &admv1beta1.AdmissionResponse{},
}
writeResponse(rw, review.Request, true, "")
admReview := getAdmissionReview(rw)
expectedAdmReview := &admv1beta1.AdmissionReview{
Response: &admv1beta1.AdmissionResponse{
Allowed: true,
Result: &v1.Status{
Reason: v1.StatusReason(""),
},
},
}
assert.Equal(t,
expectedAdmReview.Response.Result.Status,
admReview.Response.Result.Status,
"writeResponse should write Allowed: true for AdmissionReviewStatus")
}
func TestNotAllowedWriteResponse(t *testing.T) {
rw := httptest.NewRecorder()
review := &admv1beta1.AdmissionReview{
Request: &admv1beta1.AdmissionRequest{},
Response: &admv1beta1.AdmissionResponse{},
}
writeResponse(rw, review.Request, false, "Duplicate domain exists.")
admReview := getAdmissionReview(rw)
expectedAdmReview := &admv1beta1.AdmissionReview{
Response: &admv1beta1.AdmissionResponse{
Allowed: false,
Result: &v1.Status{
Reason: v1.StatusReason("Duplicate domain exists."),
},
},
}
assert.Equal(t,
expectedAdmReview.Response.Result.Status,
admReview.Response.Result.Status,
"writeResponse should write Allowed: false for AdmissionReviewStatus")
}
func TestWrongMethodWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
req := httptest.NewRequest("GET", "http://localhost:8080/ingress", nil)
webhookHandler(rw, req)
assert.Equal(t, rw.Code, 405)
}
func TestWrongPathWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
req := httptest.NewRequest("POST", "http://localhost:8080/ingress", nil)
webhookHandler(rw, req)
assert.Equal(t, rw.Code, 404)
body, err := ioutil.ReadAll(rw.Result().Body)
assert.Nil(t, err, "Error should be nil")
assert.Contains(t, string(body), "/ingress 404 Not Found")
}
func TestWrongReqBodyWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
req := httptest.NewRequest("POST", "http://localhost:8080/", nil)
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should fail if request doesn't have a body")
assert.Contains(t, admReview.Response.Result.Reason, "Failed to decode the request body json into an "+
"AdmissionReview resource: ")
}
func TestAdmitAllWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
*admitAll = true
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.True(t, admReview.Response.Allowed, "should allow ingress to pass through if admitAll flag is set")
*admitAll = false
}
func TestIngressResourceTypeWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := &admv1beta1.AdmissionReview{
Request: &admv1beta1.AdmissionRequest{
Resource: v1.GroupVersionResource{
Group: "",
Version: "v1",
Resource: "pods",
},
},
}
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should reject if the resource is not Ingress type")
assert.Contains(t, admReview.Response.Result.Reason, "Incoming resource: { v1 pods} is not an Ingress resource")
}
func TestIngressDecodeWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testSpec.Request.Object.Raw = []byte("\"{}\"")
body, _ := ioutil.ReadAll(constructPostBody(testSpec))
bytes := new(bytes.Buffer)
bytes.WriteString(string(body))
req := httptest.NewRequest("POST", "http://localhost:8080/", bytes)
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should reject if the review object cannot be decoded to an Ingress")
assert.Contains(t, admReview.Response.Result.Reason, "Failed to decode the raw object resource on the "+
"admission review request into an Ingress resource: ")
}
func TestIngressValidationWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testIngress := templateIngress.DeepCopy()
testIngress.Annotations[string(provider.Ports)] = ""
setIngressOnAdmissionReview(testSpec, testIngress)
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should reject if the Ingress validation checks fail")
assert.Contains(t, admReview.Response.Result.Reason, "Ingress validation checks failed: ")
}
func TestNoDuplicateDomainsWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testIngress := templateIngress.DeepCopy()
testIngress2 := templateIngress.DeepCopy()
testIngress2.Annotations[string(provider.DefaultDomain)] = "app-domain-default2.company.com"
testIngress2.Annotations[string(provider.Ports)] = "443,80"
testIngress2.Annotations[string(provider.Aliases)] = "app-domain-test2.company.com"
testIngress2.Name = "second-ingress"
testIngress2.Namespace = "second-namespace"
indexer = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc,
cache.Indexers{provider.ATS: helper.GetProviderByName(provider.ATS).DomainsIndexFunc})
indexer.Add(testIngress2)
helper.SetIndexer(indexer)
setIngressOnAdmissionReview(testSpec, testIngress)
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.True(t, admReview.Response.Allowed, "should approve if no duplicate domains found")
}
func TestNoDuplicateDomainsInSameIngressWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testIngress := templateIngress.DeepCopy()
indexer = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc,
cache.Indexers{provider.ATS: helper.GetProviderByName(provider.ATS).DomainsIndexFunc})
indexer.Add(testIngress)
helper.SetIndexer(indexer)
setIngressOnAdmissionReview(testSpec, testIngress)
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.True(t, admReview.Response.Allowed, "should approve even if domain exists within the same ingress object")
}
func TestDuplicateDomainsInSameNamespaceWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testIngress := templateIngress.DeepCopy()
testIngress2 := templateIngress.DeepCopy()
testIngress2.Annotations[string(provider.DefaultDomain)] = "app-domain-default.company.com"
testIngress2.Annotations[string(provider.Ports)] = "443,80"
testIngress2.Name = "second-ingress"
testIngress2.Namespace = "test-namespace"
indexer = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc,
cache.Indexers{provider.ATS: helper.GetProviderByName(provider.ATS).DomainsIndexFunc})
indexer.Add(testIngress2)
helper.SetIndexer(indexer)
setIngressOnAdmissionReview(testSpec, testIngress)
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should reject if duplicate domain exists even within the same ns")
assert.Contains(t, admReview.Response.Result.Reason, "Domain app-domain-default.company.com already "+
"exists. Ingress second-ingress in namespace test-namespace owns this domain.")
}
func TestDuplicateDomainsWebhookHandler(t *testing.T) {
rw := httptest.NewRecorder()
testSpec := templateAdmReview.DeepCopy()
testIngress := templateIngress.DeepCopy()
testIngress2 := templateIngress.DeepCopy()
testIngress2.Annotations[string(provider.DefaultDomain)] = "default-app-domain.company.com"
testIngress2.Annotations[string(provider.Ports)] = "443,80"
testIngress2.Annotations[string(provider.Aliases)] = "app-domain-alias.company.com"
testIngress2.Name = "second-ingress"
testIngress2.Namespace = "second-namespace"
indexer = cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc,
cache.Indexers{provider.ATS: helper.GetProviderByName(provider.ATS).DomainsIndexFunc})
indexer.Add(testIngress2)
helper.SetIndexer(indexer)
setIngressOnAdmissionReview(testSpec, testIngress)
req := httptest.NewRequest("POST", "http://localhost:8080/", constructPostBody(testSpec))
webhookHandler(rw, req)
admReview := getAdmissionReview(rw)
assert.False(t, admReview.Response.Allowed, "should reject if duplicate domain exists on any other ns/ingress")
assert.Contains(t, admReview.Response.Result.Reason, "Domain app-domain-alias.company.com already "+
"exists. Ingress second-ingress in namespace second-namespace owns this domain.")
}
func TestStatusHandler200(t *testing.T) {
rw := httptest.NewRecorder()
req := httptest.NewRequest("GET", "http://localhost:8080/status.html", nil)
statusHandler(rw, req)
assert.Equal(t, http.StatusOK, rw.Code, "/status.html should return 200")
}