Skip to content

Commit

Permalink
bugfix: can't stop a paused container
Browse files Browse the repository at this point in the history
can't stop a paused container
after restart pouch, can't unpause a paused container

Signed-off-by: Eric Li <lcy041536@gmail.com>
  • Loading branch information
shaloulcy committed May 4, 2018
1 parent cde0bd7 commit 45e24b7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
7 changes: 4 additions & 3 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,12 @@ func (mgr *ContainerManager) Restore(ctx context.Context) error {
// put container into cache.
mgr.cache.Put(containerMeta.ID, &Container{meta: containerMeta})

if containerMeta.State.Status != types.StatusRunning {
if containerMeta.State.Status != types.StatusRunning &&
containerMeta.State.Status != types.StatusPaused {
return nil
}

// recover the running container.
// recover the running or paused container.
io, err := mgr.openContainerIO(containerMeta.ID, containerMeta.Config.OpenStdin)
if err != nil {
logrus.Errorf("failed to recover container: %s, %v", containerMeta.ID, err)
Expand Down Expand Up @@ -695,7 +696,7 @@ func (mgr *ContainerManager) Stop(ctx context.Context, name string, timeout int6
c.Lock()
defer c.Unlock()

if !c.IsRunning() {
if !c.IsRunning() && !c.IsPaused() {
// stopping a non-running container is valid.
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions test/api_container_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"net/url"

"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/test/environment"
"github.com/alibaba/pouch/test/request"

Expand Down Expand Up @@ -67,3 +68,31 @@ func (suite *APIContainerStopSuite) TestInvalidParam(c *check.C) {
//TODO
// 1. invalid timeout value
}

// TestStopPausedContainer tests stop a paused container.
func (suite *APIContainerStopSuite) TestStopPausedContainer(c *check.C) {
cname := "TestStopPausedContainer"

CreateBusyboxContainerOk(c, cname)
StartContainerOk(c, cname)

// pause the container
PauseContainerOk(c, cname)

// stop the container
resp, err := request.Post("/containers/" + cname + "/stop")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 204)

// check the container status
resp, err = request.Get("/containers/" + cname + "/json")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 200)
defer resp.Body.Close()

got := types.ContainerJSON{}
err = request.DecodeBody(&got, resp.Body)
c.Assert(string(got.State.Status), check.Equals, "stopped")

DelContainerForceOk(c, cname)
}
64 changes: 63 additions & 1 deletion test/z_cli_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
dcfg, err := StartDefaultDaemonDebug()
// Start a test daemon with test args.
if err != nil {
c.Skip("deamon start failed.")
c.Skip("daemon start failed.")
}
// Must kill it, as we may loose the pid in next call.
defer dcfg.KillDaemon()
Expand Down Expand Up @@ -268,6 +268,68 @@ func (suite *PouchDaemonSuite) TestDaemonRestart(c *check.C) {
c.Assert(string(result[0].State.Status), check.Equals, "running")
}

// TestDaemonRestartWithPausedContainer tests daemon with paused container.
func (suite *PouchDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
dcfg, err := StartDefaultDaemonDebug()
//Start a test daemon with test args.
if err != nil {
c.Skip("daemon start failed")
}
defer dcfg.KillDaemon()

{
result := RunWithSpecifiedDaemon(dcfg, "pull", busyboxImage)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pull image failed, err: %v", result)
}
}

cname := TestDaemonRestartWithPausedContainer
{
result := RunWithSpecifiedDaemon(dcfg, "run", "-d", "--name", cname,
"-p", "1234:80", busyboxImage, "top")
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("run container failed, err: %v", result)
}

// pause the container
result = RunWithSpecifiedDaemon(dcfg, "pause", cname)
if result.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("pause container failed, err: %v", result)
}
}
defer DelContainerForceMultyTime(c, cname)

// restart daemon
err = RestartDaemon(dcfg)
c.Assert(err, check.IsNil)

// test if the container is paused.
output := RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
result := []types.ContainerJSON{}
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Fatalf("failed to decode inspect output: %v", err)
}
c.Assert(string(result[0].State.Status), check.Equals, "paused")

// unpause the container
output = RunWithSpecifiedDaemon(dcfg, "unpause", cname).Stdout()
if output.ExitCode != 0 {
dcfg.DumpLog()
c.Fatalf("unpause container failed, err: %v", result)
}

//test if the container is running
output = RunWithSpecifiedDaemon(dcfg, "inspect", cname).Stdout()
if err := json.Unmarshal([]byte(output), &result); err != nil {
c.Fatalf("failed to decode inspect output: %v", err)
}
c.Assert(string(result[0].State.Status), check.Equals, "running")
}

// TestDaemonLabel tests start daemon with label works.
func (suite *PouchDaemonSuite) TestDaemonLabel(c *check.C) {
dcfg, err := StartDefaultDaemonDebug("--label", "a=b")
Expand Down

0 comments on commit 45e24b7

Please sign in to comment.