From 3c5e99cdb509b31f9f604a7388e57e302b9bb9c7 Mon Sep 17 00:00:00 2001 From: Allen Sun Date: Wed, 25 Apr 2018 11:50:07 +0800 Subject: [PATCH] fix: make restart can restart stopped container Signed-off-by: Allen Sun --- daemon/mgr/container.go | 15 +++++++-------- test/api_container_restart_test.go | 2 +- test/cli_restart_test.go | 10 +++------- test/cli_upgrade_test.go | 2 +- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/daemon/mgr/container.go b/daemon/mgr/container.go index 9739d1ec0..98e3bab9a 100644 --- a/daemon/mgr/container.go +++ b/daemon/mgr/container.go @@ -1160,20 +1160,19 @@ func (mgr *ContainerManager) Restart(ctx context.Context, name string, timeout i c.Lock() defer c.Unlock() - if !c.IsRunning() { - return fmt.Errorf("cannot restart a non running container") - } - if timeout == 0 { timeout = c.StopTimeout() } - // stop container - if err := mgr.stop(ctx, c, timeout); err != nil { - return errors.Wrapf(err, "failed to stop container") + if c.IsRunning() { + // stop container if it is running. + if err := mgr.stop(ctx, c, timeout); err != nil { + logrus.Errorf("failed to stop container %s when restarting: %v", c.ID(), err) + return errors.Wrapf(err, fmt.Sprintf("failed to stop container %s", c.ID())) + } } - logrus.Debug("Restart: container " + c.ID() + " stopped succeeded") + logrus.Debugf("start container %s when restarting", c.ID()) // start container return mgr.start(ctx, c, "") } diff --git a/test/api_container_restart_test.go b/test/api_container_restart_test.go index c9a960b91..bfb4fe61f 100644 --- a/test/api_container_restart_test.go +++ b/test/api_container_restart_test.go @@ -54,7 +54,7 @@ func (suite *APIContainerRestartSuite) TestAPIRestartStoppedContainer(c *check.C resp, err := request.Post("/containers/"+cname+"/restart", query) c.Assert(err, check.IsNil) - CheckRespStatus(c, resp, 500) + CheckRespStatus(c, resp, 204) DelContainerForceOk(c, cname) } diff --git a/test/cli_restart_test.go b/test/cli_restart_test.go index 1bd74e088..76d3b79e9 100644 --- a/test/cli_restart_test.go +++ b/test/cli_restart_test.go @@ -34,7 +34,7 @@ func (suite *PouchRestartSuite) TearDownTest(c *check.C) { func (suite *PouchRestartSuite) TestPouchRestart(c *check.C) { name := "TestPouchRestart" - command.PouchRun("run", "-d", "--cpu-share", "20", "--name", name, busyboxImage, "top").Assert(c, icmd.Success) + command.PouchRun("run", "-d", "--cpu-share", "20", "--name", name, busyboxImage).Assert(c, icmd.Success) res := command.PouchRun("restart", "-t", "1", name) c.Assert(res.Error, check.IsNil) @@ -47,18 +47,14 @@ func (suite *PouchRestartSuite) TestPouchRestart(c *check.C) { } // TestPouchRestartStoppedContainer is to verify the correctness of restarting a stopped container. +// Pouch should be compatible with moby's API. Restarting a stopped container is allowed. func (suite *PouchRestartSuite) TestPouchRestartStoppedContainer(c *check.C) { name := "TestPouchRestartStoppedContainer" command.PouchRun("create", "--name", name, busyboxImage).Assert(c, icmd.Success) res := command.PouchRun("restart", "-t", "1", name) - c.Assert(res.Error, check.NotNil) - - expectString := "cannot restart a non running container" - if out := res.Combined(); !strings.Contains(out, expectString) { - c.Fatalf("unexpected output: %s, expected: %s", out, expectString) - } + c.Assert(res.Error, check.IsNil) command.PouchRun("rm", "-f", name).Assert(c, icmd.Success) } diff --git a/test/cli_upgrade_test.go b/test/cli_upgrade_test.go index b3c5e031d..41e155f01 100644 --- a/test/cli_upgrade_test.go +++ b/test/cli_upgrade_test.go @@ -82,7 +82,7 @@ func (suite *PouchUpgradeSuite) TestPouchUpgradeStoppedContainer(c *check.C) { c.Assert(res.Error, check.IsNil) if out := res.Combined(); !strings.Contains(out, name) { - c.Fatal("unexpected output: %s, expected %s", out, name) + c.Fatalf("unexpected output: %s, expected %s", out, name) } command.PouchRun("start", name).Assert(c, icmd.Success)