Skip to content

Commit 58ad6bc

Browse files
committed
fix(tests): Fix test cases for machineController
Signed-off-by: Suhas Agasthya <suhasagasthya@gmail.com>
1 parent d828e13 commit 58ad6bc

File tree

2 files changed

+34
-64
lines changed

2 files changed

+34
-64
lines changed

internal/controllers/cloudstackmachine_controller_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,16 @@ func TestCloudStackMachineReconcilerIntegrationTests(t *testing.T) {
166166
Name: dummies.CAPIMachine.Name,
167167
UID: "uniqueness",
168168
})
169+
if dummies.CSMachine1.Labels == nil {
170+
dummies.CSMachine1.Labels = map[string]string{}
171+
}
172+
dummies.CSMachine1.Labels[infrav1.FailureDomainLabelName] =
173+
infrav1.FailureDomainHashedMetaName(dummies.CSMachine1.Spec.FailureDomainName, dummies.CAPICluster.Name)
169174

170175
return ph.Patch(ctx, dummies.CSMachine1, v1beta1patch.WithStatusObservedGeneration{})
171176
}, timeout).Should(Succeed())
172-
173-
setClusterReady(g, testEnv.Client)
177+
// Mark both Cluster and CloudStackCluster ready (v1beta2 readiness semantics).
178+
markClustersReady(ctx, g, testEnv.Client, dummies.CAPICluster, dummies.CSCluster)
174179

175180
defer func() {
176181
if err := recover(); err != nil {
@@ -308,7 +313,7 @@ func TestCloudStackMachineReconcilerIntegrationTests(t *testing.T) {
308313
dummies.CAPIMachine.Spec.Bootstrap.DataSecretName = &dummies.BootstrapSecret.Name
309314
g.Expect(testEnv.Create(ctx, dummies.BootstrapSecret)).To(Succeed())
310315

311-
// Create CAPI and CS machines.
316+
// Create CAPI & set NodeRef.
312317
g.Expect(testEnv.Create(ctx, dummies.CAPIMachine)).To(Succeed())
313318
// Set the NodeRef on the CAPI machine to simulate that the machine is operational.
314319
g.Eventually(func() error {
@@ -339,13 +344,19 @@ func TestCloudStackMachineReconcilerIntegrationTests(t *testing.T) {
339344
UID: "uniqueness",
340345
})
341346
controllerutil.AddFinalizer(dummies.CSMachine1, infrav1.MachineFinalizer)
342-
// Set the instance state to running to simulate that the machine is operational.
343347
dummies.CSMachine1.Status.InstanceState = cloud.InstanceStateRunning
348+
// Keep original logical FailureDomainName ("fd1"); only ensure label is present.
349+
if dummies.CSMachine1.Labels == nil {
350+
dummies.CSMachine1.Labels = map[string]string{}
351+
}
352+
dummies.CSMachine1.Labels[infrav1.FailureDomainLabelName] =
353+
infrav1.FailureDomainHashedMetaName(dummies.CSMachine1.Spec.FailureDomainName, dummies.CAPICluster.Name)
344354

345355
return ph.Patch(ctx, dummies.CSMachine1, v1beta1patch.WithStatusObservedGeneration{})
346356
}, timeout).Should(Succeed())
347357

348-
setClusterReady(g, testEnv.Client)
358+
// Mark clusters ready (replaces setClusterReady & checkClusterReady).
359+
markClustersReady(ctx, g, testEnv.Client, dummies.CAPICluster, dummies.CSCluster)
349360

350361
defer func() {
351362
if err := recover(); err != nil {
@@ -354,9 +365,6 @@ func TestCloudStackMachineReconcilerIntegrationTests(t *testing.T) {
354365
g.Expect(testEnv.Cleanup(ctx, dummies.CAPICluster, dummies.CSCluster, dummies.ACSEndpointSecret1, dummies.CSFailureDomain1, dummies.CSISONet1, dummies.BootstrapSecret, dummies.CAPIMachine, dummies.CSMachine1, ns)).To(Succeed())
355366
}()
356367

357-
// Check that the CAPI and CloudStack cluster are ready before reconciling.
358-
checkClusterReady(ctx, g, testEnv.Client)
359-
360368
// Check that the machine was created correctly before reconciling.
361369
machineKey := client.ObjectKey{Namespace: ns.Name, Name: dummies.CSMachine1.Name}
362370
machine := &infrav1.CloudStackMachine{}

internal/controllers/helpers_test.go

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,73 +20,35 @@ import (
2020
"context"
2121

2222
. "github.com/onsi/gomega"
23-
"k8s.io/apimachinery/pkg/types"
23+
"k8s.io/utils/ptr"
2424
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
2525
v1beta1patch "sigs.k8s.io/cluster-api/util/deprecated/v1beta1/patch"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727

2828
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
29-
dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta3"
3029
)
3130

32-
// setClusterReady patches the CAPI and CloudStack cluster with ready status true.
33-
func setClusterReady(g *WithT, client client.Client) {
34-
setCAPIClusterReady(g, client)
35-
setCloudStackClusterReady(g, client)
36-
}
37-
38-
// checkClusterReady checks if the CAPI and CloudStack cluster are ready.
39-
func checkClusterReady(ctx context.Context, g *WithT, client client.Client) {
40-
checkCAPIClusterReady(ctx, g, client)
41-
checkCloudStackClusterReady(ctx, g, client)
42-
}
43-
44-
// checkCAPIClusterReady checks if the CAPI cluster is ready.
45-
func checkCAPIClusterReady(ctx context.Context, g *WithT, client client.Client) {
46-
g.Eventually(func() bool {
47-
capiCluster := &clusterv1.Cluster{}
48-
if err := client.Get(ctx, types.NamespacedName{Namespace: dummies.CAPICluster.Namespace, Name: dummies.CAPICluster.Name}, capiCluster); err == nil {
49-
if *capiCluster.Status.Initialization.InfrastructureProvisioned {
50-
return true
51-
}
31+
// markClustersReady patches Cluster & CloudStackCluster status to satisfy controller readiness checks (v1beta2 semantics).
32+
func markClustersReady(ctx context.Context, g *WithT, c client.Client, cluster *clusterv1.Cluster, csCluster *infrav1.CloudStackCluster) {
33+
g.Eventually(func() error {
34+
ph, err := v1beta1patch.NewHelper(cluster, c)
35+
if err != nil {
36+
return err
5237
}
53-
54-
return false
55-
}, timeout).Should(BeTrue())
56-
}
57-
58-
// checkCloudStackClusterReady checks if the CloudStack cluster is ready.
59-
func checkCloudStackClusterReady(ctx context.Context, g *WithT, client client.Client) {
60-
g.Eventually(func() bool {
61-
csCluster := &infrav1.CloudStackCluster{}
62-
if err := client.Get(ctx, types.NamespacedName{Namespace: dummies.CSCluster.Namespace, Name: dummies.CSCluster.Name}, csCluster); err == nil {
63-
if csCluster.Status.Ready {
64-
return true
65-
}
38+
if cluster.Status.Initialization.InfrastructureProvisioned == nil || !*cluster.Status.Initialization.InfrastructureProvisioned {
39+
cluster.Status.Initialization.InfrastructureProvisioned = ptr.To(true)
6640
}
67-
68-
return false
69-
}, timeout).Should(BeTrue())
70-
}
71-
72-
// setCAPIClusterReady patches the cluster with ready status true.
73-
func setCAPIClusterReady(g *WithT, client client.Client) {
74-
g.Eventually(func() error {
75-
ph, err := v1beta1patch.NewHelper(dummies.CAPICluster, client)
76-
g.Expect(err).ToNot(HaveOccurred())
77-
*dummies.CAPICluster.Status.Initialization.InfrastructureProvisioned = true
78-
79-
return ph.Patch(ctx, dummies.CAPICluster, v1beta1patch.WithStatusObservedGeneration{})
41+
return ph.Patch(ctx, cluster, v1beta1patch.WithStatusObservedGeneration{})
8042
}, timeout).Should(Succeed())
81-
}
8243

83-
// setCloudStackClusterReady patches the cluster with ready status true.
84-
func setCloudStackClusterReady(g *WithT, client client.Client) {
8544
g.Eventually(func() error {
86-
ph, err := v1beta1patch.NewHelper(dummies.CSCluster, client)
87-
g.Expect(err).ToNot(HaveOccurred())
88-
dummies.CSCluster.Status.Ready = true
89-
90-
return ph.Patch(ctx, dummies.CSCluster, v1beta1patch.WithStatusObservedGeneration{})
45+
ph, err := v1beta1patch.NewHelper(csCluster, c)
46+
if err != nil {
47+
return err
48+
}
49+
if !csCluster.Status.Ready {
50+
csCluster.Status.Ready = true
51+
}
52+
return ph.Patch(ctx, csCluster, v1beta1patch.WithStatusObservedGeneration{})
9153
}, timeout).Should(Succeed())
9254
}

0 commit comments

Comments
 (0)