9
9
)
10
10
11
11
func TestCondition (t * testing.T ) {
12
- t .Run ("with namespaces" , func (t * testing.T ) {
12
+ t .Run ("with violating namespaces" , func (t * testing.T ) {
13
13
namespaces := []string {"namespace1" , "namespace2" }
14
14
expectedCondition := operatorv1.OperatorCondition {
15
15
Type : PodSecurityCustomerType ,
@@ -18,7 +18,35 @@ func TestCondition(t *testing.T) {
18
18
Message : "Violations detected in namespaces: [namespace1 namespace2]" ,
19
19
}
20
20
21
- condition := makeCondition (PodSecurityCustomerType , namespaces )
21
+ condition := makeCondition (PodSecurityCustomerType , violationReason , namespaces )
22
+
23
+ if condition .Type != expectedCondition .Type {
24
+ t .Errorf ("expected condition type %s, got %s" , expectedCondition .Type , condition .Type )
25
+ }
26
+
27
+ if condition .Status != expectedCondition .Status {
28
+ t .Errorf ("expected condition status %s, got %s" , expectedCondition .Status , condition .Status )
29
+ }
30
+
31
+ if condition .Reason != expectedCondition .Reason {
32
+ t .Errorf ("expected condition reason %s, got %s" , expectedCondition .Reason , condition .Reason )
33
+ }
34
+
35
+ if condition .Message != expectedCondition .Message {
36
+ t .Errorf ("expected condition message %s, got %s" , expectedCondition .Message , condition .Message )
37
+ }
38
+ })
39
+
40
+ t .Run ("with inconclusive namespaces" , func (t * testing.T ) {
41
+ namespaces := []string {"namespace1" , "namespace2" }
42
+ expectedCondition := operatorv1.OperatorCondition {
43
+ Type : PodSecurityCustomerType ,
44
+ Status : operatorv1 .ConditionTrue ,
45
+ Reason : "PSViolationDecisionInconclusive" ,
46
+ Message : "Could not evaluate violations for namespaces: [namespace1 namespace2]" ,
47
+ }
48
+
49
+ condition := makeCondition (PodSecurityCustomerType , inconclusiveReason , namespaces )
22
50
23
51
if condition .Type != expectedCondition .Type {
24
52
t .Errorf ("expected condition type %s, got %s" , expectedCondition .Type , condition .Type )
@@ -45,7 +73,7 @@ func TestCondition(t *testing.T) {
45
73
Reason : "ExpectedReason" ,
46
74
}
47
75
48
- condition := makeCondition (PodSecurityCustomerType , namespaces )
76
+ condition := makeCondition (PodSecurityCustomerType , violationReason , namespaces )
49
77
50
78
if condition .Type != expectedCondition .Type {
51
79
t .Errorf ("expected condition type %s, got %s" , expectedCondition .Type , condition .Type )
@@ -68,28 +96,32 @@ func TestCondition(t *testing.T) {
68
96
69
97
func TestOperatorStatus (t * testing.T ) {
70
98
for _ , tt := range []struct {
71
- name string
72
- namespace []* corev1.Namespace
73
- expected map [string ]operatorv1.ConditionStatus
99
+ name string
100
+ namespace []* corev1.Namespace
101
+ expected map [string ]operatorv1.ConditionStatus
102
+ addViolation , addInconclusive bool
74
103
}{
75
104
{
76
- name : "with default namespace" ,
105
+ name : "with violating default namespace" ,
77
106
namespace : []* corev1.Namespace {
78
107
{
79
108
ObjectMeta : metav1.ObjectMeta {
80
109
Name : "syncer-by-default" ,
81
110
},
82
111
},
83
112
},
113
+ addViolation : true ,
114
+ addInconclusive : false ,
84
115
expected : map [string ]operatorv1.ConditionStatus {
85
116
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
86
117
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
87
118
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
88
119
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
120
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
89
121
},
90
122
},
91
123
{
92
- name : "with customer disabled syncer" ,
124
+ name : "with violating customer disabled syncer" ,
93
125
namespace : []* corev1.Namespace {
94
126
{
95
127
ObjectMeta : metav1.ObjectMeta {
@@ -100,15 +132,17 @@ func TestOperatorStatus(t *testing.T) {
100
132
},
101
133
},
102
134
},
135
+ addViolation : true ,
103
136
expected : map [string ]operatorv1.ConditionStatus {
104
137
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
105
138
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
106
139
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
107
140
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
141
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
108
142
},
109
143
},
110
144
{
111
- name : "with customer re-enabled syncer" ,
145
+ name : "with violating customer re-enabled syncer" ,
112
146
namespace : []* corev1.Namespace {
113
147
{
114
148
ObjectMeta : metav1.ObjectMeta {
@@ -119,47 +153,53 @@ func TestOperatorStatus(t *testing.T) {
119
153
},
120
154
},
121
155
},
156
+ addViolation : true ,
122
157
expected : map [string ]operatorv1.ConditionStatus {
123
158
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
124
159
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
125
160
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
126
161
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
162
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
127
163
},
128
164
},
129
165
{
130
- name : "with openshift namespace" ,
166
+ name : "with violating openshift namespace" ,
131
167
namespace : []* corev1.Namespace {
132
168
{
133
169
ObjectMeta : metav1.ObjectMeta {
134
170
Name : "openshift-fail" ,
135
171
},
136
172
},
137
173
},
174
+ addViolation : true ,
138
175
expected : map [string ]operatorv1.ConditionStatus {
139
176
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
140
177
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
141
178
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
142
179
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
180
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
143
181
},
144
182
},
145
183
{
146
- name : "with run-level 0 namespace" ,
184
+ name : "with violating run-level 0 namespace" ,
147
185
namespace : []* corev1.Namespace {
148
186
{
149
187
ObjectMeta : metav1.ObjectMeta {
150
188
Name : "kube-system" ,
151
189
},
152
190
},
153
191
},
192
+ addViolation : true ,
154
193
expected : map [string ]operatorv1.ConditionStatus {
155
194
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
156
195
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
157
196
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
158
197
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
198
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
159
199
},
160
200
},
161
201
{
162
- name : "with other customer types in combination" ,
202
+ name : "with other violating customer types in combination" ,
163
203
namespace : []* corev1.Namespace {
164
204
{
165
205
ObjectMeta : metav1.ObjectMeta {
@@ -175,15 +215,17 @@ func TestOperatorStatus(t *testing.T) {
175
215
},
176
216
},
177
217
},
218
+ addViolation : true ,
178
219
expected : map [string ]operatorv1.ConditionStatus {
179
220
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
180
221
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
181
222
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
182
223
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
224
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
183
225
},
184
226
},
185
227
{
186
- name : "with other system types in combination" ,
228
+ name : "with other violating system types in combination" ,
187
229
namespace : []* corev1.Namespace {
188
230
{
189
231
ObjectMeta : metav1.ObjectMeta {
@@ -203,11 +245,32 @@ func TestOperatorStatus(t *testing.T) {
203
245
},
204
246
},
205
247
},
248
+ addViolation : true ,
206
249
expected : map [string ]operatorv1.ConditionStatus {
207
250
"PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
208
251
"PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
209
252
"PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
210
253
"PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
254
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
255
+ },
256
+ },
257
+ {
258
+ name : "with inconclusive namespace" ,
259
+ namespace : []* corev1.Namespace {
260
+ {
261
+ ObjectMeta : metav1.ObjectMeta {
262
+ Name : "syncer-by-default" ,
263
+ },
264
+ },
265
+ },
266
+ addViolation : false ,
267
+ addInconclusive : true ,
268
+ expected : map [string ]operatorv1.ConditionStatus {
269
+ "PodSecurityCustomerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
270
+ "PodSecurityOpenshiftEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
271
+ "PodSecurityRunLevelZeroEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
272
+ "PodSecurityDisabledSyncerEvaluationConditionsDetected" : operatorv1 .ConditionFalse ,
273
+ "PodSecurityInconclusiveEvaluationConditionsDetected" : operatorv1 .ConditionTrue ,
211
274
},
212
275
},
213
276
} {
@@ -216,7 +279,12 @@ func TestOperatorStatus(t *testing.T) {
216
279
cond := podSecurityOperatorConditions {}
217
280
218
281
for _ , ns := range tt .namespace {
219
- cond .addViolation (ns )
282
+ if tt .addViolation {
283
+ cond .addViolation (ns )
284
+ }
285
+ if tt .addInconclusive {
286
+ cond .addInconclusive (ns )
287
+ }
220
288
}
221
289
222
290
status := & operatorv1.OperatorStatus {}
0 commit comments