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

fix: make restart can restart stopped container #1208

Merged
merged 1 commit into from
Apr 25, 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
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
2 changes: 1 addition & 1 deletion test/api_container_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
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