@@ -2,13 +2,16 @@ package kube
2
2
3
3
import (
4
4
"context"
5
- "net/http"
6
5
"testing"
7
6
7
+ "golang.org/x/xerrors"
8
8
authorizationv1 "k8s.io/api/authorization/v1"
9
9
rbacv1 "k8s.io/api/rbac/v1"
10
+ "k8s.io/apimachinery/pkg/runtime"
10
11
"k8s.io/client-go/kubernetes"
12
+ fake "k8s.io/client-go/kubernetes/fake"
11
13
"k8s.io/client-go/rest"
14
+ k8stesting "k8s.io/client-go/testing"
12
15
13
16
"cdr.dev/slog/sloggers/slogtest/assert"
14
17
@@ -17,7 +20,6 @@ import (
17
20
18
21
func Test_CheckRBAC_Error (t * testing.T ) {
19
22
t .Parallel ()
20
-
21
23
srv := newTestHTTPServer (t , 500 , nil )
22
24
defer srv .Close ()
23
25
client , err := kubernetes .NewForConfig (& rest.Config {Host : srv .URL })
@@ -28,6 +30,7 @@ func Test_CheckRBAC_Error(t *testing.T) {
28
30
assert .True (t , "should contain one result" , len (results ) == 1 )
29
31
assert .True (t , "result should be failed" , results [0 ].State == api .StateFailed )
30
32
}
33
+
31
34
func Test_CheckRBACFallback (t * testing.T ) {
32
35
t .Parallel ()
33
36
@@ -42,7 +45,7 @@ func Test_CheckRBACFallback(t *testing.T) {
42
45
F : func (t * testing.T , results []* api.CheckResult ) {
43
46
assert .False (t , "results should not be empty" , len (results ) == 0 )
44
47
for _ , result := range results {
45
- assert .True (t , result .Name + " should not error" , result .Details ["error" ] == nil )
48
+ assert .Equal (t , result .Name + " should not error" , result .Details ["error" ], nil )
46
49
assert .True (t , result .Name + " should pass" , result .State == api .StatePassed )
47
50
}
48
51
},
@@ -65,11 +68,13 @@ func Test_CheckRBACFallback(t *testing.T) {
65
68
t .Run (test .Name , func (t * testing.T ) {
66
69
t .Parallel ()
67
70
68
- server := newTestHTTPServer (t , http .StatusOK , test .Response )
69
- defer server .Close ()
70
-
71
- client , err := kubernetes .NewForConfig (& rest.Config {Host : server .URL })
72
- assert .Success (t , "failed to create client" , err )
71
+ client := fake .NewSimpleClientset ()
72
+ fakeAction := func (action k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
73
+ return true , test .Response , nil
74
+ }
75
+ // NOTE: Use PrependReactor! AddReactor appends the action after the reaction chain
76
+ // which by default includes a "catch-all" action which is not what we want here!
77
+ client .Fake .PrependReactor ("create" , "selfsubjectaccessreviews" , fakeAction )
73
78
74
79
checker := NewKubernetesChecker (client )
75
80
results := checker .checkRBACFallback (context .Background ())
@@ -80,11 +85,13 @@ func Test_CheckRBACFallback(t *testing.T) {
80
85
81
86
func Test_CheckRBACFallback_ClientError (t * testing.T ) {
82
87
t .Parallel ()
83
-
84
- server := newTestHTTPServer (t , http .StatusInternalServerError , nil )
85
-
86
- client , err := kubernetes .NewForConfig (& rest.Config {Host : server .URL })
87
- assert .Success (t , "failed to create client" , err )
88
+ client := fake .NewSimpleClientset ()
89
+ fakeAction := func (action k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
90
+ return true , nil , xerrors .New ("ouch" )
91
+ }
92
+ // NOTE: Use PrependReactor! AddReactor appends the action after the reaction chain
93
+ // which by default includes a "catch-all" action which is not what we want here!
94
+ client .Fake .PrependReactor ("create" , "selfsubjectaccessreviews" , fakeAction )
88
95
89
96
checker := NewKubernetesChecker (client )
90
97
results := checker .checkRBACFallback (context .Background ())
@@ -97,12 +104,14 @@ func Test_CheckRBACFallback_ClientError(t *testing.T) {
97
104
var selfSubjectAccessReviewAllowed authorizationv1.SelfSubjectAccessReview = authorizationv1.SelfSubjectAccessReview {
98
105
Status : authorizationv1.SubjectAccessReviewStatus {
99
106
Allowed : true ,
107
+ Reason : "test says yes" ,
100
108
},
101
109
}
102
110
103
111
var selfSubjectAccessReviewDenied authorizationv1.SelfSubjectAccessReview = authorizationv1.SelfSubjectAccessReview {
104
112
Status : authorizationv1.SubjectAccessReviewStatus {
105
113
Allowed : false ,
114
+ Reason : "test says no" ,
106
115
},
107
116
}
108
117
@@ -133,11 +142,14 @@ func Test_CheckRBACDefault(t *testing.T) {
133
142
t .Run (test .Name , func (t * testing.T ) {
134
143
t .Parallel ()
135
144
136
- server := newTestHTTPServer (t , http .StatusOK , test .Response )
137
- defer server .Close ()
145
+ client := fake .NewSimpleClientset ()
138
146
139
- client , err := kubernetes .NewForConfig (& rest.Config {Host : server .URL })
140
- assert .Success (t , "failed to create client" , err )
147
+ fakeAction := func (action k8stesting.Action ) (handled bool , ret runtime.Object , err error ) {
148
+ return true , test .Response , nil
149
+ }
150
+ // NOTE: Use PrependReactor! AddReactor appends the action after the reaction chain
151
+ // which by default includes a "catch-all" action which is not what we want here!
152
+ client .Fake .PrependReactor ("create" , "selfsubjectrulesreviews" , fakeAction )
141
153
142
154
checker := NewKubernetesChecker (client )
143
155
results , err := checker .checkRBACDefault (context .Background ())
0 commit comments