@@ -461,6 +461,35 @@ func TestCreateVolume(t *testing.T) {
461
461
}
462
462
},
463
463
},
464
+ {
465
+ name : "fail locked volume request" ,
466
+ testFunc : func (t * testing.T ) {
467
+ req := & csi.CreateVolumeRequest {
468
+ Name : "random-vol-name" ,
469
+ CapacityRange : stdCapRange ,
470
+ VolumeCapabilities : stdVolCap ,
471
+ Parameters : nil ,
472
+ }
473
+
474
+ ctx := context .Background ()
475
+ mockCtl := gomock .NewController (t )
476
+ defer mockCtl .Finish ()
477
+
478
+ mockCloud := mocks .NewMockCloud (mockCtl )
479
+
480
+ powervsDriver := controllerService {
481
+ cloud : mockCloud ,
482
+ driverOptions : & Options {},
483
+ volumeLocks : util .NewVolumeLocks (),
484
+ }
485
+
486
+ powervsDriver .volumeLocks .TryAcquire (req .Name )
487
+ defer powervsDriver .volumeLocks .Release (req .Name )
488
+
489
+ _ , err := powervsDriver .CreateVolume (ctx , req )
490
+ checkExpectedErrorCode (t , err , codes .Aborted )
491
+ },
492
+ },
464
493
}
465
494
466
495
for _ , tc := range testCases {
@@ -575,6 +604,32 @@ func TestDeleteVolume(t *testing.T) {
575
604
}
576
605
},
577
606
},
607
+ {
608
+ name : "fail if volume is already locked" ,
609
+ testFunc : func (t * testing.T ) {
610
+ req := & csi.DeleteVolumeRequest {
611
+ VolumeId : "vol-test" ,
612
+ }
613
+
614
+ ctx := context .Background ()
615
+ mockCtl := gomock .NewController (t )
616
+ defer mockCtl .Finish ()
617
+
618
+ mockCloud := mocks .NewMockCloud (mockCtl )
619
+
620
+ powervsDriver := controllerService {
621
+ cloud : mockCloud ,
622
+ driverOptions : & Options {},
623
+ volumeLocks : util .NewVolumeLocks (),
624
+ }
625
+
626
+ powervsDriver .volumeLocks .TryAcquire (req .VolumeId )
627
+ defer powervsDriver .volumeLocks .Release (req .VolumeId )
628
+
629
+ _ , err := powervsDriver .DeleteVolume (ctx , req )
630
+ checkExpectedErrorCode (t , err , codes .Aborted )
631
+ },
632
+ },
578
633
}
579
634
580
635
for _ , tc := range testCases {
@@ -855,6 +910,34 @@ func TestControllerPublishVolume(t *testing.T) {
855
910
}
856
911
},
857
912
},
913
+ {
914
+ name : "fail if volume already locked" ,
915
+ testFunc : func (t * testing.T ) {
916
+ req := & csi.ControllerPublishVolumeRequest {
917
+ NodeId : expInstanceID ,
918
+ VolumeCapability : stdVolCap ,
919
+ VolumeId : volumeName ,
920
+ }
921
+
922
+ ctx := context .Background ()
923
+ mockCtl := gomock .NewController (t )
924
+ defer mockCtl .Finish ()
925
+
926
+ mockCloud := mocks .NewMockCloud (mockCtl )
927
+ powervsDriver := controllerService {
928
+ cloud : mockCloud ,
929
+ driverOptions : & Options {},
930
+ volumeLocks : util .NewVolumeLocks (),
931
+ }
932
+
933
+ powervsDriver .volumeLocks .TryAcquire (req .VolumeId )
934
+ defer powervsDriver .volumeLocks .Release (req .VolumeId )
935
+
936
+ _ , err := powervsDriver .ControllerPublishVolume (ctx , req )
937
+ checkExpectedErrorCode (t , err , codes .Aborted )
938
+
939
+ },
940
+ },
858
941
}
859
942
860
943
for _ , tc := range testCases {
@@ -1000,6 +1083,32 @@ func TestControllerUnpublishVolume(t *testing.T) {
1000
1083
}
1001
1084
},
1002
1085
},
1086
+ {
1087
+ name : "fail if volume already locked" ,
1088
+ testFunc : func (t * testing.T ) {
1089
+ req := & csi.ControllerUnpublishVolumeRequest {
1090
+ NodeId : expInstanceID ,
1091
+ VolumeId : "vol-test" ,
1092
+ }
1093
+
1094
+ ctx := context .Background ()
1095
+ mockCtl := gomock .NewController (t )
1096
+ defer mockCtl .Finish ()
1097
+
1098
+ mockCloud := mocks .NewMockCloud (mockCtl )
1099
+ powervsDriver := controllerService {
1100
+ cloud : mockCloud ,
1101
+ driverOptions : & Options {},
1102
+ volumeLocks : util .NewVolumeLocks (),
1103
+ }
1104
+
1105
+ powervsDriver .volumeLocks .TryAcquire (req .VolumeId )
1106
+ defer powervsDriver .volumeLocks .Release (req .VolumeId )
1107
+
1108
+ _ , err := powervsDriver .ControllerUnpublishVolume (ctx , req )
1109
+ checkExpectedErrorCode (t , err , codes .Aborted )
1110
+ },
1111
+ },
1003
1112
}
1004
1113
1005
1114
for _ , tc := range testCases {
@@ -1009,11 +1118,12 @@ func TestControllerUnpublishVolume(t *testing.T) {
1009
1118
1010
1119
func TestControllerExpandVolume (t * testing.T ) {
1011
1120
testCases := []struct {
1012
- name string
1013
- req * csi.ControllerExpandVolumeRequest
1014
- newSize int64
1015
- expResp * csi.ControllerExpandVolumeResponse
1016
- expError bool
1121
+ name string
1122
+ req * csi.ControllerExpandVolumeRequest
1123
+ newSize int64
1124
+ expResp * csi.ControllerExpandVolumeResponse
1125
+ expError bool
1126
+ volumeLock bool
1017
1127
}{
1018
1128
{
1019
1129
name : "success normal" ,
@@ -1043,6 +1153,16 @@ func TestControllerExpandVolume(t *testing.T) {
1043
1153
},
1044
1154
expError : true ,
1045
1155
},
1156
+ {
1157
+ name : "fail if volume already locked" ,
1158
+ req : & csi.ControllerExpandVolumeRequest {
1159
+ VolumeId : "vol-test" ,
1160
+ CapacityRange : & csi.CapacityRange {
1161
+ RequiredBytes : 5 * util .GiB ,
1162
+ },
1163
+ },
1164
+ volumeLock : true ,
1165
+ },
1046
1166
}
1047
1167
1048
1168
for _ , tc := range testCases {
@@ -1059,34 +1179,43 @@ func TestControllerExpandVolume(t *testing.T) {
1059
1179
}
1060
1180
1061
1181
mockCloud := mocks .NewMockCloud (mockCtl )
1062
- mockCloud .EXPECT ().ResizeDisk (gomock .Eq (tc .req .VolumeId ), gomock .Any ()).Return (retSizeGiB , nil ).AnyTimes ()
1063
-
1064
1182
powervsDriver := controllerService {
1065
1183
cloud : mockCloud ,
1066
1184
driverOptions : & Options {},
1067
1185
volumeLocks : util .NewVolumeLocks (),
1068
1186
}
1069
1187
1070
- resp , err := powervsDriver .ControllerExpandVolume (ctx , tc .req )
1071
- if err != nil {
1072
- srvErr , ok := status .FromError (err )
1073
- if ! ok {
1074
- t .Fatalf ("Could not get error status code from error: %v" , srvErr )
1075
- }
1076
- if ! tc .expError {
1077
- t .Fatalf ("Unexpected error: %v" , err )
1078
- }
1188
+ if tc .volumeLock {
1189
+ powervsDriver .volumeLocks .TryAcquire (tc .req .VolumeId )
1190
+ defer powervsDriver .volumeLocks .Release (tc .req .VolumeId )
1191
+
1192
+ _ , err := powervsDriver .ControllerExpandVolume (ctx , tc .req )
1193
+ checkExpectedErrorCode (t , err , codes .Aborted )
1194
+
1079
1195
} else {
1080
- if tc .expError {
1081
- t .Fatalf ("Expected error from ControllerExpandVolume, got nothing" )
1196
+ mockCloud .EXPECT ().ResizeDisk (gomock .Eq (tc .req .VolumeId ), gomock .Any ()).Return (retSizeGiB , nil ).AnyTimes ()
1197
+ resp , err := powervsDriver .ControllerExpandVolume (ctx , tc .req )
1198
+ if err != nil {
1199
+ srvErr , ok := status .FromError (err )
1200
+ if ! ok {
1201
+ t .Fatalf ("Could not get error status code from error: %v" , srvErr )
1202
+ }
1203
+ if ! tc .expError {
1204
+ t .Fatalf ("Unexpected error: %v" , err )
1205
+ }
1206
+ } else {
1207
+ if tc .expError {
1208
+ t .Fatalf ("Expected error from ControllerExpandVolume, got nothing" )
1209
+ }
1082
1210
}
1083
- }
1084
1211
1085
- sizeGiB := util .BytesToGiB (resp .GetCapacityBytes ())
1086
- expSizeGiB := util .BytesToGiB (tc .expResp .GetCapacityBytes ())
1087
- if sizeGiB != expSizeGiB {
1088
- t .Fatalf ("Expected size %d GiB, got %d GiB" , expSizeGiB , sizeGiB )
1212
+ sizeGiB := util .BytesToGiB (resp .GetCapacityBytes ())
1213
+ expSizeGiB := util .BytesToGiB (tc .expResp .GetCapacityBytes ())
1214
+ if sizeGiB != expSizeGiB {
1215
+ t .Fatalf ("Expected size %d GiB, got %d GiB" , expSizeGiB , sizeGiB )
1216
+ }
1089
1217
}
1218
+
1090
1219
})
1091
1220
}
1092
1221
}
0 commit comments