Skip to content

Commit

Permalink
fix: make restart can restart stopped container
Browse files Browse the repository at this point in the history
Signed-off-by: Allen Sun <allensun.shl@alibaba-inc.com>
  • Loading branch information
allencloud committed Apr 25, 2018
1 parent 0713c83 commit d982ca8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
15 changes: 7 additions & 8 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}
Expand Down
10 changes: 3 additions & 7 deletions test/cli_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion test/cli_upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d982ca8

Please sign in to comment.