Skip to content

Commit

Permalink
Bugfix 1534 nvme block resize (#50)
Browse files Browse the repository at this point in the history
* Update getSysBlockDevicesForVolumeWWN to return nvme block device

* Add method to return nvme controller device

* added getNVMeController mock

* fixed linters warning

* added getNVMeController mock

* fixed linters warning

* Add UT

---------

Co-authored-by: Rishabh Raj <[email protected]>
  • Loading branch information
AkshaySainiDell and rishabhatdell authored Oct 25, 2024
1 parent 4ee1ece commit bbd60d6
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 10 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ go 1.23

require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.13.0
)

require gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e // indirect
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e h1:3i3ny04XV6HbZ2N1oIBw1UBYATHAOpo4tfTF83JM3Z0=
Expand Down
14 changes: 13 additions & 1 deletion gofsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type FSinterface interface {
findFSType(ctx context.Context, mountpoint string) (fsType string, err error)
getMpathNameFromDevice(ctx context.Context, device string) (string, error)
fsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error)
getNVMeController(device string) (string, error)

// Architecture agnostic implementations, generally just wrappers
GetDiskFormat(ctx context.Context, disk string) (string, error)
Expand All @@ -73,6 +74,7 @@ type FSinterface interface {
FindFSType(ctx context.Context, mountpoint string) (fsType string, err error)
GetMpathNameFromDevice(ctx context.Context, device string) (string, error)
FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error)
GetNVMeController(device string) (string, error)
}

// MultipathDevDiskByIDPrefix is a pathname prefix for items located in /dev/disk/by-id
Expand All @@ -84,7 +86,7 @@ var (
ErrNotImplemented = errors.New("not implemented")

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

// ContextKey is a variable containing context-keys
Expand All @@ -100,6 +102,11 @@ 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 Expand Up @@ -296,3 +303,8 @@ func GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN string) ([]st
func FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
return fs.fsInfo(ctx, path)
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func GetNVMeController(device string) (string, error) {
return fs.getNVMeController(device)
}
7 changes: 7 additions & 0 deletions gofsutil_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
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 Expand Up @@ -238,3 +240,8 @@ func (fs *FS) GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN stri
func (fs *FS) FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
return fs.fsInfo(ctx, path)
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func (fs *FS) GetNVMeController(device string) (string, error) {
return fs.getNVMeController(device)
}
23 changes: 23 additions & 0 deletions gofsutil_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ var (
GOFSRescanCallback func(scan string)
// GOFSMockMountInfo contains mount information for filesystem volumes
GOFSMockMountInfo *DeviceMountInfo
// GONVMEDeviceToControllerMap has device to controller mapping
GONVMEDeviceToControllerMap map[string]string
// GONVMEValidDevices mocks existing devices
GONVMEValidDevices map[string]bool

// GOFSMock allows you to induce errors in the various routine.
GOFSMock struct {
Expand All @@ -66,6 +70,7 @@ var (
InduceResizeFSError bool
InduceGetMpathNameFromDeviceError bool
InduceFilesystemInfoError bool
InduceGetNVMeControllerError bool
}
)

Expand Down Expand Up @@ -544,3 +549,21 @@ func (fs *mockfs) getSysBlockDevicesForVolumeWWN(_ context.Context, volumeWWN st
}
return result, nil
}

// GetNVMeController retrieves the NVMe controller for a given NVMe device.
func (fs *mockfs) GetNVMeController(device string) (string, error) {
return fs.getNVMeController(device)
}

func (fs *mockfs) getNVMeController(device string) (string, error) {
if GOFSMock.InduceGetNVMeControllerError {
return "", errors.New("induced error")
}
if _, exists := GONVMEValidDevices[device]; !exists {
return "", fmt.Errorf("device %s does not exist", device)
}
if controller, found := GONVMEDeviceToControllerMap[device]; found {
return controller, nil
}
return "", fmt.Errorf("controller not found for device %s", device)
}
134 changes: 134 additions & 0 deletions gofsutil_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ package gofsutil_test

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/dell/gofsutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBindMount(t *testing.T) {
Expand Down Expand Up @@ -81,3 +86,132 @@ func TestGetMounts(t *testing.T) {
t.Logf("%+v", m)
}
}

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

tests := []struct {
name string
wwn string
nguid string
deviceName string
deviceWwidPath []string
expect []string
errString string
}{
{
name: "iscsi block device",
wwn: "example-volume-wwn",
deviceName: "sdx",
deviceWwidPath: []string{"device", "wwid"},
expect: []string{"sdx"},
errString: "",
},
{
name: "PowerStore nvme block device",
wwn: "naa.68ccf098001111a2222b3d4444a1b23c",
nguid: "eui.1111a2222b3d44448ccf096800a1b23c",
deviceName: "nvme0n1",
deviceWwidPath: []string{"wwid"},
expect: []string{"nvme0n1"},
errString: "",
},
{
name: "PowerMax nvme block device",
wwn: "naa.60000970000120001263533030313434",
nguid: "eui.12635330303134340000976000012000",
deviceName: "nvme0n2",
deviceWwidPath: []string{"wwid"},
expect: []string{"nvme0n2"},
errString: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create the necessary directories and files
path := []string{tempDir, tt.deviceName}
path = append(path, tt.deviceWwidPath...)
deviceWwidFile := filepath.Join(path...)
err := os.MkdirAll(filepath.Dir(deviceWwidFile), 0o755)
require.Nil(t, err)
if strings.HasPrefix(tt.deviceName, "nvme") {
err = os.WriteFile(deviceWwidFile, []byte(tt.nguid), 0o600)
} else {
err = os.WriteFile(deviceWwidFile, []byte(tt.wwn), 0o600)
}
require.Nil(t, err)

// Call the function with the test input
result, err := gofsutil.GetSysBlockDevicesForVolumeWWN(context.Background(), tt.wwn)
assert.Nil(t, err)
assert.Equal(t, tt.expect, result)
})
}
}

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

tests := map[string]struct {
device string
controller string
path []string
expectedErr error
}{
"device exists and is an NVMe controller": {
device: "nvme0n1",
controller: "nvme0",
path: []string{"virtual", "nvme-fabrics", "ctl", "nvme0", "nvme0n1"},
expectedErr: nil,
},
"device exists but is not an NVMe controller": {
device: "nvme1n1",
controller: "",
path: []string{"virtual", "nvme-fabrics", "nvme-subsystem", "nvme-subsys0", "nvme1n1"},
expectedErr: nil,
},
"device exists but NVMe controller not found": {
device: "nvme2n1",
controller: "",
path: []string{"virtual", "nvme-fabrics", "ctl", "nvme2n1"},
expectedErr: fmt.Errorf("controller not found for device nvme2n1"),
},
"device does not exist": {
device: "nonexistent",
controller: "",
expectedErr: fmt.Errorf("device %s does not exist", "nonexistent"),
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
if name != "device does not exist" {
// Create the necessary directories and files
realPath := []string{tempDir}
realPath = append(realPath, test.path...)
err := os.MkdirAll(filepath.Join(realPath...), 0o755)
require.NoError(t, err)

sysBlockNVMeDeviceDir := filepath.Join(tempDir, test.device)
err = os.Symlink(filepath.Join(realPath...), sysBlockNVMeDeviceDir)
require.NoError(t, err)
}

// Call the function with the test input
controller, err := gofsutil.GetNVMeController(test.device)
if test.expectedErr != nil && err == nil {
t.Errorf("getNVMeController() did not return error, expected %v", test.expectedErr)
} else if test.expectedErr == nil && err != nil {
t.Errorf("getNVMeController() returned error %v, expected no error", err)
} else if err != nil && err.Error() != test.expectedErr.Error() {
t.Errorf("getNVMeController() returned error %v, expected %v", err, test.expectedErr)
}
if controller != test.controller {
t.Errorf("getNVMeController() = %v, expected %v", controller, test.controller)
}
})
}
}
Loading

0 comments on commit bbd60d6

Please sign in to comment.