@@ -15,6 +15,19 @@ import (
15
15
"github.com/cdr/coder-doctor/internal/api"
16
16
)
17
17
18
+ func Test_CheckRBAC_Error (t * testing.T ) {
19
+ t .Parallel ()
20
+
21
+ srv := newTestHTTPServer (t , 500 , nil )
22
+ defer srv .Close ()
23
+ client , err := kubernetes .NewForConfig (& rest.Config {Host : srv .URL })
24
+ assert .Success (t , "failed to create client" , err )
25
+
26
+ checker := NewKubernetesChecker (client )
27
+ results := checker .CheckRBAC (context .Background ())
28
+ assert .True (t , "should contain one result" , len (results ) == 1 )
29
+ assert .True (t , "result should be failed" , results [0 ].State == api .StateFailed )
30
+ }
18
31
func Test_CheckRBACFallback (t * testing.T ) {
19
32
t .Parallel ()
20
33
@@ -99,15 +112,16 @@ func Test_CheckRBACDefault(t *testing.T) {
99
112
tests := []struct {
100
113
Name string
101
114
Response * authorizationv1.SelfSubjectRulesReview
102
- F func (* testing.T , []* api.CheckResult )
115
+ TestFunc func (* testing.T , []* api.CheckResult , error )
103
116
}{
104
117
{
105
118
Name : "nothing allowed" ,
106
119
Response : & selfSubjectRulesReviewEmpty ,
107
- F : func (t * testing.T , results []* api.CheckResult ) {
120
+ TestFunc : func (t * testing.T , results []* api.CheckResult , err error ) {
108
121
assert .False (t , "results should not be empty" , len (results ) == 0 )
109
122
for _ , result := range results {
110
- assert .True (t , result .Name + " should have an error" , result .Details ["error" ] != nil )
123
+ assert .True (t , result .Name + " should not return an error" , err == nil )
124
+ assert .True (t , result .Name + " should contain an error in details" , result .Details ["error" ] != nil )
111
125
assert .True (t , result .Name + " should fail" , result .State == api .StateFailed )
112
126
}
113
127
},
@@ -126,16 +140,16 @@ func Test_CheckRBACDefault(t *testing.T) {
126
140
assert .Success (t , "failed to create client" , err )
127
141
128
142
checker := NewKubernetesChecker (client )
129
- results := checker .checkRBACDefault (context .Background ())
130
- test .F (t , results )
143
+ results , err := checker .checkRBACDefault (context .Background ())
144
+ test .TestFunc (t , results , err )
131
145
})
132
146
}
133
147
}
134
148
135
149
func Test_Satisfies (t * testing.T ) {
136
150
t .Parallel ()
137
151
138
- testCases := []struct {
152
+ tests := []struct {
139
153
Name string
140
154
Requirement * ResourceRequirement
141
155
Verbs ResourceVerbs
@@ -209,14 +223,14 @@ func Test_Satisfies(t *testing.T) {
209
223
// TODO(cian): add many, many, more.
210
224
}
211
225
212
- for _ , testCase := range testCases {
213
- testCase := testCase
214
- t .Run (testCase .Name , func (t * testing.T ) {
215
- actual := satisfies (testCase .Requirement , testCase .Verbs , testCase .Rules )
216
- if testCase .Expected == nil {
217
- assert .Success (t , testCase .Name + "- should not error" , actual )
226
+ for _ , test := range tests {
227
+ test := test
228
+ t .Run (test .Name , func (t * testing.T ) {
229
+ actual := satisfies (test .Requirement , test .Verbs , test .Rules )
230
+ if test .Expected == nil {
231
+ assert .Success (t , test .Name + "- should not error" , actual )
218
232
} else {
219
- assert .ErrorContains (t , testCase .Name + " - expected error contains" , actual , * testCase .Expected )
233
+ assert .ErrorContains (t , test .Name + " - expected error contains" , actual , * test .Expected )
220
234
}
221
235
})
222
236
}
0 commit comments