Skip to content

Commit bbd60d6

Browse files
Bugfix 1534 nvme block resize (dell#50)
* 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]>
1 parent 4ee1ece commit bbd60d6

File tree

7 files changed

+303
-10
lines changed

7 files changed

+303
-10
lines changed

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ go 1.23
44

55
require (
66
github.com/sirupsen/logrus v1.9.3
7+
github.com/stretchr/testify v1.7.0
78
golang.org/x/sys v0.13.0
89
)
910

10-
require gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e // indirect
11+
require (
12+
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e // indirect
15+
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1111
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1212
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
1313
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1415
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1516
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1617
gopkg.in/yaml.v3 v3.0.0-20220521103104-8f96da9f5d5e h1:3i3ny04XV6HbZ2N1oIBw1UBYATHAOpo4tfTF83JM3Z0=

gofsutil.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type FSinterface interface {
4747
findFSType(ctx context.Context, mountpoint string) (fsType string, err error)
4848
getMpathNameFromDevice(ctx context.Context, device string) (string, error)
4949
fsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error)
50+
getNVMeController(device string) (string, error)
5051

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

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

8688
// fs is the default FS instance.
87-
fs FSinterface = &FS{ScanEntry: defaultEntryScanFunc}
89+
fs FSinterface = &FS{ScanEntry: defaultEntryScanFunc, SysBlockDir: "/sys/block"}
8890
)
8991

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

105+
// UseMockSysBlockDir creates a file system for testing.
106+
func UseMockSysBlockDir(mockSysBlockDir string) {
107+
fs = &FS{ScanEntry: defaultEntryScanFunc, SysBlockDir: mockSysBlockDir}
108+
}
109+
103110
// GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
104111
func GetDiskFormat(ctx context.Context, disk string) (string, error) {
105112
return fs.GetDiskFormat(ctx, disk)
@@ -296,3 +303,8 @@ func GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN string) ([]st
296303
func FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
297304
return fs.fsInfo(ctx, path)
298305
}
306+
307+
// GetNVMeController retrieves the NVMe controller for a given NVMe device.
308+
func GetNVMeController(device string) (string, error) {
309+
return fs.getNVMeController(device)
310+
}

gofsutil_fs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
type FS struct {
2424
// ScanEntry is the function used to process mount table entries.
2525
ScanEntry EntryScanFunc
26+
// SysBlockDir is used to set the directory of block devices.
27+
SysBlockDir string
2628
}
2729

2830
// GetDiskFormat uses 'lsblk' to see if the given disk is unformatted.
@@ -238,3 +240,8 @@ func (fs *FS) GetSysBlockDevicesForVolumeWWN(ctx context.Context, volumeWWN stri
238240
func (fs *FS) FsInfo(ctx context.Context, path string) (int64, int64, int64, int64, int64, int64, error) {
239241
return fs.fsInfo(ctx, path)
240242
}
243+
244+
// GetNVMeController retrieves the NVMe controller for a given NVMe device.
245+
func (fs *FS) GetNVMeController(device string) (string, error) {
246+
return fs.getNVMeController(device)
247+
}

gofsutil_mock.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ var (
4040
GOFSRescanCallback func(scan string)
4141
// GOFSMockMountInfo contains mount information for filesystem volumes
4242
GOFSMockMountInfo *DeviceMountInfo
43+
// GONVMEDeviceToControllerMap has device to controller mapping
44+
GONVMEDeviceToControllerMap map[string]string
45+
// GONVMEValidDevices mocks existing devices
46+
GONVMEValidDevices map[string]bool
4347

4448
// GOFSMock allows you to induce errors in the various routine.
4549
GOFSMock struct {
@@ -66,6 +70,7 @@ var (
6670
InduceResizeFSError bool
6771
InduceGetMpathNameFromDeviceError bool
6872
InduceFilesystemInfoError bool
73+
InduceGetNVMeControllerError bool
6974
}
7075
)
7176

@@ -544,3 +549,21 @@ func (fs *mockfs) getSysBlockDevicesForVolumeWWN(_ context.Context, volumeWWN st
544549
}
545550
return result, nil
546551
}
552+
553+
// GetNVMeController retrieves the NVMe controller for a given NVMe device.
554+
func (fs *mockfs) GetNVMeController(device string) (string, error) {
555+
return fs.getNVMeController(device)
556+
}
557+
558+
func (fs *mockfs) getNVMeController(device string) (string, error) {
559+
if GOFSMock.InduceGetNVMeControllerError {
560+
return "", errors.New("induced error")
561+
}
562+
if _, exists := GONVMEValidDevices[device]; !exists {
563+
return "", fmt.Errorf("device %s does not exist", device)
564+
}
565+
if controller, found := GONVMEDeviceToControllerMap[device]; found {
566+
return controller, nil
567+
}
568+
return "", fmt.Errorf("controller not found for device %s", device)
569+
}

gofsutil_mount_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ package gofsutil_test
1414

1515
import (
1616
"context"
17+
"fmt"
1718
"os"
19+
"path/filepath"
20+
"strings"
1821
"testing"
1922

2023
"github.com/dell/gofsutil"
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
2126
)
2227

2328
func TestBindMount(t *testing.T) {
@@ -81,3 +86,132 @@ func TestGetMounts(t *testing.T) {
8186
t.Logf("%+v", m)
8287
}
8388
}
89+
90+
func TestGetSysBlockDevicesForVolumeWWN(t *testing.T) {
91+
tempDir := t.TempDir()
92+
gofsutil.UseMockSysBlockDir(tempDir)
93+
94+
tests := []struct {
95+
name string
96+
wwn string
97+
nguid string
98+
deviceName string
99+
deviceWwidPath []string
100+
expect []string
101+
errString string
102+
}{
103+
{
104+
name: "iscsi block device",
105+
wwn: "example-volume-wwn",
106+
deviceName: "sdx",
107+
deviceWwidPath: []string{"device", "wwid"},
108+
expect: []string{"sdx"},
109+
errString: "",
110+
},
111+
{
112+
name: "PowerStore nvme block device",
113+
wwn: "naa.68ccf098001111a2222b3d4444a1b23c",
114+
nguid: "eui.1111a2222b3d44448ccf096800a1b23c",
115+
deviceName: "nvme0n1",
116+
deviceWwidPath: []string{"wwid"},
117+
expect: []string{"nvme0n1"},
118+
errString: "",
119+
},
120+
{
121+
name: "PowerMax nvme block device",
122+
wwn: "naa.60000970000120001263533030313434",
123+
nguid: "eui.12635330303134340000976000012000",
124+
deviceName: "nvme0n2",
125+
deviceWwidPath: []string{"wwid"},
126+
expect: []string{"nvme0n2"},
127+
errString: "",
128+
},
129+
}
130+
131+
for _, tt := range tests {
132+
t.Run(tt.name, func(t *testing.T) {
133+
// Create the necessary directories and files
134+
path := []string{tempDir, tt.deviceName}
135+
path = append(path, tt.deviceWwidPath...)
136+
deviceWwidFile := filepath.Join(path...)
137+
err := os.MkdirAll(filepath.Dir(deviceWwidFile), 0o755)
138+
require.Nil(t, err)
139+
if strings.HasPrefix(tt.deviceName, "nvme") {
140+
err = os.WriteFile(deviceWwidFile, []byte(tt.nguid), 0o600)
141+
} else {
142+
err = os.WriteFile(deviceWwidFile, []byte(tt.wwn), 0o600)
143+
}
144+
require.Nil(t, err)
145+
146+
// Call the function with the test input
147+
result, err := gofsutil.GetSysBlockDevicesForVolumeWWN(context.Background(), tt.wwn)
148+
assert.Nil(t, err)
149+
assert.Equal(t, tt.expect, result)
150+
})
151+
}
152+
}
153+
154+
func TestGetNVMeController(t *testing.T) {
155+
tempDir := t.TempDir()
156+
gofsutil.UseMockSysBlockDir(tempDir)
157+
158+
tests := map[string]struct {
159+
device string
160+
controller string
161+
path []string
162+
expectedErr error
163+
}{
164+
"device exists and is an NVMe controller": {
165+
device: "nvme0n1",
166+
controller: "nvme0",
167+
path: []string{"virtual", "nvme-fabrics", "ctl", "nvme0", "nvme0n1"},
168+
expectedErr: nil,
169+
},
170+
"device exists but is not an NVMe controller": {
171+
device: "nvme1n1",
172+
controller: "",
173+
path: []string{"virtual", "nvme-fabrics", "nvme-subsystem", "nvme-subsys0", "nvme1n1"},
174+
expectedErr: nil,
175+
},
176+
"device exists but NVMe controller not found": {
177+
device: "nvme2n1",
178+
controller: "",
179+
path: []string{"virtual", "nvme-fabrics", "ctl", "nvme2n1"},
180+
expectedErr: fmt.Errorf("controller not found for device nvme2n1"),
181+
},
182+
"device does not exist": {
183+
device: "nonexistent",
184+
controller: "",
185+
expectedErr: fmt.Errorf("device %s does not exist", "nonexistent"),
186+
},
187+
}
188+
189+
for name, test := range tests {
190+
t.Run(name, func(t *testing.T) {
191+
if name != "device does not exist" {
192+
// Create the necessary directories and files
193+
realPath := []string{tempDir}
194+
realPath = append(realPath, test.path...)
195+
err := os.MkdirAll(filepath.Join(realPath...), 0o755)
196+
require.NoError(t, err)
197+
198+
sysBlockNVMeDeviceDir := filepath.Join(tempDir, test.device)
199+
err = os.Symlink(filepath.Join(realPath...), sysBlockNVMeDeviceDir)
200+
require.NoError(t, err)
201+
}
202+
203+
// Call the function with the test input
204+
controller, err := gofsutil.GetNVMeController(test.device)
205+
if test.expectedErr != nil && err == nil {
206+
t.Errorf("getNVMeController() did not return error, expected %v", test.expectedErr)
207+
} else if test.expectedErr == nil && err != nil {
208+
t.Errorf("getNVMeController() returned error %v, expected no error", err)
209+
} else if err != nil && err.Error() != test.expectedErr.Error() {
210+
t.Errorf("getNVMeController() returned error %v, expected %v", err, test.expectedErr)
211+
}
212+
if controller != test.controller {
213+
t.Errorf("getNVMeController() = %v, expected %v", controller, test.controller)
214+
}
215+
})
216+
}
217+
}

0 commit comments

Comments
 (0)