From e52903a6bd89584003e56f5bc88df121eb3fb919 Mon Sep 17 00:00:00 2001 From: Michael Wan Date: Mon, 3 Sep 2018 02:39:36 -0400 Subject: [PATCH] bugfix: set pid of stopped container to 0 Signed-off-by: Michael Wan --- daemon/mgr/container_state.go | 4 ++-- test/cli_stop_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/daemon/mgr/container_state.go b/daemon/mgr/container_state.go index 8f1cfc12e..394a4221a 100644 --- a/daemon/mgr/container_state.go +++ b/daemon/mgr/container_state.go @@ -105,7 +105,7 @@ func (c *Container) SetStatusStopped(exitCode int64, errMsg string) { defer c.Unlock() c.State.Status = types.StatusStopped c.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout) - c.State.Pid = -1 + c.State.Pid = 0 c.State.ExitCode = exitCode c.State.Error = errMsg c.setStatusFlags(types.StatusStopped) @@ -117,7 +117,7 @@ func (c *Container) SetStatusExited(exitCode int64, errMsg string) { defer c.Unlock() c.State.Status = types.StatusExited c.State.FinishedAt = time.Now().UTC().Format(utils.TimeLayout) - c.State.Pid = -1 + c.State.Pid = 0 c.State.ExitCode = exitCode c.State.Error = errMsg c.setStatusFlags(types.StatusExited) diff --git a/test/cli_stop_test.go b/test/cli_stop_test.go index 3d3aba08b..0d9b9551b 100644 --- a/test/cli_stop_test.go +++ b/test/cli_stop_test.go @@ -126,3 +126,36 @@ func (suite *PouchStopSuite) TestStopMultiContainers(c *check.C) { c.Assert(string(result[0].State.Status), check.Equals, "stopped") } + +// TestStopPidValue ensure stopped container's pid is 0 +func (suite *PouchStopSuite) TestStopPidValue(c *check.C) { + name := "test-stop-pid-value" + + command.PouchRun("run", "-d", "--name", name, busyboxImage, "top").Assert(c, icmd.Success) + defer DelContainerForceMultyTime(c, name) + + // 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)) +} + +// TestAutoStopPidValue ensure stopped container's pid is 0 +func (suite *PouchStopSuite) TestAutoStopPidValue(c *check.C) { + name := "test-auto-stop-pid-value" + + 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)) +}