1
2
3
4
5
6
7
8
9
10
11
12
13
14 package cron
15
16 import (
17 "testing"
18 "time"
19
20 . "github.com/onsi/gomega"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 "k8s.io/utils/pointer"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 )
26
27 func TestGetRecentUnmetScheduleTime(t *testing.T) {
28 g := NewGomegaWithT(t)
29
30 type testCase struct {
31 now string
32 lastScheduleTime string
33 creationTimeStamp string
34 schedule string
35 missedRun *string
36 nextRun *string
37 err error
38 }
39
40 zeroTime := "0001-01-01T00:00:00.000Z"
41 testCases := []testCase{
42 {
43 now: "2021-04-28T05:59:43.5Z",
44 lastScheduleTime: "2021-04-28T05:59:38.0Z",
45 creationTimeStamp: zeroTime,
46 schedule: "@every 5s",
47 missedRun: pointer.StringPtr("2021-04-28T05:59:43.0Z"),
48 nextRun: pointer.StringPtr("2021-04-28T05:59:48.0Z"),
49 err: nil,
50 },
51 {
52 now: "2021-04-28T06:49:35.079Z",
53 lastScheduleTime: "2021-04-28T06:49:35.000Z",
54 creationTimeStamp: zeroTime,
55 schedule: "@every 5s",
56 missedRun: nil,
57 nextRun: pointer.StringPtr("2021-04-28T06:49:40.000Z"),
58 err: nil,
59 },
60 {
61 now: "2021-04-28T06:49:35.079Z",
62 lastScheduleTime: zeroTime,
63 creationTimeStamp: "2021-04-28T06:49:35.000Z",
64 schedule: "@every 5s",
65 missedRun: nil,
66 nextRun: pointer.StringPtr("2021-04-28T06:49:40.000Z"),
67 err: nil,
68 },
69 {
70 now: "2021-04-28T06:49:38.079Z",
71 lastScheduleTime: zeroTime,
72 creationTimeStamp: "2021-04-28T06:49:35.000Z",
73 schedule: "@every 5s",
74 missedRun: nil,
75 nextRun: pointer.StringPtr("2021-04-28T06:49:40.000Z"),
76 err: nil,
77 },
78 {
79 now: "2021-04-28T06:49:40.079Z",
80 lastScheduleTime: zeroTime,
81 creationTimeStamp: "2021-04-28T06:49:35.000Z",
82 schedule: "@every 5s",
83 missedRun: pointer.StringPtr("2021-04-28T06:49:40.000Z"),
84 nextRun: pointer.StringPtr("2021-04-28T06:49:45.000Z"),
85 err: nil,
86 },
87 }
88 for _, t := range testCases {
89 now, err := time.Parse(time.RFC3339, t.now)
90 g.Expect(err).To(BeNil())
91 lastScheduleTime, err := time.Parse(time.RFC3339, t.lastScheduleTime)
92 g.Expect(err).To(BeNil())
93 createTimeStamp, err := time.Parse(time.RFC3339, t.creationTimeStamp)
94 g.Expect(err).To(BeNil())
95
96 schedule := v1alpha1.Schedule{
97 ObjectMeta: metav1.ObjectMeta{
98 CreationTimestamp: metav1.Time{
99 Time: createTimeStamp,
100 },
101 },
102 Spec: v1alpha1.ScheduleSpec{
103 Schedule: t.schedule,
104 },
105 Status: v1alpha1.ScheduleStatus{
106 LastScheduleTime: metav1.Time{
107 Time: lastScheduleTime,
108 },
109 },
110 }
111 missedRun, nextRun, err := getRecentUnmetScheduleTime(&schedule, now)
112
113 expectedMissedRun := BeNil()
114 expectedNextRun := BeNil()
115 expectedErr := BeNil()
116 if t.missedRun != nil {
117 missedRun, err := time.Parse(time.RFC3339, *t.missedRun)
118 g.Expect(err).To(BeNil())
119 expectedMissedRun = Equal(&missedRun)
120 }
121 if t.nextRun != nil {
122 nextRun, err := time.Parse(time.RFC3339, *t.nextRun)
123 g.Expect(err).To(BeNil())
124 expectedNextRun = Equal(&nextRun)
125 }
126 if t.err != nil {
127 expectedErr = Equal(t.err)
128 }
129
130 g.Expect(err).To(expectedErr)
131 g.Expect(missedRun).To(expectedMissedRun)
132 g.Expect(nextRun).To(expectedNextRun)
133 }
134 }
135