Browse code

Merge pull request #9356 from cc272309126/fix-exec-paused-container

Fix the issue that when docker exec a paused container, it will always

Michael Crosby authored on 2014/12/10 04:01:44
Showing 3 changed files
... ...
@@ -98,7 +98,9 @@ func (d *Daemon) getActiveContainer(name string) (*Container, error) {
98 98
 	if !container.IsRunning() {
99 99
 		return nil, fmt.Errorf("Container %s is not running", name)
100 100
 	}
101
-
101
+	if container.IsPaused() {
102
+		return nil, fmt.Errorf("Container %s is paused, unpause the container before exec", name)
103
+	}
102 104
 	return container, nil
103 105
 }
104 106
 
... ...
@@ -230,3 +230,36 @@ func TestExecExitStatus(t *testing.T) {
230 230
 
231 231
 	logDone("exec - exec non-zero ExitStatus")
232 232
 }
233
+
234
+func TestExecPausedContainer(t *testing.T) {
235
+
236
+	defer deleteAllContainers()
237
+	defer unpauseAllContainers()
238
+
239
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
240
+	out, _, err := runCommandWithOutput(runCmd)
241
+	if err != nil {
242
+		t.Fatal(out, err)
243
+	}
244
+
245
+	ContainerID := stripTrailingCharacters(out)
246
+
247
+	pausedCmd := exec.Command(dockerBinary, "pause", "testing")
248
+	out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
249
+	if err != nil {
250
+		t.Fatal(out, err)
251
+	}
252
+
253
+	execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
254
+	out, _, err = runCommandWithOutput(execCmd)
255
+	if err == nil {
256
+		t.Fatal("container should fail to exec new command if it is paused")
257
+	}
258
+
259
+	expected := ContainerID + " is paused, unpause the container before exec"
260
+	if !strings.Contains(out, expected) {
261
+		t.Fatal("container should not exec new command if it is paused")
262
+	}
263
+
264
+	logDone("exec - exec should not exec a pause container")
265
+}
... ...
@@ -328,6 +328,46 @@ func deleteAllContainers() error {
328 328
 	return nil
329 329
 }
330 330
 
331
+func getPausedContainers() (string, error) {
332
+	getPausedContainersCmd := exec.Command(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
333
+	out, exitCode, err := runCommandWithOutput(getPausedContainersCmd)
334
+	if exitCode != 0 && err == nil {
335
+		err = fmt.Errorf("failed to get a list of paused containers: %v\n", out)
336
+	}
337
+
338
+	return out, err
339
+}
340
+
341
+func unpauseContainer(container string) error {
342
+	unpauseCmd := exec.Command(dockerBinary, "unpause", container)
343
+	exitCode, err := runCommand(unpauseCmd)
344
+	if exitCode != 0 && err == nil {
345
+		err = fmt.Errorf("failed to unpause container")
346
+	}
347
+
348
+	return nil
349
+}
350
+
351
+func unpauseAllContainers() error {
352
+	containers, err := getPausedContainers()
353
+	if err != nil {
354
+		fmt.Println(containers)
355
+		return err
356
+	}
357
+
358
+	containers = strings.Replace(containers, "\n", " ", -1)
359
+	containers = strings.Trim(containers, " ")
360
+	containerList := strings.Split(containers, " ")
361
+
362
+	for _, value := range containerList {
363
+		if err = unpauseContainer(value); err != nil {
364
+			return err
365
+		}
366
+	}
367
+
368
+	return nil
369
+}
370
+
331 371
 func deleteImages(images ...string) error {
332 372
 	args := make([]string, 1, 2)
333 373
 	args[0] = "rmi"