Skip to content

Commit

Permalink
Fix: Improve error handling for ResizeVolume method
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen005 committed Feb 3, 2025
1 parent 014c028 commit 4687409
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/driver/controller_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,14 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller
}

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

// Resizes can take a while, double the number of normal retries
available, err := d.waitForVolumeStatus(volume, "available", CivoVolumeAvailableRetries*2)
Expand Down
148 changes: 148 additions & 0 deletions pkg/driver/controller_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver_test

import (
"context"
"fmt"
"testing"

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

}


func TestControllerExpandVolume(t *testing.T) {
tests := []struct {
name string
volumeID string
capacityRange *csi.CapacityRange
initialVolume *civogo.Volume
expectedError error
expectedSizeGB int64
mockError error
}{
{
name: "Successfully expand volume",
volumeID: "vol-123",
capacityRange: &csi.CapacityRange{
RequiredBytes: 20,
},
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "available",
},
expectedError: nil,
expectedSizeGB: 20,
mockError: nil,
},
{
name: "Volume ID is missing",
volumeID: "",
capacityRange: &csi.CapacityRange{
RequiredBytes: 20,
},
initialVolume: nil,
expectedError: status.Error(codes.InvalidArgument, "must provide a VolumeId to ControllerExpandVolume"),

Check failure on line 328 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 328 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes

Check failure on line 328 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 328 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes
expectedSizeGB: 0,
mockError: nil,
},
{
name: "Capacity range is missing",
volumeID: "vol-123",
capacityRange: nil,
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "available",
},
expectedError: status.Error(codes.InvalidArgument, "must provide a capacity range to ControllerExpandVolume"),

Check failure on line 341 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 341 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes

Check failure on line 341 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 341 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes
expectedSizeGB: 0,
mockError: nil,
},
{
name: "Volume is already resizing",
volumeID: "vol-123",
capacityRange: &csi.CapacityRange{
RequiredBytes: 20,
},
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "resizing",
},
expectedError: status.Error(codes.Aborted, "volume is already being resized"),

Check failure on line 356 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 356 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes

Check failure on line 356 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 356 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes
expectedSizeGB: 0,
mockError: nil,
},
{
name: "Volume is not available for expansion",
volumeID: "vol-123",
capacityRange: &csi.CapacityRange{
RequiredBytes: 20,
},
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "attached",
},
expectedError: status.Error(codes.FailedPrecondition, "volume is not in an availble state for OFFLINE expansion"),

Check failure on line 371 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 371 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes

Check failure on line 371 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 371 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes
expectedSizeGB: 0,
mockError: nil,
},
{
name: "Desired size is smaller than current size",
volumeID: "vol-123",
capacityRange: &csi.CapacityRange{
RequiredBytes: 5,
},
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "available",
},
expectedError: nil,
expectedSizeGB: 10, // Current size should be returned
mockError: nil,
},
{
name: "Failed to resize volume in Civo API",
volumeID: "vol-123",
capacityRange: &csi.CapacityRange{
RequiredBytes: 20,
},
initialVolume: &civogo.Volume{
ID: "vol-123",
SizeGigabytes: 10,
Status: "available",
},
expectedError: status.Errorf(codes.Internal, "cannot resize volume vol-123: API error"),

Check failure on line 401 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 401 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes

Check failure on line 401 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: status

Check failure on line 401 in pkg/driver/controller_server_test.go

View workflow job for this annotation

GitHub Actions / test-suite

undefined: codes
expectedSizeGB: 0,
mockError: fmt.Errorf("API error"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a fake Civo client
fc := &civogo.FakeClient{
Volumes: []civogo.Volume{*tt.initialVolume},
}
d, _ := NewTestDriver(fc)

// Mock the ResizeVolume method
fc.ResizeVolumeFn = func(volumeID string, size int) (*civogo.Volume, error) {
if tt.mockError != nil {
return nil, tt.mockError
}
tt.initialVolume.SizeGigabytes = size
return tt.initialVolume, nil
}

// Call the method under test
resp, err := d.ControllerExpandVolume(context.Background(), &csi.ControllerExpandVolumeRequest{
VolumeId: tt.volumeID,
CapacityRange: tt.capacityRange,
})

// Assert the expected error
if tt.expectedError != nil {
assert.Equal(t, tt.expectedError, err)
} else {
assert.Nil(t, err)
assert.Equal(t, tt.expectedSizeGB*BytesInGigabyte, resp.CapacityBytes)
assert.True(t, resp.NodeExpansionRequired)
}
})
}
}

0 comments on commit 4687409

Please sign in to comment.