Skip to content

Commit

Permalink
Add unit tests for mount_linux.go
Browse files Browse the repository at this point in the history
Signed-off-by: torredil <[email protected]>
  • Loading branch information
torredil committed Apr 10, 2024
1 parent f10692c commit bd4b87d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 378 deletions.
80 changes: 0 additions & 80 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1572,71 +1572,6 @@ func TestNodePublishVolume(t *testing.T) {
},
expectedErr: status.Error(codes.Internal, "Failed to find device path /dev/xvdba. device path error"),
},
{
name: "nodePublishVolumeForBlock_path_exists_check",
req: &csi.NodePublishVolumeRequest{
VolumeId: "vol-test",
StagingTargetPath: "/staging/path",
TargetPath: "/target/path",
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
},
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
PublishContext: map[string]string{
DevicePathKey: "/dev/xvdba",
},
},
mounterMock: func(ctrl *gomock.Controller) *mockmounter.MockMounter {
m := mockmounter.NewMockMounter(ctrl)

m.EXPECT().FindDevicePath(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("/dev/xvdba", nil)
m.EXPECT().PathExists(gomock.Any()).Return(false, errors.New("path exists error"))
return m
},
metadataMock: func(ctrl *gomock.Controller) *metadata.MockMetadataService {
m := metadata.NewMockMetadataService(ctrl)
m.EXPECT().GetRegion().Return("us-west-2")
return m
},
expectedErr: status.Error(codes.Internal, "Could not check if path exists \"/target\": path exists error"),
},
{
name: "nodePublishVolumeForBlock_make_file_failure",
req: &csi.NodePublishVolumeRequest{
VolumeId: "vol-test",
StagingTargetPath: "/staging/path",
TargetPath: "/target/path",
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
},
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
},
PublishContext: map[string]string{
DevicePathKey: "/dev/xvdba",
},
},
mounterMock: func(ctrl *gomock.Controller) *mockmounter.MockMounter {
m := mockmounter.NewMockMounter(ctrl)

m.EXPECT().FindDevicePath(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return("/dev/xvdba", nil)
m.EXPECT().PathExists(gomock.Any()).Return(false, nil)
m.EXPECT().MakeDir(gomock.Any()).Return(errors.New("make file error"))
return m
},
metadataMock: func(ctrl *gomock.Controller) *metadata.MockMetadataService {
m := metadata.NewMockMetadataService(ctrl)
m.EXPECT().GetRegion().Return("us-west-2")
return m
},
expectedErr: status.Error(codes.Internal, "Could not create dir \"/target\": make file error"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down Expand Up @@ -2352,21 +2287,6 @@ func TestNodeGetVolumeStats(t *testing.T) {
return nil
},
},
{
name: "metrics_stat_error",
validVolId: true,
validPath: true,
mounterMock: func(ctrl *gomock.Controller, dir string) *mockmounter.MockMounter {
m := mockmounter.NewMockMounter(ctrl)
m.EXPECT().PathExists(gomock.Any()).Return(true, nil)
m.EXPECT().IsBlockDevice(gomock.Any()).Return(false, nil)
return m
},
expectedErr: func(dir string) error {
return status.Errorf(codes.Internal, "failed to get fs info on path %s: %v", "fake-path", "failed to get FsInfo due to error no such file or directory")
},
metricsStatErr: true,
},
}

for _, tc := range testCases {
Expand Down
67 changes: 1 addition & 66 deletions pkg/mounter/mount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,13 @@ func (m *NodeMounter) FindDevicePath(devicePath, volumeID, partition, region str
}

if canonicalDevicePath == "" {
return "", errNoDevicePathFound(devicePath, volumeID)
return "", fmt.Errorf("no device path for device %q volume %q found", devicePath, volumeID)
}

canonicalDevicePath = m.appendPartition(canonicalDevicePath, partition)
return canonicalDevicePath, nil
}

func errNoDevicePathFound(devicePath, volumeID string) error {
return fmt.Errorf("no device path for device %q volume %q found", devicePath, volumeID)
}

// findNvmeVolume looks for the nvme volume with the specified name
// It follows the symlink (if it exists) and returns the absolute path to the device
func findNvmeVolume(findName string) (device string, err error) {
Expand Down Expand Up @@ -259,67 +255,6 @@ func (m *NodeMounter) NeedResize(devicePath string, deviceMountPath string) (boo
return mountutils.NewResizeFs(m.Exec).NeedResize(devicePath, deviceMountPath)
}

func (m *NodeMounter) getExtSize(devicePath string) (uint64, uint64, error) {
output, err := m.SafeFormatAndMount.Exec.Command("dumpe2fs", "-h", devicePath).CombinedOutput()
if err != nil {
return 0, 0, fmt.Errorf("failed to read size of filesystem on %s: %w: %s", devicePath, err, string(output))
}

blockSize, blockCount, _ := m.parseFsInfoOutput(string(output), ":", "block size", "block count")

if blockSize == 0 {
return 0, 0, fmt.Errorf("could not find block size of device %s", devicePath)
}
if blockCount == 0 {
return 0, 0, fmt.Errorf("could not find block count of device %s", devicePath)
}
return blockSize, blockSize * blockCount, nil
}

func (m *NodeMounter) getXFSSize(devicePath string) (uint64, uint64, error) {
output, err := m.SafeFormatAndMount.Exec.Command("xfs_io", "-c", "statfs", devicePath).CombinedOutput()
if err != nil {
return 0, 0, fmt.Errorf("failed to read size of filesystem on %s: %w: %s", devicePath, err, string(output))
}

blockSize, blockCount, _ := m.parseFsInfoOutput(string(output), "=", "geom.bsize", "geom.datablocks")

if blockSize == 0 {
return 0, 0, fmt.Errorf("could not find block size of device %s", devicePath)
}
if blockCount == 0 {
return 0, 0, fmt.Errorf("could not find block count of device %s", devicePath)
}
return blockSize, blockSize * blockCount, nil
}

func (m *NodeMounter) parseFsInfoOutput(cmdOutput string, spliter string, blockSizeKey string, blockCountKey string) (uint64, uint64, error) {
lines := strings.Split(cmdOutput, "\n")
var blockSize, blockCount uint64
var err error

for _, line := range lines {
tokens := strings.Split(line, spliter)
if len(tokens) != 2 {
continue
}
key, value := strings.ToLower(strings.TrimSpace(tokens[0])), strings.ToLower(strings.TrimSpace(tokens[1]))
if key == blockSizeKey {
blockSize, err = strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("failed to parse block size %s: %w", value, err)
}
}
if key == blockCountKey {
blockCount, err = strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("failed to parse block count %s: %w", value, err)
}
}
}
return blockSize, blockCount, err
}

func (m *NodeMounter) Unpublish(path string) error {
// On linux, unpublish and unstage both perform an unmount
return m.Unstage(path)
Expand Down
Loading

0 comments on commit bd4b87d

Please sign in to comment.