Skip to content

Commit

Permalink
bugfix: we should set Running flag to true when started container
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <zirenwan@gmail.com>
  • Loading branch information
HusterWan committed Jun 28, 2018
1 parent b5b25ae commit f1eeb96
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions daemon/mgr/container_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (c *Container) SetStatusRunning(pid int64) {
c.State.StartedAt = time.Now().UTC().Format(utils.TimeLayout)
c.State.Pid = pid
c.State.ExitCode = 0
c.setStatusFlags(types.StatusRunning)
}

// SetStatusStopped sets a container to be status stopped.
Expand All @@ -107,6 +108,7 @@ func (c *Container) SetStatusStopped(exitCode int64, errMsg string) {
c.State.Pid = -1
c.State.ExitCode = exitCode
c.State.Error = errMsg
c.setStatusFlags(types.StatusStopped)
}

// SetStatusExited sets a container to be status exited.
Expand All @@ -118,13 +120,15 @@ func (c *Container) SetStatusExited(exitCode int64, errMsg string) {
c.State.Pid = -1
c.State.ExitCode = exitCode
c.State.Error = errMsg
c.setStatusFlags(types.StatusExited)
}

// SetStatusPaused sets a container to be status paused.
func (c *Container) SetStatusPaused() {
c.Lock()
defer c.Unlock()
c.State.Status = types.StatusPaused
c.setStatusFlags(types.StatusPaused)
}

// SetStatusUnpaused sets a container to be status running.
Expand All @@ -133,4 +137,33 @@ func (c *Container) SetStatusUnpaused() {
c.Lock()
defer c.Unlock()
c.State.Status = types.StatusRunning
c.setStatusFlags(types.StatusRunning)
}

// Notes(ziren): i still feel uncomfortable for a function hasing no return
// setStatusFlags set the specified status flag to true, and unset others
func (c *Container) setStatusFlags(status types.Status) {
statusFlags := map[types.Status]bool{
types.StatusDead: false,
types.StatusRunning: false,
types.StatusPaused: false,
types.StatusRestarting: false,
}

if _, exists := statusFlags[status]; exists {
statusFlags[status] = true
}

for k, v := range statusFlags {
switch k {
case types.StatusDead:
c.State.Dead = v
case types.StatusPaused:
c.State.Paused = v
case types.StatusRunning:
c.State.Running = v
case types.StatusRestarting:
c.State.Restarting = v
}
}
}
19 changes: 19 additions & 0 deletions test/cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,22 @@ func (suite *PouchRunMemorySuite) TestRunWithShm(c *check.C) {

util.PartialEqual(res.Stdout(), "1048576")
}

// TestRunSetRunningFlag is to verfy whether set Running Flag in ContainerState
// when started a container
func (suite *PouchRunSuite) TestRunSetRunningFlag(c *check.C) {
cname := "TestRunSetRunningFlag"
res := command.PouchRun("run", "-d", "--name", cname, busyboxImage, "top")
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

// test if the value is in inspect result
res = command.PouchRun("inspect", cname)
res.Assert(c, icmd.Success)

result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(res.Stdout()), &result); err != nil {
c.Errorf("failed to decode inspect output: %v", err)
}
c.Assert(result[0].State.Running, check.Equals, true)
}

0 comments on commit f1eeb96

Please sign in to comment.