Skip to content

Commit

Permalink
fix: make container state in inpsect API correct
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <allensun.shl@alibaba-inc.com>
  • Loading branch information
allencloud committed Sep 20, 2018
1 parent 73ce641 commit 61f2486
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 19 deletions.
10 changes: 9 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ definitions:

ContainerState:
type: "object"
required: [StartedAt, FinishedAt]
required: [StartedAt, FinishedAt, Pid, ExitCode, Error, OOMKilled, Dead, Paused, Restarting, Running, Status]
properties:
Status:
$ref: "#/definitions/Status"
Expand All @@ -3396,27 +3396,35 @@ definitions:
Use the `Status` field instead to determine if a container's state is "running".
type: "boolean"
x-nullable: false
Paused:
description: "Whether this container is paused."
type: "boolean"
x-nullable: false
Restarting:
description: "Whether this container is restarting."
type: "boolean"
x-nullable: false
OOMKilled:
description: "Whether this container has been killed because it ran out of memory."
type: "boolean"
x-nullable: false
Dead:
description: "Whether this container is dead."
type: "boolean"
x-nullable: false
Pid:
x-nullable: false
description: "The process ID of this container"
type: "integer"
ExitCode:
x-nullable: false
description: "The last exit code of this container"
type: "integer"
Error:
description: "The error message of this container"
type: "string"
x-nullable: false
StartedAt:
description: "The time when this container was last started."
type: "string"
Expand Down
143 changes: 130 additions & 13 deletions apis/types/container_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/types/exec_create_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 35 additions & 4 deletions test/cli_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (suite *PouchInspectSuite) TearDownTest(c *check.C) {
func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
name := "inspect-format-print"

res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage)
res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

Expand All @@ -61,7 +61,7 @@ func (suite *PouchInspectSuite) TestInspectFormat(c *check.C) {
func (suite *PouchInspectSuite) TestInspectWrongFormat(c *check.C) {
name := "inspect-wrong-format-print"

res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage)
res := command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

Expand All @@ -82,7 +82,7 @@ func (suite *PouchInspectSuite) TestMultiInspect(c *check.C) {
}
setUp := func() {
for _, name := range names {
command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage).Assert(c, icmd.Success)
command.PouchRun("create", "-m", "30M", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
}
}
cleanUp := func() {
Expand All @@ -100,7 +100,6 @@ func (suite *PouchInspectSuite) TestMultiInspect(c *check.C) {

// TestMultiInspect is to verify inspect command with multiple args.
func (suite *PouchInspectSuite) TestMultiInspectErrors(c *check.C) {

errorCases := []struct {
containers []string
args []string
Expand Down Expand Up @@ -140,3 +139,35 @@ func (suite *PouchInspectSuite) TestMultiInspectErrors(c *check.C) {
c.Assert(output, check.Equals, errCase.expectedOutput)
}
}

//
func (suite *PouchInspectSuite) TestContainerInspectState(c *check.C) {
// 1. /bin/sh will exit immediately
name := "TestContainerInspectStateAutoExit"
res := command.PouchRun("run", "--name", name, busyboxImage, "/bin/sh")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)

output := command.PouchRun("inspect", "-f", "{{.State.Pid}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "0")
output = command.PouchRun("inspect", "-f", "{{.State.ExitCode}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "0")
output = command.PouchRun("inspect", "-f", "{{.State.Status}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "exited")

// 2. stop a container and check the exit code and
name = "TestContainerInspectStateManullyStop"
res = command.PouchRun("run", "-d", "--name", name, busyboxImage, "top")
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
// stop container
res = command.PouchRun("stop", "-t", "0", name)
res.Assert(c, icmd.Success)

output = command.PouchRun("inspect", "-f", "{{.State.Pid}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "0")
output = command.PouchRun("inspect", "-f", "{{.State.ExitCode}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Not(check.Equals), "0")
output = command.PouchRun("inspect", "-f", "{{.State.Status}}", name).Stdout()
c.Assert(strings.TrimSpace(output), check.Equals, "stopped")
}

0 comments on commit 61f2486

Please sign in to comment.