diff --git a/test/cli_stop_test.go b/test/cli_stop_test.go index 24ebb4bc1..b06dfaa45 100644 --- a/test/cli_stop_test.go +++ b/test/cli_stop_test.go @@ -1,10 +1,6 @@ package main import ( - "encoding/json" - "strings" - - "github.com/alibaba/pouch/apis/types" "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/environment" @@ -50,37 +46,26 @@ func (suite *PouchStopSuite) TestStopWorks(c *check.C) { // test stop a stopped container command.PouchRun("stop", name).Assert(c, icmd.Success) - res := command.PouchRun("ps", "-a") - - // FIXME: It's better if we use inspect to filter status. - if out := res.Combined(); !strings.Contains(out, "Stopped") { - c.Fatalf("unexpected output %s expected Stopped\n", out) - } + status, err := inspectFilter(name, ".State.Status") + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, "stopped") // test stop container with timeout(*seconds) command.PouchRun("start", name).Assert(c, icmd.Success) command.PouchRun("stop", "-t", "3", name).Assert(c, icmd.Success) - res = command.PouchRun("ps", "-a") - - output := command.PouchRun("inspect", name).Stdout() - result := []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(string(result[0].State.Status), check.Equals, "stopped") + status, err = inspectFilter(name, ".State.Status") + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, "stopped") // test stop a paused container command.PouchRun("start", name).Assert(c, icmd.Success) command.PouchRun("pause", name).Assert(c, icmd.Success) command.PouchRun("stop", name).Assert(c, icmd.Success) - output = command.PouchRun("inspect", name).Stdout() - result = []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(string(result[0].State.Status), check.Equals, "stopped") + status, err = inspectFilter(name, ".State.Status") + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, "stopped") } // TestStopInWrongWay tries to run create in wrong way. @@ -105,24 +90,19 @@ func (suite *PouchStopSuite) TestStopMultiContainers(c *check.C) { command.PouchRun("run", "-d", "-m", "300M", "--name", name1, busyboxImage, "top").Assert(c, icmd.Success) command.PouchRun("run", "-d", "-m", "300M", "--name", name2, busyboxImage, "top").Assert(c, icmd.Success) + defer DelContainerForceMultyTime(c, name1) + defer DelContainerForceMultyTime(c, name2) command.PouchRun("stop", "-t", "3", name1, name2).Assert(c, icmd.Success) // test if the container is already stopped - output := command.PouchRun("inspect", name1).Stdout() - result := []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(string(result[0].State.Status), check.Equals, "stopped") - - output = command.PouchRun("inspect", name2).Stdout() - result = []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(string(result[0].State.Status), check.Equals, "stopped") + status, err := inspectFilter(name1, ".State.Status") + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, "stopped") + status, err = inspectFilter(name2, ".State.Status") + c.Assert(err, check.IsNil) + c.Assert(status, check.Equals, "stopped") } // TestStopPidValue ensure stopped container's pid is 0 @@ -135,12 +115,9 @@ func (suite *PouchStopSuite) TestStopPidValue(c *check.C) { // test stop a created container command.PouchRun("stop", name).Assert(c, icmd.Success) - output := command.PouchRun("inspect", name).Stdout() - result := []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(result[0].State.Pid, check.Equals, int64(0)) + pid, err := inspectFilter(name, ".State.Pid") + c.Assert(err, check.IsNil) + c.Assert(pid, check.Equals, "0") } // TestAutoStopPidValue ensure stopped container's pid is 0 @@ -150,10 +127,7 @@ func (suite *PouchStopSuite) TestAutoStopPidValue(c *check.C) { command.PouchRun("run", "--name", name, busyboxImage, "echo", "hi").Assert(c, icmd.Success) defer DelContainerForceMultyTime(c, name) - output := command.PouchRun("inspect", name).Stdout() - result := []types.ContainerJSON{} - if err := json.Unmarshal([]byte(output), &result); err != nil { - c.Errorf("failed to decode inspect output: %v", err) - } - c.Assert(result[0].State.Pid, check.Equals, int64(0)) + pid, err := inspectFilter(name, ".State.Pid") + c.Assert(err, check.IsNil) + c.Assert(pid, check.Equals, "0") } diff --git a/test/utils.go b/test/utils.go index 7b987ab45..6840a8e72 100644 --- a/test/utils.go +++ b/test/utils.go @@ -3,7 +3,9 @@ package main import ( "fmt" "os" + "strings" + "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/environment" "github.com/go-check/check" @@ -95,3 +97,13 @@ func IsTLSExist() bool { } return true } + +// inspectFilter get the string of info via inspect -f +func inspectFilter(name, filter string) (string, error) { + format := fmt.Sprintf("{{%s}}", filter) + result := command.PouchRun("inspect", "-f", format, name) + if result.Error != nil || result.ExitCode != 0 { + return "", fmt.Errorf("failed to inspect container %s via filter %s: %s", name, filter, result.Combined()) + } + return strings.TrimSpace(result.Combined()), nil +}