Skip to content

Commit

Permalink
fix(tests): Fix flaky test by making sure CAPI/CS cluster objects are…
Browse files Browse the repository at this point in the history
… ready
  • Loading branch information
hrak committed Feb 27, 2025
1 parent 2c5a9f2 commit 38c01e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/controllers/cloudstackmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ func TestCloudStackMachineReconcilerIntegrationTests(t *testing.T) {
g.Expect(testEnv.Cleanup(ctx, dummies.CAPICluster, dummies.CSCluster, dummies.ACSEndpointSecret1, dummies.CSFailureDomain1, dummies.CSISONet1, dummies.BootstrapSecret, dummies.CAPIMachine, dummies.CSMachine1, ns)).To(Succeed())
}()

// Check that the CAPI and CloudStack cluster are ready before reconciling.
checkClusterReady(ctx, g, testEnv.Client)

// Check that the machine was created correctly before reconciling.
machineKey := client.ObjectKey{Namespace: ns.Name, Name: dummies.CSMachine1.Name}
machine := &infrav1.CloudStackMachine{}
Expand Down
39 changes: 39 additions & 0 deletions internal/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ limitations under the License.
package controllers

import (
"context"

. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"

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

Expand All @@ -30,6 +35,40 @@ func setClusterReady(g *WithT, client client.Client) {
setCloudStackClusterReady(g, client)
}

// checkClusterReady checks if the CAPI and CloudStack cluster are ready.
func checkClusterReady(ctx context.Context, g *WithT, client client.Client) {
checkCAPIClusterReady(ctx, g, client)
checkCloudStackClusterReady(ctx, g, client)
}

// checkCAPIClusterReady checks if the CAPI cluster is ready.
func checkCAPIClusterReady(ctx context.Context, g *WithT, client client.Client) {
g.Eventually(func() bool {
capiCluster := &clusterv1.Cluster{}
if err := client.Get(ctx, types.NamespacedName{Namespace: dummies.CAPICluster.Namespace, Name: dummies.CAPICluster.Name}, capiCluster); err == nil {
if capiCluster.Status.InfrastructureReady {
return true
}
}

return false
}, timeout).Should(BeTrue())
}

// checkCloudStackClusterReady checks if the CloudStack cluster is ready.
func checkCloudStackClusterReady(ctx context.Context, g *WithT, client client.Client) {
g.Eventually(func() bool {
csCluster := &infrav1.CloudStackCluster{}
if err := client.Get(ctx, types.NamespacedName{Namespace: dummies.CSCluster.Namespace, Name: dummies.CSCluster.Name}, csCluster); err == nil {
if csCluster.Status.Ready {
return true
}
}

return false
}, timeout).Should(BeTrue())
}

// setCAPIClusterReady patches the cluster with ready status true.
func setCAPIClusterReady(g *WithT, client client.Client) {
g.Eventually(func() error {
Expand Down

0 comments on commit 38c01e6

Please sign in to comment.