Skip to content

Commit 686a16f

Browse files
committed
Covers issue in unit tests
Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
1 parent bdebb58 commit 686a16f

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

internal/cmd/controller/reconciler/schedule_controller.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,6 @@ func setClustersScheduled(ctx context.Context, c client.Client, clusters []strin
416416
func updateScheduledClusters(ctx context.Context, scheduler quartz.Scheduler, c client.Client, clustersNew []string, clustersOld []string, namespace string) error {
417417
// first look for clusters that are not scheduled yet and flag them as scheduled
418418
for _, cluster := range clustersNew {
419-
// set schedule to true and also reset the activeSchedule property
420-
// When we update a Schedule it begins from a offSchedule status until
421-
// executeStart is called.
422419
if err := setClusterScheduled(ctx, c, cluster, namespace, true); err != nil {
423420
return err
424421
}

internal/cmd/controller/reconciler/schedule_controller_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ var _ = Describe("ScheduleReconciler", func() {
271271
ObjectMeta: metav1.ObjectMeta{
272272
Name: "test-cluster2",
273273
Namespace: "default",
274-
Labels: map[string]string{"env": "test"},
274+
Labels: map[string]string{"env": "test", "foo": "bar"},
275275
},
276276
}
277277

@@ -305,10 +305,28 @@ var _ = Describe("ScheduleReconciler", func() {
305305

306306
// clusters 1, 2 and 3 should be scheduled in the quartz.Scheduler and also
307307
// be flagged as Scheduled
308-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", true, true)
309-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true)
310-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", true, true)
311-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", false, false)
308+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", true, true, false)
309+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, false)
310+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", true, true, false)
311+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", false, false, false)
312+
313+
// force the start of the schedule (so it sets .Status.ActiveSchedule=true)
314+
jobKey := scheduleKey(schedule)
315+
job, err := scheduler.GetScheduledJob(jobKey)
316+
Expect(err).NotTo(HaveOccurred())
317+
318+
cronDurationJob, ok := job.JobDetail().Job().(*CronDurationJob)
319+
Expect(ok).To(BeTrue())
320+
321+
// Manually trigger start
322+
err = cronDurationJob.executeStart(ctx)
323+
Expect(err).NotTo(HaveOccurred())
324+
325+
// check now that the clusters have the expected values, specially Status.ActiveSchedule
326+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", true, true, true)
327+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, true)
328+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", true, true, true)
329+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", false, false, false)
312330

313331
// update the schedule, now it only looks for the label foo=bar
314332
scheduleUpdated := &fleet.Schedule{}
@@ -326,11 +344,13 @@ var _ = Describe("ScheduleReconciler", func() {
326344
_, err = reconciler.Reconcile(ctx, req)
327345
Expect(err).NotTo(HaveOccurred())
328346

329-
// now only 1 cluster should be flagged as scheduled and be in the scheduled jobs list
330-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", false, false)
331-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", false, false)
332-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", false, false)
333-
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", true, true)
347+
// cluster 2 and 4 should be still targetted.
348+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster", "default", false, false, false)
349+
// cluster 2 had Status.ActiveSchedule set to true, but because we updated the Schedule
350+
// it should be back to false.
351+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster2", "default", true, true, false)
352+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster3", "default", false, false, false)
353+
checkClusterIsScheduled(scheduler, k8sclient, "test-cluster4", "default", true, true, false)
334354
})
335355
})
336356
})
@@ -339,7 +359,7 @@ func checkClusterIsScheduled(
339359
scheduler quartz.Scheduler,
340360
k8sclient client.Client,
341361
cluster, namespace string,
342-
scheduledExpected, flaggedAsScheduledExpected bool) {
362+
scheduledExpected, flaggedAsScheduledExpected, activeScheduleExpected bool) {
343363
isScheduled, err := isClusterScheduled(scheduler, cluster, namespace)
344364
Expect(err).NotTo(HaveOccurred())
345365
Expect(isScheduled).To(Equal(scheduledExpected))
@@ -349,4 +369,5 @@ func checkClusterIsScheduled(
349369
err = k8sclient.Get(context.Background(), key, clusterObj)
350370
Expect(err).NotTo(HaveOccurred())
351371
Expect(clusterObj.Status.Scheduled).To(Equal(flaggedAsScheduledExpected))
372+
Expect(clusterObj.Status.ActiveSchedule).To(Equal(activeScheduleExpected))
352373
}

0 commit comments

Comments
 (0)