refactor: move mock container driver from test file
This commit is contained in:
parent
673c20cc30
commit
c876477891
2 changed files with 61 additions and 60 deletions
61
internal/runner/mock_container_driver.go
Normal file
61
internal/runner/mock_container_driver.go
Normal 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{}
|
||||
}
|
|
@ -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")})
|
||||
|
|
Loading…
Reference in a new issue