Skip to content

Commit

Permalink
test: refactor the cli stop test case
Browse files Browse the repository at this point in the history
Signed-off-by: zhangyue <[email protected]>
  • Loading branch information
zhangyue committed Oct 28, 2018
1 parent 72a1ea5 commit 799051d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 49 deletions.
72 changes: 23 additions & 49 deletions test/cli_stop_test.go
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
}
12 changes: 12 additions & 0 deletions test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 799051d

Please sign in to comment.