Skip to content

Commit f64ee6a

Browse files
committed
add ut to check conditions
1 parent 5c5862e commit f64ee6a

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

controllers/ibmpowervsimage_controller.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ func (r *IBMPowerVSImageReconciler) SetupWithManager(mgr ctrl.Manager) error {
342342
}
343343

344344
func patchIBMPowerVSImage(ctx context.Context, patchHelper *v1beta1patch.Helper, ibmPowerVSImage *infrav1.IBMPowerVSImage) error {
345+
// Before computing ready condition, make sure that ImageReady is always set.
346+
// NOTE: This is required because v1beta2 conditions comply to guideline requiring conditions to be set at the
347+
// first reconcile.
348+
if c := v1beta2conditions.Get(ibmPowerVSImage, infrav1.IBMPowerVSImageReadyV1Beta2Condition); c == nil {
349+
if ibmPowerVSImage.Status.Ready {
350+
v1beta2conditions.Set(ibmPowerVSImage, metav1.Condition{
351+
Type: infrav1.IBMPowerVSImageReadyV1Beta2Condition,
352+
Status: metav1.ConditionTrue,
353+
Reason: infrav1.IBMPowerVSImageReadyV1Beta2Reason,
354+
})
355+
} else {
356+
v1beta2conditions.Set(ibmPowerVSImage, metav1.Condition{
357+
Type: infrav1.IBMPowerVSImageReadyV1Beta2Condition,
358+
Status: metav1.ConditionFalse,
359+
Reason: infrav1.IBMPowerVSImageNotReadyV1Beta2Reason,
360+
})
361+
}
362+
}
363+
345364
// always update the readyCondition.
346365
v1beta1conditions.SetSummary(ibmPowerVSImage,
347366
v1beta1conditions.WithConditions(

controllers/ibmpowervsimage_controller_test.go

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import (
3636

3737
clusterv1beta1 "sigs.k8s.io/cluster-api/api/core/v1beta1" //nolint:staticcheck
3838
"sigs.k8s.io/cluster-api/util"
39-
v1beta1conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions" //nolint:staticcheck
39+
v1beta1conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions" //nolint:staticcheck
40+
v1beta2conditions "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/conditions/v1beta2" //nolint:staticcheck
4041

4142
infrav1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta2"
4243
"sigs.k8s.io/cluster-api-provider-ibmcloud/cloud/scope"
@@ -89,12 +90,12 @@ func TestIBMPowerVSImageReconciler_Reconcile(t *testing.T) {
8990

9091
if tc.powervsImage != nil {
9192
g.Eventually(func() bool {
92-
machine := &infrav1.IBMPowerVSImage{}
93+
image := &infrav1.IBMPowerVSImage{}
9394
key := client.ObjectKey{
9495
Name: tc.powervsImage.Name,
9596
Namespace: ns.Name,
9697
}
97-
err = testEnv.Get(ctx, key, machine)
98+
err = testEnv.Get(ctx, key, image)
9899
return err == nil
99100
}, 10*time.Second).Should(Equal(true))
100101

@@ -413,3 +414,94 @@ func expectConditionsImage(g *WithT, m *infrav1.IBMPowerVSImage, expected []cond
413414
g.Expect(actual.Reason).To(Equal(c.reason))
414415
}
415416
}
417+
418+
func TestIBMPowerVSImageReconciler_Reconcile_Conditions(t *testing.T) {
419+
testCases := []struct {
420+
name string
421+
powervsCluster *infrav1.IBMPowerVSCluster
422+
powervsImage *infrav1.IBMPowerVSImage
423+
expectError bool
424+
}{
425+
426+
{
427+
name: "Conditions should be set after reconcile",
428+
powervsCluster: &infrav1.IBMPowerVSCluster{
429+
ObjectMeta: metav1.ObjectMeta{
430+
Name: "capi-powervs-cluster"},
431+
},
432+
powervsImage: &infrav1.IBMPowerVSImage{
433+
ObjectMeta: metav1.ObjectMeta{
434+
Name: "capi-image",
435+
Finalizers: []string{infrav1.IBMPowerVSImageFinalizer},
436+
},
437+
Spec: infrav1.IBMPowerVSImageSpec{
438+
ClusterName: "capi-powervs-cluster",
439+
Object: ptr.To("capi-image.ova.gz"),
440+
Region: ptr.To("us-south"),
441+
Bucket: ptr.To("capi-bucket"),
442+
},
443+
},
444+
expectError: true,
445+
},
446+
}
447+
448+
for _, tc := range testCases {
449+
t.Run(tc.name, func(t *testing.T) {
450+
g := NewWithT(t)
451+
reconciler := &IBMPowerVSImageReconciler{
452+
Client: testEnv.Client,
453+
}
454+
455+
ns, err := testEnv.CreateNamespace(ctx, fmt.Sprintf("namespace-%s", util.RandomString(5)))
456+
g.Expect(err).To(BeNil())
457+
458+
createObject(g, tc.powervsImage, ns.Name)
459+
createCluster(g, tc.powervsCluster, ns.Name)
460+
defer cleanupObject(g, tc.powervsImage)
461+
defer cleanupCluster(g, tc.powervsCluster, ns)
462+
463+
if tc.powervsImage != nil {
464+
g.Eventually(func() bool {
465+
image := &infrav1.IBMPowerVSImage{}
466+
key := client.ObjectKey{
467+
Name: tc.powervsImage.Name,
468+
Namespace: ns.Name,
469+
}
470+
err = testEnv.Get(ctx, key, image)
471+
return err == nil
472+
}, 10*time.Second).Should(Equal(true))
473+
474+
_, err := reconciler.Reconcile(ctx, ctrl.Request{
475+
NamespacedName: client.ObjectKey{
476+
Namespace: tc.powervsImage.Namespace,
477+
Name: tc.powervsImage.Name,
478+
},
479+
})
480+
if tc.expectError {
481+
g.Expect(err).ToNot(BeNil())
482+
image := &infrav1.IBMPowerVSImage{}
483+
key := client.ObjectKey{
484+
Namespace: tc.powervsImage.Namespace,
485+
Name: tc.powervsImage.Name,
486+
}
487+
err = testEnv.Get(ctx, key, image)
488+
g.Expect(err).To(BeNil())
489+
expectConditionsImagev1beta2(g, image, []metav1.Condition{{Type: infrav1.IBMPowerVSImageReadyV1Beta2Condition, Status: metav1.ConditionFalse, Reason: infrav1.IBMPowerVSImageNotReadyV1Beta2Reason}})
490+
} else {
491+
g.Expect(err).To(BeNil())
492+
}
493+
}
494+
})
495+
}
496+
}
497+
498+
func expectConditionsImagev1beta2(g *WithT, m *infrav1.IBMPowerVSImage, expected []metav1.Condition) {
499+
g.Expect(len(m.Status.V1Beta2.Conditions)).To(BeNumerically(">=", len(expected)))
500+
for _, c := range expected {
501+
actual := v1beta2conditions.Get(m, infrav1.IBMPowerVSImageReadyV1Beta2Condition)
502+
g.Expect(actual).To(Not(BeNil()))
503+
g.Expect(actual.Type).To(Equal(c.Type))
504+
g.Expect(actual.Status).To(Equal(c.Status))
505+
g.Expect(actual.Reason).To(Equal(c.Reason))
506+
}
507+
}

0 commit comments

Comments
 (0)