@@ -57,6 +57,47 @@ var _ = Describe("Conditions helpers", func() {
57
57
},
58
58
}
59
59
})
60
+
61
+ Describe ("GetNamespacedName" , func () {
62
+ It ("should error when name of the operator condition cannot be found" , func () {
63
+ err := os .Unsetenv (operatorCondEnvVar )
64
+ Expect (err ).NotTo (HaveOccurred ())
65
+
66
+ objKey , err := GetNamespacedName ()
67
+ Expect (err ).To (HaveOccurred ())
68
+ Expect (objKey ).To (BeNil ())
69
+ Expect (err .Error ()).To (ContainSubstring ("could not determine operator condition name" ))
70
+ })
71
+
72
+ It ("should error when object namespace cannot be found" , func () {
73
+ err := os .Setenv (operatorCondEnvVar , "test" )
74
+ Expect (err ).NotTo (HaveOccurred ())
75
+
76
+ readNamespace = func () (string , error ) {
77
+ return "" , os .ErrNotExist
78
+ }
79
+
80
+ objKey , err := GetNamespacedName ()
81
+ Expect (err ).To (HaveOccurred ())
82
+ Expect (objKey ).To (BeNil ())
83
+ Expect (err .Error ()).To (ContainSubstring ("could not determine operator namespace" ))
84
+ })
85
+
86
+ It ("should return the right namespaced name from SA namespace file" , func () {
87
+ err := os .Setenv (operatorCondEnvVar , "test" )
88
+ Expect (err ).NotTo (HaveOccurred ())
89
+
90
+ readNamespace = func () (string , error ) {
91
+ return "testns" , nil
92
+ }
93
+ objKey , err := GetNamespacedName ()
94
+ Expect (err ).NotTo (HaveOccurred ())
95
+ Expect (objKey ).NotTo (BeNil ())
96
+ Expect (objKey .Name ).To (BeEquivalentTo ("test" ))
97
+ Expect (objKey .Namespace ).To (BeEquivalentTo ("testns" ))
98
+ })
99
+ })
100
+
60
101
Describe ("SetOperatorCondition" , func () {
61
102
It ("should set condition status" , func () {
62
103
newCond := metav1.Condition {
@@ -110,222 +151,145 @@ var _ = Describe("Conditions helpers", func() {
110
151
})
111
152
112
153
Describe ("RemoveOperatorCondition" , func () {
113
- var (
114
- rmvConditionType string
115
- )
116
154
It ("should remove the condition" , func () {
117
- rmvConditionType = conditionFoo
118
- Expect (RemoveOperatorCondition (operatorCondition , rmvConditionType )).NotTo (HaveOccurred ())
155
+ Expect (RemoveOperatorCondition (operatorCondition , conditionFoo )).NotTo (HaveOccurred ())
119
156
Expect (len (operatorCondition .Status .Conditions )).To (BeEquivalentTo (0 ))
120
157
})
121
158
It ("should not error when condition to be removed is not available" , func () {
122
- rmvConditionType = conditionBar
123
- Expect (RemoveOperatorCondition (operatorCondition , rmvConditionType )).NotTo (HaveOccurred ())
159
+ Expect (RemoveOperatorCondition (operatorCondition , conditionBar )).NotTo (HaveOccurred ())
124
160
Expect (len (operatorCondition .Status .Conditions )).To (BeEquivalentTo (1 ))
125
161
})
126
162
It ("should error when operatorCondition is nil" , func () {
127
- rmvConditionType = conditionFoo
128
- err := RemoveOperatorCondition (nil , rmvConditionType )
163
+ err := RemoveOperatorCondition (nil , conditionFoo )
129
164
Expect (err ).To (HaveOccurred ())
130
165
Expect (err ).Should (MatchError (ErrNoOperatorCondition ))
131
166
})
132
167
})
133
168
134
169
Describe ("FindOperatorCondition" , func () {
135
- var (
136
- findConditionType string
137
- )
138
-
139
170
It ("should return the condition if it exists" , func () {
140
- findConditionType = conditionFoo
141
171
conditionToFind := & metav1.Condition {
142
172
Type : conditionFoo ,
143
173
Status : metav1 .ConditionTrue ,
144
174
Reason : "foo" ,
145
175
Message : "The operator is in foo condition" ,
146
176
LastTransitionTime : transitionTime ,
147
177
}
148
- c , err := FindOperatorCondition (operatorCondition , findConditionType )
178
+ c , err := FindOperatorCondition (operatorCondition , conditionFoo )
149
179
Expect (err ).NotTo (HaveOccurred ())
150
180
Expect (reflect .DeepEqual (c , conditionToFind )).To (BeTrue ())
151
181
})
152
182
It ("should return error when condition does not exist" , func () {
153
- findConditionType = conditionBar
154
- c , err := FindOperatorCondition (operatorCondition , findConditionType )
183
+ c , err := FindOperatorCondition (operatorCondition , conditionBar )
155
184
Expect (err ).To (HaveOccurred ())
156
185
Expect (c ).To (BeNil ())
157
- Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , findConditionType )))
186
+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionBar )))
158
187
})
159
188
It ("should error when operatorCondition is nil" , func () {
160
- findConditionType = conditionFoo
161
- c , err := FindOperatorCondition (nil , findConditionType )
189
+ c , err := FindOperatorCondition (nil , conditionFoo )
162
190
Expect (err ).To (HaveOccurred ())
163
191
Expect (c ).To (BeNil ())
164
192
Expect (err ).Should (MatchError (ErrNoOperatorCondition ))
165
193
})
166
194
})
167
195
168
196
Describe ("Verfiy status of the condition" , func () {
169
- var (
170
- conditionStatusFor string
171
- )
172
-
173
197
It ("should return correct value when condition exists" , func () {
174
- conditionStatusFor = conditionFoo
175
-
176
198
// IsConditionStatusTrue should return true
177
- val , err := IsConditionStatusTrue (operatorCondition , conditionStatusFor )
199
+ val , err := IsConditionStatusTrue (operatorCondition , conditionFoo )
178
200
Expect (err ).NotTo (HaveOccurred ())
179
201
Expect (val ).To (BeTrue ())
180
202
181
203
// IsConditionStatusFalse should return false
182
- val , err = IsConditionStatusFalse (operatorCondition , conditionStatusFor )
204
+ val , err = IsConditionStatusFalse (operatorCondition , conditionFoo )
183
205
Expect (err ).NotTo (HaveOccurred ())
184
206
Expect (val ).To (BeFalse ())
185
207
186
208
// IsConditionStatusUnknown should return false
187
- val , err = IsConditionStatusUnknown (operatorCondition , conditionStatusFor )
209
+ val , err = IsConditionStatusUnknown (operatorCondition , conditionFoo )
188
210
Expect (err ).NotTo (HaveOccurred ())
189
211
Expect (val ).To (BeFalse ())
190
212
})
191
213
192
214
It ("should return false if condition status is not set to true" , func () {
193
- conditionStatusFor = conditionFoo
194
215
operatorCondition .Status .Conditions [0 ].Status = metav1 .ConditionFalse
195
216
196
217
// IsConditionStatusTrue should return false
197
- val , err := IsConditionStatusTrue (operatorCondition , conditionStatusFor )
218
+ val , err := IsConditionStatusTrue (operatorCondition , conditionFoo )
198
219
Expect (err ).NotTo (HaveOccurred ())
199
220
Expect (val ).To (BeFalse ())
200
221
201
222
// IsConditionStatusFalse should return true
202
- val , err = IsConditionStatusFalse (operatorCondition , conditionStatusFor )
223
+ val , err = IsConditionStatusFalse (operatorCondition , conditionFoo )
203
224
Expect (err ).NotTo (HaveOccurred ())
204
225
Expect (val ).To (BeTrue ())
205
226
206
227
// IsConditionStatusUnknown should return false
207
- val , err = IsConditionStatusUnknown (operatorCondition , conditionStatusFor )
228
+ val , err = IsConditionStatusUnknown (operatorCondition , conditionFoo )
208
229
Expect (err ).NotTo (HaveOccurred ())
209
230
Expect (val ).To (BeFalse ())
210
231
})
211
232
It ("should return false if condition status is unknown" , func () {
212
- conditionStatusFor = conditionFoo
213
233
operatorCondition .Status .Conditions [0 ].Status = metav1 .ConditionUnknown
214
234
215
235
// IsConditionStatusTrue should return false
216
- val , err := IsConditionStatusTrue (operatorCondition , conditionStatusFor )
236
+ val , err := IsConditionStatusTrue (operatorCondition , conditionFoo )
217
237
Expect (err ).NotTo (HaveOccurred ())
218
238
Expect (val ).To (BeFalse ())
219
239
220
240
// IsConditionStatusFalse should return false
221
- val , err = IsConditionStatusFalse (operatorCondition , conditionStatusFor )
241
+ val , err = IsConditionStatusFalse (operatorCondition , conditionFoo )
222
242
Expect (err ).NotTo (HaveOccurred ())
223
243
Expect (val ).To (BeFalse ())
224
244
225
245
// IsConditionStatusUnknown should return true
226
- val , err = IsConditionStatusUnknown (operatorCondition , conditionStatusFor )
246
+ val , err = IsConditionStatusUnknown (operatorCondition , conditionFoo )
227
247
Expect (err ).NotTo (HaveOccurred ())
228
248
Expect (val ).To (BeTrue ())
229
249
230
250
})
231
251
It ("should error when condition cannot be found" , func () {
232
- conditionStatusFor = conditionBar
233
-
234
252
// IsConditionStatusTrue should return error
235
- val , err := IsConditionStatusTrue (operatorCondition , conditionStatusFor )
253
+ val , err := IsConditionStatusTrue (operatorCondition , conditionBar )
236
254
Expect (err ).To (HaveOccurred ())
237
- Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionStatusFor )))
255
+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionBar )))
238
256
Expect (val ).To (BeFalse ())
239
257
240
258
// IsConditionStatusFalse should return error
241
- val , err = IsConditionStatusFalse (operatorCondition , conditionStatusFor )
259
+ val , err = IsConditionStatusFalse (operatorCondition , conditionBar )
242
260
Expect (err ).To (HaveOccurred ())
243
- Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionStatusFor )))
261
+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionBar )))
244
262
Expect (val ).To (BeFalse ())
245
263
246
264
// IsConditionStatusUnknown should return error
247
- val , err = IsConditionStatusUnknown (operatorCondition , conditionStatusFor )
265
+ val , err = IsConditionStatusUnknown (operatorCondition , conditionBar )
248
266
Expect (err ).To (HaveOccurred ())
249
- Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionStatusFor )))
267
+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("%s not found" , conditionBar )))
250
268
Expect (val ).To (BeFalse ())
251
269
})
252
270
})
253
271
254
272
Describe ("IsConditionStatusPresentAndEqual" , func () {
255
- var (
256
- conditionType string
257
- conditionStatus metav1.ConditionStatus
258
- )
259
273
260
274
It ("should return true when condition is in the specified status" , func () {
261
- conditionType = conditionFoo
262
- conditionStatus = metav1 .ConditionTrue
263
-
264
- val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionType , conditionStatus )
275
+ val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionFoo , metav1 .ConditionTrue )
265
276
Expect (err ).NotTo (HaveOccurred ())
266
277
Expect (val ).To (BeTrue ())
267
278
})
268
279
269
280
It ("should return false when condition is not present in the specified status" , func () {
270
- conditionType = conditionFoo
271
- conditionStatus = metav1 .ConditionUnknown
272
-
273
- val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionType , conditionStatus )
281
+ val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionFoo , metav1 .ConditionUnknown )
274
282
Expect (err ).NotTo (HaveOccurred ())
275
283
Expect (val ).To (BeFalse ())
276
284
})
277
285
278
286
It ("should return error when condition is not present" , func () {
279
- conditionType = conditionBar
280
- conditionStatus = metav1 .ConditionTrue
281
-
282
- val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionType , conditionStatus )
287
+ val , err := IsConditionStatusPresentAndEqual (operatorCondition , conditionBar , metav1 .ConditionTrue )
283
288
Expect (err ).To (HaveOccurred ())
284
289
Expect (val ).To (BeFalse ())
285
290
})
286
291
287
292
})
288
-
289
- Describe ("GetNamespacedName" , func () {
290
- It ("should error when name of the operator condition cannot be found" , func () {
291
- err := os .Unsetenv (operatorCondEnvVar )
292
- Expect (err ).NotTo (HaveOccurred ())
293
-
294
- objKey , err := GetNamespacedName ()
295
- Expect (err ).To (HaveOccurred ())
296
- Expect (objKey ).To (BeNil ())
297
- Expect (err .Error ()).To (ContainSubstring ("cannot find operator condition CR for the operator" ))
298
- })
299
-
300
- It ("should error when object namespace cannot be found" , func () {
301
- err := os .Setenv (operatorCondEnvVar , "test" )
302
- Expect (err ).NotTo (HaveOccurred ())
303
-
304
- readNamespace = func () ([]byte , error ) {
305
- return nil , os .ErrNotExist
306
- }
307
-
308
- objKey , err := GetNamespacedName ()
309
- Expect (err ).To (HaveOccurred ())
310
- Expect (objKey ).To (BeNil ())
311
- Expect (err .Error ()).To (ContainSubstring ("cannot find namespace" ))
312
- })
313
-
314
- It ("should return the right namespaced name" , func () {
315
- err := os .Setenv (operatorCondEnvVar , "test" )
316
- Expect (err ).NotTo (HaveOccurred ())
317
-
318
- readNamespace = func () ([]byte , error ) {
319
- return []byte ("testns" ), nil
320
- }
321
- objKey , err := GetNamespacedName ()
322
- Expect (err ).NotTo (HaveOccurred ())
323
- Expect (objKey ).NotTo (BeNil ())
324
- Expect (objKey .Name ).To (BeEquivalentTo ("test" ))
325
- Expect (objKey .Namespace ).To (BeEquivalentTo ("testns" ))
326
- })
327
- })
328
-
329
293
})
330
294
331
295
func isConditionPresent (arr []metav1.Condition , con metav1.Condition ) bool {
0 commit comments