-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
Check failure on line 1 in gonvme_tcp_fc_test.go GitHub Actions / golangci-lint
|
||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// 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] == "which" && args[1] == DefaultNVMeCommand { | ||
// Simulate finding the nvme command | ||
println("/usr/bin/nvme") | ||
} else { | ||
// Simulate not finding the nvme command | ||
os.Exit(1) | ||
} | ||
os.Exit(0) | ||
} | ||
|
||
func TestNewNVMe(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 | ||
|
||
tests := []struct { | ||
name string | ||
opts map[string]string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "nvme command found", | ||
opts: map[string]string{}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "nvme command not found", | ||
opts: map[string]string{ChrootDirectory: "/nonexistent"}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
nvme, err := NewNVMe(tt.opts) | ||
if (err != nil) != tt.expectError { | ||
t.Errorf("NewNVMe() error = %v, expectError %v", err, tt.expectError) | ||
return | ||
} | ||
if !tt.expectError && nvme.NVMeCommand != "/usr/bin/nvme" { | ||
t.Errorf("Expected NVMeCommand to be /usr/bin/nvme, got %s", nvme.NVMeCommand) | ||
} | ||
}) | ||
} | ||
} |