@@ -140,6 +140,13 @@ func Test_InstallPostgresqlOperator(t *testing.T) {
140
140
if params [0 ] == "describe" {
141
141
return "error" , errors .New ("some error" )
142
142
}
143
+ if params [0 ] == "get" {
144
+ if params [1 ] == "pod" {
145
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
146
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
147
+ return "'Running'" , nil
148
+ }
149
+ }
143
150
return "" , nil
144
151
}
145
152
var expect error = nil
@@ -171,3 +178,180 @@ func Test_InstallPostgresqlOperator(t *testing.T) {
171
178
}
172
179
})
173
180
}
181
+
182
+ func Test_waitPsqlOperatorRunning (t * testing.T ) {
183
+ k8sImpl := NewK8sSetUp ().(* k8sSetUpImpl )
184
+
185
+ t .Run ("must run until database is running" , func (t * testing.T ) {
186
+ count := 0
187
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
188
+ if params [0 ] == "get" {
189
+ if params [1 ] == "pod" {
190
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
191
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
192
+ count ++
193
+ if count == 3 {
194
+ return "'Running'" , nil
195
+ }
196
+ return "'Waiting'" , nil
197
+ }
198
+ }
199
+ return "" , nil
200
+ }
201
+
202
+ k8sImpl .waitPsqlOperatorRunning ()
203
+ expect := 3
204
+ got := count
205
+ if got != expect {
206
+ t .Fatalf ("Got %v, expect %v" , got , expect )
207
+ }
208
+ })
209
+
210
+ t .Run ("must run until there is no error and running" , func (t * testing.T ) {
211
+ var errInvalid = errors .New ("invalid" )
212
+ count := 0
213
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
214
+ if params [0 ] == "get" {
215
+ if params [1 ] == "pod" {
216
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
217
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
218
+ count ++
219
+ if count == 2 {
220
+ return "'Running'" , nil
221
+ }
222
+ return "" , errInvalid
223
+ }
224
+ }
225
+ return "" , nil
226
+ }
227
+
228
+ k8sImpl .waitPsqlOperatorRunning ()
229
+ expect := 2
230
+ got := count
231
+ if got != expect {
232
+ t .Fatalf ("Got %v, expect %v" , got , expect )
233
+ }
234
+ })
235
+ }
236
+
237
+ func Test_isPsqlOperatorRunning (t * testing.T ) {
238
+ k8sImpl := NewK8sSetUp ().(* k8sSetUpImpl )
239
+
240
+ t .Run ("must return true if postgres operator is running" , func (t * testing.T ) {
241
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
242
+ if params [0 ] == "get" {
243
+ if params [1 ] == "pod" {
244
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
245
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
246
+ return "'Running'" , nil
247
+ }
248
+ }
249
+ return "" , nil
250
+ }
251
+
252
+ expect := true
253
+ var expectErr error = nil
254
+ got , gotErr := k8sImpl .isPsqlOperatorRunning ()
255
+
256
+ if gotErr != expectErr {
257
+ t .Fatalf ("Got error %v, expect error %v" , gotErr , expectErr )
258
+ }
259
+ if got != expect {
260
+ t .Fatalf ("Got %v, expect %v" , got , expect )
261
+ }
262
+ })
263
+
264
+ t .Run ("must return false if postgres operator does not exist" , func (t * testing.T ) {
265
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
266
+ if params [0 ] == "get" {
267
+ if params [1 ] == "pod" {
268
+ return "pod/test-1234" , nil
269
+ }
270
+ }
271
+ return "" , nil
272
+ }
273
+
274
+ expect := false
275
+ var expectErr error = nil
276
+ got , gotErr := k8sImpl .isPsqlOperatorRunning ()
277
+
278
+ if gotErr != expectErr {
279
+ t .Fatalf ("Got error %v, expect error %v" , gotErr , expectErr )
280
+ }
281
+ if got != expect {
282
+ t .Fatalf ("Got %v, expect %v" , got , expect )
283
+ }
284
+ })
285
+
286
+ t .Run ("must return false if postgres operator is in progress" , func (t * testing.T ) {
287
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
288
+ if params [0 ] == "get" {
289
+ if params [1 ] == "pod" {
290
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
291
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
292
+ return "'Waiting'" , nil
293
+ }
294
+ }
295
+ return "" , nil
296
+ }
297
+
298
+ expect := false
299
+ var expectErr error = nil
300
+ got , gotErr := k8sImpl .isPsqlOperatorRunning ()
301
+
302
+ if gotErr != expectErr {
303
+ t .Fatalf ("Got error %v, expect error %v" , gotErr , expectErr )
304
+ }
305
+ if got != expect {
306
+ t .Fatalf ("Got %v, expect %v" , got , expect )
307
+ }
308
+ })
309
+
310
+ t .Run ("must return false if get pods fails" , func (t * testing.T ) {
311
+ var errInvalid = errors .New ("invalid" )
312
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
313
+ if params [0 ] == "get" {
314
+ if params [1 ] == "pod" {
315
+ return "" , errInvalid
316
+ }
317
+ }
318
+ return "" , nil
319
+ }
320
+
321
+ expect := false
322
+ expectErr := errInvalid
323
+ got , gotErr := k8sImpl .isPsqlOperatorRunning ()
324
+
325
+ if gotErr != expectErr {
326
+ t .Fatalf ("Got error %v, expect error %v" , gotErr , expectErr )
327
+ }
328
+ if got != expect {
329
+ t .Fatalf ("Got %v, expect %v" , got , expect )
330
+ }
331
+ })
332
+
333
+ t .Run ("must return false if postgres operator status check fails" , func (t * testing.T ) {
334
+ var errInvalid = errors .New ("invalid" )
335
+ k8sImpl .executeCommand = func (cmdName string , params ... string ) (string , error ) {
336
+ if params [0 ] == "get" {
337
+ if params [1 ] == "pod" {
338
+ return "pod/postgres-operator-59bb89464c-dx2rs" , nil
339
+ } else if params [1 ] == "pod/postgres-operator-59bb89464c-dx2rs" {
340
+ return "" , errInvalid
341
+ }
342
+ }
343
+ return "" , nil
344
+ }
345
+
346
+ expect := false
347
+ expectErr := errInvalid
348
+ got , gotErr := k8sImpl .isPsqlOperatorRunning ()
349
+
350
+ if gotErr != expectErr {
351
+ t .Fatalf ("Got error %v, expect error %v" , gotErr , expectErr )
352
+ }
353
+ if got != expect {
354
+ t .Fatalf ("Got %v, expect %v" , got , expect )
355
+ }
356
+ })
357
+ }
0 commit comments