Skip to content

Commit

Permalink
Added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ishan16696 committed Jan 13, 2023
1 parent d480f23 commit f98c0ab
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions pkg/snapshot/snapshotter/snapshotter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit f98c0ab

Please sign in to comment.