Skip to content

Commit

Permalink
bugfix: fix start container twice error
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <[email protected]>
  • Loading branch information
HusterWan committed Sep 11, 2018
1 parent a1afa11 commit a0171dc
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions apis/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func HandleErrorResponse(w http.ResponseWriter, err error) {
code = http.StatusBadRequest
} else if errtypes.IsAlreadyExisted(err) {
code = http.StatusConflict
} else if errtypes.IsNotModified(err) {
code = http.StatusNotModified
}

w.Header().Set("Content-Type", "application/json")
Expand Down
2 changes: 2 additions & 0 deletions apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ paths:
responses:
204:
description: "no error"
304:
description: "container already started"
404:
$ref: "#/responses/404ErrorResponse"
409:
Expand Down
5 changes: 5 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ func (mgr *ContainerManager) Start(ctx context.Context, id string, options *type
return err
}

// check if container's status is running
if c.IsRunning() {
return errors.Wrapf(errtypes.ErrNotModified, "container already started")
}

return mgr.start(ctx, c, options)
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/errtypes/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var (

// ErrVolumeNotFound represents that no such volume.
ErrVolumeNotFound = errorType{codeNotFound, "no such volume"}

// ErrNotModified represents that the resource is not modified
ErrNotModified = errorType{codeNotModified, "not modified"}
)

const (
Expand All @@ -46,6 +49,7 @@ const (
codeLockfailed
codeNotImplemented
codeInUse
codeNotModified
)

type errorType struct {
Expand Down Expand Up @@ -82,6 +86,11 @@ func IsInUse(err error) bool {
return checkError(err, codeInUse)
}

// IsNotModified checks the error is not modified erro or not.
func IsNotModified(err error) bool {
return checkError(err, codeNotModified)
}

func checkError(err error, code int) bool {
err = causeError(err)

Expand Down
15 changes: 15 additions & 0 deletions test/api_container_start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,18 @@ func (suite *APIContainerStartSuite) TestInvalidParam(c *check.C) {
// TODO: missing case
helpwantedForMissingCase(c, "container api start bad request")
}

// TestStartAlreadyRunningContainer tests starting a running container
func (suite *APIContainerStartSuite) TestStartAlreadyRunningContainer(c *check.C) {
cname := "TestStartAlreadyRunningContainer"

CreateBusyboxContainerOk(c, cname)
defer DelContainerForceMultyTime(c, cname)

StartContainerOk(c, cname)

resp, err := request.Post("/containers/" + cname + "/start")
c.Assert(err, check.IsNil)
CheckRespStatus(c, resp, 304)

}
9 changes: 9 additions & 0 deletions test/cli_stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,12 @@ func (suite *PouchStopSuite) TestAutoStopPidValue(c *check.C) {
}
c.Assert(result[0].State.Pid, check.Equals, int64(0))
}

// TestStartContainerTwice tries to start a container twice
func (suite *PouchStartSuite) TestStartContainerTwice(c *check.C) {
name := "TestStartContainerTwice"

command.PouchRun("create", "--name", name, busyboxImage, "top").Assert(c, icmd.Success)
command.PouchRun("start", name).Assert(c, icmd.Success)
command.PouchRun("start", name).Assert(c, icmd.Success)
}

0 comments on commit a0171dc

Please sign in to comment.