-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler_test.go
121 lines (108 loc) · 3.05 KB
/
handler_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
package admissioncontrol
import (
"bytes"
"encoding/json"
"errors"
admission "k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"net/http/httptest"
"testing"
)
func newTestAdmitFunc(allowed bool, returnError bool) AdmitFunc {
return func(admissionReview *admission.AdmissionReview) (*admission.AdmissionResponse, error) {
ar := &admission.AdmissionResponse{
Allowed: allowed,
Result: &metav1.Status{},
}
if !allowed {
return ar, errors.New("admission not allowed")
}
return ar, nil
}
}
func TestAdmissionHandler(t *testing.T) {
t.Parallel()
var handlerTests = []struct {
testName string
admitFunc AdmitFunc
incomingReview *admission.AdmissionReview
shouldPass bool
}{
{
testName: "Pass-through AdmitFunc returns HTTP 200 & allows admission",
admitFunc: newTestAdmitFunc(true, false),
incomingReview: &admission.AdmissionReview{
Request: &admission.AdmissionRequest{},
},
shouldPass: true,
},
{
testName: "AdmitFunc returns HTTP 200 & denies admission",
admitFunc: newTestAdmitFunc(false, true),
incomingReview: &admission.AdmissionReview{
Request: &admission.AdmissionRequest{},
},
shouldPass: false,
},
{
testName: "Reject a nil/empty AdmissionReview",
admitFunc: newTestAdmitFunc(false, true),
incomingReview: nil,
shouldPass: false,
},
{
testName: "Reject a malformed AdmissionReview (no Kind)",
admitFunc: newTestAdmitFunc(false, true),
incomingReview: &admission.AdmissionReview{
Request: &admission.AdmissionRequest{},
},
shouldPass: false,
},
{
testName: "Return an error for a malformed outgoing AdmissionReview",
admitFunc: func(_ *admission.AdmissionReview) (*admission.AdmissionResponse, error) {
return nil, nil
},
incomingReview: &admission.AdmissionReview{
Request: &admission.AdmissionRequest{},
},
shouldPass: false,
},
}
for _, tt := range handlerTests {
t.Run(tt.testName, func(t *testing.T) {
handler := &AdmissionHandler{
AdmitFunc: tt.admitFunc,
Logger: &noopLogger{},
}
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(&tt.incomingReview)
if err != nil {
t.Fatalf("error marshalling incomingReview: %v", err)
}
rr := httptest.NewRecorder()
req := httptest.NewRequest(
http.MethodPost,
"/",
buf,
)
handler.ServeHTTP(rr, req)
// Testing for:
// 1. Did we get a non-nil response body?
// 2. Did it return a valid AdmissionReview object?
// 3. Was the status code as expected?
// 4. Did the AdmissionReview object set Allowed to the expected value?
if rr.Body.Len() == 0 {
t.Fatalf("received an empty response body")
}
review := &admission.AdmissionReview{}
if err := json.Unmarshal(rr.Body.Bytes(), review); err != nil {
t.Fatalf("couldn't marshal the review response: %v", err)
}
if allowed := review.Response.Allowed; allowed != tt.shouldPass {
t.Fatalf("invalid review response: got allowed: %t (want %t)", allowed, tt.shouldPass)
}
})
}
}