Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
found usage of normal time instead of abtime
  • Loading branch information
sklarsa committed Jun 21, 2023
commit 795e0d83fbe457ffb10113ce4db6ec7059ba5911
2 changes: 1 addition & 1 deletion internal/controller/questdbsnapshotschedule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *QuestDBSnapshotScheduleReconciler) Reconcile(ctx context.Context, req c

// Calculate the requeue time before any modifications
// are made to the NextSnapshot time
requeueTime := time.Until(nextSnapshotTime)
requeueTime := nextSnapshotTime.Sub(r.TimeSource.Now())
if requeueTime < 0 {
requeueTime = 0
}
Expand Down
40 changes: 19 additions & 21 deletions internal/controller/questdbsnapshotschedule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
q *crdv1beta1.QuestDB
sched *crdv1beta1.QuestDBSnapshotSchedule

timeSource = abtime.NewManual()
timeSource *abtime.ManualTime

timeout = time.Second * 2
//consistencyTimeout = time.Millisecond * 600
Expand All @@ -40,7 +40,7 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
Client: k8sClient,
Scheme: scheme.Scheme,
Recorder: record.NewFakeRecorder(100),
TimeSource: timeSource,
TimeSource: abtime.NewManual(),
}

By("Creating a QuestDB")
Expand All @@ -60,25 +60,34 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
Schedule: "*/1 * * * *",
},
}

})

It("should requeue at the correct time when a schedule is created", func() {

Expect(k8sClient.Create(ctx, sched)).To(Succeed())
r.TimeSource = abtime.NewManualAtTime(sched.CreationTimestamp.Time)
timeSource = r.TimeSource.(*abtime.ManualTime)

By("Reconciling the QuestDBSnapshotSchedule")
_, err := r.Reconcile(ctx, ctrl.Request{
res, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(err).ToNot(HaveOccurred())
nextRunTime := r.TimeSource.Now().Add(time.Minute).Truncate(time.Minute)
Expect(res.RequeueAfter).To(Equal(nextRunTime.Sub(r.TimeSource.Now())))

})

It("should create a snapshot if the cron schedule has triggered", func() {

By("Bumping the clock a bit more than 1 minute")
timeSource.Advance(time.Minute + 10*time.Second)
By("Bumping the clock more than 1 minute")
timeSource.Advance(time.Minute + 5*time.Second)

By("Forcing a reconcile")
res, err := r.Reconcile(ctx, ctrl.Request{
_, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())

By("Checking that a snapshot has been created")
Expand All @@ -104,10 +113,9 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
}, timeout, interval).Should(Succeed())

By("Forcing a reconcile")
res, err := r.Reconcile(ctx, ctrl.Request{
_, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(res).To(Equal(ctrl.Result{}))
Expect(err).ToNot(HaveOccurred())

By("Checking that the status has been updated")
Expand All @@ -117,7 +125,7 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {

It("should take a second snapshot if the cron schedule has triggered", func() {
By("Bumping the clock more than 1 minute")
timeSource.Advance(time.Minute + 10*time.Second)
timeSource.Advance(time.Minute + 5*time.Second)

By("Forcing a reconcile")
res, err := r.Reconcile(ctx, ctrl.Request{
Expand All @@ -134,15 +142,6 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
}, timeout, interval).Should(Succeed())
})

It("should requeue the request to the time of the next trigger", func() {
By("Forcing a reconcile")
result, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(err).ToNot(HaveOccurred())
Expect(result.RequeueAfter).ToNot(BeZero())
})

It("should delete the snapshot if the retention policy is set to 1", func() {
By("Setting the retention policy to 1")
Eventually(func(g Gomega) {
Expand All @@ -163,11 +162,10 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
}

By("Forcing a reconcile")
res, err := r.Reconcile(ctx, ctrl.Request{
_, err := r.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKeyFromObject(sched),
})
Expect(err).ToNot(HaveOccurred())
Expect(res).To(Equal(ctrl.Result{}))

By("Checking that a snapshot has been deleted")
Eventually(func(g Gomega) {
Expand Down