Skip to content

Commit de8c024

Browse files
committed
Fix deletion policy tests
The deletion policy was not being set at all for tests. Why? Because BeforeEach nodes run BEFORE the JustBeforeEach. Therefore, the BeforeEach was setting the deletion policy in the spec, and right after the JustAfterEach in the top container was assigning a "zero" value for the topology type. This effectively "overrode" the value set for deletion policy. This was probably passing because Create/Delete were called one after the other, and sometimes the deletion behaviour simply skipped because the object was not found, since it was "too soon" and not returned by the informer cache. These tests should have been rightfuly red. The were flaking to green.
1 parent 342851f commit de8c024

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

controllers/federation_controller_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,26 +233,31 @@ var _ = Describe("federation-controller", func() {
233233
When("the Federation has DeletionPolicy set to retain", func() {
234234
BeforeEach(func() {
235235
federationName = "federation-with-retain-policy"
236-
federation.Spec.DeletionPolicy = "retain"
237236
fakeRabbitMQClient.DeleteFederationUpstreamReturns(&http.Response{
238237
Status: "200 OK",
239238
StatusCode: http.StatusOK,
240239
}, nil)
240+
fakeRabbitMQClient.PutFederationUpstreamReturns(&http.Response{StatusCode: http.StatusCreated, Status: "201 Created"}, nil)
241241
})
242242

243243
It("deletes the k8s resource but preserves the federation in RabbitMQ server", func() {
244+
federation.Spec.DeletionPolicy = "retain"
244245
Expect(k8sClient.Create(ctx, &federation)).To(Succeed())
245-
Expect(k8sClient.Delete(ctx, &federation)).To(Succeed())
246+
Eventually(fakeRabbitMQClient.PutFederationUpstreamCallCount).
247+
WithPolling(time.Second).
248+
Within(time.Second*3).
249+
Should(BeNumerically(">=", 1), "Expected to call RMQ API to create federation")
246250

251+
Expect(k8sClient.Delete(ctx, &federation)).To(Succeed())
247252
Eventually(func() bool {
248253
err := k8sClient.Get(ctx, types.NamespacedName{Name: federation.Name, Namespace: federation.Namespace}, &federation)
249254
return apierrors.IsNotFound(err)
250255
}).
251256
Within(statusEventsUpdateTimeout).
252257
WithPolling(time.Second).
253-
Should(BeTrue())
258+
Should(BeTrue(), "Federation should not be found")
254259

255-
Expect(fakeRabbitMQClient.DeleteFederationUpstreamCallCount()).To(Equal(0))
260+
Expect(fakeRabbitMQClient.DeleteFederationUpstreamCallCount()).To(Equal(0), "Expected Federation to be deleted and no calls to RMQ API")
256261
})
257262
})
258263
})

controllers/queue_controller_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,26 +229,30 @@ var _ = Describe("queue-controller", func() {
229229
When("the Queue has DeletionPolicy set to retain", func() {
230230
BeforeEach(func() {
231231
queueName = "queue-with-retain-policy"
232-
queue.Spec.DeletionPolicy = "retain"
233232
fakeRabbitMQClient.DeleteQueueReturns(&http.Response{
234233
Status: "200 OK",
235234
StatusCode: http.StatusOK,
236235
}, nil)
237236
})
238237

239238
It("deletes the k8s resource but preserves the queue in RabbitMQ server", func() {
239+
queue.Spec.DeletionPolicy = "retain"
240240
Expect(k8sClient.Create(ctx, &queue)).To(Succeed())
241-
Expect(k8sClient.Delete(ctx, &queue)).To(Succeed())
241+
Eventually(fakeRabbitMQClient.DeclareQueueCallCount).
242+
WithPolling(time.Second).
243+
Within(time.Second*3).
244+
Should(BeNumerically(">=", 1), "Expected to call RMQ API to declare queue")
242245

246+
Expect(k8sClient.Delete(ctx, &queue)).To(Succeed())
243247
Eventually(func() bool {
244248
err := k8sClient.Get(ctx, types.NamespacedName{Name: queue.Name, Namespace: queue.Namespace}, &queue)
245249
return apierrors.IsNotFound(err)
246250
}).
247251
Within(statusEventsUpdateTimeout).
248252
WithPolling(time.Second).
249-
Should(BeTrue())
253+
Should(BeTrue(), "Queue should not be found")
250254

251-
Expect(fakeRabbitMQClient.DeleteQueueCallCount()).To(Equal(0))
255+
Expect(fakeRabbitMQClient.DeleteQueueCallCount()).To(Equal(0), "Expected to delete queue and no calls to RMQ API")
252256
})
253257
})
254258
})

controllers/shovel_controller_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,24 @@ var _ = Describe("shovel-controller", func() {
290290
})
291291
})
292292
})
293+
293294
When("the Shovel has DeletionPolicy set to retain", func() {
294295
BeforeEach(func() {
295296
shovelName = "shovel-with-retain-policy"
296-
shovel.Spec.DeletionPolicy = "retain"
297297
fakeRabbitMQClient.DeleteShovelReturns(&http.Response{
298298
Status: "200 OK",
299299
StatusCode: http.StatusOK,
300300
}, nil)
301+
fakeRabbitMQClient.DeclareShovelReturns(&http.Response{StatusCode: http.StatusCreated, Status: "201 Created"}, nil)
301302
})
302303

303304
It("deletes the k8s resource but preserves the shovel in RabbitMQ server", func() {
305+
shovel.Spec.DeletionPolicy = "retain"
304306
Expect(k8sClient.Create(ctx, &shovel)).To(Succeed())
307+
Eventually(fakeRabbitMQClient.DeclareShovelCallCount).
308+
WithPolling(time.Second).
309+
Within(time.Second*3).
310+
Should(BeNumerically(">=", 1), "Expected to call RMQ API to declare shovel")
305311
Expect(k8sClient.Delete(ctx, &shovel)).To(Succeed())
306312

307313
Eventually(func() bool {
@@ -310,9 +316,9 @@ var _ = Describe("shovel-controller", func() {
310316
}).
311317
Within(statusEventsUpdateTimeout).
312318
WithPolling(time.Second).
313-
Should(BeTrue())
319+
Should(BeTrue(), "Expected shovel to not be found")
314320

315-
Expect(fakeRabbitMQClient.DeleteShovelCallCount()).To(Equal(0))
321+
Expect(fakeRabbitMQClient.DeleteShovelCallCount()).To(Equal(0), "Shovel object should have been deleted and no calls to RMQ API")
316322
})
317323
})
318324
})

controllers/vhost_controller_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,31 @@ var _ = Describe("vhost-controller", func() {
369369
When("the Vhost has DeletionPolicy set to retain", func() {
370370
BeforeEach(func() {
371371
vhostName = "vhost-with-retain-policy"
372-
vhost.Spec.DeletionPolicy = "retain"
373372
fakeRabbitMQClient.DeleteVhostReturns(&http.Response{
374373
Status: "200 OK",
375374
StatusCode: http.StatusOK,
376375
}, nil)
376+
fakeRabbitMQClient.PutVhostReturns(&http.Response{StatusCode: http.StatusCreated, Status: "201 Created"}, nil)
377377
})
378378

379379
It("deletes the k8s resource but preserves the vhost in RabbitMQ server", func() {
380+
vhost.Spec.DeletionPolicy = "retain"
380381
Expect(k8sClient.Create(ctx, &vhost)).To(Succeed())
381-
Expect(k8sClient.Delete(ctx, &vhost)).To(Succeed())
382+
Eventually(fakeRabbitMQClient.PutVhostCallCount).
383+
WithPolling(time.Second).
384+
Within(time.Second*3).
385+
Should(BeNumerically(">=", 1), "Expected to call RMQ API to create vhost")
382386

387+
Expect(k8sClient.Delete(ctx, &vhost)).To(Succeed())
383388
Eventually(func() bool {
384389
err := k8sClient.Get(ctx, types.NamespacedName{Name: vhost.Name, Namespace: vhost.Namespace}, &vhost)
385390
return apierrors.IsNotFound(err)
386391
}).
387392
Within(statusEventsUpdateTimeout).
388393
WithPolling(time.Second).
389-
Should(BeTrue())
394+
Should(BeTrue(), "vhost should not be found")
390395

391-
Expect(fakeRabbitMQClient.DeleteVhostCallCount()).To(Equal(0))
396+
Expect(fakeRabbitMQClient.DeleteVhostCallCount()).To(Equal(0), "Expected vhost to be deleted and no calls to RMQ API")
392397
})
393398
})
394399
})

0 commit comments

Comments
 (0)