@@ -27,14 +27,21 @@ import (
2727 "k8s.io/apimachinery/pkg/types"
2828
2929 fleetv1beta1 "github.com/kubefleet-dev/kubefleet/apis/placement/v1beta1"
30+ "github.com/kubefleet-dev/kubefleet/pkg/utils/controller"
3031)
3132
3233const (
3334 testCRPName = "my-crp"
35+ testRPName = "my-rp"
3436 testSnapshotName = "my-snapshot"
37+ testNamespace = "test-namespace"
38+
39+ eventuallyTimeout = time .Second * 10
40+ consistentlyDuration = time .Second * 10
41+ interval = time .Millisecond * 250
3542)
3643
37- func policySnapshot () * fleetv1beta1.ClusterSchedulingPolicySnapshot {
44+ func clusterSchedulingPolicySnapshotForTest () * fleetv1beta1.ClusterSchedulingPolicySnapshot {
3845 return & fleetv1beta1.ClusterSchedulingPolicySnapshot {
3946 ObjectMeta : metav1.ObjectMeta {
4047 Name : testSnapshotName ,
@@ -50,28 +57,39 @@ func policySnapshot() *fleetv1beta1.ClusterSchedulingPolicySnapshot {
5057 }
5158}
5259
53- var _ = Describe ("Test clusterSchedulingPolicySnapshot Controller" , func () {
54- const (
55- eventuallyTimeout = time .Second * 10
56- consistentlyDuration = time .Second * 10
57- interval = time .Millisecond * 250
58- )
60+ func schedulingPolicySnapshotForTest () * fleetv1beta1.SchedulingPolicySnapshot {
61+ return & fleetv1beta1.SchedulingPolicySnapshot {
62+ ObjectMeta : metav1.ObjectMeta {
63+ Name : testSnapshotName ,
64+ Namespace : testNamespace ,
65+ Labels : map [string ]string {
66+ fleetv1beta1 .PolicyIndexLabel : "1" ,
67+ fleetv1beta1 .IsLatestSnapshotLabel : "true" ,
68+ fleetv1beta1 .PlacementTrackingLabel : testRPName ,
69+ },
70+ },
71+ Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
72+ PolicyHash : []byte ("hash" ),
73+ },
74+ }
75+ }
5976
77+ var _ = Describe ("Test clusterSchedulingPolicySnapshot Controller" , func () {
6078 var (
6179 createdSnapshot = & fleetv1beta1.ClusterSchedulingPolicySnapshot {}
6280 )
6381
6482 BeforeEach (func () {
6583 fakePlacementController .ResetQueue ()
6684 By ("By creating a new clusterSchedulingPolicySnapshot" )
67- snapshot := policySnapshot ()
85+ snapshot := clusterSchedulingPolicySnapshotForTest ()
6886 Expect (k8sClient .Create (ctx , snapshot )).Should (Succeed ())
6987 })
7088
7189 Context ("When creating new clusterSchedulingPolicySnapshot" , func () {
7290 AfterEach (func () {
7391 By ("By deleting snapshot" )
74- createdSnapshot = policySnapshot ()
92+ createdSnapshot = clusterSchedulingPolicySnapshotForTest ()
7593 Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
7694
7795 By ("By checking snapshot" )
@@ -97,7 +115,7 @@ var _ = Describe("Test clusterSchedulingPolicySnapshot Controller", func() {
97115
98116 AfterEach (func () {
99117 By ("By deleting snapshot" )
100- createdSnapshot = policySnapshot ()
118+ createdSnapshot = clusterSchedulingPolicySnapshotForTest ()
101119 Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
102120
103121 By ("By checking snapshot" )
@@ -147,7 +165,7 @@ var _ = Describe("Test clusterSchedulingPolicySnapshot Controller", func() {
147165
148166 It ("Should ignore the event" , func () {
149167 By ("By deleting snapshot" )
150- createdSnapshot = policySnapshot ()
168+ createdSnapshot = clusterSchedulingPolicySnapshotForTest ()
151169 Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
152170
153171 By ("By checking snapshot" )
@@ -162,3 +180,111 @@ var _ = Describe("Test clusterSchedulingPolicySnapshot Controller", func() {
162180 })
163181 })
164182})
183+
184+ var _ = Describe ("Test schedulingPolicySnapshot Controller" , func () {
185+ var (
186+ createdSnapshot = & fleetv1beta1.SchedulingPolicySnapshot {}
187+ key = controller .GetObjectKeyFromNamespaceName (testNamespace , testRPName )
188+ )
189+
190+ BeforeEach (func () {
191+ fakePlacementController .ResetQueue ()
192+ By ("By creating a new schedulingPolicySnapshot" )
193+ snapshot := schedulingPolicySnapshotForTest ()
194+ Expect (k8sClient .Create (ctx , snapshot )).Should (Succeed ())
195+ })
196+
197+ Context ("When creating new schedulingPolicySnapshot" , func () {
198+ AfterEach (func () {
199+ By ("By deleting snapshot" )
200+ createdSnapshot = schedulingPolicySnapshotForTest ()
201+ Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
202+
203+ By ("By checking snapshot" )
204+ Eventually (func () bool {
205+ return errors .IsNotFound (k8sClient .Get (ctx , types.NamespacedName {Name : testSnapshotName , Namespace : testNamespace }, createdSnapshot ))
206+ }, eventuallyTimeout , interval ).Should (BeTrue (), "snapshot should be deleted" )
207+ })
208+
209+ It ("Should ignore the event" , func () {
210+ By ("By checking placement controller queue" )
211+ Consistently (func () bool {
212+ return fakePlacementController .Key () == ""
213+ }, consistentlyDuration , interval ).Should (BeTrue (), "controller should ignore the create event and not enqueue the request into the placementController queue" )
214+
215+ })
216+ })
217+
218+ Context ("When updating schedulingPolicySnapshot" , func () {
219+ BeforeEach (func () {
220+ By ("By resetting the placement queue" )
221+ fakePlacementController .ResetQueue ()
222+ })
223+
224+ AfterEach (func () {
225+ By ("By deleting snapshot" )
226+ createdSnapshot = schedulingPolicySnapshotForTest ()
227+ Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
228+
229+ By ("By checking snapshot" )
230+ Eventually (func () bool {
231+ return errors .IsNotFound (k8sClient .Get (ctx , types.NamespacedName {Name : testSnapshotName , Namespace : testNamespace }, createdSnapshot ))
232+ }, eventuallyTimeout , interval ).Should (BeTrue (), "snapshot should be deleted" )
233+ })
234+
235+ It ("Updating the spec and should enqueue the event" , func () {
236+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : testSnapshotName , Namespace : testNamespace }, createdSnapshot )).Should (Succeed ())
237+
238+ By ("By updating the schedulingPolicySnapshot spec" )
239+ createdSnapshot .Spec .PolicyHash = []byte ("modified-hash" )
240+ Expect (k8sClient .Update (ctx , createdSnapshot )).Should (Succeed ())
241+
242+ By ("By checking placement controller queue" )
243+ Eventually (func () bool {
244+ return fakePlacementController .Key () == key
245+ }, eventuallyTimeout , interval ).Should (BeTrue (), "placementController should receive the RP key" )
246+ })
247+
248+ It ("Updating the status and should enqueue the event" , func () {
249+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : testSnapshotName , Namespace : testNamespace }, createdSnapshot )).Should (Succeed ())
250+
251+ By ("By updating the schedulingPolicySnapshot status" )
252+ newCondition := metav1.Condition {
253+ Type : string (fleetv1beta1 .PolicySnapshotScheduled ),
254+ Status : metav1 .ConditionTrue ,
255+ Reason : "scheduled" ,
256+ ObservedGeneration : createdSnapshot .GetGeneration (),
257+ }
258+ meta .SetStatusCondition (& createdSnapshot .Status .Conditions , newCondition )
259+ Expect (k8sClient .Status ().Update (ctx , createdSnapshot )).Should (Succeed ())
260+
261+ By ("By checking placement controller queue" )
262+ Eventually (func () bool {
263+ return fakePlacementController .Key () == key
264+ }, eventuallyTimeout , interval ).Should (BeTrue (), "placementController should receive the RP key" )
265+ })
266+ })
267+
268+ Context ("When deleting schedulingPolicySnapshot" , func () {
269+ BeforeEach (func () {
270+ By ("By resetting the placement queue" )
271+ fakePlacementController .ResetQueue ()
272+ })
273+
274+ It ("Should ignore the event" , func () {
275+ By ("By deleting snapshot" )
276+ createdSnapshot = schedulingPolicySnapshotForTest ()
277+ Expect (k8sClient .Delete (ctx , createdSnapshot )).Should (Succeed ())
278+
279+ By ("By checking snapshot" )
280+ Eventually (func () bool {
281+ return errors .IsNotFound (k8sClient .Get (ctx , types.NamespacedName {Name : testSnapshotName , Namespace : testNamespace }, createdSnapshot ))
282+ }, eventuallyTimeout , interval ).Should (BeTrue (), "snapshot should be deleted" )
283+
284+ By ("By checking placement controller queue" )
285+ Consistently (func () bool {
286+ return fakePlacementController .Key () == ""
287+ }, consistentlyDuration , interval ).Should (BeTrue (), "controller should ignore the delete event and not enqueue the request into the placementController queue" )
288+ })
289+ })
290+ })
0 commit comments