Skip to content

Commit

Permalink
Add method to rescan a nvme device (#48)
Browse files Browse the repository at this point in the history
* Add method to rescan a nvme device

* Fix mock

* Fix golangci-lint error

* Fix lint error

* DeviceRescan UT

* DeviceRescan UT formatting

* Update gonvme_test.go

---------

Co-authored-by: Kakde <[email protected]>
Co-authored-by: KshitijaKakde <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 642b92f commit 241f049
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
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
}
}

0 comments on commit 241f049

Please sign in to comment.