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 4046e86
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 298 deletions.
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 4046e86

Please sign in to comment.