-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathpostgres_test.go
74 lines (63 loc) · 1.36 KB
/
postgres_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package postgres
import (
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
"gitlab.com/postgres-ai/database-lab/v3/internal/provision/runners"
)
type MockRunner struct {
mock.Mock
output string
err error
}
func (m *MockRunner) Run(cmd string, _ ...bool) (string, error) {
m.Called(cmd)
err := m.err
if m.err != nil {
err = runners.NewRunnerError(cmd, m.err.Error(), m.err)
}
return m.output, err
}
func TestRemoveContainers(t *testing.T) {
p := &resources.Pool{}
testCases := []struct {
output string
err error
}{
{
output: "",
err: nil,
},
{
err: runners.RunnerError{Msg: "test fail"},
output: "Unknown error",
},
{
err: nil,
output: "Error: No such container:",
},
}
for _, tc := range testCases {
runner := &MockRunner{
output: tc.output,
err: tc.err,
}
runner.On("Run",
mock.MatchedBy(
func(cmd string) bool {
return strings.HasPrefix(cmd, "docker container rm --force --volumes ")
})).
Return("", tc.err).
On("Run",
mock.MatchedBy(
func(cmd string) bool {
return strings.HasPrefix(cmd, "rm -rf ")
})).
Return("", nil)
err := Stop(runner, p, "test_clone")
assert.Equal(t, tc.err, errors.Cause(err))
}
}