diff --git a/gonvme_mock.go b/gonvme_mock.go index a9e5cdf..147edfa 100644 --- a/gonvme_mock.go +++ b/gonvme_mock.go @@ -20,6 +20,8 @@ import ( "errors" "fmt" "strconv" + + log "github.com/sirupsen/logrus" ) const ( @@ -53,6 +55,27 @@ type MockNVMe struct { NVMeType } +type CommandRunner interface { + RunCommand(name string, args ...string) (string, error) +} + +type MockCommandRunner struct { + Output string + Err error +} + +func (m *MockCommandRunner) RunCommand(name string, args ...string) (string, error) { + // Return pre-defined output or error for testing + return m.Output, m.Err +} + +func RunCommandWithRunner(runner CommandRunner, command []string) (string, error) { + if len(command) == 0 { + return "", errors.New("command is empty") + } + return runner.RunCommand(command[0], command[1:]...) +} + // NewMockNVMe - returns a mock NVMe client func NewMockNVMe(opts map[string]string) *MockNVMe { nvme := MockNVMe{ @@ -62,6 +85,24 @@ func NewMockNVMe(opts map[string]string) *MockNVMe { }, } + mockRunner := &MockCommandRunner{ + Output: string("/sbin/nvme"), + Err: nil, + } + + command := []string{"chroot", "which", "nvme"} + output, err := RunCommandWithRunner(mockRunner, command) + + if err != nil{ + log.Errorf("Empty command") + } + + // Assertions + expectedPath := "/sbin/nvme" + if output != expectedPath { + log.Errorf("expected %v, got %v", expectedPath, output) + } + return &nvme } diff --git a/gonvme_tcp_fc_test.go b/gonvme_tcp_fc_test.go deleted file mode 100644 index 04ff05e..0000000 --- a/gonvme_tcp_fc_test.go +++ /dev/null @@ -1,107 +0,0 @@ -package gonvme - -import ( - "os" - "os/exec" - "testing" -) - -var execCommand = exec.Command -var opts map[string]string - -// MockExecCommand is a mock function for exec.Command -var MockExecCommand = func(command string, args ...string) *exec.Cmd { - cs := []string{"-test.run=TestHelperProcess", "--", command} - cs = append(cs, args...) - cmd := exec.Command(os.Args[0], cs...) - cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} - return cmd -} - -// TestHelperProcess is a helper process used by MockExecCommand -func TestHelperProcess(t *testing.T) { - if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { - return - } - args := os.Args[3:] - if len(args) > 0 && args[0] == "nvme" && args[1] == "connect" { - // Simulate successful connection - println("connected") - } else { - // Simulate failure - os.Exit(1) - } - os.Exit(0) -} - -func TestDiscoverNVMeTCPTargets(t *testing.T) { - tests := []struct { - name string - address string - login bool - expectError bool - }{ - {"Valid address", "192.168.1.1", true, false}, - {"Empty address", "", true, true}, - } - c := NewNVMe(map[string]string{"kar": "kar1"}) - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - targets, err := c.DiscoverNVMeTCPTargets(tt.address, tt.login) - if (err != nil) != tt.expectError { - t.Errorf("DiscoverNVMeTCPTargets() error = %v, expectError %v", err, tt.expectError) - return - } - if !tt.expectError && len(targets) == 0 { - t.Errorf("Expected targets, got none") - } - }) - } -} - -func TestBuildNVMECommand(t *testing.T) { - initial := []string{"/bin/ls"} - opts[ChrootDirectory] = "/test" - c := NewNVMe(map[string]string{}) - command := c.buildNVMeCommand(initial) - // the length of the resulting command should the length of the initial command +2 - if len(command) != (len(initial) + 2) { - t.Errorf("Expected to %d items in the command slice but received %v", len(initial)+2, command) - } - if command[0] != "chroot" { - t.Error("Expected the command to be run with chroot") - } - if command[1] != opts[ChrootDirectory] { - t.Errorf("Expected the command to chroot to %s but got %s", opts[ChrootDirectory], command[1]) - } -} - -func TestNVMeTCPConnect(t *testing.T) { - // Save the original exec.Command function - originalExecCommand := execCommand - // Restore the original exec.Command function after the test - defer func() { execCommand = originalExecCommand }() - - // Replace exec.Command with the mock function - execCommand = MockExecCommand - c := NewNVMe(map[string]string{}) - tests := []struct { - name string - target NVMeTarget - duplicateConnect bool - expectError bool - }{ - {"Valid target", NVMeTarget{HostAdr: "192.168.1.1"}, false, false}, - {"Empty target address", NVMeTarget{HostAdr: ""}, false, true}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := c.NVMeTCPConnect(tt.target, tt.duplicateConnect) - if (err != nil) != tt.expectError { - t.Errorf("NVMeTCPConnect() error = %v, expectError %v", err, tt.expectError) - } - }) - } -}