11package main
22
33import (
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}
0 commit comments