Browse code

docker kill should return error if container is not running.

Assuming that docker kill is trying to actually kill the container
is a mistake. If the container is not running we should report it
back to the caller as a error.

Docker-DCO-1.1-Signed-off-by: Dan Walsh <dwalsh@redhat.com> (github: rhatdan)
Docker-DCO-1.1-Signed-off-by: Regan McCooey <rmccooey27@aol.com> (github: rmccooey27)

Docker-DCO-1.1-Signed-off-by: Regan McCooey <rmccooey27@aol.com> (github: rhatdan)

Regan McCooey authored on 2015/04/14 00:17:07
Showing 3 changed files
... ...
@@ -286,7 +286,7 @@ func (s *Server) postContainersKill(eng *engine.Engine, version version.Version,
286 286
 	name := vars["name"]
287 287
 
288 288
 	// If we have a signal, look at it. Otherwise, do nothing
289
-	if sigStr := vars["signal"]; sigStr != "" {
289
+	if sigStr := r.Form.Get("signal"); sigStr != "" {
290 290
 		// Check if we passed the signal as a number:
291 291
 		// The largest legal signal is 31, so let's parse on 5 bits
292 292
 		sig, err = strconv.ParseUint(sigStr, 10, 5)
... ...
@@ -710,7 +710,7 @@ func (container *Container) KillSig(sig int) error {
710 710
 	}
711 711
 
712 712
 	if !container.Running {
713
-		return nil
713
+		return fmt.Errorf("Container %s is not running", container.ID)
714 714
 	}
715 715
 
716 716
 	// signal to the monitor that it should not restart the container
... ...
@@ -40,6 +40,28 @@ func (s *DockerSuite) TestKillContainer(c *check.C) {
40 40
 
41 41
 }
42 42
 
43
+func (s *DockerSuite) TestKillofStoppedContainer(c *check.C) {
44
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
45
+	out, _, err := runCommandWithOutput(runCmd)
46
+	if err != nil {
47
+		c.Fatal(out, err)
48
+	}
49
+
50
+	cleanedContainerID := strings.TrimSpace(out)
51
+
52
+	stopCmd := exec.Command(dockerBinary, "stop", cleanedContainerID)
53
+	if out, _, err = runCommandWithOutput(stopCmd); err != nil {
54
+		c.Fatalf("failed to stop container: %s, %v", out, err)
55
+	}
56
+
57
+	killCmd := exec.Command(dockerBinary, "kill", "-s", "30", cleanedContainerID)
58
+	if _, _, err = runCommandWithOutput(killCmd); err == nil {
59
+		c.Fatalf("kill succeeded on a stopped container")
60
+	}
61
+
62
+	deleteContainer(cleanedContainerID)
63
+}
64
+
43 65
 func (s *DockerSuite) TestKillDifferentUserContainer(c *check.C) {
44 66
 	runCmd := exec.Command(dockerBinary, "run", "-u", "daemon", "-d", "busybox", "top")
45 67
 	out, _, err := runCommandWithOutput(runCmd)