Skip to content

Commit 84b3e9f

Browse files
Modify unit tests
1 parent 2dc4a5e commit 84b3e9f

File tree

2 files changed

+53
-57
lines changed

2 files changed

+53
-57
lines changed

controllers/operator_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func verifyBDStatus(dep *rukpakv1alpha1.BundleDeployment) (metav1.ConditionStatu
309309
if isInstalledCond != nil && isInstalledCond.Status == metav1.ConditionTrue {
310310
return metav1.ConditionTrue, "install was successful"
311311
}
312-
return metav1.ConditionUnknown, fmt.Sprintf("waiting for rukpak to install bundleDeployment successfully %s", dep.Name)
312+
return metav1.ConditionUnknown, fmt.Sprintf("could not determine the state of bundleDeployment %s", dep.Name)
313313
}
314314

315315
// mapBDStatusToReadyCondition returns the operator object's "TypeReady" condition based on the bundle deployment statuses.
@@ -320,7 +320,8 @@ func mapBDStatusToReadyCondition(existingBD *rukpakv1alpha1.BundleDeployment, ob
320320
// 3. If the Operator "Ready" status is "False": There is error observed from Rukpak. Update the status accordingly.
321321
status, message := verifyBDStatus(existingBD)
322322
var reason string
323-
if status == metav1.ConditionTrue || status == metav1.ConditionUnknown {
323+
// TODO: introduce a new reason for condition Unknown, instead of defaulting it to Installation Succeeded.
324+
if status == metav1.ConditionTrue {
324325
reason = operatorsv1alpha1.ReasonInstallationSucceeded
325326
} else {
326327
reason = operatorsv1alpha1.ReasonBundleDeploymentFailed

controllers/operator_controller_test.go

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var _ = Describe("Reconcile Test", func() {
9191
err := cl.Create(ctx, operator)
9292
Expect(err).NotTo(HaveOccurred())
9393
})
94+
9495
When("the BundleDeployment does not exist", func() {
9596
BeforeEach(func() {
9697
By("running reconcile")
@@ -111,14 +112,12 @@ var _ = Describe("Reconcile Test", func() {
111112
Expect(bd.Spec.Template.Spec.Source.Image).NotTo(BeNil())
112113
Expect(bd.Spec.Template.Spec.Source.Image.Ref).To(Equal("quay.io/operatorhubio/prometheus@sha256:5b04c49d8d3eff6a338b56ec90bdf491d501fe301c9cdfb740e5bff6769a21ed"))
113114
})
114-
It("sets resolution success status", func() {
115-
Eventually(func() {
116-
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
117-
Expect(cond).NotTo(BeNil())
118-
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
119-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
120-
Expect(cond.Message).To(Equal("install was successful"))
121-
})
115+
It("sets the status on operator", func() {
116+
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
117+
Expect(cond).NotTo(BeNil())
118+
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
119+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
120+
Expect(cond.Message).To(ContainSubstring("waiting for bundleDeployment"))
122121
})
123122
})
124123
When("the expected BundleDeployment already exists", func() {
@@ -176,14 +175,12 @@ var _ = Describe("Reconcile Test", func() {
176175
Expect(bd.Spec.Template.Spec.Source.Image).NotTo(BeNil())
177176
Expect(bd.Spec.Template.Spec.Source.Image.Ref).To(Equal("quay.io/operatorhubio/prometheus@sha256:5b04c49d8d3eff6a338b56ec90bdf491d501fe301c9cdfb740e5bff6769a21ed"))
178177
})
179-
It("sets resolution success status", func() {
180-
Eventually(func() {
181-
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
182-
Expect(cond).NotTo(BeNil())
183-
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
184-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
185-
Expect(cond.Message).To(Equal("install was successful"))
186-
})
178+
It("sets the status on operator", func() {
179+
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
180+
Expect(cond).NotTo(BeNil())
181+
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
182+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
183+
Expect(cond.Message).To(ContainSubstring("waiting for bundleDeployment"))
187184
})
188185
})
189186
When("an out-of-date BundleDeployment exists", func() {
@@ -227,14 +224,11 @@ var _ = Describe("Reconcile Test", func() {
227224
Expect(bd.Spec.Template.Spec.Source.Image.Ref).To(Equal("quay.io/operatorhubio/prometheus@sha256:5b04c49d8d3eff6a338b56ec90bdf491d501fe301c9cdfb740e5bff6769a21ed"))
228225
})
229226
It("sets resolution success status", func() {
230-
Eventually(func() {
231-
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
232-
Expect(cond).NotTo(BeNil())
233-
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
234-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
235-
Expect(cond.Message).To(Equal("install was successful"))
236-
})
237-
227+
cond := apimeta.FindStatusCondition(operator.Status.Conditions, operatorsv1alpha1.TypeReady)
228+
Expect(cond).NotTo(BeNil())
229+
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
230+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
231+
Expect(cond.Message).To(ContainSubstring("waiting for bundleDeployment"))
238232
})
239233
})
240234
})
@@ -268,12 +262,16 @@ var _ = Describe("Reconcile Test", func() {
268262
})
269263
When("the operator specifies a duplicate package", func() {
270264
const pkgName = "prometheus"
265+
var dupOperator *operatorsv1alpha1.Operator
266+
271267
BeforeEach(func() {
272268
By("initializing cluster state")
273-
err := cl.Create(ctx, &operatorsv1alpha1.Operator{
269+
dupOperator = &operatorsv1alpha1.Operator{
274270
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("orig-%s", opKey.Name)},
275271
Spec: operatorsv1alpha1.OperatorSpec{PackageName: pkgName},
276-
})
272+
}
273+
274+
err := cl.Create(ctx, dupOperator)
277275
Expect(err).NotTo(HaveOccurred())
278276

279277
operator = &operatorsv1alpha1.Operator{
@@ -283,6 +281,12 @@ var _ = Describe("Reconcile Test", func() {
283281
err = cl.Create(ctx, operator)
284282
Expect(err).NotTo(HaveOccurred())
285283
})
284+
285+
AfterEach(func() {
286+
err := cl.Delete(ctx, dupOperator)
287+
Expect(err).NotTo(HaveOccurred())
288+
})
289+
286290
It("sets resolution failure status", func() {
287291
By("running reconcile")
288292
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey})
@@ -303,11 +307,11 @@ var _ = Describe("Reconcile Test", func() {
303307
When("the existing operator status is based on bundleDeployment", func() {
304308
const pkgName = "prometheus"
305309
var (
306-
bd rukpakv1alpha1.BundleDeployment
310+
bd *rukpakv1alpha1.BundleDeployment
307311
)
308312
BeforeEach(func() {
309313
By("creating the expected BundleDeployment")
310-
bd := &rukpakv1alpha1.BundleDeployment{
314+
bd = &rukpakv1alpha1.BundleDeployment{
311315
ObjectMeta: metav1.ObjectMeta{Name: opKey.Name},
312316
Spec: rukpakv1alpha1.BundleDeploymentSpec{
313317
ProvisionerClassName: "core-rukpak-io-plain",
@@ -338,7 +342,13 @@ var _ = Describe("Reconcile Test", func() {
338342
Expect(err).NotTo(HaveOccurred())
339343

340344
})
341-
It("verify if operator status when bundle deployment is waiting to be created", func() {
345+
346+
AfterEach(func() {
347+
err := cl.Delete(ctx, bd)
348+
Expect(err).NotTo(HaveOccurred())
349+
})
350+
351+
It("verify operator status when bundle deployment is waiting to be created", func() {
342352
By("running reconcile")
343353
res, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: opKey})
344354
Expect(res).To(Equal(ctrl.Result{}))
@@ -353,14 +363,11 @@ var _ = Describe("Reconcile Test", func() {
353363
cond := apimeta.FindStatusCondition(op.Status.Conditions, operatorsv1alpha1.TypeReady)
354364
Expect(cond).NotTo(BeNil())
355365
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
356-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
366+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
357367
Expect(cond.Message).To(ContainSubstring(`waiting for bundleDeployment`))
358368
})
359369

360370
It("verify operator status when `HasValidBundle` condition of rukpak is false", func() {
361-
err := cl.Get(ctx, opKey, &bd)
362-
Expect(err).NotTo(HaveOccurred())
363-
364371
apimeta.SetStatusCondition(&bd.Status.Conditions, metav1.Condition{
365372
Type: rukpakv1alpha1.TypeHasValidBundle,
366373
Status: metav1.ConditionFalse,
@@ -369,7 +376,7 @@ var _ = Describe("Reconcile Test", func() {
369376
})
370377

371378
By("updating the status of bundleDeployment")
372-
err = cl.Status().Update(ctx, &bd)
379+
err := cl.Status().Update(ctx, bd)
373380
Expect(err).NotTo(HaveOccurred())
374381

375382
By("running reconcile")
@@ -391,9 +398,6 @@ var _ = Describe("Reconcile Test", func() {
391398
})
392399

393400
It("verify operator status when `InstallReady` condition of rukpak is false", func() {
394-
err := cl.Get(ctx, opKey, &bd)
395-
Expect(err).NotTo(HaveOccurred())
396-
397401
apimeta.SetStatusCondition(&bd.Status.Conditions, metav1.Condition{
398402
Type: rukpakv1alpha1.TypeInstalled,
399403
Status: metav1.ConditionFalse,
@@ -402,7 +406,7 @@ var _ = Describe("Reconcile Test", func() {
402406
})
403407

404408
By("updating the status of bundleDeployment")
405-
err = cl.Status().Update(ctx, &bd)
409+
err := cl.Status().Update(ctx, bd)
406410
Expect(err).NotTo(HaveOccurred())
407411

408412
By("running reconcile")
@@ -424,9 +428,6 @@ var _ = Describe("Reconcile Test", func() {
424428
})
425429

426430
It("verify operator status when `InstallReady` condition of rukpak is true", func() {
427-
err := cl.Get(ctx, opKey, &bd)
428-
Expect(err).NotTo(HaveOccurred())
429-
430431
apimeta.SetStatusCondition(&bd.Status.Conditions, metav1.Condition{
431432
Type: rukpakv1alpha1.TypeInstalled,
432433
Status: metav1.ConditionTrue,
@@ -435,7 +436,7 @@ var _ = Describe("Reconcile Test", func() {
435436
})
436437

437438
By("updating the status of bundleDeployment")
438-
err = cl.Status().Update(ctx, &bd)
439+
err := cl.Status().Update(ctx, bd)
439440
Expect(err).NotTo(HaveOccurred())
440441

441442
By("running reconcile")
@@ -457,9 +458,6 @@ var _ = Describe("Reconcile Test", func() {
457458
})
458459

459460
It("verify any other unknown status of bundledeployment", func() {
460-
err := cl.Get(ctx, opKey, &bd)
461-
Expect(err).NotTo(HaveOccurred())
462-
463461
apimeta.SetStatusCondition(&bd.Status.Conditions, metav1.Condition{
464462
Type: rukpakv1alpha1.TypeHasValidBundle,
465463
Status: metav1.ConditionUnknown,
@@ -475,7 +473,7 @@ var _ = Describe("Reconcile Test", func() {
475473
})
476474

477475
By("updating the status of bundleDeployment")
478-
err = cl.Status().Update(ctx, &bd)
476+
err := cl.Status().Update(ctx, bd)
479477
Expect(err).NotTo(HaveOccurred())
480478

481479
By("running reconcile")
@@ -492,14 +490,11 @@ var _ = Describe("Reconcile Test", func() {
492490
cond := apimeta.FindStatusCondition(op.Status.Conditions, operatorsv1alpha1.TypeReady)
493491
Expect(cond).NotTo(BeNil())
494492
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
495-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
496-
Expect(cond.Message).To(ContainSubstring(`waiting for rukpak to install bundleDeployment successfully`))
493+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
494+
Expect(cond.Message).To(ContainSubstring(`could not determine the state of bundleDeployment`))
497495
})
498496

499497
It("verify operator status when bundleDeployment installation status is unknown", func() {
500-
err := cl.Get(ctx, opKey, &bd)
501-
Expect(err).NotTo(HaveOccurred())
502-
503498
apimeta.SetStatusCondition(&bd.Status.Conditions, metav1.Condition{
504499
Type: rukpakv1alpha1.TypeInstalled,
505500
Status: metav1.ConditionUnknown,
@@ -508,7 +503,7 @@ var _ = Describe("Reconcile Test", func() {
508503
})
509504

510505
By("updating the status of bundleDeployment")
511-
err = cl.Status().Update(ctx, &bd)
506+
err := cl.Status().Update(ctx, bd)
512507
Expect(err).NotTo(HaveOccurred())
513508

514509
By("running reconcile")
@@ -525,14 +520,14 @@ var _ = Describe("Reconcile Test", func() {
525520
cond := apimeta.FindStatusCondition(op.Status.Conditions, operatorsv1alpha1.TypeReady)
526521
Expect(cond).NotTo(BeNil())
527522
Expect(cond.Status).To(Equal(metav1.ConditionUnknown))
528-
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonInstallationSucceeded))
529-
Expect(cond.Message).To(ContainSubstring(`waiting for rukpak to install bundleDeployment successfully`))
523+
Expect(cond.Reason).To(Equal(operatorsv1alpha1.ReasonBundleDeploymentFailed))
524+
Expect(cond.Message).To(ContainSubstring(`could not determine the state of bundleDeployment`))
530525
})
531526
})
532527
AfterEach(func() {
533528
verifyInvariants(ctx, operator)
534529

535-
err := cl.DeleteAllOf(ctx, operator)
530+
err := cl.Delete(ctx, operator)
536531
Expect(err).To(Not(HaveOccurred()))
537532
})
538533
})

0 commit comments

Comments
 (0)