|
| 1 | +package etcd_cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDailyScheduleFor(t *testing.T) { |
| 12 | + t.Run("result is within the 20:00–03:59 window", func(t *testing.T) { |
| 13 | + g := NewWithT(t) |
| 14 | + identities := [][2]string{ |
| 15 | + {"ns-a", "cluster-1"}, |
| 16 | + {"ns-b", "cluster-2"}, |
| 17 | + {"production", "control-plane"}, |
| 18 | + {"kube-system", "etcd-backup"}, |
| 19 | + {"", "no-namespace"}, |
| 20 | + } |
| 21 | + for _, id := range identities { |
| 22 | + expr := dailyScheduleFor(id[0], id[1]) |
| 23 | + |
| 24 | + var minute, hour int |
| 25 | + _, parseErr := fmt.Sscanf(expr, "%d %d * * *", &minute, &hour) |
| 26 | + g.Expect(parseErr).NotTo(HaveOccurred(), "expression %q could not be parsed", expr) |
| 27 | + g.Expect(minute).To(BeNumerically(">=", 0)) |
| 28 | + g.Expect(minute).To(BeNumerically("<=", 59)) |
| 29 | + // Hour must be within the 8-hour window: 20, 21, 22, 23, 0, 1, 2, 3 |
| 30 | + validHours := []int{20, 21, 22, 23, 0, 1, 2, 3} |
| 31 | + g.Expect(validHours). |
| 32 | + To(ContainElement(hour), "hour %d is outside the 8-hour window for %s/%s", hour, id[0], id[1]) |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("is deterministic for the same cluster", func(t *testing.T) { |
| 37 | + g := NewWithT(t) |
| 38 | + g.Expect(dailyScheduleFor("default", "my-cluster")).To(Equal(dailyScheduleFor("default", "my-cluster"))) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("produces different schedules for different clusters", func(t *testing.T) { |
| 42 | + g := NewWithT(t) |
| 43 | + g.Expect(dailyScheduleFor("ns", "cluster-a")).NotTo(Equal(dailyScheduleFor("ns", "cluster-b"))) |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +func TestResolveBackupSchedule(t *testing.T) { |
| 48 | + t.Run("@daily returns a usable schedule within the midnight window", func(t *testing.T) { |
| 49 | + g := NewWithT(t) |
| 50 | + schedule, err := resolveBackupSchedule("@daily", "default", "my-cluster") |
| 51 | + g.Expect(err).NotTo(HaveOccurred()) |
| 52 | + |
| 53 | + // The resolved schedule must fire exactly once per day; its next-run from a known |
| 54 | + // base should be within 24h and land in the 20:00–03:59 window. |
| 55 | + base := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 56 | + next := schedule.Next(base) |
| 57 | + g.Expect(next).NotTo(BeZero()) |
| 58 | + hour := next.Hour() |
| 59 | + validHours := []int{20, 21, 22, 23, 0, 1, 2, 3} |
| 60 | + g.Expect(validHours).To(ContainElement(hour)) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("@daily is deterministic for the same cluster", func(t *testing.T) { |
| 64 | + g := NewWithT(t) |
| 65 | + base := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 66 | + s1, _ := resolveBackupSchedule("@daily", "default", "my-cluster") |
| 67 | + s2, _ := resolveBackupSchedule("@daily", "default", "my-cluster") |
| 68 | + g.Expect(s1.Next(base)).To(Equal(s2.Next(base))) |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("@daily produces different next-run times for different clusters", func(t *testing.T) { |
| 72 | + g := NewWithT(t) |
| 73 | + base := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 74 | + a, _ := resolveBackupSchedule("@daily", "ns", "cluster-a") |
| 75 | + b, _ := resolveBackupSchedule("@daily", "ns", "cluster-b") |
| 76 | + g.Expect(a.Next(base)).NotTo(Equal(b.Next(base))) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("standard cron parses successfully", func(t *testing.T) { |
| 80 | + g := NewWithT(t) |
| 81 | + schedule, err := resolveBackupSchedule("0 2 * * *", "ns", "cluster") |
| 82 | + g.Expect(err).NotTo(HaveOccurred()) |
| 83 | + base := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC) |
| 84 | + g.Expect(schedule.Next(base).Hour()).To(Equal(2)) |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("invalid schedule returns an error", func(t *testing.T) { |
| 88 | + g := NewWithT(t) |
| 89 | + _, err := resolveBackupSchedule("invalid cron", "ns", "cluster") |
| 90 | + g.Expect(err).To(HaveOccurred()) |
| 91 | + }) |
| 92 | +} |
0 commit comments