Skip to content

Commit

Permalink
fix(unit-test/sys): getSystemBlockDeviceInfo
Browse files Browse the repository at this point in the history
longhorn-7511

Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang authored and innobead committed Feb 19, 2024
1 parent 4b3c094 commit c0c78c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
10 changes: 5 additions & 5 deletions sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func GetOSDistro(osReleaseContent string) (string, error) {

// GetSystemBlockDeviceInfo returns the block device info for the system.
func GetSystemBlockDeviceInfo() (map[string]types.BlockDeviceInfo, error) {
return getSystemBlockDeviceInfo(os.ReadDir, os.ReadFile)
return getSystemBlockDeviceInfo(types.SysClassBlockDirectory, os.ReadDir, os.ReadFile)
}

// getSystemBlockDeviceInfo returns the block device info for the system.
// It injects the readDirFn and readFileFn for testing.
func getSystemBlockDeviceInfo(readDirFn func(string) ([]os.DirEntry, error), readFileFn func(string) ([]byte, error)) (map[string]types.BlockDeviceInfo, error) {
devices, err := readDirFn(types.SysClassBlockDirectory)
func getSystemBlockDeviceInfo(sysClassBlockDirectory string, readDirFn func(string) ([]os.DirEntry, error), readFileFn func(string) ([]byte, error)) (map[string]types.BlockDeviceInfo, error) {
devices, err := readDirFn(sysClassBlockDirectory)
if err != nil {
return nil, err
}
Expand All @@ -82,12 +82,12 @@ func getSystemBlockDeviceInfo(readDirFn func(string) ([]os.DirEntry, error), rea
deviceInfo := make(map[string]types.BlockDeviceInfo, len(devices))
for _, device := range devices {
deviceName := device.Name()
devicePath := filepath.Join(types.SysClassBlockDirectory, deviceName, "dev")
devicePath := filepath.Join(sysClassBlockDirectory, deviceName, "dev")

if _, err := os.Stat(devicePath); os.IsNotExist(err) {
// If the device path does not exist, check if the device path exists in the "device" directory.
// Some devices such as "nvme0cn1" created from SPDK do not have "dev" file under their sys/class/block directory.
alternativeDevicePath := filepath.Join(types.SysClassBlockDirectory, deviceName, "device", "dev")
alternativeDevicePath := filepath.Join(sysClassBlockDirectory, deviceName, "device", "dev")
if _, altErr := os.Stat(alternativeDevicePath); os.IsNotExist(altErr) {
errs := fmt.Errorf("primary error: %w; alternative error: %w", err, altErr)
logrus.WithFields(logrus.Fields{
Expand Down
21 changes: 20 additions & 1 deletion sys/sys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sys
import (
"io/fs"
"os"
"path/filepath"
"reflect"
"testing"

Expand Down Expand Up @@ -85,6 +86,11 @@ ID_LIKE="suse"`,
}

func (s *TestSuite) TestGetSystemBlockDevices(c *C) {
fakeDir := fake.CreateTempDirectory("", c)
defer func() {
_ = os.RemoveAll(fakeDir)
}()

type testCase struct {
mockDirEntries []os.DirEntry
mockData []byte
Expand Down Expand Up @@ -122,7 +128,20 @@ func (s *TestSuite) TestGetSystemBlockDevices(c *C) {
Data: testCase.mockData,
}

result, err := getSystemBlockDeviceInfo(fakeFS.ReadDir, fakeFS.ReadFile)
for _, entry := range testCase.mockDirEntries {
// Create device directory
deviceDir := filepath.Join(fakeDir, entry.Name())
err := os.MkdirAll(deviceDir, 0755)
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName, err))

// Create device file
devicePath := filepath.Join(deviceDir, "dev")
deviceFile, err := os.Create(devicePath)
deviceFile.Close()
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName, err))
}

result, err := getSystemBlockDeviceInfo(fakeDir, fakeFS.ReadDir, fakeFS.ReadFile)
c.Assert(err, IsNil, Commentf(test.ErrErrorFmt, testName, err))
c.Assert(reflect.DeepEqual(result, testCase.expected), Equals, true, Commentf(test.ErrResultFmt, testName))
}
Expand Down

0 comments on commit c0c78c3

Please sign in to comment.