This repository was archived by the owner on Jul 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroutes_test.go
259 lines (227 loc) · 7.13 KB
/
routes_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
package main
import (
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/gavv/httpexpect"
log "github.com/sirupsen/logrus"
)
func init() {
// set defaults to global pointers for config
rulesFile := "rules.yaml"
filePath = &rulesFile
// Init map to store compiled regexs
r.compiledRegexs = make(map[string]*regexp.Regexp)
// Load initial rules into memory
err := r.load(*filePath)
if err != nil {
log.Error(err)
}
// load handlers into gin engine
routes(g)
}
func TestRulesEndpoint(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.GET("/rules").
Expect().
Status(http.StatusOK).JSON().Array()
// Ensure correct count of rules
response.Length().Equal(len(r.Rules))
// Ensure no data is malformed from yaml to response
for i := range response.Iter() {
response.Element(i).Object().ValueEqual("name", r.Rules[i].Name)
response.Element(i).Object().ValueEqual("key", r.Rules[i].Key)
response.Element(i).Object().Value("value").Object().ValueEqual("regex", r.Rules[i].Value.Regex)
}
}
func TestReloadEndpoint(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.POST("/reload").
Expect().
Status(http.StatusOK).JSON()
response.Object().ValueEqual("reloaded", true)
response.Object().ValueEqual("yamlErr", "")
response.Object().ValueEqual("ruleErr", nil)
newRulesResponse := response.Object().Value("newRules").Array()
// Ensure correct count of rules
newRulesResponse.Length().Equal(len(r.Rules))
// Ensure no data is malformed from yaml to response
for i := range newRulesResponse.Iter() {
newRulesResponse.Element(i).Object().ValueEqual("name", r.Rules[i].Name)
newRulesResponse.Element(i).Object().ValueEqual("key", r.Rules[i].Key)
newRulesResponse.Element(i).Object().Value("value").Object().ValueEqual("regex", r.Rules[i].Value.Regex)
}
}
func TestValidateEndpoint(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.GET("/validate").
Expect().
Status(http.StatusOK).JSON()
response.Object().ValueEqual("rulesValid", true)
response.Object().ValueEqual("errors", nil)
}
func TestRootEndpointNoMatchLabels(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
request := &k8sRequest{
APIVersion: "admission.k8s.io/v1",
Kind: "AdmissionReview",
Request: request{
Object: object{
Metadata: metadata{
UID: "123",
Labels: map[string]interface{}{
"test": "value",
},
},
},
},
}
// is it working?
response := e.POST("/").WithJSON(request).
Expect().
Status(http.StatusOK).JSON()
// Validate response to k8s
response.Object().ValueEqual("apiVersion", "admission.k8s.io/v1")
response.Object().ValueEqual("kind", "AdmissionReview")
response.Object().Value("response").Object().ValueEqual("allowed", false)
response.Object().Value("response").Object().ValueEqual("uid", "123")
response.Object().Value("response").Object().Value("status").Object().ValueEqual("code", 403)
//nolint
response.Object().Value("response").Object().Value("status").Object().ValueEqual("message", "phone-number not in labels")
}
// nolint
func TestRootEndpointLabelsInvalidRegex(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
request := &k8sRequest{
APIVersion: "admission.k8s.io/v1",
Kind: "AdmissionReview",
Request: request{
Object: object{
Metadata: metadata{
UID: "123",
Labels: map[string]interface{}{
"phone-number": "value",
"number": "0",
},
},
},
},
}
// is it working?
response := e.POST("/").WithJSON(request).
Expect().
Status(http.StatusOK).JSON()
// Validate response to k8s
response.Object().ValueEqual("apiVersion", "admission.k8s.io/v1")
response.Object().ValueEqual("kind", "AdmissionReview")
response.Object().Value("response").Object().ValueEqual("allowed", false)
response.Object().Value("response").Object().ValueEqual("uid", "123")
response.Object().Value("response").Object().Value("status").Object().ValueEqual("code", 403)
response.Object().Value("response").Object().Value("status").Object().ValueEqual("message", "Value for label 'phone-number' does not match expression '[0-9]{3}-[0-9]{3}-[0-9]{4}'")
}
// nolint
func TestRootEndpointValidLabels(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
request := &k8sRequest{
APIVersion: "admission.k8s.io/v1",
Kind: "AdmissionReview",
Request: request{
Object: object{
Metadata: metadata{
UID: "123",
Labels: map[string]interface{}{
"phone-number": "555-555-5555",
"number": 0,
},
},
},
},
}
// is it working?
response := e.POST("/").WithJSON(request).
Expect().
Status(http.StatusOK).JSON()
// Validate response to k8s
response.Object().ValueEqual("apiVersion", "admission.k8s.io/v1")
response.Object().ValueEqual("kind", "AdmissionReview")
response.Object().Value("response").Object().ValueEqual("allowed", true)
response.Object().Value("response").Object().ValueEqual("uid", "123")
response.Object().Value("response").Object().Value("status").Object().ValueEqual("code", 200)
response.Object().Value("response").Object().Value("status").Object().ValueEqual("message", "Labels conform to ruleset")
}
func TestRootEndpointNoPayload(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.POST("/").WithJSON("").
Expect().
Status(http.StatusBadRequest).JSON()
// Validate err response to empty body POST
response.Object().ValueEqual("err", "Improperly formatted request sent")
}
func TestUndefinedRouteRedirect(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.GET("/notthere").
Expect().
Status(http.StatusOK)
// Ensure proper redirect to swagger docs
response.Body().Contains("swagger-ui")
}
func TestMetricsEndpoint(t *testing.T) {
t.Parallel()
// run server using httptest
server := httptest.NewServer(g)
defer server.Close()
// create httpexpect instance
e := httpexpect.New(t, server.URL)
// is it working?
response := e.GET("/metrics").
Expect().
Status(http.StatusOK).ContentType("text/plain")
// Ensure proper redirect to swagger docs
response.Body().Contains("gin_request_duration_seconds_sum")
}