Skip to content

Commit

Permalink
fixed failing test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chaganti-rajitha committed Jan 29, 2025
1 parent 2543f72 commit 38a01f6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
7 changes: 1 addition & 6 deletions gofsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var (
ErrNotImplemented = errors.New("not implemented")

// fs is the default FS instance.
fs FSinterface = &FS{ScanEntry: defaultEntryScanFunc, SysBlockDir: "/sys/block"}
fs FSinterface = &FS{ScanEntry: defaultEntryScanFunc}
)

// ContextKey is a variable containing context-keys
Expand All @@ -102,11 +102,6 @@ func UseMockFS() {
fs = &mockfs{ScanEntry: defaultEntryScanFunc}
}

// UseMockSysBlockDir creates a file system for testing.
func UseMockSysBlockDir(mockSysBlockDir string) {
fs = &FS{ScanEntry: defaultEntryScanFunc, SysBlockDir: mockSysBlockDir}
}

// GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
func GetDiskFormat(ctx context.Context, disk string) (string, error) {
return fs.GetDiskFormat(ctx, disk)
Expand Down
5 changes: 3 additions & 2 deletions gofsutil_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import (
"golang.org/x/sys/unix"
)

// SysBlockDir is used to set the directory of block devices.
var SysBlockDir string = "/sys/block"

Check failure on line 23 in gofsutil_fs.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

var-declaration: should omit type string from declaration of var SysBlockDir; it will be inferred from the right-hand side (revive)

// FS provides many filesystem-specific functions, such as mount, format, etc.
type FS struct {
// ScanEntry is the function used to process mount table entries.
ScanEntry EntryScanFunc
// SysBlockDir is used to set the directory of block devices.
SysBlockDir string
}

// GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
Expand Down
4 changes: 2 additions & 2 deletions gofsutil_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestGetMounts(t *testing.T) {

func TestGetSysBlockDevicesForVolumeWWN(t *testing.T) {
tempDir := t.TempDir()
gofsutil.UseMockSysBlockDir(tempDir)
gofsutil.SysBlockDir = tempDir

tests := []struct {
name string
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestGetSysBlockDevicesForVolumeWWN(t *testing.T) {

func TestGetNVMeController(t *testing.T) {
tempDir := t.TempDir()
gofsutil.UseMockSysBlockDir(tempDir)
gofsutil.SysBlockDir = tempDir

tests := map[string]struct {
device string
Expand Down
10 changes: 5 additions & 5 deletions gofsutil_mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,9 @@ func (fs *FS) issueLIPToAllFCHosts(_ context.Context) error {
func (fs *FS) getSysBlockDevicesForVolumeWWN(_ context.Context, volumeWWN string) ([]string, error) {
start := time.Now()
result := make([]string, 0)
sysBlocks, err := os.ReadDir(fs.SysBlockDir)
sysBlocks, err := os.ReadDir(SysBlockDir)
if err != nil {
return result, fmt.Errorf("Error reading %s: %s", fs.SysBlockDir, err)
return result, fmt.Errorf("Error reading %s: %s", SysBlockDir, err)
}

for _, sysBlock := range sysBlocks {
Expand All @@ -665,9 +665,9 @@ func (fs *FS) getSysBlockDevicesForVolumeWWN(_ context.Context, volumeWWN string
// Set the WWID path based on the device type
var wwidPath string
if strings.HasPrefix(name, "nvme") {
wwidPath = fs.SysBlockDir + "/" + name + "/wwid" // For NVMe devices
wwidPath = SysBlockDir + "/" + name + "/wwid" // For NVMe devices
} else {
wwidPath = fs.SysBlockDir + "/" + name + "/device/wwid" // For SCSI devices
wwidPath = SysBlockDir + "/" + name + "/device/wwid" // For SCSI devices
}

bytes, err := os.ReadFile(filepath.Clean(wwidPath))
Expand Down Expand Up @@ -746,7 +746,7 @@ func wwnMatches(nguid, wwn string) bool {

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func (fs *FS) getNVMeController(device string) (string, error) {
devicePath := filepath.Join(fs.SysBlockDir, device)
devicePath := filepath.Join(SysBlockDir, device)

// Check if the device path exists
if _, err := os.Stat(devicePath); os.IsNotExist(err) {
Expand Down
13 changes: 8 additions & 5 deletions gofsutil_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func TestMountArgs(t *testing.T) {
}

func TestWWNToDevicePath(t *testing.T) {
tempDir := t.TempDir()
SysBlockDir = tempDir

tests := []struct {
src string
tgt string
Expand Down Expand Up @@ -251,7 +254,7 @@ func TestValidateMountArgs(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := FS{SysBlockDir: "string"}
fs := FS{}
err := fs.validateMountArgs(tt.source, tt.target, tt.fstype, tt.opts...)
assert.Equal(t, tt.expect, err)
})
Expand Down Expand Up @@ -291,7 +294,7 @@ func TestDoMount(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := FS{SysBlockDir: "string"}
fs := FS{}
err := fs.doMount(tt.ctx, tt.mntCmnd, tt.source, tt.target, tt.fstype, tt.opts...)
assert.Equal(t, true, strings.Contains(err.Error(), tt.expect))
})
Expand Down Expand Up @@ -319,7 +322,7 @@ func TestUnMount(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := FS{SysBlockDir: "string"}
fs := FS{}
err := fs.unmount(tt.ctx, tt.target)
assert.Equal(t, true, strings.Contains(err.Error(), tt.expect))
})
Expand Down Expand Up @@ -398,7 +401,7 @@ func TestMultipathCommand(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := FS{SysBlockDir: "string"}
fs := FS{}
_, err := fs.multipathCommand(tt.ctx, tt.timeoutSeconds, tt.chroot, tt.arguments...)
assert.Equal(t, tt.expectErr, err)
})
Expand All @@ -421,7 +424,7 @@ func TestIsBind(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testname, func(t *testing.T) {
fs := FS{SysBlockDir: "string"}
fs := FS{}
_, err := fs.isBind(tt.ctx, tt.opts...)
assert.Equal(t, tt.expect, err)
})
Expand Down

0 comments on commit 38a01f6

Please sign in to comment.