Skip to content

Commit 6e8ce23

Browse files
committed
Merge branch '280-destroy-clone' into 'master'
fix: destroy a clone even if the clone container does not exist (#280) Closes #280 See merge request postgres-ai/database-lab!328
2 parents f1d065a + a289270 commit 6e8ce23

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,7 @@ github.com/stefanberger/go-pkcs11uri v0.0.0-20201008174630-78d3cae3a980/go.mod h
671671
github.com/stretchr/objx v0.0.0-20180129172003-8a3f7159479f/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
672672
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
673673
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
674+
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
674675
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
675676
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
676677
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

pkg/services/provision/databases/postgres/postgres.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ func Stop(r runners.Runner, p *resources.Pool, name string) error {
124124
log.Dbg("Stopping Postgres container...")
125125

126126
if _, err := docker.RemoveContainer(r, name); err != nil {
127-
return errors.Wrap(err, "failed to remove container")
127+
const errorPrefix = "Error: No such container:"
128+
129+
if e, ok := err.(runners.RunnerError); ok && !strings.HasPrefix(e.Stderr, errorPrefix) {
130+
return errors.Wrap(err, "failed to remove container")
131+
}
132+
133+
log.Msg("docker container was not found, ignore", err)
134+
128135
}
129136

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

0 commit comments

Comments
 (0)