1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add tests for rm with stop and kill options

Docker-DCO-1.1-Signed-off-by: Adrien Folie <folie.adrien@gmail.com> (github: folieadrien)
This commit is contained in:
Adrien Folie 2014-07-06 22:44:56 +02:00 committed by Victor Vieux
parent 7810070630
commit d689a5e1e2
3 changed files with 93 additions and 41 deletions

View file

@ -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 { 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) return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
} }

View file

@ -42,25 +42,59 @@ func TestRemoveContainerWithVolume(t *testing.T) {
logDone("rm - volume") logDone("rm - volume")
} }
func TestRemoveContainerRunning(t *testing.T) { func TestRemoveRunningContainer(t *testing.T) {
cmd := exec.Command(dockerBinary, "run", "-dt", "--name", "foo", "busybox", "top") createRunningContainer(t, "foo")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}
// Test cannot remove running container // Test cannot remove running container
cmd = exec.Command(dockerBinary, "rm", "foo") cmd := exec.Command(dockerBinary, "rm", "foo")
if _, err := runCommand(cmd); err == nil { if _, err := runCommand(cmd); err == nil {
t.Fatalf("Expected error, can't rm a running container") 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() deleteAllContainers()
logDone("rm - running container") 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)
}
}

View file

@ -768,35 +768,6 @@ func TestPostContainersAttachStderr(t *testing.T) {
containerWait(eng, containerID, 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) { func TestOptionsRoute(t *testing.T) {
eng := NewTestEngine(t) eng := NewTestEngine(t)
defer mkDaemonFromEngine(eng, t).Nuke() defer mkDaemonFromEngine(eng, t).Nuke()