Skip to content

Commit

Permalink
Ad Unit Test for the changes in code
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Nov 14, 2024
1 parent bfb352f commit 803993f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 107 deletions.
41 changes: 41 additions & 0 deletions gonvme_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"fmt"
"strconv"

log "github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -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) {

Check failure on line 67 in gonvme_mock.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unused-parameter: parameter 'name' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 67 in gonvme_mock.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
// 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{
Expand All @@ -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
}

Expand Down
107 changes: 0 additions & 107 deletions gonvme_tcp_fc_test.go

This file was deleted.

0 comments on commit 803993f

Please sign in to comment.