Skip to content

Commit de843f8

Browse files
committed
Update unit tests
1 parent 16f81fd commit de843f8

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

internal/controllers/machinedeployment/machinedeployment_controller_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ import (
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
"k8s.io/client-go/tools/record"
2828
"k8s.io/client-go/util/retry"
29+
utilfeature "k8s.io/component-base/featuregate/testing"
2930
"k8s.io/utils/ptr"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
3132
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3233
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3334

3435
clusterv1 "sigs.k8s.io/cluster-api/api/core/v1beta2"
3536
"sigs.k8s.io/cluster-api/controllers/external"
37+
"sigs.k8s.io/cluster-api/feature"
3638
"sigs.k8s.io/cluster-api/util"
3739
v1beta1conditions "sigs.k8s.io/cluster-api/util/conditions/deprecated/v1beta1"
3840
"sigs.k8s.io/cluster-api/util/patch"
@@ -49,6 +51,8 @@ func TestMachineDeploymentReconciler(t *testing.T) {
4951
setup := func(t *testing.T, g *WithT) (*corev1.Namespace, *clusterv1.Cluster) {
5052
t.Helper()
5153

54+
utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.MachineTaintPropagation, true)
55+
5256
t.Log("Creating the namespace")
5357
ns, err := env.CreateNamespace(ctx, machineDeploymentNamespace)
5458
g.Expect(err).ToNot(HaveOccurred())
@@ -416,6 +420,30 @@ func TestMachineDeploymentReconciler(t *testing.T) {
416420
g.Expect(machineSets.Items[1].Spec.Deletion.Order).Should(Equal(clusterv1.NewestMachineSetDeletionOrder))
417421
}).Should(Succeed())
418422

423+
// Update the taints of the MachineDeployment,
424+
// expect the Reconcile to be called and the MachineSet to be updated in-place.
425+
t.Log("Updating deletion.order on the MachineDeployment")
426+
additionalTaint := clusterv1.MachineTaint{
427+
Key: "additional-taint-key",
428+
Value: "additional-taint-value",
429+
Effect: corev1.TaintEffectNoSchedule,
430+
Propagation: clusterv1.MachineTaintPropagationAlways,
431+
}
432+
modifyFunc = func(d *clusterv1.MachineDeployment) {
433+
d.Spec.Template.Spec.Taints = append(d.Spec.Template.Spec.Taints, additionalTaint)
434+
}
435+
g.Expect(updateMachineDeployment(ctx, env, deployment, modifyFunc)).To(Succeed())
436+
g.Eventually(func(g Gomega) {
437+
g.Expect(env.List(ctx, machineSets, msListOpts...)).Should(Succeed())
438+
// Verify we still only have 2 MachineSets.
439+
g.Expect(machineSets.Items).To(HaveLen(2))
440+
// Verify the taints value is updated
441+
g.Expect(machineSets.Items[0].Spec.Template.Spec.Taints).Should(ContainElement(additionalTaint))
442+
443+
// Verify that the old machine set has the new taints.
444+
g.Expect(machineSets.Items[1].Spec.Template.Spec.Taints).Should(ContainElement(additionalTaint))
445+
}).Should(Succeed())
446+
419447
// Verify that all the MachineSets have the expected OwnerRef.
420448
t.Log("Verifying MachineSet owner references")
421449
g.Eventually(func() bool {

internal/controllers/machinedeployment/mdutil/util_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ func TestFindNewAndOldMachineSets(t *testing.T) {
415415

416416
matchingMSDiffersInPlaceMutableFields := generateMS(deployment)
417417
matchingMSDiffersInPlaceMutableFields.Spec.Template.Spec.Deletion.NodeDrainTimeoutSeconds = ptr.To(int32(20))
418+
matchingMSDiffersInPlaceMutableFields.Spec.Template.Spec.Taints = []clusterv1.MachineTaint{
419+
{Key: "taint-key", Value: "taint-value", Effect: corev1.TaintEffectNoSchedule, Propagation: clusterv1.MachineTaintPropagationAlways},
420+
}
418421

419422
oldMS := generateMS(deployment)
420423
oldMS.Spec.Template.Spec.InfrastructureRef.Name = "old-infra-ref"

internal/controllers/machineset/machineset_controller_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,28 +472,39 @@ func TestMachineSetReconciler(t *testing.T) {
472472
}
473473

474474
// Verify that in-place mutable fields propagate from MachineSet to Machines.
475-
t.Log("Updating NodeDrainTimeoutSeconds on MachineSet")
475+
t.Log("Updating NodeDrainTimeoutSeconds and Taints on MachineSet")
476476
patchHelper, err := patch.NewHelper(instance, env)
477477
g.Expect(err).ToNot(HaveOccurred())
478478
instance.Spec.Template.Spec.Deletion.NodeDrainTimeoutSeconds = duration5m
479+
additionalTaint := clusterv1.MachineTaint{
480+
Key: "additional-taint-key",
481+
Value: "additional-taint-value",
482+
Effect: corev1.TaintEffectNoSchedule,
483+
Propagation: clusterv1.MachineTaintPropagationAlways,
484+
}
485+
instance.Spec.Template.Spec.Taints = []clusterv1.MachineTaint{additionalTaint}
479486
g.Expect(patchHelper.Patch(ctx, instance)).Should(Succeed())
480487

481488
t.Log("Verifying new NodeDrainTimeoutSeconds value is set on Machines")
482489
g.Eventually(func() bool {
483490
if err := env.List(ctx, machines, client.InNamespace(namespace.Name)); err != nil {
484491
return false
485492
}
486-
// All the machines should have the new NodeDrainTimeoutValue
493+
// All the machines should have the new NodeDrainTimeoutValue and the new taint
487494
for _, m := range machines.Items {
488495
if m.Spec.Deletion.NodeDrainTimeoutSeconds == nil {
489496
return false
490497
}
491498
if *m.Spec.Deletion.NodeDrainTimeoutSeconds != *duration5m {
492499
return false
493500
}
501+
if len(m.Spec.Taints) != 1 || m.Spec.Taints[0].Key != additionalTaint.Key ||
502+
m.Spec.Taints[0].Value != additionalTaint.Value || m.Spec.Taints[0].Effect != additionalTaint.Effect {
503+
return false
504+
}
494505
}
495506
return true
496-
}, timeout).Should(BeTrue(), "machine should have the updated NodeDrainTimeoutSeconds value")
507+
}, timeout).Should(BeTrue(), "machine should have the updated NodeDrainTimeoutSeconds and Taints values")
497508

498509
// Try to delete 1 machine and check the MachineSet scales back up.
499510
machineToBeDeleted := machines.Items[0]
@@ -3528,6 +3539,19 @@ func TestComputeDesiredMachine(t *testing.T) {
35283539
duration5s := ptr.To(int32(5))
35293540
duration10s := ptr.To(int32(10))
35303541

3542+
machineTaint := clusterv1.MachineTaint{
3543+
Key: "taint-key",
3544+
Value: "taint-value",
3545+
Effect: corev1.TaintEffectNoSchedule,
3546+
Propagation: clusterv1.MachineTaintPropagationAlways,
3547+
}
3548+
changedMachineTaint := clusterv1.MachineTaint{
3549+
Key: "changed-taint-key",
3550+
Value: "changed-taint-value",
3551+
Effect: corev1.TaintEffectNoSchedule,
3552+
Propagation: clusterv1.MachineTaintPropagationAlways,
3553+
}
3554+
35313555
namingTemplateKey := "-md"
35323556
mdName := "testmd"
35333557
msName := "ms1"
@@ -3567,6 +3591,9 @@ func TestComputeDesiredMachine(t *testing.T) {
35673591
NodeDeletionTimeoutSeconds: duration10s,
35683592
},
35693593
MinReadySeconds: ptr.To[int32](10),
3594+
Taints: []clusterv1.MachineTaint{
3595+
machineTaint,
3596+
},
35703597
},
35713598
}
35723599

@@ -3590,6 +3617,7 @@ func TestComputeDesiredMachine(t *testing.T) {
35903617
NodeDeletionTimeoutSeconds: duration10s,
35913618
},
35923619
MinReadySeconds: ptr.To[int32](10),
3620+
Taints: []clusterv1.MachineTaint{machineTaint},
35933621
},
35943622
}
35953623

@@ -3618,6 +3646,7 @@ func TestComputeDesiredMachine(t *testing.T) {
36183646
existingMachine.Spec.Deletion.NodeDeletionTimeoutSeconds = duration5s
36193647
existingMachine.Spec.Deletion.NodeVolumeDetachTimeoutSeconds = duration5s
36203648
existingMachine.Spec.MinReadySeconds = ptr.To[int32](5)
3649+
existingMachine.Spec.Taints = []clusterv1.MachineTaint{changedMachineTaint}
36213650

36223651
expectedUpdatedMachine := skeletonMachine.DeepCopy()
36233652
expectedUpdatedMachine.Name = existingMachine.Name

0 commit comments

Comments
 (0)