refactor: move mock container driver from test file

This commit is contained in:
Marc 2024-08-18 00:33:03 -04:00
parent 673c20cc30
commit c876477891
Signed by: marc
GPG key ID: 048E042F22B5DC79
2 changed files with 61 additions and 60 deletions

View file

@ -0,0 +1,61 @@
package runner
type MockCall struct {
fname string
args []string
}
type MockDriver struct {
calls map[string][]MockCall
mockResult *CommandResult
}
func NewMockDriver() MockDriver {
return MockDriver{
calls: map[string][]MockCall{},
}
}
func (d *MockDriver) WithCommandResult(c *CommandResult) {
d.mockResult = c
}
func (d *MockDriver) Pull(uri string) error {
if _, init := d.calls["Pull"]; !init {
d.calls["Pull"] = []MockCall{}
}
d.calls["Pull"] = append(d.calls["Pull"], MockCall{fname: "Pull", args: []string{uri}})
return nil
}
func (d *MockDriver) Start(uri string, containerName string) error {
if _, init := d.calls["Start"]; !init {
d.calls["Start"] = []MockCall{}
}
d.calls["Start"] = append(d.calls["Start"], MockCall{fname: "Start", args: []string{uri, containerName}})
return nil
}
func (d *MockDriver) Stop(uri string) error {
if _, init := d.calls["Stop"]; !init {
d.calls["Stop"] = []MockCall{}
}
d.calls["Stop"] = append(d.calls["Stop"], MockCall{fname: "Stop", args: []string{uri}})
return nil
}
func (d *MockDriver) Exec(containerName string, command string, cwd string) CommandResult {
if _, init := d.calls["Exec"]; !init {
d.calls["Exec"] = []MockCall{}
}
d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: []string{containerName, command, cwd}})
if d.mockResult != nil {
return *d.mockResult
}
return CommandResult{}
}

View file

@ -8,70 +8,10 @@ import (
"testing" "testing"
) )
type MockCall struct {
fname string
args []string
}
type MockDriver struct {
calls map[string][]MockCall
mockResult *CommandResult
}
func init() { func init() {
logger.ConfigureLogger() logger.ConfigureLogger()
} }
func NewMockDriver() MockDriver {
return MockDriver{
calls: map[string][]MockCall{},
}
}
func (d *MockDriver) WithCommandResult(c *CommandResult) {
d.mockResult = c
}
func (d *MockDriver) Pull(uri string) error {
if _, init := d.calls["Pull"]; !init {
d.calls["Pull"] = []MockCall{}
}
d.calls["Pull"] = append(d.calls["Pull"], MockCall{fname: "Pull", args: []string{uri}})
return nil
}
func (d *MockDriver) Start(uri string, containerName string) error {
if _, init := d.calls["Start"]; !init {
d.calls["Start"] = []MockCall{}
}
d.calls["Start"] = append(d.calls["Start"], MockCall{fname: "Start", args: []string{uri, containerName}})
return nil
}
func (d *MockDriver) Stop(uri string) error {
if _, init := d.calls["Stop"]; !init {
d.calls["Stop"] = []MockCall{}
}
d.calls["Stop"] = append(d.calls["Stop"], MockCall{fname: "Stop", args: []string{uri}})
return nil
}
func (d *MockDriver) Exec(containerName string, command string, cwd string) CommandResult {
if _, init := d.calls["Exec"]; !init {
d.calls["Exec"] = []MockCall{}
}
d.calls["Exec"] = append(d.calls["Exec"], MockCall{fname: "Exec", args: []string{containerName, command, cwd}})
if d.mockResult != nil {
return *d.mockResult
}
return CommandResult{}
}
func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) { func TestRunnerRunCommandInContainerReturnsErrorFromDriver(t *testing.T) {
mockDriver := NewMockDriver() mockDriver := NewMockDriver()
mockDriver.WithCommandResult(&CommandResult{ExitCode: 0, Error: errors.New("test")}) mockDriver.WithCommandResult(&CommandResult{ExitCode: 0, Error: errors.New("test")})