Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Nov 17, 2023
1 parent 01ed1e2 commit 39ca0f8
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions controlplane/kubeadm/internal/controllers/remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,36 @@ func TestReconcileUnhealthyMachines(t *testing.T) {

g.Expect(env.Cleanup(ctx, m1, m2)).To(Succeed())
})
t.Run("Remediation does not happen if there is an healthy machine being provisioned - 4 CP (during 3 CP rolling upgrade)", func(t *testing.T) {
g := NewWithT(t)

m1 := createMachine(ctx, g, ns.Name, "m1-unhealthy-", withMachineHealthCheckFailed())
m2 := createMachine(ctx, g, ns.Name, "m2-healthy-")
m3 := createMachine(ctx, g, ns.Name, "m3-healthy-")
m4 := createMachine(ctx, g, ns.Name, "m4-healthy-", withoutNodeRef()) // Provisioning
controlPlane := &internal.ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: utilpointer.Int32(3),
},
Status: controlplanev1.KubeadmControlPlaneStatus{
Initialized: true,
},
},
Cluster: &clusterv1.Cluster{},
Machines: collections.FromMachines(m1, m2, m3, m4),
}
ret, err := r.reconcileUnhealthyMachines(ctx, controlPlane)

g.Expect(ret.IsZero()).To(BeTrue()) // Remediation skipped
g.Expect(err).ToNot(HaveOccurred())

g.Expect(controlPlane.KCP.Annotations).ToNot(HaveKey(controlplanev1.RemediationInProgressAnnotation))

assertMachineCondition(ctx, g, m1, clusterv1.MachineOwnerRemediatedCondition, corev1.ConditionFalse, clusterv1.WaitingForRemediationReason, clusterv1.ConditionSeverityWarning, "KCP waiting for control plane machine provisioning to complete before triggering remediation")

g.Expect(env.Cleanup(ctx, m1, m2)).To(Succeed())
})
t.Run("Remediation does not happen if there is at least one additional unhealthy etcd member on a 3 machine CP", func(t *testing.T) {
g := NewWithT(t)

Expand Down Expand Up @@ -828,6 +858,110 @@ func TestReconcileUnhealthyMachines(t *testing.T) {
removeFinalizer(g, m1)
g.Expect(env.Cleanup(ctx, m1, m2, m3)).To(Succeed())
})
t.Run("Remediation deletes unhealthy machine failed to provision - 3 CP", func(t *testing.T) {
g := NewWithT(t)

m1 := createMachine(ctx, g, ns.Name, "m1-unhealthy-", withMachineHealthCheckFailed(), withWaitBeforeDeleteFinalizer(), withoutNodeRef())
m2 := createMachine(ctx, g, ns.Name, "m2-healthy-", withHealthyEtcdMember())
m3 := createMachine(ctx, g, ns.Name, "m3-healthy-", withHealthyEtcdMember())

controlPlane := &internal.ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: utilpointer.Int32(3),
Version: "v1.19.1",
},
Status: controlplanev1.KubeadmControlPlaneStatus{
Initialized: true,
},
},
Cluster: &clusterv1.Cluster{},
Machines: collections.FromMachines(m1, m2, m3),
}

r := &KubeadmControlPlaneReconciler{
Client: env.GetClient(),
recorder: record.NewFakeRecorder(32),
managementCluster: &fakeManagementCluster{
Workload: fakeWorkloadCluster{
EtcdMembersResult: nodes(controlPlane.Machines),
},
},
}
controlPlane.InjectTestManagementCluster(r.managementCluster)

ret, err := r.reconcileUnhealthyMachines(ctx, controlPlane)

g.Expect(ret.IsZero()).To(BeFalse()) // Remediation completed, requeue
g.Expect(err).ToNot(HaveOccurred())

g.Expect(controlPlane.KCP.Annotations).To(HaveKey(controlplanev1.RemediationInProgressAnnotation))
remediationData, err := RemediationDataFromAnnotation(controlPlane.KCP.Annotations[controlplanev1.RemediationInProgressAnnotation])
g.Expect(err).ToNot(HaveOccurred())
g.Expect(remediationData.Machine).To(Equal(m1.Name))
g.Expect(remediationData.RetryCount).To(Equal(0))

assertMachineCondition(ctx, g, m1, clusterv1.MachineOwnerRemediatedCondition, corev1.ConditionFalse, clusterv1.RemediationInProgressReason, clusterv1.ConditionSeverityWarning, "")

err = env.Get(ctx, client.ObjectKey{Namespace: m1.Namespace, Name: m1.Name}, m1)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(m1.ObjectMeta.DeletionTimestamp.IsZero()).To(BeFalse())

removeFinalizer(g, m1)
g.Expect(env.Cleanup(ctx, m1, m2, m3)).To(Succeed())
})
t.Run("Remediation deletes unhealthy machine which failed to provision - 3 CP", func(t *testing.T) {
g := NewWithT(t)

m1 := createMachine(ctx, g, ns.Name, "m1-unhealthy-", withMachineHealthCheckFailed(), withWaitBeforeDeleteFinalizer(), withoutNodeRef())
m2 := createMachine(ctx, g, ns.Name, "m2-healthy-", withHealthyEtcdMember())
m3 := createMachine(ctx, g, ns.Name, "m3-healthy-", withHealthyEtcdMember())

controlPlane := &internal.ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: utilpointer.Int32(3),
Version: "v1.19.1",
},
Status: controlplanev1.KubeadmControlPlaneStatus{
Initialized: true,
},
},
Cluster: &clusterv1.Cluster{},
Machines: collections.FromMachines(m1, m2, m3),
}

r := &KubeadmControlPlaneReconciler{
Client: env.GetClient(),
recorder: record.NewFakeRecorder(32),
managementCluster: &fakeManagementCluster{
Workload: fakeWorkloadCluster{
EtcdMembersResult: nodes(controlPlane.Machines),
},
},
}
controlPlane.InjectTestManagementCluster(r.managementCluster)

ret, err := r.reconcileUnhealthyMachines(ctx, controlPlane)

g.Expect(ret.IsZero()).To(BeFalse()) // Remediation completed, requeue
g.Expect(err).ToNot(HaveOccurred())

g.Expect(controlPlane.KCP.Annotations).To(HaveKey(controlplanev1.RemediationInProgressAnnotation))
remediationData, err := RemediationDataFromAnnotation(controlPlane.KCP.Annotations[controlplanev1.RemediationInProgressAnnotation])
g.Expect(err).ToNot(HaveOccurred())
g.Expect(remediationData.Machine).To(Equal(m1.Name))
g.Expect(remediationData.RetryCount).To(Equal(0))

assertMachineCondition(ctx, g, m1, clusterv1.MachineOwnerRemediatedCondition, corev1.ConditionFalse, clusterv1.RemediationInProgressReason, clusterv1.ConditionSeverityWarning, "")

err = env.Get(ctx, client.ObjectKey{Namespace: m1.Namespace, Name: m1.Name}, m1)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(m1.ObjectMeta.DeletionTimestamp.IsZero()).To(BeFalse())

removeFinalizer(g, m1)
g.Expect(env.Cleanup(ctx, m1, m2, m3)).To(Succeed())
})
t.Run("Remediation deletes unhealthy machine - 4 CP (during 3 CP rolling upgrade)", func(t *testing.T) {
g := NewWithT(t)

Expand Down Expand Up @@ -881,6 +1015,59 @@ func TestReconcileUnhealthyMachines(t *testing.T) {
removeFinalizer(g, m1)
g.Expect(env.Cleanup(ctx, m1, m2, m3, m4)).To(Succeed())
})
t.Run("Remediation deletes unhealthy machine failed to provision - 4 CP (during 3 CP rolling upgrade)", func(t *testing.T) {
g := NewWithT(t)

m1 := createMachine(ctx, g, ns.Name, "m1-unhealthy-", withMachineHealthCheckFailed(), withWaitBeforeDeleteFinalizer(), withoutNodeRef())
m2 := createMachine(ctx, g, ns.Name, "m2-healthy-", withHealthyEtcdMember())
m3 := createMachine(ctx, g, ns.Name, "m3-healthy-", withHealthyEtcdMember())
m4 := createMachine(ctx, g, ns.Name, "m4-healthy-", withHealthyEtcdMember())

controlPlane := &internal.ControlPlane{
KCP: &controlplanev1.KubeadmControlPlane{
Spec: controlplanev1.KubeadmControlPlaneSpec{
Replicas: utilpointer.Int32(4),
Version: "v1.19.1",
},
Status: controlplanev1.KubeadmControlPlaneStatus{
Initialized: true,
},
},
Cluster: &clusterv1.Cluster{},
Machines: collections.FromMachines(m1, m2, m3, m4),
}

r := &KubeadmControlPlaneReconciler{
Client: env.GetClient(),
recorder: record.NewFakeRecorder(32),
managementCluster: &fakeManagementCluster{
Workload: fakeWorkloadCluster{
EtcdMembersResult: nodes(controlPlane.Machines),
},
},
}
controlPlane.InjectTestManagementCluster(r.managementCluster)

ret, err := r.reconcileUnhealthyMachines(ctx, controlPlane)

g.Expect(ret.IsZero()).To(BeFalse()) // Remediation completed, requeue
g.Expect(err).ToNot(HaveOccurred())

g.Expect(controlPlane.KCP.Annotations).To(HaveKey(controlplanev1.RemediationInProgressAnnotation))
remediationData, err := RemediationDataFromAnnotation(controlPlane.KCP.Annotations[controlplanev1.RemediationInProgressAnnotation])
g.Expect(err).ToNot(HaveOccurred())
g.Expect(remediationData.Machine).To(Equal(m1.Name))
g.Expect(remediationData.RetryCount).To(Equal(0))

assertMachineCondition(ctx, g, m1, clusterv1.MachineOwnerRemediatedCondition, corev1.ConditionFalse, clusterv1.RemediationInProgressReason, clusterv1.ConditionSeverityWarning, "")

err = env.Get(ctx, client.ObjectKey{Namespace: m1.Namespace, Name: m1.Name}, m1)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(m1.ObjectMeta.DeletionTimestamp.IsZero()).To(BeFalse())

removeFinalizer(g, m1)
g.Expect(env.Cleanup(ctx, m1, m2, m3, m4)).To(Succeed())
})
t.Run("Remediation fails gracefully if no healthy Control Planes are available to become etcd leader", func(t *testing.T) {
g := NewWithT(t)

Expand Down

0 comments on commit 39ca0f8

Please sign in to comment.