Skip to content

Commit eb6661c

Browse files
committed
Adjust tests to new fakeClient deletionTimestamp handling
Signed-off-by: Stefan Büringer [email protected]
1 parent 3741b10 commit eb6661c

File tree

7 files changed

+43
-51
lines changed

7 files changed

+43
-51
lines changed

controlplane/kubeadm/internal/controllers/controller_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
637637
})
638638

639639
t.Run("Deleted KubeadmControlPlanes don't adopt machines", func(t *testing.T) {
640-
// Usually we won't get into the inner reconcile with a deleted control plane, but it's possible when deleting with "oprhanDependents":
640+
// Usually we won't get into the inner reconcile with a deleted control plane, but it's possible when deleting with "orphanDependents":
641641
// 1. The deletion timestamp is set in the API server, but our cache has not yet updated
642642
// 2. The garbage collector removes our ownership reference from a Machine, triggering a re-reconcile (or we get unlucky with the periodic reconciliation)
643643
// 3. We get into the inner reconcile function and re-adopt the Machine
@@ -652,6 +652,9 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) {
652652

653653
now := metav1.Now()
654654
kcp.DeletionTimestamp = &now
655+
// We also have to set a finalizer as fake client doesn't accept objects
656+
// with a deletionTimestamp without a finalizer.
657+
kcp.Finalizers = []string{"block-deletion"}
655658

656659
fmc := &fakeManagementCluster{
657660
Machines: collections.Machines{},

exp/internal/controllers/machinepool_controller_phases_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ func TestReconcileMachinePoolPhases(t *testing.T) {
460460

461461
// Set Deletion Timestamp.
462462
machinepool.SetDeletionTimestamp(&deletionTimestamp)
463+
machinepool.Finalizers = []string{expv1.MachinePoolFinalizer}
463464

464465
r := &MachinePoolReconciler{
465466
Client: fake.NewClientBuilder().WithObjects(defaultCluster, defaultKubeconfigSecret, machinepool, bootstrapConfig, infraConfig, builder.TestBootstrapConfigCRD, builder.TestInfrastructureMachineTemplateCRD).Build(),

internal/controllers/cluster/cluster_controller_phases_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ func TestClusterReconciler_reconcilePhase(t *testing.T) {
346346
ObjectMeta: metav1.ObjectMeta{
347347
Name: "test-cluster",
348348
DeletionTimestamp: &metav1.Time{Time: time.Now().UTC()},
349+
Finalizers: []string{clusterv1.ClusterFinalizer},
349350
},
350351
Status: clusterv1.ClusterStatus{
351352
InfrastructureReady: true,

internal/controllers/machine/machine_controller_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
15011501
Name: "test-cluster",
15021502
Namespace: metav1.NamespaceDefault,
15031503
DeletionTimestamp: &deletionts,
1504+
Finalizers: []string{clusterv1.ClusterFinalizer},
15041505
},
15051506
},
15061507
machine: &clusterv1.Machine{},
@@ -1642,6 +1643,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
16421643
mcpBeingDeleted.SetName("test-cluster-2")
16431644
mcpBeingDeleted.SetNamespace("test-cluster")
16441645
mcpBeingDeleted.SetDeletionTimestamp(&metav1.Time{Time: time.Now()})
1646+
mcpBeingDeleted.SetFinalizers([]string{"block-deletion"})
16451647

16461648
empBeingDeleted := &unstructured.Unstructured{
16471649
Object: map[string]interface{}{
@@ -1655,6 +1657,7 @@ func TestIsDeleteNodeAllowed(t *testing.T) {
16551657
empBeingDeleted.SetName("test-cluster-3")
16561658
empBeingDeleted.SetNamespace("test-cluster")
16571659
empBeingDeleted.SetDeletionTimestamp(&metav1.Time{Time: time.Now()})
1660+
empBeingDeleted.SetFinalizers([]string{"block-deletion"})
16581661

16591662
for _, tc := range testCases {
16601663
t.Run(tc.name, func(t *testing.T) {

internal/controllers/machineset/machineset_controller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ func TestMachineSetReconcile(t *testing.T) {
505505
Name: "machineset1",
506506
Namespace: metav1.NamespaceDefault,
507507
DeletionTimestamp: &dt,
508+
Finalizers: []string{"block-deletion"},
508509
},
509510
Spec: clusterv1.MachineSetSpec{
510511
ClusterName: testClusterName,

internal/controllers/topology/machinedeployment/machinedeployment_controller_test.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,14 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
4646
md := mdBuilder.Build()
4747
mdWithFinalizer := mdBuilder.Build()
4848
mdWithFinalizer.Finalizers = []string{clusterv1.MachineDeploymentTopologyFinalizer}
49-
mdWithDeletionTimestamp := mdBuilder.Build()
50-
deletionTimestamp := metav1.Now()
51-
mdWithDeletionTimestamp.DeletionTimestamp = &deletionTimestamp
52-
53-
mdWithDeletionTimestampAndFinalizer := mdWithDeletionTimestamp.DeepCopy()
54-
mdWithDeletionTimestampAndFinalizer.Finalizers = []string{clusterv1.MachineDeploymentTopologyFinalizer}
5549

5650
testCases := []struct {
5751
name string
5852
md *clusterv1.MachineDeployment
5953
expectFinalizer bool
6054
}{
55+
// Note: We are not testing the case of a MD with deletionTimestamp and no finalizer.
56+
// This case is impossible to reproduce in fake client without deleting the object.
6157
{
6258
name: "should add ClusterTopology finalizer to a MachineDeployment with no finalizer",
6359
md: md,
@@ -68,11 +64,6 @@ func TestMachineDeploymentTopologyFinalizer(t *testing.T) {
6864
md: mdWithFinalizer,
6965
expectFinalizer: true,
7066
},
71-
{
72-
name: "should not add ClusterTopology finalizer on MachineDeployment with Deletion Timestamp and no finalizer ",
73-
md: mdWithDeletionTimestamp,
74-
expectFinalizer: false,
75-
},
7667
}
7768

7869
for _, tc := range testCases {
@@ -117,10 +108,14 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
117108
Build()
118109
mhc := builder.MachineHealthCheck(metav1.NamespaceDefault, "md").Build()
119110
md.SetDeletionTimestamp(&deletionTimeStamp)
111+
md.SetFinalizers([]string{clusterv1.MachineDeploymentTopologyFinalizer})
120112

121113
t.Run("Should delete templates of a MachineDeployment", func(t *testing.T) {
122114
g := NewWithT(t)
123115

116+
// Copying the MD so changes made by reconcileDelete do not affect other tests.
117+
md := md.DeepCopy()
118+
124119
fakeClient := fake.NewClientBuilder().
125120
WithScheme(fakeScheme).
126121
WithObjects(md, mdBT, mdIMT).
@@ -133,10 +128,7 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
133128
_, err := r.reconcileDelete(ctx, md)
134129
g.Expect(err).ToNot(HaveOccurred())
135130

136-
afterMD := &clusterv1.MachineDeployment{}
137-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(md), afterMD)).To(Succeed())
138-
139-
g.Expect(controllerutil.ContainsFinalizer(afterMD, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
131+
g.Expect(controllerutil.ContainsFinalizer(md, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
140132
g.Expect(templateExists(fakeClient, mdBT)).To(BeFalse())
141133
g.Expect(templateExists(fakeClient, mdIMT)).To(BeFalse())
142134
})
@@ -149,6 +141,7 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
149141
WithInfrastructureTemplate(mdWithoutBootstrapTemplateIMT).
150142
Build()
151143
mdWithoutBootstrapTemplate.SetDeletionTimestamp(&deletionTimeStamp)
144+
mdWithoutBootstrapTemplate.SetFinalizers([]string{clusterv1.MachineDeploymentTopologyFinalizer})
152145

153146
fakeClient := fake.NewClientBuilder().
154147
WithScheme(fakeScheme).
@@ -162,16 +155,16 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
162155
_, err := r.reconcileDelete(ctx, mdWithoutBootstrapTemplate)
163156
g.Expect(err).ToNot(HaveOccurred())
164157

165-
afterMD := &clusterv1.MachineDeployment{}
166-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(mdWithoutBootstrapTemplate), afterMD)).To(Succeed())
167-
168-
g.Expect(controllerutil.ContainsFinalizer(afterMD, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
158+
g.Expect(controllerutil.ContainsFinalizer(mdWithoutBootstrapTemplate, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
169159
g.Expect(templateExists(fakeClient, mdWithoutBootstrapTemplateIMT)).To(BeFalse())
170160
})
171161

172162
t.Run("Should not delete templates of a MachineDeployment when they are still in use in a MachineSet", func(t *testing.T) {
173163
g := NewWithT(t)
174164

165+
// Copying the MD so changes made by reconcileDelete do not affect other tests.
166+
md := md.DeepCopy()
167+
175168
ms := builder.MachineSet(md.Namespace, "md").
176169
WithBootstrapTemplate(mdBT).
177170
WithInfrastructureTemplate(mdIMT).
@@ -192,16 +185,16 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
192185
_, err := r.reconcileDelete(ctx, md)
193186
g.Expect(err).ToNot(HaveOccurred())
194187

195-
afterMD := &clusterv1.MachineDeployment{}
196-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(md), afterMD)).To(Succeed())
197-
198-
g.Expect(controllerutil.ContainsFinalizer(afterMD, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
188+
g.Expect(controllerutil.ContainsFinalizer(md, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
199189
g.Expect(templateExists(fakeClient, mdBT)).To(BeTrue())
200190
g.Expect(templateExists(fakeClient, mdIMT)).To(BeTrue())
201191
})
202192
t.Run("Should delete a MachineHealthCheck when its linked MachineDeployment is deleted", func(t *testing.T) {
203193
g := NewWithT(t)
204194

195+
// Copying the MD so changes made by reconcileDelete do not affect other tests.
196+
md := md.DeepCopy()
197+
205198
fakeClient := fake.NewClientBuilder().
206199
WithScheme(fakeScheme).
207200
WithObjects(md, mhc).
@@ -214,6 +207,8 @@ func TestMachineDeploymentReconciler_ReconcileDelete(t *testing.T) {
214207
_, err := r.reconcileDelete(ctx, md)
215208
g.Expect(err).ToNot(HaveOccurred())
216209

210+
g.Expect(controllerutil.ContainsFinalizer(md, clusterv1.MachineDeploymentTopologyFinalizer)).To(BeFalse())
211+
217212
gotMHC := clusterv1.MachineHealthCheck{}
218213
err = fakeClient.Get(ctx, client.ObjectKeyFromObject(mhc), &gotMHC)
219214
g.Expect(apierrors.IsNotFound(err)).To(BeTrue())

internal/controllers/topology/machineset/machineset_controller_test.go

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,14 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
6060
ms := msBuilder.Build()
6161
msWithFinalizer := msBuilder.Build()
6262
msWithFinalizer.Finalizers = []string{clusterv1.MachineSetTopologyFinalizer}
63-
msWithDeletionTimestamp := msBuilder.Build()
64-
deletionTimestamp := metav1.Now()
65-
msWithDeletionTimestamp.DeletionTimestamp = &deletionTimestamp
66-
67-
msWithDeletionTimestampAndFinalizer := msWithDeletionTimestamp.DeepCopy()
68-
msWithDeletionTimestampAndFinalizer.Finalizers = []string{clusterv1.MachineSetTopologyFinalizer}
6963

7064
testCases := []struct {
7165
name string
7266
ms *clusterv1.MachineSet
7367
expectFinalizer bool
7468
}{
69+
// Note: We are not testing the case of a MS with deletionTimestamp and no finalizer.
70+
// This case is impossible to reproduce in fake client without deleting the object.
7571
{
7672
name: "should add ClusterTopology finalizer to a MachineSet with no finalizer",
7773
ms: ms,
@@ -82,11 +78,6 @@ func TestMachineSetTopologyFinalizer(t *testing.T) {
8278
ms: msWithFinalizer,
8379
expectFinalizer: true,
8480
},
85-
{
86-
name: "should not add ClusterTopology finalizer on MachineSet with Deletion Timestamp and no finalizer ",
87-
ms: msWithDeletionTimestamp,
88-
expectFinalizer: false,
89-
},
9081
}
9182

9283
for _, tc := range testCases {
@@ -135,6 +126,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
135126
}).
136127
Build()
137128
ms.SetDeletionTimestamp(&deletionTimeStamp)
129+
ms.SetFinalizers([]string{clusterv1.MachineSetTopologyFinalizer})
138130
ms.SetOwnerReferences([]metav1.OwnerReference{
139131
{
140132
Kind: "MachineDeployment",
@@ -146,6 +138,9 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
146138
t.Run("Should delete templates of a MachineSet", func(t *testing.T) {
147139
g := NewWithT(t)
148140

141+
// Copying the MS so changes made by reconcileDelete do not affect other tests.
142+
ms := ms.DeepCopy()
143+
149144
fakeClient := fake.NewClientBuilder().
150145
WithScheme(fakeScheme).
151146
WithObjects(ms, msBT, msIMT).
@@ -158,10 +153,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
158153
_, err := r.reconcileDelete(ctx, ms)
159154
g.Expect(err).ToNot(HaveOccurred())
160155

161-
afterMS := &clusterv1.MachineSet{}
162-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(ms), afterMS)).To(Succeed())
163-
164-
g.Expect(controllerutil.ContainsFinalizer(afterMS, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
156+
g.Expect(controllerutil.ContainsFinalizer(ms, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
165157
g.Expect(templateExists(fakeClient, msBT)).To(BeFalse())
166158
g.Expect(templateExists(fakeClient, msIMT)).To(BeFalse())
167159
})
@@ -177,6 +169,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
177169
}).
178170
Build()
179171
msWithoutBootstrapTemplate.SetDeletionTimestamp(&deletionTimeStamp)
172+
msWithoutBootstrapTemplate.SetFinalizers([]string{clusterv1.MachineSetTopologyFinalizer})
180173
msWithoutBootstrapTemplate.SetOwnerReferences([]metav1.OwnerReference{
181174
{
182175
Kind: "MachineDeployment",
@@ -197,16 +190,16 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
197190
_, err := r.reconcileDelete(ctx, msWithoutBootstrapTemplate)
198191
g.Expect(err).ToNot(HaveOccurred())
199192

200-
afterMS := &clusterv1.MachineSet{}
201-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(msWithoutBootstrapTemplate), afterMS)).To(Succeed())
202-
203-
g.Expect(controllerutil.ContainsFinalizer(afterMS, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
193+
g.Expect(controllerutil.ContainsFinalizer(msWithoutBootstrapTemplate, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
204194
g.Expect(templateExists(fakeClient, msWithoutBootstrapTemplateIMT)).To(BeFalse())
205195
})
206196

207197
t.Run("Should not delete templates of a MachineSet when they are still in use in a MachineDeployment", func(t *testing.T) {
208198
g := NewWithT(t)
209199

200+
// Copying the MS so changes made by reconcileDelete do not affect other tests.
201+
ms := ms.DeepCopy()
202+
210203
md := builder.MachineDeployment(metav1.NamespaceDefault, "md").
211204
WithBootstrapTemplate(msBT).
212205
WithInfrastructureTemplate(msIMT).
@@ -224,10 +217,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
224217
_, err := r.reconcileDelete(ctx, ms)
225218
g.Expect(err).ToNot(HaveOccurred())
226219

227-
afterMS := &clusterv1.MachineSet{}
228-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(ms), afterMS)).To(Succeed())
229-
230-
g.Expect(controllerutil.ContainsFinalizer(afterMS, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
220+
g.Expect(controllerutil.ContainsFinalizer(ms, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
231221
g.Expect(templateExists(fakeClient, msBT)).To(BeTrue())
232222
g.Expect(templateExists(fakeClient, msIMT)).To(BeTrue())
233223
})
@@ -240,6 +230,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
240230
WithInfrastructureTemplate(msIMT).
241231
Build()
242232
md.SetDeletionTimestamp(&deletionTimeStamp)
233+
md.SetFinalizers([]string{clusterv1.MachineDeploymentTopologyFinalizer})
243234

244235
// anotherMS is another MachineSet of the same MachineDeployment using the same templates.
245236
// Because anotherMS is not in deleting, reconcileDelete should not delete the templates.
@@ -270,10 +261,7 @@ func TestMachineSetReconciler_ReconcileDelete(t *testing.T) {
270261
_, err := r.reconcileDelete(ctx, ms)
271262
g.Expect(err).ToNot(HaveOccurred())
272263

273-
afterMS := &clusterv1.MachineSet{}
274-
g.Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(ms), afterMS)).To(Succeed())
275-
276-
g.Expect(controllerutil.ContainsFinalizer(afterMS, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
264+
g.Expect(controllerutil.ContainsFinalizer(ms, clusterv1.MachineSetTopologyFinalizer)).To(BeFalse())
277265
g.Expect(templateExists(fakeClient, msBT)).To(BeTrue())
278266
g.Expect(templateExists(fakeClient, msIMT)).To(BeTrue())
279267
})

0 commit comments

Comments
 (0)