@@ -47,6 +47,7 @@ import (
47
47
commoncotypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/csi/service/common/commonco/types"
48
48
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/internalapis/cnsvolumeoperationrequest"
49
49
"sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer"
50
+ cnsoperatortypes "sigs.k8s.io/vsphere-csi-driver/v3/pkg/syncer/cnsoperator/types"
50
51
)
51
52
52
53
type mockVolumeManager struct {
@@ -450,10 +451,16 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
450
451
patches .ApplyFunc (common .QueryVolumeByID , func (ctx context.Context , vm cnsvolume.Manager ,
451
452
volumeID string , querySelection * cnstypes.CnsQuerySelection ) (* cnstypes.CnsVolume , error ) {
452
453
return & cnstypes.CnsVolume {
453
- VolumeId : cnstypes.CnsVolumeId {Id : volumeID },
454
- DatastoreUrl : "dummy-ds-url" ,
455
- StoragePolicyId : "dummy-storage-policy-id" ,
456
- }, nil
454
+ VolumeId : cnstypes.CnsVolumeId {Id : volumeID },
455
+ DatastoreUrl : "dummy-ds-url" ,
456
+ StoragePolicyId : "dummy-storage-policy-id" ,
457
+ BackingObjectDetails : & cnstypes.CnsBlockBackingDetails {
458
+ CnsBackingObjectDetails : cnstypes.CnsBackingObjectDetails {
459
+ CapacityInMb : 1024 ,
460
+ },
461
+ },
462
+ },
463
+ nil
457
464
})
458
465
459
466
patches .ApplyFunc (common .GetClusterComputeResourceMoIds , func (ctx context.Context ) ([]string , bool , error ) {
@@ -522,6 +529,88 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
522
529
Expect (err ).NotTo (HaveOccurred ())
523
530
Expect (result ).To (Equal (reconcile.Result {RequeueAfter : 0 }))
524
531
})
532
+
533
+ It ("should add vm name and storage policy reservation labels to PVC when both present on CR" , func () {
534
+ // Test the getPersistentVolumeClaimSpec function directly
535
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
536
+ ObjectMeta : metav1.ObjectMeta {
537
+ Name : "test-volume-with-labels" ,
538
+ Namespace : "test-ns" ,
539
+ Labels : map [string ]string {
540
+ cnsoperatortypes .LabelVirtualMachineName : "test-vm-name" ,
541
+ cnsoperatortypes .LabelStoragePolicyReservationName : "test-storage-policy-reservation-name" ,
542
+ },
543
+ },
544
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
545
+ PvcName : "test-pvc-with-labels" ,
546
+ VolumeID : "dummy-volume-id" ,
547
+ AccessMode : "ReadWriteOnce" ,
548
+ },
549
+ }
550
+
551
+ // Test getPersistentVolumeClaimSpec function directly
552
+ pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
553
+ "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
554
+
555
+ Expect (err ).NotTo (HaveOccurred ())
556
+ Expect (pvcSpec ).NotTo (BeNil ())
557
+ Expect (pvcSpec .Labels ).To (HaveKeyWithValue (cnsoperatortypes .LabelVirtualMachineName , "test-vm-name" ))
558
+ Expect (pvcSpec .Labels ).To (HaveKeyWithValue (cnsoperatortypes .LabelStoragePolicyReservationName ,
559
+ "test-storage-policy-reservation-name" ))
560
+ })
561
+
562
+ It ("should not add labels to PVC when not present on CR" , func () {
563
+ // Test the getPersistentVolumeClaimSpec function directly without labels
564
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
565
+ ObjectMeta : metav1.ObjectMeta {
566
+ Name : "test-volume-without-labels" ,
567
+ Namespace : "test-ns" ,
568
+ // No labels set
569
+ },
570
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
571
+ PvcName : "test-pvc-without-labels" ,
572
+ VolumeID : "dummy-volume-id" ,
573
+ AccessMode : "ReadWriteOnce" ,
574
+ },
575
+ }
576
+
577
+ // Test getPersistentVolumeClaimSpec function directly
578
+ pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
579
+ "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
580
+
581
+ Expect (err ).NotTo (HaveOccurred ())
582
+ Expect (pvcSpec ).NotTo (BeNil ())
583
+ Expect (pvcSpec .Labels ).NotTo (HaveKey (cnsoperatortypes .LabelVirtualMachineName ))
584
+ Expect (pvcSpec .Labels ).NotTo (HaveKey (cnsoperatortypes .LabelStoragePolicyReservationName ))
585
+ })
586
+
587
+ It ("should not add vm name label when only that label is present on CR" , func () {
588
+ // Test the getPersistentVolumeClaimSpec function directly with only vm name label
589
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
590
+ ObjectMeta : metav1.ObjectMeta {
591
+ Name : "test-volume-with-vm-label" ,
592
+ Namespace : "test-ns" ,
593
+ Labels : map [string ]string {
594
+ cnsoperatortypes .LabelVirtualMachineName : "test-vm-name" ,
595
+ // Missing storage policy reservation label
596
+ },
597
+ },
598
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
599
+ PvcName : "test-pvc-with-vm-label" ,
600
+ VolumeID : "dummy-volume-id" ,
601
+ AccessMode : "ReadWriteOnce" ,
602
+ },
603
+ }
604
+
605
+ // Test getPersistentVolumeClaimSpec function directly
606
+ pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
607
+ "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
608
+
609
+ Expect (err ).NotTo (HaveOccurred ())
610
+ Expect (pvcSpec ).NotTo (BeNil ())
611
+ Expect (pvcSpec .Labels ).NotTo (HaveKey (cnsoperatortypes .LabelVirtualMachineName ))
612
+ Expect (pvcSpec .Labels ).NotTo (HaveKey (cnsoperatortypes .LabelStoragePolicyReservationName ))
613
+ })
525
614
})
526
615
527
616
func TestCnsRegisterVolumeController (t * testing.T ) {
0 commit comments