Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to rescan a nvme device #48

Merged
merged 7 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gonvme.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type NVMEinterface interface {
// generic implementations
isMock() bool
getOptions() map[string]string

// DeviceRescan rescan the NVMe controller device
DeviceRescan(device string) error
}

// NVMeType is the base structure for each platform implementation
Expand Down
12 changes: 12 additions & 0 deletions gonvme_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,15 @@ func (nvme *MockNVMe) NVMeDisconnect(target NVMeTarget) error {
func (nvme *MockNVMe) GetSessions() ([]NVMESession, error) {
return nvme.getSessions()
}

// DeviceRescan rescan the NVMe device
func (nvme *MockNVMe) DeviceRescan(device string) error {
return nvme.deviceRescan(device)
}

func (nvme *MockNVMe) deviceRescan(_ string) error {
if GONVMEMock.InduceGetSessionsError {
return errors.New("deviceRescan induced error")
}
return nil
}
11 changes: 11 additions & 0 deletions gonvme_tcp_fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,14 @@ func isNoObjsExitCode(err error) bool {
}
return false
}

// DeviceRescan rescan the NVMe controller device
func (nvme *NVMe) DeviceRescan(device string) error {
exe := nvme.buildNVMeCommand([]string{"nvme", "ns-rescan", device})
cmd := exec.Command(exe[0], exe[1:]...) // #nosec G204
_, err := cmd.Output()
if err != nil {
return err
}
return nil
}
21 changes: 21 additions & 0 deletions gonvme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,24 @@ func compareStr(t *testing.T, str1 string, str2 string) {
t.Errorf("strings are not equal: %s != %s", str1, str2)
}
}

func TestMockDeviceRescan(t *testing.T) {
reset()

// Create a mock NVMe interface
c := NewMockNVMe(map[string]string{})

// Test successful rescan (no induced error)
err := c.DeviceRescan("testDevice")
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}

// Induce an error and test failure case
GONVMEMock.InduceGetSessionsError = true
err = c.DeviceRescan("testDevice")
if err == nil {
t.Error("Expected an induced error but got nil")
return
}
}
Loading