Skip to content

Commit f46304e

Browse files
committed
Add ps_command to be executed at clean up
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 46a3b4d commit f46304e

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

commands/compose-ref.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: "compose-ref"
22
command: "compose-ref"
3+
ps_command: "docker ps -aq"
34
global_opts:
45
- name: "-f"
56
value: "compose.yaml"

commands/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: "docker-composeV1"
22
command: "docker-compose"
3+
ps_command: "docker ps -aq"
34
global_opts:
45
- name: "-f"
56
value: "compose.yaml"

tests_helper.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"io/ioutil"
76
"net/http"
@@ -10,8 +9,6 @@ import (
109
"strings"
1110
"testing"
1211

13-
"github.com/docker/docker/api/types"
14-
"github.com/docker/docker/client"
1512
"gopkg.in/yaml.v2"
1613
"gotest.tools/v3/assert"
1714
"gotest.tools/v3/icmd"
@@ -25,6 +22,7 @@ const (
2522
type Config struct {
2623
Name string `yaml:"name"`
2724
Command string `yaml:"command"`
25+
PsCommand string `yaml:"ps_command"`
2826
GlobalOpts []Opt `yaml:"global_opts,omitempty"`
2927
Up Verb `yaml:"up,omitempty"`
3028
Down Verb `yaml:"down,omitempty"`
@@ -61,7 +59,7 @@ func (h TestHelper) TestUpDown(fun func()) {
6159
h.executeUp(c)
6260
fun()
6361
h.executeDown(c)
64-
h.checkDown()
62+
h.checkCleanUp(c)
6563
})
6664
}
6765
}
@@ -156,14 +154,17 @@ func (h TestHelper) listFiles(dir string) []string {
156154
return configFiles
157155
}
158156

159-
func (h TestHelper) checkDown() {
160-
cli, err := client.NewEnvClient()
161-
assert.NilError(h.T, err)
162-
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
163-
All: true,
164-
})
165-
assert.NilError(h.T, err)
166-
assert.Assert(h.T, len(containers) == 0)
157+
func (h TestHelper) checkCleanUp(c *Config) {
158+
command := strings.Split(c.PsCommand, " ")
159+
cmd := icmd.Command(command[0], command[1:]...)
160+
ret := icmd.RunCmd(cmd).Assert(h.T, icmd.Success)
161+
out := strings.Trim(ret.Stdout(), "\n")
162+
nLines := len(strings.Split(out, "\n")) - 1
163+
assert.Check(
164+
h.T,
165+
0 == nLines,
166+
"Problem checking containers' state. "+
167+
"There shouldn't be any containers before or after a test.")
167168
}
168169

169170
func (h TestHelper) getHttpBody(address string) string {

0 commit comments

Comments
 (0)