Skip to content

Commit

Permalink
Update the command
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikk92 committed Nov 13, 2024
1 parent 783aca5 commit 07c5008
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions gonvme_tcp_fc_test.go
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

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

Check failure on line 1 in gonvme_tcp_fc_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package main; expected package gonvme (typecheck)

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)
}
})
}
}

0 comments on commit 07c5008

Please sign in to comment.