Skip to content

Commit

Permalink
Write e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
tahsinrahman committed Sep 4, 2019
1 parent 0af6478 commit 620f3ea
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions controllers/machinedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (r *MachineDeploymentReconciler) getMachineSetsForDeployment(d *clusterv1.M
continue
}

// Skip this MachineSet unless either selector matches or it has a controller ref pointing to this MachineDeployment
if !selector.Matches(labels.Set(ms.Labels)) && !metav1.IsControlledBy(ms, d) {
klog.V(4).Infof("Skipping MachineSet %v, label mismatch", ms.Name)
continue
Expand Down
55 changes: 55 additions & 0 deletions controllers/machinedeployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,61 @@ var _ = Describe("MachineDeployment Reconciler", func() {
return len(machineSets.Items)
}, timeout*3).Should(BeEquivalentTo(1))

//
// Update a MachineDeployment spec.Selector.Matchlabels spec.Template.Labels
// expect Reconcile to be called and a new MachineSet to appear
// expect old MachineSets with old labels to be deleted
//
oldLabels := deployment.Spec.Selector.MatchLabels
newLabels := map[string]string{
"new-key": "new-value",
}

err = updateMachineDeployment(k8sClient, deployment, func(d *clusterv1.MachineDeployment) {
d.Spec.Selector.MatchLabels = newLabels
d.Spec.Template.Labels = newLabels
})
Expect(err).ToNot(HaveOccurred())

// verify new MachineSet with updated labels are created
Eventually(func() int {
listOpts := client.MatchingLabels(newLabels)
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
return -1
}
return len(machineSets.Items)
}, timeout).Should(BeEquivalentTo(1))

// verify new MachineSet have desired number of available replicas
Eventually(func() bool {
// Set the all non-deleted machines as ready with a NodeRef, so the MachineSet controller can proceed
// to properly set AvailableReplicas.
machines := &clusterv1.MachineList{}
Expect(k8sClient.List(ctx, machines, client.InNamespace(namespace.Name))).NotTo(HaveOccurred())
for _, m := range machines.Items {
if !m.DeletionTimestamp.IsZero() {
continue
}
fakeInfrastructureRefReady(m.Spec.InfrastructureRef, infraResource)
fakeMachineNodeRef(&m)
}

listOpts := client.MatchingLabels(newLabels)
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
return false
}
return machineSets.Items[0].Status.Replicas == *deployment.Spec.Replicas
}, timeout*5).Should(BeTrue())

// verify old MachineSets with old labels are deleted
Eventually(func() int {
listOpts := client.MatchingLabels(oldLabels)
if err := k8sClient.List(ctx, machineSets, listOpts); err != nil {
return -1
}

return len(machineSets.Items)
}, timeout*5).Should(BeEquivalentTo(0))
})
})

Expand Down

0 comments on commit 620f3ea

Please sign in to comment.