@@ -26,6 +26,7 @@ import (
26
26
"github.com/agiledragon/gomonkey/v2"
27
27
. "github.com/onsi/ginkgo/v2"
28
28
. "github.com/onsi/gomega"
29
+ "github.com/stretchr/testify/assert"
29
30
cnstypes "github.com/vmware/govmomi/cns/types"
30
31
"github.com/vmware/govmomi/object"
31
32
vim25types "github.com/vmware/govmomi/vim25/types"
@@ -517,6 +518,7 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
517
518
})
518
519
519
520
patches .ApplyFunc (constructCreateSpecForInstance , func (
521
+ ctx context.Context ,
520
522
r * ReconcileCnsRegisterVolume ,
521
523
instance * cnsregistervolumev1alpha1.CnsRegisterVolume ,
522
524
host string ,
@@ -568,7 +570,7 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
568
570
569
571
// Test getPersistentVolumeClaimSpec function directly
570
572
pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
571
- "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
573
+ "test-storage-class" , corev1 .ReadWriteOnce , corev1 . PersistentVolumeFilesystem , "test-pv" , nil , instance )
572
574
573
575
Expect (err ).NotTo (HaveOccurred ())
574
576
Expect (pvcSpec ).NotTo (BeNil ())
@@ -594,7 +596,7 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
594
596
595
597
// Test getPersistentVolumeClaimSpec function directly
596
598
pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
597
- "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
599
+ "test-storage-class" , corev1 .ReadWriteOnce , corev1 . PersistentVolumeFilesystem , "test-pv" , nil , instance )
598
600
599
601
Expect (err ).NotTo (HaveOccurred ())
600
602
Expect (pvcSpec ).NotTo (BeNil ())
@@ -622,7 +624,7 @@ var _ = Describe("Reconcile Accessibility Logic", func() {
622
624
623
625
// Test getPersistentVolumeClaimSpec function directly
624
626
pvcSpec , err := getPersistentVolumeClaimSpec (ctx , "test-pvc" , "test-ns" , 1024 ,
625
- "test-storage-class" , corev1 .ReadWriteOnce , "test-pv" , nil , instance )
627
+ "test-storage-class" , corev1 .ReadWriteOnce , corev1 . PersistentVolumeFilesystem , "test-pv" , nil , instance )
626
628
627
629
Expect (err ).NotTo (HaveOccurred ())
628
630
Expect (pvcSpec ).NotTo (BeNil ())
@@ -1089,3 +1091,131 @@ func TestCnsRegisterVolumeController(t *testing.T) {
1089
1091
RegisterFailHandler (Fail )
1090
1092
RunSpecs (t , "CnsRegisterVolumeController Suite" )
1091
1093
}
1094
+
1095
+ func TestValidateCnsRegisterVolumeSpecWithDiskUrlPath (t * testing.T ) {
1096
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
1097
+ ObjectMeta : metav1.ObjectMeta {
1098
+ Name : "register-vol" ,
1099
+ Namespace : "test-ns" ,
1100
+ },
1101
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
1102
+ PvcName : "pvc-1" ,
1103
+ DiskURLPath : "som-url" ,
1104
+ AccessMode : corev1 .ReadWriteMany ,
1105
+ VolumeMode : corev1 .PersistentVolumeFilesystem ,
1106
+ },
1107
+ }
1108
+
1109
+ isSharedDiskEnabled = true
1110
+ err := validateCnsRegisterVolumeSpec (context .TODO (), instance )
1111
+ assert .Error (t , err )
1112
+ assert .Equal (t , "DiskURLPath cannot be used with accessMode: ReadWriteMany and volumeMode: Filesystem" , err .Error ())
1113
+ }
1114
+
1115
+ func TestValidateCnsRegisterVolumeSpecWithVolumeIdAndNoAccessMode (t * testing.T ) {
1116
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
1117
+ ObjectMeta : metav1.ObjectMeta {
1118
+ Name : "register-vol" ,
1119
+ Namespace : "test-ns" ,
1120
+ },
1121
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
1122
+ PvcName : "pvc-1" ,
1123
+ VolumeID : "123456" ,
1124
+ VolumeMode : corev1 .PersistentVolumeFilesystem ,
1125
+ },
1126
+ }
1127
+
1128
+ isSharedDiskEnabled = true
1129
+ err := validateCnsRegisterVolumeSpec (context .TODO (), instance )
1130
+ assert .Error (t , err )
1131
+ assert .Equal (t , "AccessMode cannot be empty when volumeID is specified" , err .Error ())
1132
+ }
1133
+
1134
+ func TestValidateCnsRegisterVolumeSpecWithVolumeIdAndAccessMode (t * testing.T ) {
1135
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
1136
+ ObjectMeta : metav1.ObjectMeta {
1137
+ Name : "register-vol" ,
1138
+ Namespace : "test-ns" ,
1139
+ },
1140
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
1141
+ PvcName : "pvc-1" ,
1142
+ VolumeID : "123456" ,
1143
+ AccessMode : corev1 .ReadWriteMany ,
1144
+ VolumeMode : corev1 .PersistentVolumeFilesystem ,
1145
+ },
1146
+ }
1147
+
1148
+ isSharedDiskEnabled = true
1149
+ commonco .ContainerOrchestratorUtility = & mockCOCommon {}
1150
+ err := validateCnsRegisterVolumeSpec (context .TODO (), instance )
1151
+ assert .NoError (t , err )
1152
+ }
1153
+
1154
+ func TestIsBlockVolumeRegisterRequestWithSharedBlockVolume (t * testing.T ) {
1155
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
1156
+ ObjectMeta : metav1.ObjectMeta {
1157
+ Name : "register-vol" ,
1158
+ Namespace : "test-ns" ,
1159
+ },
1160
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
1161
+ PvcName : "pvc-1" ,
1162
+ VolumeID : "123456" ,
1163
+ VolumeMode : corev1 .PersistentVolumeBlock ,
1164
+ AccessMode : corev1 .ReadWriteMany ,
1165
+ },
1166
+ }
1167
+
1168
+ isSharedDiskEnabled = true
1169
+ isBlockVolume := isBlockVolumeRegisterRequest (t .Context (), instance )
1170
+ assert .Equal (t , true , isBlockVolume )
1171
+ }
1172
+
1173
+ func TestIsBlockVolumeRegisterRequestWithFileVolume (t * testing.T ) {
1174
+ instance := & cnsregistervolumev1alpha1.CnsRegisterVolume {
1175
+ ObjectMeta : metav1.ObjectMeta {
1176
+ Name : "register-vol" ,
1177
+ Namespace : "test-ns" ,
1178
+ },
1179
+ Spec : cnsregistervolumev1alpha1.CnsRegisterVolumeSpec {
1180
+ PvcName : "pvc-1" ,
1181
+ VolumeID : "123456" ,
1182
+ AccessMode : corev1 .ReadWriteMany ,
1183
+ VolumeMode : corev1 .PersistentVolumeFilesystem ,
1184
+ },
1185
+ }
1186
+
1187
+ isSharedDiskEnabled = true
1188
+ isBlockVolume := isBlockVolumeRegisterRequest (t .Context (), instance )
1189
+ assert .Equal (t , false , isBlockVolume )
1190
+ }
1191
+
1192
+ func TestGetPersistentVolumeSpecWhenVolumeModeIsEmpty (t * testing.T ) {
1193
+ var (
1194
+ volumeName = "vol-1"
1195
+ volumeID = "123456"
1196
+ capacity = 256
1197
+ accessMode = corev1 .ReadWriteMany
1198
+ scName = "testsc"
1199
+ )
1200
+
1201
+ isSharedDiskEnabled = true
1202
+ commonco .ContainerOrchestratorUtility = & mockCOCommon {}
1203
+ pv := getPersistentVolumeSpec (volumeName , volumeID , int64 (capacity ), accessMode , "" , scName , nil )
1204
+ assert .Equal (t , corev1 .PersistentVolumeFilesystem , * pv .Spec .VolumeMode )
1205
+ }
1206
+
1207
+ func TestGetPersistentVolumeSpecWithVolumeMode (t * testing.T ) {
1208
+ var (
1209
+ volumeName = "vol-1"
1210
+ volumeID = "123456"
1211
+ capacity = 256
1212
+ accessMode = corev1 .ReadWriteMany
1213
+ scName = "testsc"
1214
+ volumeMode = corev1 .PersistentVolumeBlock
1215
+ )
1216
+
1217
+ isSharedDiskEnabled = true
1218
+ pv := getPersistentVolumeSpec (volumeName , volumeID ,
1219
+ int64 (capacity ), accessMode , volumeMode , scName , nil )
1220
+ assert .Equal (t , volumeMode , * pv .Spec .VolumeMode )
1221
+ }
0 commit comments