@@ -7,7 +7,11 @@ import (
77 . "github.com/onsi/gomega"
88 crdv1beta1 "github.com/questdb/questdb-operator/api/v1beta1"
99 testutils "github.com/questdb/questdb-operator/tests/utils"
10+ "github.com/thejerf/abtime"
1011 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+ "k8s.io/client-go/kubernetes/scheme"
13+ "k8s.io/client-go/tools/record"
14+ ctrl "sigs.k8s.io/controller-runtime"
1115 "sigs.k8s.io/controller-runtime/pkg/client"
1216)
1317
@@ -16,75 +20,121 @@ var _ = Describe("QuestDBSnapshotSchedule Controller", func() {
1620 q * crdv1beta1.QuestDB
1721 sched * crdv1beta1.QuestDBSnapshotSchedule
1822
23+ timeSource = abtime .NewManual ()
24+
1925 timeout = time .Second * 2
2026 //consistencyTimeout = time.Millisecond * 600
2127 interval = time .Millisecond * 100
22- )
23-
24- BeforeEach (func () {
25-
26- q = testutils .BuildAndCreateMockQuestDB (ctx , k8sClient )
2728
28- sched = & crdv1beta1.QuestDBSnapshotSchedule {
29- ObjectMeta : metav1.ObjectMeta {
30- Name : q .Name ,
31- Namespace : q .Namespace ,
32- },
33- Spec : crdv1beta1.QuestDBSnapshotScheduleSpec {
34- Snapshot : crdv1beta1.QuestDBSnapshotSpec {
35- QuestDBName : q .Name ,
36- VolumeSnapshotClassName : "csi-hostpath-snapclass" ,
37- },
38- Schedule : "*/1 * * * *" ,
39- },
40- }
41- })
29+ r * QuestDBSnapshotScheduleReconciler
30+ )
4231
4332 Context ("golden path case" , Ordered , func () {
4433 var (
45- origSched crdv1beta1.QuestDBSnapshotSchedule
46-
4734 snapList = & crdv1beta1.QuestDBSnapshotList {}
4835 )
4936
5037 BeforeAll (func () {
51- By ("Creating the QuestDBSnapshotSchedule" )
38+ r = & QuestDBSnapshotScheduleReconciler {
39+ Client : k8sClient ,
40+ Scheme : scheme .Scheme ,
41+ Recorder : record .NewFakeRecorder (100 ),
42+ TimeSource : timeSource ,
43+ }
44+
45+ By ("Creating a QuestDB" )
46+ q = testutils .BuildAndCreateMockQuestDB (ctx , k8sClient )
47+
48+ By ("Creating a QuestDBSnapshotSchedule" )
49+ sched = & crdv1beta1.QuestDBSnapshotSchedule {
50+ ObjectMeta : metav1.ObjectMeta {
51+ Name : q .Name ,
52+ Namespace : q .Namespace ,
53+ },
54+ Spec : crdv1beta1.QuestDBSnapshotScheduleSpec {
55+ Snapshot : crdv1beta1.QuestDBSnapshotSpec {
56+ QuestDBName : q .Name ,
57+ VolumeSnapshotClassName : "csi-hostpath-snapclass" ,
58+ },
59+ Schedule : "*/1 * * * *" ,
60+ },
61+ }
5262 Expect (k8sClient .Create (ctx , sched )).To (Succeed ())
53- origSched = * sched
63+
64+ By ("Reconciling the QuestDBSnapshotSchedule" )
65+ _ , err := r .Reconcile (ctx , ctrl.Request {
66+ NamespacedName : client .ObjectKeyFromObject (sched ),
67+ })
68+ Expect (err ).ToNot (HaveOccurred ())
5469 })
5570
5671 It ("should create a snapshot if the cron schedule has triggered" , func () {
5772
58- By ("Bumping the clock 1 minute" )
73+ By ("Bumping the clock more than 1 minute" )
5974 timeSource .Advance (time .Minute + time .Second )
6075
76+ By ("Forcing a reconcile" )
77+ _ , err := r .Reconcile (ctx , ctrl.Request {
78+ NamespacedName : client .ObjectKeyFromObject (sched ),
79+ })
80+ Expect (err ).ToNot (HaveOccurred ())
81+
6182 By ("Checking that a snapshot has been created" )
62- Eventually (func (g Gomega ) {
63- g .Expect (k8sClient .List (ctx , snapList , client .InNamespace (origSched .Namespace ))).Should (Succeed ())
64- g .Expect (snapList .Items ).To (HaveLen (1 ))
65- g .Expect (snapList .Items [0 ].OwnerReferences ).To (HaveLen (1 ))
66- g .Expect (snapList .Items [0 ].OwnerReferences [0 ].Name ).To (Equal (origSched .Name ))
67- }, timeout , interval ).Should (Succeed ())
83+ Expect (k8sClient .List (ctx , snapList , client .InNamespace (sched .Namespace ))).Should (Succeed ())
84+ Expect (snapList .Items ).To (HaveLen (1 ))
85+ Expect (snapList .Items [0 ].OwnerReferences ).To (HaveLen (1 ))
86+ Expect (snapList .Items [0 ].OwnerReferences [0 ].Name ).To (Equal (sched .Name ))
6887 })
6988
7089 It ("should report the phase of the latest snapshot" , func () {
7190 By ("Getting the latest snapshot" )
7291 snapList := & crdv1beta1.QuestDBSnapshotList {}
73- Eventually (func (g Gomega ) {
74- g .Expect (k8sClient .List (ctx , snapList , client .InNamespace (origSched .Namespace ))).Should (Succeed ())
75- g .Expect (snapList .Items ).To (HaveLen (1 ))
76- }, timeout , interval ).Should (Succeed ())
92+ Expect (k8sClient .List (ctx , snapList , client .InNamespace (sched .Namespace ))).Should (Succeed ())
93+ Expect (snapList .Items ).To (HaveLen (1 ))
7794
7895 latestSnap := & snapList .Items [0 ]
7996
8097 By ("Setting the phase to Succeeded" )
81- latestSnap .Status .Phase = crdv1beta1 .SnapshotSucceeded
82- Expect (k8sClient .Status ().Update (ctx , latestSnap )).To (Succeed ())
83-
8498 Eventually (func (g Gomega ) {
85- g .Expect (k8sClient .Get (ctx , client.ObjectKey {Name : origSched .Name , Namespace : origSched .Namespace }, & origSched )).To (Succeed ())
86- g .Expect (origSched .Status .SnapshotPhase ).To (Equal (crdv1beta1 .SnapshotSucceeded ))
99+ k8sClient .Get (ctx , client .ObjectKeyFromObject (latestSnap ), latestSnap )
100+ latestSnap .Status .Phase = crdv1beta1 .SnapshotSucceeded
101+ g .Expect (k8sClient .Status ().Update (ctx , latestSnap )).To (Succeed ())
87102 }, timeout , interval ).Should (Succeed ())
103+
104+ By ("Forcing a reconcile" )
105+ _ , err := r .Reconcile (ctx , ctrl.Request {
106+ NamespacedName : client .ObjectKeyFromObject (sched ),
107+ })
108+ Expect (err ).ToNot (HaveOccurred ())
109+
110+ By ("Checking that the status has been updated" )
111+ Expect (k8sClient .Get (ctx , client .ObjectKeyFromObject (sched ), sched )).To (Succeed ())
112+ Expect (sched .Status .SnapshotPhase ).To (Equal (crdv1beta1 .SnapshotSucceeded ))
113+ })
114+
115+ It ("should take a second snapshot if the cron schedule has triggered" , func () {
116+ By ("Bumping the clock more than 1 minute" )
117+ timeSource .Advance (time .Minute + time .Second )
118+
119+ By ("Forcing a reconcile" )
120+ _ , err := r .Reconcile (ctx , ctrl.Request {
121+ NamespacedName : client .ObjectKeyFromObject (sched ),
122+ })
123+ Expect (err ).ToNot (HaveOccurred ())
124+
125+ By ("Checking that a snapshot has been created" )
126+ snapList := & crdv1beta1.QuestDBSnapshotList {}
127+ Expect (k8sClient .List (ctx , snapList , client .InNamespace (sched .Namespace ))).Should (Succeed ())
128+ Expect (snapList .Items ).To (HaveLen (2 ))
129+ })
130+
131+ It ("should requeue the request to the time of the next trigger" , func () {
132+ By ("Forcing a reconcile" )
133+ result , err := r .Reconcile (ctx , ctrl.Request {
134+ NamespacedName : client .ObjectKeyFromObject (sched ),
135+ })
136+ Expect (err ).ToNot (HaveOccurred ())
137+ Expect (result .RequeueAfter ).ToNot (BeZero ())
88138 })
89139
90140 })
0 commit comments