Skip to content

Commit

Permalink
Add tests for rm with stop and kill options
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Adrien Folie <folie.adrien@gmail.com> (github: folieadrien)
  • Loading branch information
foliea authored and vieux committed Jul 11, 2014
1 parent 7810070 commit d689a5e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 38 deletions.
47 changes: 47 additions & 0 deletions api/server/server_unit_test.go
Expand Up @@ -451,6 +451,53 @@ func TestGetImagesByName(t *testing.T) {
}
}

func TestDeleteContainers(t *testing.T) {
eng := engine.New()
name := "foo"
var called bool
eng.Register("container_delete", func(job *engine.Job) engine.Status {
called = true
if len(job.Args) == 0 {
t.Fatalf("Job arguments is empty")
}
if job.Args[0] != name {
t.Fatalf("name != '%s': %#v", name, job.Args[0])
}
return engine.StatusOK
})
r := serveRequest("DELETE", "/containers/"+name, nil, eng, t)
if !called {
t.Fatalf("handler was not called")
}
if r.Code != http.StatusNoContent {
t.Fatalf("Got status %d, expected %d", r.Code, http.StatusNoContent)
}
}

func TestDeleteContainersWithStopAndKill(t *testing.T) {
if api.APIVERSION.LessThan("1.14") {
return
}
eng := engine.New()
var called bool
eng.Register("container_delete", func(job *engine.Job) engine.Status {
called = true
return engine.StatusOK
})
r := serveRequest("DELETE", "/containers/foo?stop=1&kill=1", nil, eng, t)
if r.Code != http.StatusBadRequest {
t.Fatalf("Got status %d, expected %d", r.Code, http.StatusBadRequest)
}
if called {
t.Fatalf("container_delete jobs was called, but it shouldn't")
}
res := strings.TrimSpace(r.Body.String())
expected := "Bad parameters: can't use stop and kill simultaneously"
if !strings.Contains(res, expected) {
t.Fatalf("Output %s, expected %s in it", res, expected)
}
}

func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
}
Expand Down
52 changes: 43 additions & 9 deletions integration-cli/docker_cli_rm_test.go
Expand Up @@ -42,25 +42,59 @@ func TestRemoveContainerWithVolume(t *testing.T) {
logDone("rm - volume")
}

func TestRemoveContainerRunning(t *testing.T) {
cmd := exec.Command(dockerBinary, "run", "-dt", "--name", "foo", "busybox", "top")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}
func TestRemoveRunningContainer(t *testing.T) {
createRunningContainer(t, "foo")

// Test cannot remove running container
cmd = exec.Command(dockerBinary, "rm", "foo")
cmd := exec.Command(dockerBinary, "rm", "foo")
if _, err := runCommand(cmd); err == nil {
t.Fatalf("Expected error, can't rm a running container")
}

// Remove with -f
cmd = exec.Command(dockerBinary, "rm", "-f", "foo")
deleteAllContainers()

logDone("rm - running container")
}

func TestStopAndRemoveRunningContainer(t *testing.T) {
createRunningContainer(t, "foo")

// Stop then remove with -s
cmd := exec.Command(dockerBinary, "rm", "-s", "foo")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}

deleteAllContainers()

logDone("rm - running container")
logDone("rm - running container with --stop=true")
}

func TestKillAndRemoveRunningContainer(t *testing.T) {
createRunningContainer(t, "foo")

// Kill then remove with -k
cmd := exec.Command(dockerBinary, "rm", "-k", "foo")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}

deleteAllContainers()

logDone("rm - running container with --kill=true")
}

func TestRemoveContainerWithStopAndKill(t *testing.T) {
cmd := exec.Command(dockerBinary, "rm", "-sk", "foo")
if _, err := runCommand(cmd); err == nil {
t.Fatalf("Expected error: can't use stop and kill simulteanously")
}
logDone("rm - with --stop=true and --kill=true")
}

func createRunningContainer(t *testing.T, name string) {
cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}
}
29 changes: 0 additions & 29 deletions integration/api_test.go
Expand Up @@ -768,35 +768,6 @@ func TestPostContainersAttachStderr(t *testing.T) {
containerWait(eng, containerID, t)
}

// FIXME: Test deleting running container
// FIXME: Test deleting container with volume
// FIXME: Test deleting volume in use by other container
func TestDeleteContainers(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()

containerID := createTestContainer(eng,
&runconfig.Config{
Image: unitTestImageID,
Cmd: []string{"touch", "/test"},
},
t,
)
req, err := http.NewRequest("DELETE", "/containers/"+containerID, nil)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRecorder()
if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
t.Fatal(err)
}
assertHttpNotError(r, t)
if r.Code != http.StatusNoContent {
t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
}
containerAssertNotExists(eng, containerID, t)
}

func TestOptionsRoute(t *testing.T) {
eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke()
Expand Down

0 comments on commit d689a5e

Please sign in to comment.