From ce638ea496672bcd17ec5e6205f77dc62b054c43 Mon Sep 17 00:00:00 2001 From: ishan tyagi Date: Fri, 13 Jan 2023 18:10:14 +0530 Subject: [PATCH] Added unit tests. --- pkg/snapshot/snapshotter/snapshotter_test.go | 88 ++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/pkg/snapshot/snapshotter/snapshotter_test.go b/pkg/snapshot/snapshotter/snapshotter_test.go index 5545d9f8b..73c1fee3b 100644 --- a/pkg/snapshot/snapshotter/snapshotter_test.go +++ b/pkg/snapshot/snapshotter/snapshotter_test.go @@ -334,6 +334,94 @@ var _ = Describe("Snapshotter", func() { }) }) }) + }) + + Describe("Scenarios to take full-snapshot during startup", func() { + var ( + ssr *Snapshotter + currentMin int + currentHour int + ) + BeforeEach(func() { + currentHour = time.Now().Hour() + currentMin = time.Now().Minute() + snapstoreConfig = &brtypes.SnapstoreConfig{Container: path.Join(outputDir, "default.bkp")} + store, err = snapstore.GetSnapstore(snapstoreConfig) + Expect(err).ShouldNot(HaveOccurred()) + }) + Context("Previous full snapshot was taken more than 24hrs before, so FullSnapshot missed", func() { + It("should return true", func() { + snapshotterConfig := &brtypes.SnapshotterConfig{ + FullSnapshotSchedule: fmt.Sprintf("%d %d * * *", (currentMin+1)%60, (currentHour+2)%24), + } + + ssr, err = NewSnapshotter(logger, snapshotterConfig, store, etcdConnectionConfig, compressionConfig, healthConfig, snapstoreConfig) + Expect(err).ShouldNot(HaveOccurred()) + + // Previous full snapshot was taken 2 days before + ssr.PrevFullSnapshot = &brtypes.Snapshot{ + CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-2, currentHour, currentMin, 0, 0, time.Local), + } + isFullSnapMissed := ssr.IsScheduledFullSnapshotMissed() + Expect(isFullSnapMissed).Should(BeTrue()) + }) + }) + + Context("Previous full snapshot was taken exactly at scheduled snapshot time, no FullSnapshot missed", func() { + It("should return false", func() { + snapshotterConfig := &brtypes.SnapshotterConfig{ + FullSnapshotSchedule: fmt.Sprintf("%d %d * * *", (currentMin+1)%60, (currentHour+2)%24), + } + + ssr, err = NewSnapshotter(logger, snapshotterConfig, store, etcdConnectionConfig, compressionConfig, healthConfig, snapstoreConfig) + Expect(err).ShouldNot(HaveOccurred()) + + // Previous full snapshot was taken 1 days before at exactly at scheduled time + ssr.PrevFullSnapshot = &brtypes.Snapshot{ + CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day()-1, (currentHour+2)%24, (currentMin+1)%60, 0, 0, time.Local), + } + isFullSnapMissed := ssr.IsScheduledFullSnapshotMissed() + Expect(isFullSnapMissed).Should(BeFalse()) + }) + }) + + Context("Previous snapshot was taken within 24hrs and not gonna missed schedule fullsnapshot", func() { + It("should return false", func() { + scheduleHour := (currentHour + 4) % 24 + snapshotterConfig := &brtypes.SnapshotterConfig{ + FullSnapshotSchedule: fmt.Sprintf("%d %d * * *", currentMin, scheduleHour), + } + + ssr, err = NewSnapshotter(logger, snapshotterConfig, store, etcdConnectionConfig, compressionConfig, healthConfig, snapstoreConfig) + Expect(err).ShouldNot(HaveOccurred()) + + // Previous full snapshot was taken 4hrs 10 mins before startup + ssr.PrevFullSnapshot = &brtypes.Snapshot{ + CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), (currentHour-4)%24, (currentMin-10)%60, 0, 0, time.Local), + } + isFullSnapMissed := ssr.IsScheduledFullSnapshotMissed() + Expect(isFullSnapMissed).Should(BeFalse()) + }) + }) + + Context("Previous snapshot was taken within 24hrs and gonna miss 24hrs of schedule fullsnapshot window", func() { + It("should return true", func() { + scheduleHour := (currentHour + 8) % 24 + snapshotterConfig := &brtypes.SnapshotterConfig{ + FullSnapshotSchedule: fmt.Sprintf("%d %d * * *", currentMin, scheduleHour), + } + + ssr, err = NewSnapshotter(logger, snapshotterConfig, store, etcdConnectionConfig, compressionConfig, healthConfig, snapstoreConfig) + Expect(err).ShouldNot(HaveOccurred()) + + // Previous full snapshot was taken 18hrs(<24hrs) before startup + ssr.PrevFullSnapshot = &brtypes.Snapshot{ + CreatedOn: time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), (currentHour-18)%24, (currentMin)%60, 0, 0, time.Local), + } + isFullSnapMissed := ssr.IsScheduledFullSnapshotMissed() + Expect(isFullSnapMissed).Should(BeTrue()) + }) + }) Context("##GarbageCollector", func() { var (