Skip to content

Commit

Permalink
MFW-4957: pTest: Allow restd to factory reset without reboot (#392)
Browse files Browse the repository at this point in the history
* Moving executor to golang-shared

* implementing golang-shared as mocks

version: minor
  • Loading branch information
acooke-arista authored May 3, 2024
1 parent 4867269 commit c2c0551
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
46 changes: 46 additions & 0 deletions testing/mocks/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mocks

import (
"os/exec"

"github.com/stretchr/testify/assert"
"github.com/untangle/golang-shared/util"
)

// MockExecutorNoErr is a struct used for tests, where Run, Output, and CombinedOutput all return with no errors
type MockExecutorNoErr struct{}

// type enforcement
var _ util.ExecutorInterface = (*MockExecutorNoErr)(nil)

func (m *MockExecutorNoErr) Run(cmd *exec.Cmd) error {
return nil
}

func (*MockExecutorNoErr) Output(cmd *exec.Cmd) ([]byte, error) {
return []byte{}, nil
}

func (*MockExecutorNoErr) CombinedOutput(cmd *exec.Cmd) ([]byte, error) {
return []byte{}, nil
}

// MockExecutorRunErr is a struct used for tests where Run returns an error, but Output and CombinedOutput do not
type MockExecutorRunErr struct{ MockExecutorNoErr }

// type enforcement
var _ util.ExecutorInterface = (*MockExecutorRunErr)(nil)

func (*MockExecutorRunErr) Run(cmd *exec.Cmd) error {
return assert.AnError
}

// MockExecutorCombinedOutputErr is a struct used for tetss where CombinedOutput returns an error, but Run and Output do not
type MockExecutorCombinedOutputErr struct{ MockExecutorNoErr }

// type enforcement
var _ util.ExecutorInterface = (*MockExecutorCombinedOutputErr)(nil)

func (*MockExecutorCombinedOutputErr) CombinedOutput(cmd *exec.Cmd) ([]byte, error) {
return []byte{}, assert.AnError
}
32 changes: 32 additions & 0 deletions util/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

import (
"os/exec"
)

// ExecutorInterface allows us to mock calls to exec.Cmd
type ExecutorInterface interface {
Run(cmd *exec.Cmd) error
Output(cmd *exec.Cmd) ([]byte, error)
CombinedOutput(cmd *exec.Cmd) ([]byte, error)
}

// Executor is the struct used by most of the code: handlerResourceProvider, dynamic_lists, and system
type Executor struct{}

var _ ExecutorInterface = (*Executor)(nil) // type enforcement

// Run will run the passed cmd command in the terminal
func (*Executor) Run(cmd *exec.Cmd) error {
return cmd.Run()
}

// Output runs the cmd command in the terminal and returns its standard output
func (*Executor) Output(cmd *exec.Cmd) ([]byte, error) {
return cmd.Output()
}

// CombinedOutput runs the cmd command in the terminal and returns its combined standard output and standard error
func (*Executor) CombinedOutput(cmd *exec.Cmd) ([]byte, error) {
return cmd.CombinedOutput()
}

0 comments on commit c2c0551

Please sign in to comment.