...
1
16
17 package admission
18
19 import (
20 "errors"
21 "testing"
22 )
23
24 type TestAdmissionError struct {
25 message string
26 reason string
27 }
28
29 func (e *TestAdmissionError) Error() string {
30 return e.message
31 }
32
33 func (e *TestAdmissionError) Type() string {
34 return e.reason
35 }
36
37 func TestAdmissionErrors(t *testing.T) {
38 testCases := []struct {
39 Error error
40 expectedAdmissionError bool
41 }{
42 {
43 nil,
44 false,
45 },
46 {
47 errors.New("Not an AdmissionError error"),
48 false,
49 },
50 {
51 &TestAdmissionError{
52 "Is an AdmissionError error",
53 "TestAdmissionError",
54 },
55 true,
56 },
57 }
58
59 for _, tc := range testCases {
60 h := GetPodAdmitResult(tc.Error)
61 if tc.Error == nil {
62 if !h.Admit {
63 t.Errorf("expected PodAdmitResult.Admit = true")
64 }
65 continue
66 }
67
68 if h.Admit {
69 t.Errorf("expected PodAdmitResult.Admit = false")
70 }
71
72 if tc.expectedAdmissionError {
73 err, ok := tc.Error.(*TestAdmissionError)
74 if !ok {
75 t.Errorf("expected TestAdmissionError")
76 }
77 if h.Reason != err.reason {
78 t.Errorf("expected PodAdmitResult.Reason = %v, got %v", err.reason, h.Reason)
79 }
80 continue
81 }
82
83 if h.Reason != ErrorReasonUnexpected {
84 t.Errorf("expected PodAdmitResult.Reason = %v, got %v", ErrorReasonUnexpected, h.Reason)
85 }
86 }
87 }
88
View as plain text