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

add c.Assert in docker_cli_ps_test.go

Signed-off-by: Zhang Kun <zkazure@gmail.com>
This commit is contained in:
Zhang Kun 2015-10-14 15:03:09 +08:00
parent c9208953fa
commit 83b03b0057

View file

@ -6,11 +6,11 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "github.com/go-check/check"
"sort" "sort"
@ -33,111 +33,79 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
fourthID := strings.TrimSpace(out) fourthID := strings.TrimSpace(out)
// make sure the second is running // make sure the second is running
c.Assert(waitRun(secondID), check.IsNil) c.Assert(waitRun(secondID), checker.IsNil)
// make sure third one is not running // make sure third one is not running
dockerCmd(c, "wait", thirdID) dockerCmd(c, "wait", thirdID)
// make sure the forth is running // make sure the forth is running
c.Assert(waitRun(fourthID), check.IsNil) c.Assert(waitRun(fourthID), checker.IsNil)
// all // all
out, _ = dockerCmd(c, "ps", "-a") out, _ = dockerCmd(c, "ps", "-a")
if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) { c.Assert(assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}), checker.Equals, true, check.Commentf("ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("ALL: Container list is not in the correct order: \n%s", out)
}
// running // running
out, _ = dockerCmd(c, "ps") out, _ = dockerCmd(c, "ps")
if !assertContainerList(out, []string{fourthID, secondID, firstID}) { c.Assert(assertContainerList(out, []string{fourthID, secondID, firstID}), checker.Equals, true, check.Commentf("RUNNING: Container list is not in the correct order: \n%s", out))
c.Errorf("RUNNING: Container list is not in the correct order: \n%s", out)
}
// from here all flag '-a' is ignored // from here all flag '-a' is ignored
// limit // limit
out, _ = dockerCmd(c, "ps", "-n=2", "-a") out, _ = dockerCmd(c, "ps", "-n=2", "-a")
expected := []string{fourthID, thirdID} expected := []string{fourthID, thirdID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("LIMIT & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "-n=2") out, _ = dockerCmd(c, "ps", "-n=2")
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
c.Errorf("LIMIT: Container list is not in the correct order: \n%s", out)
}
// since // since
out, _ = dockerCmd(c, "ps", "--since", firstID, "-a") out, _ = dockerCmd(c, "ps", "--since", firstID, "-a")
expected = []string{fourthID, thirdID, secondID} expected = []string{fourthID, thirdID, secondID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--since", firstID) out, _ = dockerCmd(c, "ps", "--since", firstID)
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE: Container list is not in the correct order: \n%s", out)
}
// before // before
out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a") out, _ = dockerCmd(c, "ps", "--before", thirdID, "-a")
expected = []string{secondID, firstID} expected = []string{secondID, firstID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("BEFORE & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--before", thirdID) out, _ = dockerCmd(c, "ps", "--before", thirdID)
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
c.Errorf("BEFORE: Container list is not in the correct order: \n%s", out)
}
// since & before // since & before
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a") out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-a")
expected = []string{thirdID, secondID} expected = []string{thirdID, secondID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID) out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID)
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out)
}
// since & limit // since & limit
out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a") out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2", "-a")
expected = []string{fourthID, thirdID} expected = []string{fourthID, thirdID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2") out, _ = dockerCmd(c, "ps", "--since", firstID, "-n=2")
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out)
}
// before & limit // before & limit
out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a") out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1", "-a")
expected = []string{thirdID} expected = []string{thirdID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1") out, _ = dockerCmd(c, "ps", "--before", fourthID, "-n=1")
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
c.Errorf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a") out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
expected = []string{thirdID} expected = []string{thirdID}
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out)
}
out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1") out, _ = dockerCmd(c, "ps", "--since", firstID, "--before", fourthID, "-n=1")
if !assertContainerList(out, expected) { c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
c.Errorf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out)
}
} }
@ -167,16 +135,12 @@ func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
baseSizeIndex := strings.Index(baseLines[0], "SIZE") baseSizeIndex := strings.Index(baseLines[0], "SIZE")
baseFoundsize := baseLines[1][baseSizeIndex:] baseFoundsize := baseLines[1][baseSizeIndex:]
baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0]) baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
name := "test_size" name := "test_size"
out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test") out, _ := dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
id, err := getIDByName(name) id, err := getIDByName(name)
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1") runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1")
@ -190,24 +154,16 @@ func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
case <-time.After(3 * time.Second): case <-time.After(3 * time.Second):
c.Fatalf("Calling \"docker ps -s\" timed out!") c.Fatalf("Calling \"docker ps -s\" timed out!")
} }
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(out, err)
}
lines := strings.Split(strings.Trim(out, "\n "), "\n") lines := strings.Split(strings.Trim(out, "\n "), "\n")
if len(lines) != 2 { c.Assert(lines, checker.HasLen, 2, check.Commentf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines)))
c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
}
sizeIndex := strings.Index(lines[0], "SIZE") sizeIndex := strings.Index(lines[0], "SIZE")
idIndex := strings.Index(lines[0], "CONTAINER ID") idIndex := strings.Index(lines[0], "CONTAINER ID")
foundID := lines[1][idIndex : idIndex+12] foundID := lines[1][idIndex : idIndex+12]
if foundID != id[:12] { c.Assert(foundID, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s", id[:12], foundID))
c.Fatalf("Expected id %s, got %s", id[:12], foundID)
}
expectedSize := fmt.Sprintf("%d B", (2 + baseBytes)) expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
foundSize := lines[1][sizeIndex:] foundSize := lines[1][sizeIndex:]
if !strings.Contains(foundSize, expectedSize) { c.Assert(foundSize, checker.Contains, expectedSize, check.Commentf("Expected size %q, got %q", expectedSize, foundSize))
c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
}
} }
@ -230,20 +186,14 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
// filter containers by exited // filter containers by exited
out, _ = dockerCmd(c, "ps", "-q", "--filter=status=exited") out, _ = dockerCmd(c, "ps", "-q", "--filter=status=exited")
containerOut := strings.TrimSpace(out) containerOut := strings.TrimSpace(out)
if containerOut != firstID[:12] { c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
}
out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=status=running") out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=status=running")
containerOut = strings.TrimSpace(out) containerOut = strings.TrimSpace(out)
if containerOut != secondID[:12] { c.Assert(containerOut, checker.Equals, secondID[:12], check.Commentf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out))
c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
}
out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish") out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
if !strings.Contains(out, "Unrecognised filter value for status") { c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out))
c.Fatalf("Expected error response due to invalid status filter output: %q", out)
}
} }
@ -259,9 +209,7 @@ func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
// filter containers by id // filter containers by id
out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID) out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID)
containerOut := strings.TrimSpace(out) containerOut := strings.TrimSpace(out)
if containerOut != firstID[:12] { c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
}
} }
@ -277,9 +225,7 @@ func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
// filter containers by name // filter containers by name
out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match") out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match")
containerOut := strings.TrimSpace(out) containerOut := strings.TrimSpace(out)
if containerOut != firstID[:12] { c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out))
c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
}
} }
@ -298,19 +244,19 @@ func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
imageID1, err := buildImage(imageName1, imageID1, err := buildImage(imageName1,
`FROM busybox `FROM busybox
LABEL match me 1`, true) LABEL match me 1`, true)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
imageName1Tagged := "images_ps_filter_test1:tag" imageName1Tagged := "images_ps_filter_test1:tag"
imageID1Tagged, err := buildImage(imageName1Tagged, imageID1Tagged, err := buildImage(imageName1Tagged,
`FROM busybox `FROM busybox
LABEL match me 1 tagged`, true) LABEL match me 1 tagged`, true)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
imageName2 := "images_ps_filter_test2" imageName2 := "images_ps_filter_test2"
imageID2, err := buildImage(imageName2, imageID2, err := buildImage(imageName2,
fmt.Sprintf(`FROM %s fmt.Sprintf(`FROM %s
LABEL match me 2`, imageName1), true) LABEL match me 2`, imageName1), true)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
// start containers // start containers
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello") out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
@ -373,9 +319,7 @@ func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expe
sort.Strings(actualIDs) sort.Strings(actualIDs)
sort.Strings(expectedIDs) sort.Strings(expectedIDs)
if len(actualIDs) != len(expectedIDs) { c.Assert(actualIDs, checker.HasLen, len(expectedIDs), check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v:%v, got %v:%v", filterName, len(expectedIDs), expectedIDs, len(actualIDs), actualIDs))
c.Fatalf("Expected filtered container(s) for %s ancestor filter to be %v:%v, got %v:%v", filterName, len(expectedIDs), expectedIDs, len(actualIDs), actualIDs)
}
if len(expectedIDs) > 0 { if len(expectedIDs) > 0 {
same := true same := true
for i := range expectedIDs { for i := range expectedIDs {
@ -385,9 +329,7 @@ func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expe
break break
} }
} }
if !same { c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs))
c.Fatalf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs)
}
} }
} }
@ -408,30 +350,24 @@ func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
// filter containers by exact match // filter containers by exact match
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
containerOut := strings.TrimSpace(out) containerOut := strings.TrimSpace(out)
if containerOut != firstID { c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
}
// filter containers by two labels // filter containers by two labels
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
containerOut = strings.TrimSpace(out) containerOut = strings.TrimSpace(out)
if containerOut != firstID { c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out))
c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
}
// filter containers by two labels, but expect not found because of AND behavior // filter containers by two labels, but expect not found because of AND behavior
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
containerOut = strings.TrimSpace(out) containerOut = strings.TrimSpace(out)
if containerOut != "" { c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out))
c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
}
// filter containers by exact key // filter containers by exact key
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
containerOut = strings.TrimSpace(out) containerOut = strings.TrimSpace(out)
if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) { c.Assert(containerOut, checker.Contains, firstID)
c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out) c.Assert(containerOut, checker.Contains, secondID)
} c.Assert(containerOut, checker.Not(checker.Contains), thirdID)
} }
func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) { func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
@ -440,57 +376,35 @@ func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
dockerCmd(c, "run", "--name", "zero1", "busybox", "true") dockerCmd(c, "run", "--name", "zero1", "busybox", "true")
firstZero, err := getIDByName("zero1") firstZero, err := getIDByName("zero1")
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
dockerCmd(c, "run", "--name", "zero2", "busybox", "true") dockerCmd(c, "run", "--name", "zero2", "busybox", "true")
secondZero, err := getIDByName("zero2") secondZero, err := getIDByName("zero2")
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
if out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false"); err == nil { out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
c.Fatal("Should fail.", out, err) c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
}
firstNonZero, err := getIDByName("nonzero1") firstNonZero, err := getIDByName("nonzero1")
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
if out, _, err := dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false"); err == nil { out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
c.Fatal("Should fail.", out, err) c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err))
}
secondNonZero, err := getIDByName("nonzero2") secondNonZero, err := getIDByName("nonzero2")
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
// filter containers by exited=0 // filter containers by exited=0
out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
ids := strings.Split(strings.TrimSpace(out), "\n") ids := strings.Split(strings.TrimSpace(out), "\n")
if len(ids) != 2 { c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d: %s", len(ids), out))
c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out) c.Assert(ids[0], checker.Equals, secondZero, check.Commentf("First in list should be %q, got %q", secondZero, ids[0]))
} c.Assert(ids[1], checker.Equals, firstZero, check.Commentf("Second in list should be %q, got %q", firstZero, ids[1]))
if ids[0] != secondZero {
c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
}
if ids[1] != firstZero {
c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
}
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1") out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
ids = strings.Split(strings.TrimSpace(out), "\n") ids = strings.Split(strings.TrimSpace(out), "\n")
if len(ids) != 2 { c.Assert(ids, checker.HasLen, 2, check.Commentf("Should be 2 zero exited containers got %d", len(ids)))
c.Fatalf("Should be 2 zero exited containers got %d", len(ids)) c.Assert(ids[0], checker.Equals, secondNonZero, check.Commentf("First in list should be %q, got %q", secondNonZero, ids[0]))
} c.Assert(ids[1], checker.Equals, firstNonZero, check.Commentf("Second in list should be %q, got %q", firstNonZero, ids[1]))
if ids[0] != secondNonZero {
c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
}
if ids[1] != firstNonZero {
c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
}
} }
@ -519,24 +433,16 @@ func (s *DockerSuite) TestPsRightTagName(c *check.C) {
lines := strings.Split(strings.TrimSpace(string(out)), "\n") lines := strings.Split(strings.TrimSpace(string(out)), "\n")
// skip header // skip header
lines = lines[1:] lines = lines[1:]
if len(lines) != 3 { c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
c.Fatalf("There should be 3 running container, got %d", len(lines))
}
for _, line := range lines { for _, line := range lines {
f := strings.Fields(line) f := strings.Fields(line)
switch f[0] { switch f[0] {
case id1: case id1:
if f[1] != "busybox" { c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]))
c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
}
case id2: case id2:
if f[1] != tag { c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1]))
c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
}
case id3: case id3:
if f[1] != imageID { c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1]))
c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
}
default: default:
c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3) c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
} }
@ -558,9 +464,7 @@ func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
fields := strings.Fields(l) fields := strings.Fields(l)
names = append(names, fields[len(fields)-1]) names = append(names, fields[len(fields)-1])
} }
if !reflect.DeepEqual(expected, names) { c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names))
c.Fatalf("Expected array: %v, got: %v", expected, names)
}
} }
func (s *DockerSuite) TestPsGroupPortRange(c *check.C) { func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
@ -570,10 +474,7 @@ func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
out, _ := dockerCmd(c, "ps") out, _ := dockerCmd(c, "ps")
// check that the port range is in the output c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out)))
if !strings.Contains(string(out), portRange) {
c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
}
} }
@ -582,9 +483,7 @@ func (s *DockerSuite) TestPsWithSize(c *check.C) {
dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top") dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
out, _ := dockerCmd(c, "ps", "--size") out, _ := dockerCmd(c, "ps", "--size")
if !strings.Contains(out, "virtual") { c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container"))
c.Fatalf("docker ps with --size should show virtual size of container")
}
} }
func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) { func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
@ -596,9 +495,7 @@ func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
// Make sure it DOESN'T show up w/o a '-a' for normal 'ps' // Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
out, _ = dockerCmd(c, "ps", "-q") out, _ = dockerCmd(c, "ps", "-q")
if strings.Contains(out, shortCID) { c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out))
c.Fatalf("Should have not seen '%s' in ps output:\n%s", shortCID, out)
}
// Make sure it DOES show up as 'Created' for 'ps -a' // Make sure it DOES show up as 'Created' for 'ps -a'
out, _ = dockerCmd(c, "ps", "-a") out, _ = dockerCmd(c, "ps", "-a")
@ -609,21 +506,15 @@ func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
continue continue
} }
hits++ hits++
if !strings.Contains(line, "Created") { c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line))
c.Fatalf("Missing 'Created' on '%s'", line)
}
} }
if hits != 1 { c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out))
c.Fatalf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)
}
// filter containers by 'create' - note, no -a needed // filter containers by 'create' - note, no -a needed
out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created") out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created")
containerOut := strings.TrimSpace(out) containerOut := strings.TrimSpace(out)
if !strings.HasPrefix(cID, containerOut) { c.Assert(cID, checker.HasPrefix, containerOut)
c.Fatalf("Expected id %s, got %s for filter, out: %s", cID, containerOut, out)
}
} }
func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) { func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
@ -640,9 +531,7 @@ func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
for _, l := range lines { for _, l := range lines {
names = append(names, l) names = append(names, l)
} }
if !reflect.DeepEqual(expected, names) { c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names))
c.Fatalf("Expected array with non-truncated names: %v, got: %v", expected, names)
}
//now list without turning off truncation and make sure we only get the non-link names //now list without turning off truncation and make sure we only get the non-link names
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}") out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
@ -652,9 +541,7 @@ func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
for _, l := range lines { for _, l := range lines {
truncNames = append(truncNames, l) truncNames = append(truncNames, l)
} }
if !reflect.DeepEqual(expected, truncNames) { c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames))
c.Fatalf("Expected array with truncated names: %v, got: %v", expected, truncNames)
}
} }
@ -662,16 +549,12 @@ func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
// make sure no-container "docker ps" still prints the header row // make sure no-container "docker ps" still prints the header row
out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}") out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}")
if out != "CONTAINER ID\n" { c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out))
c.Fatalf(`Expected 'CONTAINER ID\n', got %v`, out)
}
// verify that "docker ps" with a container still prints the header row also // verify that "docker ps" with a container still prints the header row also
dockerCmd(c, "run", "--name=test", "-d", "busybox", "top") dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}") out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}")
if out != "NAMES\ntest\n" { c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out))
c.Fatalf(`Expected 'NAMES\ntest\n', got %v`, out)
}
} }
func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) { func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
@ -680,19 +563,17 @@ func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
"psFormat": "{{ .ID }} default" "psFormat": "{{ .ID }} default"
}` }`
d, err := ioutil.TempDir("", "integration-cli-") d, err := ioutil.TempDir("", "integration-cli-")
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
defer os.RemoveAll(d) defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644) err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
out, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top") out, _ := dockerCmd(c, "run", "--name=test", "-d", "busybox", "top")
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
out, _ = dockerCmd(c, "--config", d, "ps", "-q") out, _ = dockerCmd(c, "--config", d, "ps", "-q")
if !strings.HasPrefix(id, strings.TrimSpace(out)) { c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
c.Fatalf("Expected to print only the container id, got %v\n", out)
}
} }
// Test for GitHub issue #12595 // Test for GitHub issue #12595
@ -704,48 +585,48 @@ func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName) runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
out, _, err := runCommandWithOutput(runCmd) out, _, err := runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
originalImageID, err := getIDByName(originalImageName) originalImageID, err := getIDByName(originalImageName)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top") runCmd = exec.Command(dockerBinary, "run", "-d", originalImageName, "top")
out, _, err = runCommandWithOutput(runCmd) out, _, err = runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
containerID := strings.TrimSpace(out) containerID := strings.TrimSpace(out)
linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n") lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
// skip header // skip header
lines = lines[1:] lines = lines[1:]
c.Assert(len(lines), check.Equals, 1) c.Assert(len(lines), checker.Equals, 1)
for _, line := range lines { for _, line := range lines {
f := strings.Fields(line) f := strings.Fields(line)
c.Assert(f[1], check.Equals, originalImageName) c.Assert(f[1], checker.Equals, originalImageName)
} }
runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName) runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
out, _, err = runCommandWithOutput(runCmd) out, _, err = runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName) runCmd = exec.Command(dockerBinary, "tag", "-f", updatedImageName, originalImageName)
out, _, err = runCommandWithOutput(runCmd) out, _, err = runCommandWithOutput(runCmd)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n") lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
// skip header // skip header
lines = lines[1:] lines = lines[1:]
c.Assert(len(lines), check.Equals, 1) c.Assert(len(lines), checker.Equals, 1)
for _, line := range lines { for _, line := range lines {
f := strings.Fields(line) f := strings.Fields(line)
c.Assert(f[1], check.Equals, originalImageID) c.Assert(f[1], checker.Equals, originalImageID)
} }
} }