Browse code

Add tests for rm with stop and kill options

Docker-DCO-1.1-Signed-off-by: Adrien Folie <folie.adrien@gmail.com> (github: folieadrien)

Adrien Folie authored on 2014/07/07 05:44:56
Showing 3 changed files
... ...
@@ -451,6 +451,53 @@ func TestGetImagesByName(t *testing.T) {
451 451
 	}
452 452
 }
453 453
 
454
+func TestDeleteContainers(t *testing.T) {
455
+	eng := engine.New()
456
+	name := "foo"
457
+	var called bool
458
+	eng.Register("container_delete", func(job *engine.Job) engine.Status {
459
+		called = true
460
+		if len(job.Args) == 0 {
461
+			t.Fatalf("Job arguments is empty")
462
+		}
463
+		if job.Args[0] != name {
464
+			t.Fatalf("name != '%s': %#v", name, job.Args[0])
465
+		}
466
+		return engine.StatusOK
467
+	})
468
+	r := serveRequest("DELETE", "/containers/"+name, nil, eng, t)
469
+	if !called {
470
+		t.Fatalf("handler was not called")
471
+	}
472
+	if r.Code != http.StatusNoContent {
473
+		t.Fatalf("Got status %d, expected %d", r.Code, http.StatusNoContent)
474
+	}
475
+}
476
+
477
+func TestDeleteContainersWithStopAndKill(t *testing.T) {
478
+	if api.APIVERSION.LessThan("1.14") {
479
+		return
480
+	}
481
+	eng := engine.New()
482
+	var called bool
483
+	eng.Register("container_delete", func(job *engine.Job) engine.Status {
484
+		called = true
485
+		return engine.StatusOK
486
+	})
487
+	r := serveRequest("DELETE", "/containers/foo?stop=1&kill=1", nil, eng, t)
488
+	if r.Code != http.StatusBadRequest {
489
+		t.Fatalf("Got status %d, expected %d", r.Code, http.StatusBadRequest)
490
+	}
491
+	if called {
492
+		t.Fatalf("container_delete jobs was called, but it shouldn't")
493
+	}
494
+	res := strings.TrimSpace(r.Body.String())
495
+	expected := "Bad parameters: can't use stop and kill simultaneously"
496
+	if !strings.Contains(res, expected) {
497
+		t.Fatalf("Output %s, expected %s in it", res, expected)
498
+	}
499
+}
500
+
454 501
 func serveRequest(method, target string, body io.Reader, eng *engine.Engine, t *testing.T) *httptest.ResponseRecorder {
455 502
 	return serveRequestUsingVersion(method, target, api.APIVERSION, body, eng, t)
456 503
 }
... ...
@@ -42,25 +42,59 @@ func TestRemoveContainerWithVolume(t *testing.T) {
42 42
 	logDone("rm - volume")
43 43
 }
44 44
 
45
-func TestRemoveContainerRunning(t *testing.T) {
46
-	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", "foo", "busybox", "top")
47
-	if _, err := runCommand(cmd); err != nil {
48
-		t.Fatal(err)
49
-	}
45
+func TestRemoveRunningContainer(t *testing.T) {
46
+	createRunningContainer(t, "foo")
50 47
 
51 48
 	// Test cannot remove running container
52
-	cmd = exec.Command(dockerBinary, "rm", "foo")
49
+	cmd := exec.Command(dockerBinary, "rm", "foo")
53 50
 	if _, err := runCommand(cmd); err == nil {
54 51
 		t.Fatalf("Expected error, can't rm a running container")
55 52
 	}
56 53
 
57
-	// Remove with -f
58
-	cmd = exec.Command(dockerBinary, "rm", "-f", "foo")
54
+	deleteAllContainers()
55
+
56
+	logDone("rm - running container")
57
+}
58
+
59
+func TestStopAndRemoveRunningContainer(t *testing.T) {
60
+	createRunningContainer(t, "foo")
61
+
62
+	// Stop then remove with -s
63
+	cmd := exec.Command(dockerBinary, "rm", "-s", "foo")
59 64
 	if _, err := runCommand(cmd); err != nil {
60 65
 		t.Fatal(err)
61 66
 	}
62 67
 
63 68
 	deleteAllContainers()
64 69
 
65
-	logDone("rm - running container")
70
+	logDone("rm - running container with --stop=true")
71
+}
72
+
73
+func TestKillAndRemoveRunningContainer(t *testing.T) {
74
+	createRunningContainer(t, "foo")
75
+
76
+	// Kill then remove with -k
77
+	cmd := exec.Command(dockerBinary, "rm", "-k", "foo")
78
+	if _, err := runCommand(cmd); err != nil {
79
+		t.Fatal(err)
80
+	}
81
+
82
+	deleteAllContainers()
83
+
84
+	logDone("rm - running container with --kill=true")
85
+}
86
+
87
+func TestRemoveContainerWithStopAndKill(t *testing.T) {
88
+	cmd := exec.Command(dockerBinary, "rm", "-sk", "foo")
89
+	if _, err := runCommand(cmd); err == nil {
90
+		t.Fatalf("Expected error: can't use stop and kill simulteanously")
91
+	}
92
+	logDone("rm - with --stop=true and --kill=true")
93
+}
94
+
95
+func createRunningContainer(t *testing.T, name string) {
96
+	cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top")
97
+	if _, err := runCommand(cmd); err != nil {
98
+		t.Fatal(err)
99
+	}
66 100
 }
... ...
@@ -768,35 +768,6 @@ func TestPostContainersAttachStderr(t *testing.T) {
768 768
 	containerWait(eng, containerID, t)
769 769
 }
770 770
 
771
-// FIXME: Test deleting running container
772
-// FIXME: Test deleting container with volume
773
-// FIXME: Test deleting volume in use by other container
774
-func TestDeleteContainers(t *testing.T) {
775
-	eng := NewTestEngine(t)
776
-	defer mkDaemonFromEngine(eng, t).Nuke()
777
-
778
-	containerID := createTestContainer(eng,
779
-		&runconfig.Config{
780
-			Image: unitTestImageID,
781
-			Cmd:   []string{"touch", "/test"},
782
-		},
783
-		t,
784
-	)
785
-	req, err := http.NewRequest("DELETE", "/containers/"+containerID, nil)
786
-	if err != nil {
787
-		t.Fatal(err)
788
-	}
789
-	r := httptest.NewRecorder()
790
-	if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
791
-		t.Fatal(err)
792
-	}
793
-	assertHttpNotError(r, t)
794
-	if r.Code != http.StatusNoContent {
795
-		t.Fatalf("%d NO CONTENT expected, received %d\n", http.StatusNoContent, r.Code)
796
-	}
797
-	containerAssertNotExists(eng, containerID, t)
798
-}
799
-
800 771
 func TestOptionsRoute(t *testing.T) {
801 772
 	eng := NewTestEngine(t)
802 773
 	defer mkDaemonFromEngine(eng, t).Nuke()