diff --git a/api/server/server_unit_test.go b/api/server/server_unit_test.go index c51ea6fc0d..6814ca8a47 100644 --- a/api/server/server_unit_test.go +++ b/api/server/server_unit_test.go @@ -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) } diff --git a/integration-cli/docker_cli_rm_test.go b/integration-cli/docker_cli_rm_test.go index 34b5df0338..b61f1c9a93 100644 --- a/integration-cli/docker_cli_rm_test.go +++ b/integration-cli/docker_cli_rm_test.go @@ -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") - if _, err := runCommand(cmd); err != nil { - t.Fatal(err) - } - 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 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) + } +} diff --git a/integration/api_test.go b/integration/api_test.go index 0da96ccd1e..b8414b6179 100644 --- a/integration/api_test.go +++ b/integration/api_test.go @@ -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()