Skip to content

Commit 211a6ef

Browse files
committed
Fix unit test flakes
Signed-off-by: Stefan Büringer [email protected]
1 parent f4fce77 commit 211a6ef

File tree

6 files changed

+16
-7
lines changed

6 files changed

+16
-7
lines changed

controlplane/kubeadm/internal/controllers/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ func TestReconcileInitializeControlPlane_withUserCA(t *testing.T) {
15291529
KubeadmConfigSpec: bootstrapv1.KubeadmConfigSpec{},
15301530
},
15311531
}
1532-
g.Expect(env.Create(ctx, kcp)).To(Succeed())
1532+
g.Expect(env.CreateAndWait(ctx, kcp)).To(Succeed())
15331533

15341534
corednsCM := &corev1.ConfigMap{
15351535
ObjectMeta: metav1.ObjectMeta{

controlplane/kubeadm/internal/controllers/helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
4040
"sigs.k8s.io/cluster-api/controllers/external"
4141
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
42+
clientutil "sigs.k8s.io/cluster-api/internal/util/client"
4243
"sigs.k8s.io/cluster-api/internal/util/ssa"
4344
"sigs.k8s.io/cluster-api/util/collections"
4445
v1beta1conditions "sigs.k8s.io/cluster-api/util/conditions/deprecated/v1beta1"
@@ -447,6 +448,13 @@ func TestCloneConfigsAndGenerateMachineAndSyncMachines(t *testing.T) {
447448
}})))
448449

449450
// Sync Machines
451+
452+
// Note: Ensure the client observed the latest objects so syncMachines below is not failing with conflict errors.
453+
// Note: Not adding a WaitForCacheToBeUpToDate for infraObj for now as we didn't have test flakes because of it and
454+
// WaitForCacheToBeUpToDate does not support Unstructured as of now.
455+
g.Expect(clientutil.WaitForCacheToBeUpToDate(ctx, r.Client, "cloneConfigsAndGenerateMachine", &m)).To(Succeed())
456+
g.Expect(clientutil.WaitForCacheToBeUpToDate(ctx, r.Client, "cloneConfigsAndGenerateMachine", kubeadmConfig)).To(Succeed())
457+
450458
controlPlane, err := internal.NewControlPlane(ctx, r.managementCluster, r.Client, cluster, kcp, collections.FromMachines(&m))
451459
g.Expect(err).ToNot(HaveOccurred())
452460
g.Expect(r.syncMachines(ctx, controlPlane)).To(Succeed())

internal/controllers/machine/machine_controller.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (retres ct
217217

218218
cluster, err := util.GetClusterByName(ctx, r.Client, m.Namespace, m.Spec.ClusterName)
219219
if err != nil {
220-
return ctrl.Result{}, errors.Wrapf(err, "failed to get cluster %q for machine %q in namespace %q",
221-
m.Spec.ClusterName, m.Name, m.Namespace)
220+
return ctrl.Result{}, err
222221
}
223222

224223
// Initialize the patch helper

internal/controllers/machine/machine_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ func TestWatchesDelete(t *testing.T) {
273273
},
274274
}
275275

276-
g.Expect(env.Create(ctx, testCluster)).To(Succeed())
276+
g.Expect(env.CreateAndWait(ctx, testCluster)).To(Succeed())
277277
g.Expect(env.CreateKubeconfigSecret(ctx, testCluster)).To(Succeed())
278-
g.Expect(env.Create(ctx, defaultBootstrap)).To(Succeed())
279-
g.Expect(env.Create(ctx, infraMachine)).To(Succeed())
278+
g.Expect(env.CreateAndWait(ctx, defaultBootstrap)).To(Succeed())
279+
g.Expect(env.CreateAndWait(ctx, infraMachine)).To(Succeed())
280280

281281
defer func(do ...client.Object) {
282282
g.Expect(env.Cleanup(ctx, do...)).To(Succeed())

test/e2e/clusterctl_upgrade.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@ func downloadToTmpFile(ctx context.Context, url string) string {
854854
Expect(err).ToNot(HaveOccurred(), "failed to get clusterctl")
855855
defer resp.Body.Close()
856856

857+
Expect(resp.StatusCode).To(Equal(http.StatusOK), "unexpected status code when downloading clusterctl")
858+
857859
// Write the body to file
858860
_, err = io.Copy(tmpFile, resp.Body)
859861
Expect(err).ToNot(HaveOccurred(), "failed to write temporary file")

util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func GetClusterByName(ctx context.Context, c client.Client, namespace, name stri
180180
}
181181

182182
if err := c.Get(ctx, key, cluster); err != nil {
183-
return nil, errors.Wrapf(err, "failed to get Cluster/%s", name)
183+
return nil, errors.Wrapf(err, "failed to get Cluster %s", klog.KRef(namespace, name))
184184
}
185185

186186
return cluster, nil

0 commit comments

Comments
 (0)