Skip to content

Commit cf065f3

Browse files
committed
Add unit test for ext4 and block node resize
1 parent 3795aad commit cf065f3

File tree

3 files changed

+112
-7
lines changed

3 files changed

+112
-7
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,17 @@ func (ns *GCENodeServer) NodeExpandVolume(ctx context.Context, req *csi.NodeExpa
372372
}
373373

374374
resizer := resizefs.NewResizeFs(ns.Mounter)
375-
_, err = resizer.Resize(devicePath, "")
375+
resized, err := resizer.Resize(devicePath, "")
376376
if err != nil {
377377
return nil, status.Error(codes.Internal, fmt.Sprintf("Error when resizing volume %s: %v", volKey.String(), err))
378378

379379
}
380380

381-
return &csi.NodeExpandVolumeResponse{
382-
CapacityBytes: reqBytes,
383-
}, nil
381+
resp := &csi.NodeExpandVolumeResponse{}
382+
if resized {
383+
resp.CapacityBytes = reqBytes
384+
}
385+
return resp, nil
384386
}
385387

386388
func (ns *GCENodeServer) GetVolumeLimits() (int64, error) {

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

+95-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ limitations under the License.
1515
package gceGCEDriver
1616

1717
import (
18+
"errors"
19+
"fmt"
1820
"testing"
1921

2022
"context"
23+
2124
csi "github.com/container-storage-interface/spec/lib/go/csi"
2225
"google.golang.org/grpc/codes"
2326
"google.golang.org/grpc/status"
27+
"k8s.io/kubernetes/pkg/util/mount"
28+
utilexec "k8s.io/utils/exec"
2429
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
2530
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
2631
)
@@ -30,8 +35,16 @@ const defaultTargetPath = "/mnt/test"
3035
const defaultStagingPath = "/staging"
3136

3237
func getTestGCEDriver(t *testing.T) *GCEDriver {
38+
return getCustomTestGCEDriver(t, mountmanager.NewFakeSafeMounter(), mountmanager.NewFakeDeviceUtils(), metadataservice.NewFakeService())
39+
}
40+
41+
func getTestGCEDriverWithCustomMounter(t *testing.T, mounter *mount.SafeFormatAndMount) *GCEDriver {
42+
return getCustomTestGCEDriver(t, mounter, mountmanager.NewFakeDeviceUtils(), metadataservice.NewFakeService())
43+
}
44+
45+
func getCustomTestGCEDriver(t *testing.T, mounter *mount.SafeFormatAndMount, deviceUtils mountmanager.DeviceUtils, metaService metadataservice.MetadataService) *GCEDriver {
3346
gceDriver := GetGCEDriver()
34-
err := gceDriver.SetupGCEDriver(nil, mountmanager.NewFakeSafeMounter(), mountmanager.NewFakeDeviceUtils(), metadataservice.NewFakeService(), driver, "test-vendor")
47+
err := gceDriver.SetupGCEDriver(nil, mounter, deviceUtils, metaService, driver, "test-vendor")
3548
if err != nil {
3649
t.Fatalf("Failed to setup GCE Driver: %v", err)
3750
}
@@ -319,6 +332,87 @@ func TestNodeStageVolume(t *testing.T) {
319332
}
320333
}
321334

335+
func TestNodeExpandVolume(t *testing.T) {
336+
// TODO: Add tests/functionality for non-existant volume
337+
var resizedBytes int64 = 2000000000
338+
volumeID := "project/test001/zones/c1/disks/testDisk"
339+
testCases := []struct {
340+
name string
341+
req *csi.NodeExpandVolumeRequest
342+
blockDevice bool
343+
expRespBytes int64
344+
expErrCode codes.Code
345+
}{
346+
{
347+
name: "ext4 fs expand",
348+
req: &csi.NodeExpandVolumeRequest{
349+
VolumeId: volumeID,
350+
VolumePath: "some-path",
351+
CapacityRange: &csi.CapacityRange{
352+
RequiredBytes: resizedBytes,
353+
},
354+
},
355+
blockDevice: false,
356+
expRespBytes: resizedBytes,
357+
},
358+
{
359+
name: "block device expand",
360+
req: &csi.NodeExpandVolumeRequest{
361+
VolumeId: volumeID,
362+
VolumePath: "some-path",
363+
CapacityRange: &csi.CapacityRange{
364+
RequiredBytes: resizedBytes,
365+
},
366+
},
367+
blockDevice: true,
368+
},
369+
}
370+
for _, tc := range testCases {
371+
t.Logf("Test case: %s", tc.name)
372+
373+
execCallback := func(cmd string, args ...string) ([]byte, error) {
374+
if cmd == "blkid" {
375+
if tc.blockDevice {
376+
// blkid returns exit code 2 when run on unformatted device
377+
return nil, utilexec.CodeExitError{
378+
Err: errors.New("this is an exit error"),
379+
Code: 2,
380+
}
381+
} else {
382+
return []byte("DEVNAME=/dev/sdb\nTYPE=ext4"), nil
383+
}
384+
} else if cmd == "resize2fs" {
385+
if tc.blockDevice {
386+
t.Fatalf("resize fs called on block device")
387+
}
388+
return nil, nil
389+
}
390+
return nil, fmt.Errorf("fake exec got unknown call to %v %v", cmd, args)
391+
}
392+
mounter := mountmanager.NewFakeSafeMounterWithCustomExec(mount.NewFakeExec(execCallback))
393+
gceDriver := getTestGCEDriverWithCustomMounter(t, mounter)
394+
395+
resp, err := gceDriver.ns.NodeExpandVolume(context.Background(), tc.req)
396+
if err != nil {
397+
serverError, ok := status.FromError(err)
398+
if !ok {
399+
t.Fatalf("Could not get error status code from err: %v", err)
400+
}
401+
if serverError.Code() != tc.expErrCode {
402+
t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err)
403+
}
404+
continue
405+
}
406+
if tc.expErrCode != codes.OK {
407+
t.Fatalf("Expected error: %v, got no error", tc.expErrCode)
408+
}
409+
410+
if resp.CapacityBytes != tc.expRespBytes {
411+
t.Fatalf("Expected bytes: %v, got: %v", tc.expRespBytes, resp.CapacityBytes)
412+
}
413+
}
414+
}
415+
322416
func TestNodeUnstageVolume(t *testing.T) {
323417
gceDriver := getTestGCEDriver(t)
324418
ns := gceDriver.ns

pkg/mount-manager/fake-safe-mounter.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@ func NewFakeSafeMounter() *mount.SafeFormatAndMount {
4040
}
4141
fakeMounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}, Log: []mount.FakeAction{}}
4242
fakeExec := mount.NewFakeExec(execCallback)
43+
return NewCustomFakeSafeMounter(fakeMounter, fakeExec)
44+
}
45+
46+
func NewFakeSafeMounterWithCustomExec(exec mount.Exec) *mount.SafeFormatAndMount {
47+
fakeMounter := &mount.FakeMounter{MountPoints: []mount.MountPoint{}, Log: []mount.FakeAction{}}
48+
return NewCustomFakeSafeMounter(fakeMounter, exec)
49+
}
50+
51+
func NewCustomFakeSafeMounter(mounter mount.Interface, exec mount.Exec) *mount.SafeFormatAndMount {
4352
return &mount.SafeFormatAndMount{
44-
Interface: fakeMounter,
45-
Exec: fakeExec,
53+
Interface: mounter,
54+
Exec: exec,
4655
}
4756
}

0 commit comments

Comments
 (0)