From c876477891d7e6978b30a82e6ac2eb97ae07bc25 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 18 Aug 2024 00:33:03 -0400 Subject: [PATCH] refactor: move mock container driver from test file --- internal/runner/mock_container_driver.go | 61 ++++++++++++++++++++++++ internal/runner/runner_flow_test.go | 60 ----------------------- 2 files changed, 61 insertions(+), 60 deletions(-) create mode 100644 internal/runner/mock_container_driver.go diff --git a/internal/runner/mock_container_driver.go b/internal/runner/mock_container_driver.go new file mode 100644 index 0000000..30a7337 --- /dev/null +++ b/internal/runner/mock_container_driver.go @@ -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{} +} diff --git a/internal/runner/runner_flow_test.go b/internal/runner/runner_flow_test.go index d225bda..164d585 100644 --- a/internal/runner/runner_flow_test.go +++ b/internal/runner/runner_flow_test.go @@ -8,70 +8,10 @@ import ( "testing" ) -type MockCall struct { - fname string - args []string -} - -type MockDriver struct { - calls map[string][]MockCall - mockResult *CommandResult -} - func init() { 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) { mockDriver := NewMockDriver() mockDriver.WithCommandResult(&CommandResult{ExitCode: 0, Error: errors.New("test")})