Skip to content

Commit

Permalink
Merge pull request #403 from yussufsh/test
Browse files Browse the repository at this point in the history
add missing TestNodeUnstageVolume test
  • Loading branch information
k8s-ci-robot authored May 24, 2023
2 parents 221b593 + c449158 commit 17b0674
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ const (
)

var (
NewDevice = device.NewLinuxDevice
NewDevice = device.NewLinuxDevice
GetDeviceWWN = device.GetDeviceWWN

// nodeCaps represents the capability of node service.
nodeCaps = []csi.NodeServiceCapability_RPC_Type{
Expand Down Expand Up @@ -265,7 +266,7 @@ func (d *nodeService) nodeUnstageVolume(req *csi.NodeUnstageVolumeRequest) error
return nil
}

klog.Infof("found staged device: %s", deviceName)
klog.V(5).Infof("found staged device: %s", deviceName)

// If mounted, then unmount the filesystem
klog.V(5).Infof("starting unmounting %s", stagingTarget, "volumeID", volumeID)
Expand All @@ -276,7 +277,7 @@ func (d *nodeService) nodeUnstageVolume(req *csi.NodeUnstageVolumeRequest) error
klog.V(5).Infof("completed unmounting %s", stagingTarget, "volumeID", volumeID)

// Delete device
klog.Infof("deleting device %s", deviceName, "volumeID", volumeID)
klog.V(5).Infof("deleting device %s", deviceName, "volumeID", volumeID)
//check if device is mounted or has holders
isDirMounted, err := d.mounter.IsMountPoint(stagingTarget)
if err != nil {
Expand All @@ -295,7 +296,7 @@ func (d *nodeService) nodeUnstageVolume(req *csi.NodeUnstageVolumeRequest) error

func (d *nodeService) deleteDevice(deviceName string) error {

wwn, err := device.GetDeviceWWN(deviceName)
wwn, err := GetDeviceWWN(deviceName)
if err != nil {
return err
}
Expand Down
210 changes: 210 additions & 0 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,216 @@ func TestNodeStageVolume(t *testing.T) {
}
}

func TestNodeUnstageVolume(t *testing.T) {
targetPath := "/test/path"
devicePath := "/dev/fake"

testCases := []struct {
name string
testFunc func(t *testing.T)
}{
{
name: "success normal",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)
mockDevice := mocks.NewMockLinuxDevice(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return(devicePath, 1, nil)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(nil)
mockMounter.EXPECT().IsMountPoint(gomock.Eq(targetPath)).Return(false, nil)

mockDevice.EXPECT().GetMapper().Return(devicePath)
mockDevice.EXPECT().Populate(false).Return(nil)
mockDevice.EXPECT().DeleteDevice().Return(nil)

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
VolumeId: volumeID,
}

GetDeviceWWN = func(pathName string) (string, error) {
return "fakeWWN", nil
}
// always use mocked device
NewDevice = func(wwn string) device.LinuxDevice {
return mockDevice
}

_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
if err != nil {
t.Fatalf("Expect no error but got: %v", err)
}
},
},
{
name: "success no device mounted at target",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}
mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return(devicePath, 0, nil)

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
VolumeId: volumeID,
}
_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
if err != nil {
t.Fatalf("Expect no error but got: %v", err)
}
},
},
{
name: "success device mounted at multiple targets",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)
mockDevice := mocks.NewMockLinuxDevice(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return(devicePath, 2, nil)
mockMounter.EXPECT().Unmount(gomock.Eq(targetPath)).Return(nil)
mockMounter.EXPECT().IsMountPoint(gomock.Eq(targetPath)).Return(false, nil)

mockDevice.EXPECT().GetMapper().Return(devicePath)
mockDevice.EXPECT().Populate(false).Return(nil)
mockDevice.EXPECT().DeleteDevice().Return(nil)

GetDeviceWWN = func(pathName string) (string, error) {
return "fakeWWN", nil
}
// always use mocked device
NewDevice = func(wwn string) device.LinuxDevice {
return mockDevice
}

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
VolumeId: volumeID,
}

_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
if err != nil {
t.Fatalf("Expect no error but got: %v", err)
}
},
},
{
name: "fail no VolumeId",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
}

_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
expectErr(t, err, codes.InvalidArgument)
},
},
{
name: "fail no StagingTargetPath",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

req := &csi.NodeUnstageVolumeRequest{
VolumeId: volumeID,
}
_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
expectErr(t, err, codes.InvalidArgument)
},
},
{
name: "fail GetDeviceName returns error",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

mockMounter.EXPECT().GetDeviceName(gomock.Eq(targetPath)).Return("", 0, errors.New("GetDeviceName faield"))

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
VolumeId: volumeID,
}

_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
expectErr(t, err, codes.Internal)
},
},
{
name: "fail if volume is already locked",
testFunc: func(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()

mockMounter := mocks.NewMockMounter(mockCtl)

powervsDriver := &nodeService{
mounter: mockMounter,
volumeLocks: util.NewVolumeLocks(),
}

req := &csi.NodeUnstageVolumeRequest{
StagingTargetPath: targetPath,
VolumeId: volumeID,
}
powervsDriver.volumeLocks.TryAcquire(req.VolumeId)
defer powervsDriver.volumeLocks.Release(req.VolumeId)

_, err := powervsDriver.NodeUnstageVolume(context.TODO(), req)
expectErr(t, err, codes.Aborted)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
}
}

func TestNodeExpandVolume(t *testing.T) {
mockCtl := gomock.NewController(t)
defer mockCtl.Finish()
Expand Down

0 comments on commit 17b0674

Please sign in to comment.