Skip to content

Commit a72836c

Browse files
committed
update error status code and add test cases
1 parent 015dede commit a72836c

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

pkg/gce-pd-csi-driver/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
11601160

11611161
volumeIsMultiZone := isMultiZoneVolKey(volKey)
11621162
if gceCS.multiZoneVolumeHandleConfig.Enable && volumeIsMultiZone {
1163-
return nil, fmt.Errorf("Snapshots are not supported with the `multi-zone` PV volumeHandle feature.")
1163+
return nil, status.Errorf(codes.InvalidArgument, "Snapshots are not supported with the multi-zone PV volumeHandle feature")
11641164
}
11651165

11661166
if acquired := gceCS.volumeLocks.TryAcquire(volumeID); !acquired {
@@ -1521,7 +1521,7 @@ func (gceCS *GCEControllerServer) ControllerExpandVolume(ctx context.Context, re
15211521

15221522
volumeIsMultiZone := isMultiZoneVolKey(volKey)
15231523
if gceCS.multiZoneVolumeHandleConfig.Enable && volumeIsMultiZone {
1524-
return nil, fmt.Errorf("Resize operation is not supported with the `multi-zone` PVC volumeHandle feature. Please re-create the disk from source if you want a larger size.")
1524+
return nil, status.Errorf(codes.InvalidArgument, "ControllerExpandVolume is not supported with the multi-zone PVC volumeHandle feature. Please re-create the volume %v from source if you want a larger size", volumeID)
15251525
}
15261526

15271527
sourceDisk, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gce.GCEAPIVersionV1)

pkg/gce-pd-csi-driver/controller_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,77 @@ func TestCreateSnapshotArguments(t *testing.T) {
255255
}
256256
}
257257

258+
func TestUnsupporteddMultiZoneCreateSnapshot(t *testing.T) {
259+
testCase := struct {
260+
name string
261+
req *csi.CreateSnapshotRequest
262+
expErrCode codes.Code
263+
}{
264+
name: "failed create snapshot for multi-zone PV", // Example values
265+
req: &csi.CreateSnapshotRequest{
266+
Name: name,
267+
SourceVolumeId: multiZoneVolumeID,
268+
},
269+
expErrCode: codes.InvalidArgument,
270+
}
271+
272+
t.Logf("test case: %s", testCase.name)
273+
274+
gceDriver := initGCEDriver(t, nil)
275+
gceDriver.cs.multiZoneVolumeHandleConfig = MultiZoneVolumeHandleConfig{
276+
Enable: true,
277+
}
278+
279+
// Start Test
280+
_, err := gceDriver.cs.CreateSnapshot(context.Background(), testCase.req)
281+
if err != nil {
282+
serverError, ok := status.FromError(err)
283+
if !ok {
284+
t.Fatalf("Could not get error status code from err: %v", serverError)
285+
}
286+
if serverError.Code() != testCase.expErrCode {
287+
t.Fatalf("Expected error code: %v, got: %v. err : %v", testCase.expErrCode, serverError.Code(), err)
288+
}
289+
} else {
290+
t.Fatalf("Expected error: %v, got no error", testCase.expErrCode)
291+
}
292+
}
293+
294+
func TestUnsupportedMultiZoneControllerExpandVolume(t *testing.T) {
295+
testCase := struct {
296+
name string
297+
req *csi.ControllerExpandVolumeRequest
298+
expErrCode codes.Code
299+
}{
300+
name: "failed create snapshot for multi-zone PV", // Example values
301+
req: &csi.ControllerExpandVolumeRequest{
302+
VolumeId: multiZoneVolumeID,
303+
},
304+
expErrCode: codes.InvalidArgument,
305+
}
306+
307+
t.Logf("test case: %s", testCase.name)
308+
309+
gceDriver := initGCEDriver(t, nil)
310+
gceDriver.cs.multiZoneVolumeHandleConfig = MultiZoneVolumeHandleConfig{
311+
Enable: true,
312+
}
313+
314+
// Start Test
315+
_, err := gceDriver.cs.ControllerExpandVolume(context.Background(), testCase.req)
316+
if err != nil {
317+
serverError, ok := status.FromError(err)
318+
if !ok {
319+
t.Fatalf("Could not get error status code from err: %v", serverError)
320+
}
321+
if serverError.Code() != testCase.expErrCode {
322+
t.Fatalf("Expected error code: %v, got: %v. err : %v", testCase.expErrCode, serverError.Code(), err)
323+
}
324+
} else {
325+
t.Fatalf("Expected error: %v, got no error", testCase.expErrCode)
326+
}
327+
}
328+
258329
func TestDeleteSnapshot(t *testing.T) {
259330
testCases := []struct {
260331
name string

0 commit comments

Comments
 (0)