Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor the cli stop test #2372

Merged
merged 1 commit into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

@allencloud allencloud Oct 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This encapsulated function looks so good to me. 😄 🌹 🍻
How about adding a comment for this function to tell others that, no matter what kind of filter input, the result will be translated into a type of string. This is different from the original way that using unmarshal way will get the real type of construct filed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

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
}