@@ -85,7 +85,6 @@ func clusterResourcePlacementForTest() *fleetv1beta1.ClusterResourcePlacement {
8585 Name : testName ,
8686 },
8787 Spec : fleetv1beta1.ClusterResourcePlacementSpec {
88- Policy : placementPolicyForTest (),
8988 ResourceSelectors : []fleetv1beta1.ClusterResourceSelector {
9089 {
9190 Group : corev1 .GroupName ,
@@ -101,9 +100,9 @@ func clusterResourcePlacementForTest() *fleetv1beta1.ClusterResourcePlacement {
101100}
102101
103102func TestGetOrCreateClusterSchedulingPolicySnapshot (t * testing.T ) {
104- wantPolicy := placementPolicyForTest ()
105- wantPolicy .NumberOfClusters = nil
106- jsonBytes , err := json .Marshal (wantPolicy )
103+ testPolicy := placementPolicyForTest ()
104+ testPolicy .NumberOfClusters = nil
105+ jsonBytes , err := json .Marshal (testPolicy )
107106 if err != nil {
108107 t .Fatalf ("failed to create the policy hash: %v" , err )
109108 }
@@ -115,13 +114,15 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
115114 unspecifiedPolicyHash := []byte (fmt .Sprintf ("%x" , sha256 .Sum256 (jsonBytes )))
116115 tests := []struct {
117116 name string
117+ policy * fleetv1beta1.PlacementPolicy
118118 revisionHistoryLimit * int32
119119 policySnapshots []fleetv1beta1.ClusterSchedulingPolicySnapshot
120120 wantPolicySnapshots []fleetv1beta1.ClusterSchedulingPolicySnapshot
121121 wantLatestSnapshotIndex int // index of the wantPolicySnapshots array
122122 }{
123123 {
124- name : "new clusterResourcePolicy and no existing policy snapshots owned by my-crp" ,
124+ name : "new clusterResourcePolicy and no existing policy snapshots owned by my-crp" ,
125+ policy : placementPolicyForTest (),
125126 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
126127 {
127128 ObjectMeta : metav1.ObjectMeta {
@@ -168,15 +169,67 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
168169 },
169170 },
170171 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
171- Policy : wantPolicy ,
172+ Policy : testPolicy ,
172173 PolicyHash : policyHash ,
173174 },
174175 },
175176 },
176177 wantLatestSnapshotIndex : 1 ,
177178 },
179+ {
180+ name : "new clusterResourcePolicy (unspecified policy) and no existing policy snapshots owned by my-crp" ,
181+ policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
182+ {
183+ ObjectMeta : metav1.ObjectMeta {
184+ Name : "another-crp-1" ,
185+ Labels : map [string ]string {
186+ fleetv1beta1 .PolicyIndexLabel : "1" ,
187+ fleetv1beta1 .IsLatestSnapshotLabel : "true" ,
188+ fleetv1beta1 .CRPTrackingLabel : "another-crp" ,
189+ },
190+ },
191+ },
192+ },
193+ wantPolicySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
194+ {
195+ ObjectMeta : metav1.ObjectMeta {
196+ Name : "another-crp-1" ,
197+ Labels : map [string ]string {
198+ fleetv1beta1 .PolicyIndexLabel : "1" ,
199+ fleetv1beta1 .IsLatestSnapshotLabel : "true" ,
200+ fleetv1beta1 .CRPTrackingLabel : "another-crp" ,
201+ },
202+ },
203+ },
204+ // new policy snapshot owned by the my-crp
205+ {
206+ ObjectMeta : metav1.ObjectMeta {
207+ Name : fmt .Sprintf (fleetv1beta1 .PolicySnapshotNameFmt , testName , 0 ),
208+ Labels : map [string ]string {
209+ fleetv1beta1 .PolicyIndexLabel : "0" ,
210+ fleetv1beta1 .IsLatestSnapshotLabel : "true" ,
211+ fleetv1beta1 .CRPTrackingLabel : testName ,
212+ },
213+ OwnerReferences : []metav1.OwnerReference {
214+ {
215+ Name : testName ,
216+ BlockOwnerDeletion : pointer .Bool (true ),
217+ Controller : pointer .Bool (true ),
218+ APIVersion : fleetAPIVersion ,
219+ Kind : "ClusterResourcePlacement" ,
220+ },
221+ },
222+ },
223+ Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
224+ PolicyHash : unspecifiedPolicyHash ,
225+ },
226+ },
227+ },
228+ wantLatestSnapshotIndex : 1 ,
229+ },
178230 {
179231 name : "new clusterResourcePolicy with invalidRevisionLimit and no existing policy snapshots owned by my-crp" ,
232+ policy : placementPolicyForTest (),
180233 revisionHistoryLimit : & invalidRevisionLimit ,
181234 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
182235 {
@@ -224,7 +277,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
224277 },
225278 },
226279 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
227- Policy : wantPolicy ,
280+ Policy : testPolicy ,
228281 PolicyHash : policyHash ,
229282 },
230283 },
@@ -233,6 +286,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
233286 },
234287 {
235288 name : "crp policy has no change" ,
289+ policy : placementPolicyForTest (),
236290 revisionHistoryLimit : & singleRevisionLimit ,
237291 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
238292 {
@@ -257,7 +311,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
257311 },
258312 },
259313 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
260- Policy : wantPolicy ,
314+ Policy : testPolicy ,
261315 PolicyHash : policyHash ,
262316 },
263317 },
@@ -285,7 +339,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
285339 },
286340 },
287341 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
288- Policy : wantPolicy ,
342+ Policy : testPolicy ,
289343 PolicyHash : policyHash ,
290344 },
291345 },
@@ -296,6 +350,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
296350 name : "crp policy has changed and there is no active snapshot" ,
297351 // It happens when last reconcile loop fails after setting the latest label to false and
298352 // before creating a new policy snapshot.
353+ policy : placementPolicyForTest (),
299354 revisionHistoryLimit : & multipleRevisionLimit ,
300355 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
301356 {
@@ -391,7 +446,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
391446 },
392447 },
393448 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
394- Policy : wantPolicy ,
449+ Policy : testPolicy ,
395450 PolicyHash : policyHash ,
396451 },
397452 },
@@ -400,6 +455,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
400455 },
401456 {
402457 name : "crp policy has changed and there is an active snapshot" ,
458+ policy : placementPolicyForTest (),
403459 revisionHistoryLimit : & singleRevisionLimit ,
404460 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
405461 {
@@ -449,15 +505,16 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
449505 },
450506 },
451507 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
452- Policy : wantPolicy ,
508+ Policy : testPolicy ,
453509 PolicyHash : policyHash ,
454510 },
455511 },
456512 },
457513 wantLatestSnapshotIndex : 0 ,
458514 },
459515 {
460- name : "crp policy has been changed and reverted back and there is no active snapshot" ,
516+ name : "crp policy has been changed and reverted back and there is no active snapshot" ,
517+ policy : placementPolicyForTest (),
461518 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
462519 {
463520 ObjectMeta : metav1.ObjectMeta {
@@ -503,7 +560,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
503560 },
504561 },
505562 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
506- Policy : wantPolicy ,
563+ Policy : testPolicy ,
507564 PolicyHash : policyHash ,
508565 },
509566 },
@@ -554,7 +611,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
554611 },
555612 },
556613 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
557- Policy : wantPolicy ,
614+ Policy : testPolicy ,
558615 PolicyHash : policyHash ,
559616 },
560617 },
@@ -564,6 +621,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
564621 {
565622 name : "crp policy has not been changed and only the numberOfCluster is changed" ,
566623 // cause no new policy snapshot is created, it does not trigger the history limit check.
624+ policy : placementPolicyForTest (),
567625 revisionHistoryLimit : & singleRevisionLimit ,
568626 policySnapshots : []fleetv1beta1.ClusterSchedulingPolicySnapshot {
569627 {
@@ -611,7 +669,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
611669 },
612670 },
613671 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
614- Policy : wantPolicy ,
672+ Policy : testPolicy ,
615673 PolicyHash : policyHash ,
616674 },
617675 },
@@ -662,7 +720,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
662720 },
663721 },
664722 Spec : fleetv1beta1.SchedulingPolicySnapshotSpec {
665- Policy : wantPolicy ,
723+ Policy : testPolicy ,
666724 PolicyHash : policyHash ,
667725 },
668726 },
@@ -674,6 +732,7 @@ func TestGetOrCreateClusterSchedulingPolicySnapshot(t *testing.T) {
674732 t .Run (tc .name , func (t * testing.T ) {
675733 ctx := context .Background ()
676734 crp := clusterResourcePlacementForTest ()
735+ crp .Spec .Policy = tc .policy
677736 crp .Spec .RevisionHistoryLimit = tc .revisionHistoryLimit
678737 objects := []client.Object {crp }
679738 for i := range tc .policySnapshots {
0 commit comments