Skip to content

Commit b728292

Browse files
committed
Added negative test
Signed-off-by: Lukas Frank <lukas.frank@sap.com>
1 parent 388293c commit b728292

1 file changed

Lines changed: 63 additions & 18 deletions

File tree

internal/controllers/compute/machine_scheduler_test.go

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ import (
2121
corev1alpha1 "github.com/ironcore-dev/ironcore/api/core/v1alpha1"
2222
)
2323

24-
func markMachinePoolReady(machinePool *computev1alpha1.MachinePool) {
24+
func setMachinePoolReady(machinePool *computev1alpha1.MachinePool, status corev1.ConditionStatus) {
2525
machinePool.Status.Conditions = computev1alpha1.SetMachinePoolCondition(machinePool.Status.Conditions,
2626
computev1alpha1.MachinePoolCondition{
2727
Type: computev1alpha1.MachinePoolReady,
28-
Status: corev1.ConditionTrue,
28+
Status: status,
2929
})
3030
}
3131

@@ -48,7 +48,7 @@ var _ = Describe("MachineScheduler", func() {
4848
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
4949
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
5050
}
51-
markMachinePoolReady(machinePool)
51+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
5252
})).Should(Succeed())
5353

5454
By("creating a machine w/ the requested machine class")
@@ -103,7 +103,7 @@ var _ = Describe("MachineScheduler", func() {
103103
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
104104
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
105105
}
106-
markMachinePoolReady(machinePool)
106+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
107107
})).Should(Succeed())
108108

109109
By("waiting for the machine to be scheduled onto the machine pool")
@@ -128,7 +128,7 @@ var _ = Describe("MachineScheduler", func() {
128128
machinePoolNoMatchingLabels.Status.Allocatable = corev1alpha1.ResourceList{
129129
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
130130
}
131-
markMachinePoolReady(machinePoolNoMatchingLabels)
131+
setMachinePoolReady(machinePoolNoMatchingLabels, corev1.ConditionTrue)
132132
})).Should(Succeed())
133133

134134
By("creating a machine pool w/ matching labels")
@@ -148,7 +148,7 @@ var _ = Describe("MachineScheduler", func() {
148148
machinePoolMatchingLabels.Status.Allocatable = corev1alpha1.ResourceList{
149149
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
150150
}
151-
markMachinePoolReady(machinePoolMatchingLabels)
151+
setMachinePoolReady(machinePoolMatchingLabels, corev1.ConditionTrue)
152152
})).Should(Succeed())
153153

154154
By("creating a machine w/ the requested machine class")
@@ -203,7 +203,7 @@ var _ = Describe("MachineScheduler", func() {
203203
taintedMachinePool.Status.Allocatable = corev1alpha1.ResourceList{
204204
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
205205
}
206-
markMachinePoolReady(taintedMachinePool)
206+
setMachinePoolReady(taintedMachinePool, corev1.ConditionTrue)
207207
})).Should(Succeed())
208208

209209
By("creating a machine")
@@ -251,7 +251,7 @@ var _ = Describe("MachineScheduler", func() {
251251

252252
By("re-asserting the machine pool is ready")
253253
Eventually(UpdateStatus(taintedMachinePool, func() {
254-
markMachinePoolReady(taintedMachinePool)
254+
setMachinePoolReady(taintedMachinePool, corev1.ConditionTrue)
255255
})).Should(Succeed())
256256

257257
By("observing the machine is scheduled onto the machine pool")
@@ -275,7 +275,7 @@ var _ = Describe("MachineScheduler", func() {
275275
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
276276
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
277277
}
278-
markMachinePoolReady(machinePool)
278+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
279279
})).Should(Succeed())
280280

281281
By("creating a second machine pool")
@@ -308,7 +308,7 @@ var _ = Describe("MachineScheduler", func() {
308308
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("5"),
309309
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, secondMachineClass.Name): resource.MustParse("100"),
310310
}
311-
markMachinePoolReady(secondMachinePool)
311+
setMachinePoolReady(secondMachinePool, corev1.ConditionTrue)
312312
})).Should(Succeed())
313313

314314
By("creating a machine")
@@ -346,7 +346,7 @@ var _ = Describe("MachineScheduler", func() {
346346
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
347347
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("50"),
348348
}
349-
markMachinePoolReady(machinePool)
349+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
350350
})).Should(Succeed())
351351

352352
By("creating a second machine pool")
@@ -365,7 +365,7 @@ var _ = Describe("MachineScheduler", func() {
365365
secondMachinePool.Status.Allocatable = corev1alpha1.ResourceList{
366366
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("50"),
367367
}
368-
markMachinePoolReady(secondMachinePool)
368+
setMachinePoolReady(secondMachinePool, corev1.ConditionTrue)
369369
})).Should(Succeed())
370370

371371
By("creating machines")
@@ -416,7 +416,7 @@ var _ = Describe("MachineScheduler", func() {
416416
By("patching the machine pool status to contain a machine class")
417417
Eventually(UpdateStatus(machinePool, func() {
418418
machinePool.Status.AvailableMachineClasses = []corev1.LocalObjectReference{{Name: machineClass.Name}}
419-
markMachinePoolReady(machinePool)
419+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
420420
})).Should(Succeed())
421421

422422
By("creating a machine")
@@ -443,7 +443,7 @@ var _ = Describe("MachineScheduler", func() {
443443
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
444444
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
445445
}
446-
markMachinePoolReady(machinePool)
446+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
447447
})).Should(Succeed())
448448

449449
By("checking that the machine is scheduled onto the machine pool")
@@ -467,7 +467,7 @@ var _ = Describe("MachineScheduler", func() {
467467
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
468468
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("2"),
469469
}
470-
markMachinePoolReady(machinePool)
470+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
471471
})).Should(Succeed())
472472

473473
By("creating the first machine")
@@ -537,7 +537,7 @@ var _ = Describe("MachineScheduler", func() {
537537
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
538538
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("5"),
539539
}
540-
markMachinePoolReady(machinePool)
540+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
541541
})).Should(Succeed())
542542

543543
By("creating a second machine pool")
@@ -571,7 +571,7 @@ var _ = Describe("MachineScheduler", func() {
571571
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("5"),
572572
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, secondMachineClass.Name): resource.MustParse("5"),
573573
}
574-
markMachinePoolReady(secondMachinePool)
574+
setMachinePoolReady(secondMachinePool, corev1.ConditionTrue)
575575
})).Should(Succeed())
576576

577577
By("creating a machine")
@@ -628,7 +628,52 @@ var _ = Describe("MachineScheduler", func() {
628628

629629
By("marking the machine pool ready")
630630
Eventually(UpdateStatus(machinePool, func() {
631-
markMachinePoolReady(machinePool)
631+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
632+
})).Should(Succeed())
633+
634+
By("waiting for the machine to be scheduled once the pool is ready")
635+
Eventually(Object(machine)).Should(SatisfyAll(
636+
HaveField("Spec.MachinePoolRef", Equal(&corev1.LocalObjectReference{Name: machinePool.Name})),
637+
HaveField("Status.State", Equal(computev1alpha1.MachineStatePending)),
638+
))
639+
})
640+
641+
It("should not schedule machines onto a machine pool whose ready condition is false", func(ctx SpecContext) {
642+
By("creating a machine pool")
643+
machinePool := &computev1alpha1.MachinePool{
644+
ObjectMeta: metav1.ObjectMeta{
645+
GenerateName: "test-pool-",
646+
},
647+
}
648+
Expect(k8sClient.Create(ctx, machinePool)).To(Succeed(), "failed to create machine pool")
649+
650+
By("patching the machine pool status to have capacity but a ready condition of false")
651+
Eventually(UpdateStatus(machinePool, func() {
652+
machinePool.Status.AvailableMachineClasses = []corev1.LocalObjectReference{{Name: machineClass.Name}}
653+
machinePool.Status.Allocatable = corev1alpha1.ResourceList{
654+
corev1alpha1.ClassCountFor(corev1alpha1.ClassTypeMachineClass, machineClass.Name): resource.MustParse("10"),
655+
}
656+
setMachinePoolReady(machinePool, corev1.ConditionFalse)
657+
})).Should(Succeed())
658+
659+
By("creating a machine w/ the requested machine class")
660+
machine := &computev1alpha1.Machine{
661+
ObjectMeta: metav1.ObjectMeta{
662+
Namespace: ns.Name,
663+
GenerateName: "test-machine-",
664+
},
665+
Spec: computev1alpha1.MachineSpec{
666+
MachineClassRef: corev1.LocalObjectReference{Name: machineClass.Name},
667+
},
668+
}
669+
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "failed to create machine")
670+
671+
By("observing the machine isn't scheduled onto the not-ready machine pool")
672+
Consistently(Object(machine)).Should(HaveField("Spec.MachinePoolRef", BeNil()))
673+
674+
By("marking the machine pool ready")
675+
Eventually(UpdateStatus(machinePool, func() {
676+
setMachinePoolReady(machinePool, corev1.ConditionTrue)
632677
})).Should(Succeed())
633678

634679
By("waiting for the machine to be scheduled once the pool is ready")

0 commit comments

Comments
 (0)