Browse code

`docker ps --filter exited=status` should not show running containers

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)

Jessica Frazelle authored on 2015/01/09 07:51:47
Showing 2 changed files
... ...
@@ -73,7 +73,6 @@ func (daemon *Daemon) Containers(job *engine.Job) engine.Status {
73 73
 		if !container.Running && !all && n <= 0 && since == "" && before == "" {
74 74
 			return nil
75 75
 		}
76
-
77 76
 		if !psFilters.Match("name", container.Name) {
78 77
 			return nil
79 78
 		}
... ...
@@ -96,10 +95,10 @@ func (daemon *Daemon) Containers(job *engine.Job) engine.Status {
96 96
 				return errLast
97 97
 			}
98 98
 		}
99
-		if len(filt_exited) > 0 && !container.Running {
99
+		if len(filt_exited) > 0 {
100 100
 			should_skip := true
101 101
 			for _, code := range filt_exited {
102
-				if code == container.ExitCode {
102
+				if code == container.ExitCode && !container.Running {
103 103
 					should_skip = false
104 104
 					break
105 105
 				}
... ...
@@ -398,11 +398,15 @@ func TestPsListContainersFilterName(t *testing.T) {
398 398
 }
399 399
 
400 400
 func TestPsListContainersFilterExited(t *testing.T) {
401
-	deleteAllContainers()
402 401
 	defer deleteAllContainers()
403
-	runCmd := exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
404
-	out, _, err := runCommandWithOutput(runCmd)
405
-	if err != nil {
402
+
403
+	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
404
+	if out, _, err := runCommandWithOutput(runCmd); err != nil {
405
+		t.Fatal(out, err)
406
+	}
407
+
408
+	runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
409
+	if out, _, err := runCommandWithOutput(runCmd); err != nil {
406 410
 		t.Fatal(out, err)
407 411
 	}
408 412
 	firstZero, err := getIDByName("zero1")
... ...
@@ -411,8 +415,7 @@ func TestPsListContainersFilterExited(t *testing.T) {
411 411
 	}
412 412
 
413 413
 	runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
414
-	out, _, err = runCommandWithOutput(runCmd)
415
-	if err != nil {
414
+	if out, _, err := runCommandWithOutput(runCmd); err != nil {
416 415
 		t.Fatal(out, err)
417 416
 	}
418 417
 	secondZero, err := getIDByName("zero2")
... ...
@@ -421,8 +424,7 @@ func TestPsListContainersFilterExited(t *testing.T) {
421 421
 	}
422 422
 
423 423
 	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
424
-	out, _, err = runCommandWithOutput(runCmd)
425
-	if err == nil {
424
+	if out, _, err := runCommandWithOutput(runCmd); err == nil {
426 425
 		t.Fatal("Should fail.", out, err)
427 426
 	}
428 427
 	firstNonZero, err := getIDByName("nonzero1")
... ...
@@ -431,8 +433,7 @@ func TestPsListContainersFilterExited(t *testing.T) {
431 431
 	}
432 432
 
433 433
 	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
434
-	out, _, err = runCommandWithOutput(runCmd)
435
-	if err == nil {
434
+	if out, _, err := runCommandWithOutput(runCmd); err == nil {
436 435
 		t.Fatal("Should fail.", out, err)
437 436
 	}
438 437
 	secondNonZero, err := getIDByName("nonzero2")
... ...
@@ -442,7 +443,7 @@ func TestPsListContainersFilterExited(t *testing.T) {
442 442
 
443 443
 	// filter containers by exited=0
444 444
 	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
445
-	out, _, err = runCommandWithOutput(runCmd)
445
+	out, _, err := runCommandWithOutput(runCmd)
446 446
 	if err != nil {
447 447
 		t.Fatal(out, err)
448 448
 	}
... ...
@@ -472,5 +473,6 @@ func TestPsListContainersFilterExited(t *testing.T) {
472 472
 	if ids[1] != firstNonZero {
473 473
 		t.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
474 474
 	}
475
+
475 476
 	logDone("ps - test ps filter exited")
476 477
 }