Skip to content

Commit

Permalink
Add unit test for scale down
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Lipovetsky <dlipovetsky@d2iq.com>
  • Loading branch information
dlipovetsky committed Feb 14, 2020
1 parent c651e06 commit e8fd512
Showing 1 changed file with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1399,7 +1399,84 @@ func TestKubeadmControlPlaneReconciler_scaleUpControlPlane(t *testing.T) {
}

func TestKubeadmControlPlaneReconciler_scaleDownControlPlane(t *testing.T) {
// TODO(dlipovetsky) Verify that
// (a) scaleDownControlPlane deletes a control plane Machine if health checks pass
// (b) scaleDownControlPlane does not delete a control plane Machine if health checks fail
t.Run("deletes a control plane Machine if health checks pass", func(t *testing.T) {
g := NewWithT(t)

fakeClient, err := fakeClient()
g.Expect(err).NotTo(HaveOccurred())

cluster, kcp, genericMachineTemplate := createClusterWithControlPlane()
g.Expect(fakeClient.Create(context.Background(), genericMachineTemplate)).To(Succeed())

fmc := &fakeManagementCluster{
Machines: []*clusterv1.Machine{},
ControlPlaneHealthy: true,
EtcdHealthy: true,
}

for i := 0; i < 2; i++ {
m, _ := createMachineNodePair(fmt.Sprintf("test-%d", i), cluster, kcp, true)
g.Expect(fakeClient.Create(context.Background(), m)).To(Succeed())
fmc.Machines = append(fmc.Machines, m)
}

r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
managementCluster: fmc,
}

fmc.ControlPlaneHealthy = true
fmc.EtcdHealthy = true
result, err := r.scaleDownControlPlane(context.Background(), &clusterv1.Cluster{}, &controlplanev1.KubeadmControlPlane{})
g.Expect(err).ToNot(HaveOccurred())
g.Expect(result).To(Equal(ctrl.Result{Requeue: true}))

controlPlaneMachines := clusterv1.MachineList{}
g.Expect(fakeClient.List(context.Background(), &controlPlaneMachines)).To(Succeed())
g.Expect(controlPlaneMachines.Items).To(HaveLen(1))
})
t.Run("does not delete a control plane Machine if health checks fail", func(t *testing.T) {
g := NewWithT(t)

fakeClient, err := fakeClient()
g.Expect(err).NotTo(HaveOccurred())

cluster, kcp, genericMachineTemplate := createClusterWithControlPlane()
g.Expect(fakeClient.Create(context.Background(), genericMachineTemplate)).To(Succeed())

fmc := &fakeManagementCluster{
Machines: []*clusterv1.Machine{},
ControlPlaneHealthy: true,
EtcdHealthy: true,
}

for i := 0; i < 2; i++ {
m, _ := createMachineNodePair(fmt.Sprintf("test-%d", i), cluster, kcp, true)
g.Expect(fakeClient.Create(context.Background(), m)).To(Succeed())
fmc.Machines = append(fmc.Machines, m)
}

r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
managementCluster: fmc,
}

controlPlaneMachines := clusterv1.MachineList{}

fmc.ControlPlaneHealthy = false
fmc.EtcdHealthy = true
result, err := r.scaleDownControlPlane(context.Background(), &clusterv1.Cluster{}, &controlplanev1.KubeadmControlPlane{})
g.Expect(err).To(HaveOccurred())
g.Expect(result).To(Equal(ctrl.Result{RequeueAfter: HealthCheckFailedRequeueAfter}))
g.Expect(fakeClient.List(context.Background(), &controlPlaneMachines)).To(Succeed())
g.Expect(controlPlaneMachines.Items).To(HaveLen(2))

fmc.ControlPlaneHealthy = true
fmc.EtcdHealthy = false
result, err = r.scaleDownControlPlane(context.Background(), &clusterv1.Cluster{}, &controlplanev1.KubeadmControlPlane{})
g.Expect(err).To(HaveOccurred())
g.Expect(result).To(Equal(ctrl.Result{RequeueAfter: HealthCheckFailedRequeueAfter}))
g.Expect(fakeClient.List(context.Background(), &controlPlaneMachines)).To(Succeed())
g.Expect(controlPlaneMachines.Items).To(HaveLen(2))
})
}

0 comments on commit e8fd512

Please sign in to comment.