Skip to content

Commit bfd65df

Browse files
committed
Write e2e test
1 parent 9bf7c28 commit bfd65df

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

controllers/machinedeployment_controller_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,67 @@ var _ = Describe("MachineDeployment Reconciler", func() {
235235
return len(machineSets.Items)
236236
}, timeout*3).Should(BeEquivalentTo(1))
237237

238+
//
239+
// Update a MachineDeployment spec.Selector.Matchlabels spec.Template.Labels
240+
// expect Reconcile to be called and a new MachineSet to appear
241+
// expect old MachineSets with old labels to be deleted
242+
//
243+
oldLabels := deployment.Spec.Selector.MatchLabels
244+
newLabels := map[string]string{
245+
"new-key": "new-value",
246+
}
247+
248+
By("Updating MachineDeployment label")
249+
err = updateMachineDeployment(k8sClient, deployment, func(d *clusterv1.MachineDeployment) {
250+
d.Spec.Selector.MatchLabels = newLabels
251+
d.Spec.Template.Labels = newLabels
252+
})
253+
Expect(err).ToNot(HaveOccurred())
254+
255+
By("Verifying if a new MachineSet with updated labels are created")
256+
Eventually(func() int {
257+
listOpts := client.MatchingLabels(newLabels)
258+
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
259+
return -1
260+
}
261+
return len(machineSets.Items)
262+
}, timeout).Should(BeEquivalentTo(1))
263+
newms := machineSets.Items[0]
264+
265+
By("Verifying new MachineSet has desired number of replicas")
266+
Eventually(func() bool {
267+
// Set the all non-deleted machines as ready with a NodeRef, so the MachineSet controller can proceed
268+
// to properly set AvailableReplicas.
269+
machines := &clusterv1.MachineList{}
270+
Expect(k8sClient.List(ctx, machines, client.InNamespace(namespace.Name))).NotTo(HaveOccurred())
271+
for _, m := range machines.Items {
272+
if !m.DeletionTimestamp.IsZero() {
273+
continue
274+
}
275+
// Skip over Machines controlled by other (previous) MachineSets
276+
if !metav1.IsControlledBy(&m, &newms) {
277+
continue
278+
}
279+
fakeInfrastructureRefReady(m.Spec.InfrastructureRef, infraResource)
280+
fakeMachineNodeRef(&m)
281+
}
282+
283+
listOpts := client.MatchingLabels(newLabels)
284+
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
285+
return false
286+
}
287+
return machineSets.Items[0].Status.Replicas == *deployment.Spec.Replicas
288+
}, timeout*5).Should(BeTrue())
289+
290+
By("Verifying MachineSets with old labels are deleted")
291+
Eventually(func() int {
292+
listOpts := client.MatchingLabels(oldLabels)
293+
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
294+
return -1
295+
}
296+
297+
return len(machineSets.Items)
298+
}, timeout*5).Should(BeEquivalentTo(0))
238299
})
239300
})
240301

0 commit comments

Comments
 (0)