Skip to content

Commit fc4396f

Browse files
zhangyueallencloud
authored andcommitted
test: refactor the cli stop test case
Signed-off-by: zhangyue <[email protected]>
1 parent 5ad887d commit fc4396f

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

test/cli_stop_test.go

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"strings"
6-
7-
"github.com/alibaba/pouch/apis/types"
84
"github.com/alibaba/pouch/test/command"
95
"github.com/alibaba/pouch/test/environment"
106

@@ -50,37 +46,26 @@ func (suite *PouchStopSuite) TestStopWorks(c *check.C) {
5046
// test stop a stopped container
5147
command.PouchRun("stop", name).Assert(c, icmd.Success)
5248

53-
res := command.PouchRun("ps", "-a")
54-
55-
// FIXME: It's better if we use inspect to filter status.
56-
if out := res.Combined(); !strings.Contains(out, "Stopped") {
57-
c.Fatalf("unexpected output %s expected Stopped\n", out)
58-
}
49+
status, err := inspectFilter(name, ".State.Status")
50+
c.Assert(err, check.IsNil)
51+
c.Assert(status, check.Equals, "stopped")
5952

6053
// test stop container with timeout(*seconds)
6154
command.PouchRun("start", name).Assert(c, icmd.Success)
6255
command.PouchRun("stop", "-t", "3", name).Assert(c, icmd.Success)
6356

64-
res = command.PouchRun("ps", "-a")
65-
66-
output := command.PouchRun("inspect", name).Stdout()
67-
result := []types.ContainerJSON{}
68-
if err := json.Unmarshal([]byte(output), &result); err != nil {
69-
c.Errorf("failed to decode inspect output: %v", err)
70-
}
71-
c.Assert(string(result[0].State.Status), check.Equals, "stopped")
57+
status, err = inspectFilter(name, ".State.Status")
58+
c.Assert(err, check.IsNil)
59+
c.Assert(status, check.Equals, "stopped")
7260

7361
// test stop a paused container
7462
command.PouchRun("start", name).Assert(c, icmd.Success)
7563
command.PouchRun("pause", name).Assert(c, icmd.Success)
7664
command.PouchRun("stop", name).Assert(c, icmd.Success)
7765

78-
output = command.PouchRun("inspect", name).Stdout()
79-
result = []types.ContainerJSON{}
80-
if err := json.Unmarshal([]byte(output), &result); err != nil {
81-
c.Errorf("failed to decode inspect output: %v", err)
82-
}
83-
c.Assert(string(result[0].State.Status), check.Equals, "stopped")
66+
status, err = inspectFilter(name, ".State.Status")
67+
c.Assert(err, check.IsNil)
68+
c.Assert(status, check.Equals, "stopped")
8469
}
8570

8671
// TestStopInWrongWay tries to run create in wrong way.
@@ -105,24 +90,19 @@ func (suite *PouchStopSuite) TestStopMultiContainers(c *check.C) {
10590

10691
command.PouchRun("run", "-d", "-m", "300M", "--name", name1, busyboxImage, "top").Assert(c, icmd.Success)
10792
command.PouchRun("run", "-d", "-m", "300M", "--name", name2, busyboxImage, "top").Assert(c, icmd.Success)
93+
defer DelContainerForceMultyTime(c, name1)
94+
defer DelContainerForceMultyTime(c, name2)
10895

10996
command.PouchRun("stop", "-t", "3", name1, name2).Assert(c, icmd.Success)
11097

11198
// test if the container is already stopped
112-
output := command.PouchRun("inspect", name1).Stdout()
113-
result := []types.ContainerJSON{}
114-
if err := json.Unmarshal([]byte(output), &result); err != nil {
115-
c.Errorf("failed to decode inspect output: %v", err)
116-
}
117-
c.Assert(string(result[0].State.Status), check.Equals, "stopped")
118-
119-
output = command.PouchRun("inspect", name2).Stdout()
120-
result = []types.ContainerJSON{}
121-
if err := json.Unmarshal([]byte(output), &result); err != nil {
122-
c.Errorf("failed to decode inspect output: %v", err)
123-
}
124-
c.Assert(string(result[0].State.Status), check.Equals, "stopped")
99+
status, err := inspectFilter(name1, ".State.Status")
100+
c.Assert(err, check.IsNil)
101+
c.Assert(status, check.Equals, "stopped")
125102

103+
status, err = inspectFilter(name2, ".State.Status")
104+
c.Assert(err, check.IsNil)
105+
c.Assert(status, check.Equals, "stopped")
126106
}
127107

128108
// TestStopPidValue ensure stopped container's pid is 0
@@ -135,12 +115,9 @@ func (suite *PouchStopSuite) TestStopPidValue(c *check.C) {
135115
// test stop a created container
136116
command.PouchRun("stop", name).Assert(c, icmd.Success)
137117

138-
output := command.PouchRun("inspect", name).Stdout()
139-
result := []types.ContainerJSON{}
140-
if err := json.Unmarshal([]byte(output), &result); err != nil {
141-
c.Errorf("failed to decode inspect output: %v", err)
142-
}
143-
c.Assert(result[0].State.Pid, check.Equals, int64(0))
118+
pid, err := inspectFilter(name, ".State.Pid")
119+
c.Assert(err, check.IsNil)
120+
c.Assert(pid, check.Equals, "0")
144121
}
145122

146123
// TestAutoStopPidValue ensure stopped container's pid is 0
@@ -150,10 +127,7 @@ func (suite *PouchStopSuite) TestAutoStopPidValue(c *check.C) {
150127
command.PouchRun("run", "--name", name, busyboxImage, "echo", "hi").Assert(c, icmd.Success)
151128
defer DelContainerForceMultyTime(c, name)
152129

153-
output := command.PouchRun("inspect", name).Stdout()
154-
result := []types.ContainerJSON{}
155-
if err := json.Unmarshal([]byte(output), &result); err != nil {
156-
c.Errorf("failed to decode inspect output: %v", err)
157-
}
158-
c.Assert(result[0].State.Pid, check.Equals, int64(0))
130+
pid, err := inspectFilter(name, ".State.Pid")
131+
c.Assert(err, check.IsNil)
132+
c.Assert(pid, check.Equals, "0")
159133
}

test/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

8+
"github.com/alibaba/pouch/test/command"
79
"github.com/alibaba/pouch/test/environment"
810

911
"github.com/go-check/check"
@@ -95,3 +97,13 @@ func IsTLSExist() bool {
9597
}
9698
return true
9799
}
100+
101+
// inspectFilter get the string of info via inspect -f
102+
func inspectFilter(name, filter string) (string, error) {
103+
format := fmt.Sprintf("{{%s}}", filter)
104+
result := command.PouchRun("inspect", "-f", format, name)
105+
if result.Error != nil || result.ExitCode != 0 {
106+
return "", fmt.Errorf("failed to inspect container %s via filter %s: %s", name, filter, result.Combined())
107+
}
108+
return strings.TrimSpace(result.Combined()), nil
109+
}

0 commit comments

Comments
 (0)