Skip to content

Commit 4687409

Browse files
committed
Fix: Improve error handling for ResizeVolume method
1 parent 014c028 commit 4687409

File tree

2 files changed

+156
-1
lines changed

2 files changed

+156
-1
lines changed

Diff for: pkg/driver/controller_server.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,14 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller
444444
}
445445

446446
log.Info().Int64("size_gb", desiredSize).Str("volume_id", volID).Msg("Volume resize request sent")
447-
d.CivoClient.ResizeVolume(volID, int(desiredSize))
447+
_, err = d.CivoClient.ResizeVolume(volID, int(desiredSize))
448+
if err != nil {
449+
log.Error().
450+
Err(err).
451+
Str("VolumeID", volID).
452+
Msg("Failed to resize volume in Civo API")
453+
return nil, status.Errorf(codes.Internal, "cannot resize volume %s: %s", volID, err.Error())
454+
}
448455

449456
// Resizes can take a while, double the number of normal retries
450457
available, err := d.waitForVolumeStatus(volume, "available", CivoVolumeAvailableRetries*2)

Diff for: pkg/driver/controller_server_test.go

+148
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package driver_test
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/civo/civo-csi/pkg/driver"
@@ -290,3 +291,150 @@ func TestGetCapacity(t *testing.T) {
290291
})
291292

292293
}
294+
295+
296+
func TestControllerExpandVolume(t *testing.T) {
297+
tests := []struct {
298+
name string
299+
volumeID string
300+
capacityRange *csi.CapacityRange
301+
initialVolume *civogo.Volume
302+
expectedError error
303+
expectedSizeGB int64
304+
mockError error
305+
}{
306+
{
307+
name: "Successfully expand volume",
308+
volumeID: "vol-123",
309+
capacityRange: &csi.CapacityRange{
310+
RequiredBytes: 20,
311+
},
312+
initialVolume: &civogo.Volume{
313+
ID: "vol-123",
314+
SizeGigabytes: 10,
315+
Status: "available",
316+
},
317+
expectedError: nil,
318+
expectedSizeGB: 20,
319+
mockError: nil,
320+
},
321+
{
322+
name: "Volume ID is missing",
323+
volumeID: "",
324+
capacityRange: &csi.CapacityRange{
325+
RequiredBytes: 20,
326+
},
327+
initialVolume: nil,
328+
expectedError: status.Error(codes.InvalidArgument, "must provide a VolumeId to ControllerExpandVolume"),
329+
expectedSizeGB: 0,
330+
mockError: nil,
331+
},
332+
{
333+
name: "Capacity range is missing",
334+
volumeID: "vol-123",
335+
capacityRange: nil,
336+
initialVolume: &civogo.Volume{
337+
ID: "vol-123",
338+
SizeGigabytes: 10,
339+
Status: "available",
340+
},
341+
expectedError: status.Error(codes.InvalidArgument, "must provide a capacity range to ControllerExpandVolume"),
342+
expectedSizeGB: 0,
343+
mockError: nil,
344+
},
345+
{
346+
name: "Volume is already resizing",
347+
volumeID: "vol-123",
348+
capacityRange: &csi.CapacityRange{
349+
RequiredBytes: 20,
350+
},
351+
initialVolume: &civogo.Volume{
352+
ID: "vol-123",
353+
SizeGigabytes: 10,
354+
Status: "resizing",
355+
},
356+
expectedError: status.Error(codes.Aborted, "volume is already being resized"),
357+
expectedSizeGB: 0,
358+
mockError: nil,
359+
},
360+
{
361+
name: "Volume is not available for expansion",
362+
volumeID: "vol-123",
363+
capacityRange: &csi.CapacityRange{
364+
RequiredBytes: 20,
365+
},
366+
initialVolume: &civogo.Volume{
367+
ID: "vol-123",
368+
SizeGigabytes: 10,
369+
Status: "attached",
370+
},
371+
expectedError: status.Error(codes.FailedPrecondition, "volume is not in an availble state for OFFLINE expansion"),
372+
expectedSizeGB: 0,
373+
mockError: nil,
374+
},
375+
{
376+
name: "Desired size is smaller than current size",
377+
volumeID: "vol-123",
378+
capacityRange: &csi.CapacityRange{
379+
RequiredBytes: 5,
380+
},
381+
initialVolume: &civogo.Volume{
382+
ID: "vol-123",
383+
SizeGigabytes: 10,
384+
Status: "available",
385+
},
386+
expectedError: nil,
387+
expectedSizeGB: 10, // Current size should be returned
388+
mockError: nil,
389+
},
390+
{
391+
name: "Failed to resize volume in Civo API",
392+
volumeID: "vol-123",
393+
capacityRange: &csi.CapacityRange{
394+
RequiredBytes: 20,
395+
},
396+
initialVolume: &civogo.Volume{
397+
ID: "vol-123",
398+
SizeGigabytes: 10,
399+
Status: "available",
400+
},
401+
expectedError: status.Errorf(codes.Internal, "cannot resize volume vol-123: API error"),
402+
expectedSizeGB: 0,
403+
mockError: fmt.Errorf("API error"),
404+
},
405+
}
406+
407+
for _, tt := range tests {
408+
t.Run(tt.name, func(t *testing.T) {
409+
// Create a fake Civo client
410+
fc := &civogo.FakeClient{
411+
Volumes: []civogo.Volume{*tt.initialVolume},
412+
}
413+
d, _ := NewTestDriver(fc)
414+
415+
// Mock the ResizeVolume method
416+
fc.ResizeVolumeFn = func(volumeID string, size int) (*civogo.Volume, error) {
417+
if tt.mockError != nil {
418+
return nil, tt.mockError
419+
}
420+
tt.initialVolume.SizeGigabytes = size
421+
return tt.initialVolume, nil
422+
}
423+
424+
// Call the method under test
425+
resp, err := d.ControllerExpandVolume(context.Background(), &csi.ControllerExpandVolumeRequest{
426+
VolumeId: tt.volumeID,
427+
CapacityRange: tt.capacityRange,
428+
})
429+
430+
// Assert the expected error
431+
if tt.expectedError != nil {
432+
assert.Equal(t, tt.expectedError, err)
433+
} else {
434+
assert.Nil(t, err)
435+
assert.Equal(t, tt.expectedSizeGB*BytesInGigabyte, resp.CapacityBytes)
436+
assert.True(t, resp.NodeExpansionRequired)
437+
}
438+
})
439+
}
440+
}

0 commit comments

Comments
 (0)