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

bugfix: we should set Running flag to true when started container #1604

Merged
merged 1 commit into from
Jun 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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

I just wonder why use a function instead of a single line like c.State.Running=true ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we have 4 flags : Dead, Paused, Restarting and Running , the flags are exclusive,so when set one flag, we should unset others. WDYT? @Ace-Tang

}

// 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)
}