261dc5e2 |
package main
import ( |
29ac29a4 |
"fmt" |
df46bfdd |
"io/ioutil"
"os" |
261dc5e2 |
"os/exec" |
df46bfdd |
"path/filepath" |
79298882 |
"sort" |
29ac29a4 |
"strconv" |
261dc5e2 |
"strings" |
41cbce8c |
"time" |
dc944ea7 |
|
83b03b00 |
"github.com/docker/docker/pkg/integration/checker" |
c1af0ac0 |
"github.com/docker/docker/pkg/stringid" |
79298882 |
"github.com/go-check/check" |
261dc5e2 |
)
|
06699f73 |
func (s *DockerSuite) TestPsListContainersBase(c *check.C) { |
2ebcb48c |
out, _ := runSleepingContainer(c, "-d") |
475c6531 |
firstID := strings.TrimSpace(out) |
261dc5e2 |
|
2ebcb48c |
out, _ = runSleepingContainer(c, "-d") |
475c6531 |
secondID := strings.TrimSpace(out) |
261dc5e2 |
// not long running |
a2787469 |
out, _ = dockerCmd(c, "run", "-d", "busybox", "true") |
475c6531 |
thirdID := strings.TrimSpace(out) |
261dc5e2 |
|
2ebcb48c |
out, _ = runSleepingContainer(c, "-d") |
475c6531 |
fourthID := strings.TrimSpace(out) |
261dc5e2 |
|
255b8444 |
// make sure the second is running |
83b03b00 |
c.Assert(waitRun(secondID), checker.IsNil) |
255b8444 |
|
261dc5e2 |
// make sure third one is not running |
a2787469 |
dockerCmd(c, "wait", thirdID) |
261dc5e2 |
|
255b8444 |
// make sure the forth is running |
83b03b00 |
c.Assert(waitRun(fourthID), checker.IsNil) |
255b8444 |
|
261dc5e2 |
// all |
a2787469 |
out, _ = dockerCmd(c, "ps", "-a") |
83b03b00 |
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)) |
261dc5e2 |
// running |
a2787469 |
out, _ = dockerCmd(c, "ps") |
83b03b00 |
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)) |
261dc5e2 |
// limit |
a2787469 |
out, _ = dockerCmd(c, "ps", "-n=2", "-a") |
261dc5e2 |
expected := []string{fourthID, thirdID} |
83b03b00 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
a2787469 |
out, _ = dockerCmd(c, "ps", "-n=2") |
83b03b00 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
// filter since
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a") |
261dc5e2 |
expected = []string{fourthID, thirdID, secondID} |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID) |
b41dba58 |
expected = []string{fourthID, secondID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
bc72883f |
out, _ = dockerCmd(c, "ps", "-f", "since="+thirdID)
expected = []string{fourthID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
1921c629 |
// filter before |
b41dba58 |
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
expected = []string{thirdID, secondID, firstID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
b41dba58 |
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID)
expected = []string{secondID, firstID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
bc72883f |
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
expected = []string{secondID, firstID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
|
1921c629 |
// filter since & before
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a") |
261dc5e2 |
expected = []string{thirdID, secondID} |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID) |
b41dba58 |
expected = []string{secondID}
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
// filter since & limit
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a") |
261dc5e2 |
expected = []string{fourthID, thirdID}
|
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2") |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
// filter before & limit
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a") |
261dc5e2 |
expected = []string{thirdID} |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1") |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
// filter since & filter before & limit
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a") |
261dc5e2 |
expected = []string{thirdID} |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out)) |
261dc5e2 |
|
1921c629 |
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1") |
b41dba58 |
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
}
|
261dc5e2 |
func assertContainerList(out string, expected []string) bool {
lines := strings.Split(strings.Trim(out, "\n "), "\n") |
b41dba58 |
|
261dc5e2 |
if len(lines)-1 != len(expected) {
return false
}
|
c0e63224 |
containerIDIndex := strings.Index(lines[0], "CONTAINER ID") |
261dc5e2 |
for i := 0; i < len(expected); i++ { |
c0e63224 |
foundID := lines[i+1][containerIDIndex : containerIDIndex+12] |
261dc5e2 |
if foundID != expected[i][:12] {
return false
}
}
return true
} |
41cbce8c |
|
8a90e8a1 |
func (s *DockerSuite) TestPsListContainersInvalidFilterName(c *check.C) {
out, _, err := dockerCmdWithError("ps", "-f", "invalidFilter=test")
c.Assert(err, checker.NotNil)
c.Assert(out, checker.Contains, "Invalid filter")
}
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersSize(c *check.C) { |
75d10745 |
// Problematic on Windows as it doesn't report the size correctly @swernli |
f9a3558a |
testRequires(c, DaemonIsLinux) |
e4468913 |
dockerCmd(c, "run", "-d", "busybox") |
a2787469 |
baseOut, _ := dockerCmd(c, "ps", "-s", "-n=1") |
ae907e7a |
baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
baseSizeIndex := strings.Index(baseLines[0], "SIZE")
baseFoundsize := baseLines[1][baseSizeIndex:]
baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0]) |
83b03b00 |
c.Assert(err, checker.IsNil) |
29ac29a4 |
|
41cbce8c |
name := "test_size" |
e4468913 |
dockerCmd(c, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test") |
41cbce8c |
id, err := getIDByName(name) |
83b03b00 |
c.Assert(err, checker.IsNil) |
41cbce8c |
|
a2787469 |
runCmd := exec.Command(dockerBinary, "ps", "-s", "-n=1") |
e4468913 |
var out string |
a2787469 |
|
41cbce8c |
wait := make(chan struct{})
go func() {
out, _, err = runCommandWithOutput(runCmd)
close(wait)
}()
select {
case <-wait:
case <-time.After(3 * time.Second): |
dc944ea7 |
c.Fatalf("Calling \"docker ps -s\" timed out!") |
41cbce8c |
} |
83b03b00 |
c.Assert(err, checker.IsNil) |
41cbce8c |
lines := strings.Split(strings.Trim(out, "\n "), "\n") |
83b03b00 |
c.Assert(lines, checker.HasLen, 2, check.Commentf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))) |
41cbce8c |
sizeIndex := strings.Index(lines[0], "SIZE")
idIndex := strings.Index(lines[0], "CONTAINER ID")
foundID := lines[1][idIndex : idIndex+12] |
83b03b00 |
c.Assert(foundID, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s", id[:12], foundID)) |
ae907e7a |
expectedSize := fmt.Sprintf("%d B", (2 + baseBytes)) |
41cbce8c |
foundSize := lines[1][sizeIndex:] |
83b03b00 |
c.Assert(foundSize, checker.Contains, expectedSize, check.Commentf("Expected size %q, got %q", expectedSize, foundSize)) |
ea09f036 |
}
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) { |
ea09f036 |
// start exited container |
a2787469 |
out, _ := dockerCmd(c, "run", "-d", "busybox") |
475c6531 |
firstID := strings.TrimSpace(out) |
ea09f036 |
|
2a07b19f |
// make sure the exited container is not running |
a2787469 |
dockerCmd(c, "wait", firstID) |
ea09f036 |
// start running container |
a2787469 |
out, _ = dockerCmd(c, "run", "-itd", "busybox") |
475c6531 |
secondID := strings.TrimSpace(out) |
ea09f036 |
// filter containers by exited |
2a07b19f |
out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=exited") |
ea09f036 |
containerOut := strings.TrimSpace(out) |
2a07b19f |
c.Assert(containerOut, checker.Equals, firstID) |
ea09f036 |
|
2a07b19f |
out, _ = dockerCmd(c, "ps", "-a", "--no-trunc", "-q", "--filter=status=running") |
ea09f036 |
containerOut = strings.TrimSpace(out) |
2a07b19f |
c.Assert(containerOut, checker.Equals, secondID) |
ea09f036 |
|
7bf26d44 |
out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish") |
83b03b00 |
c.Assert(out, checker.Contains, "Unrecognised filter value for status", check.Commentf("Expected error response due to invalid status filter output: %q", out)) |
7bf26d44 |
|
75d10745 |
// Windows doesn't support pausing of containers
if daemonPlatform != "windows" {
// pause running container
out, _ = dockerCmd(c, "run", "-itd", "busybox")
pausedID := strings.TrimSpace(out)
dockerCmd(c, "pause", pausedID)
// make sure the container is unpaused to let the daemon stop it properly
defer func() { dockerCmd(c, "unpause", pausedID) }()
out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused")
containerOut = strings.TrimSpace(out)
c.Assert(containerOut, checker.Equals, pausedID)
} |
41cbce8c |
} |
16346253 |
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) { |
16346253 |
// start container |
a2787469 |
out, _ := dockerCmd(c, "run", "-d", "busybox") |
475c6531 |
firstID := strings.TrimSpace(out) |
16346253 |
// start another container |
777ee34b |
runSleepingContainer(c) |
16346253 |
// filter containers by id |
a2787469 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID) |
16346253 |
containerOut := strings.TrimSpace(out) |
83b03b00 |
c.Assert(containerOut, checker.Equals, firstID[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)) |
16346253 |
}
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) { |
16346253 |
// start container |
e4468913 |
dockerCmd(c, "run", "--name=a_name_to_match", "busybox")
id, err := getIDByName("a_name_to_match")
c.Assert(err, check.IsNil) |
16346253 |
// start another container |
777ee34b |
runSleepingContainer(c, "--name=b_name_to_match") |
16346253 |
// filter containers by name |
e4468913 |
out, _ := dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match") |
16346253 |
containerOut := strings.TrimSpace(out) |
e4468913 |
c.Assert(containerOut, checker.Equals, id[:12], check.Commentf("Expected id %s, got %s for exited filter, output: %q", id[:12], containerOut, out)) |
16346253 |
} |
03ea2166 |
|
c1af0ac0 |
// Test for the ancestor filter for ps.
// There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
//
// What the test setups :
// - Create 2 image based on busybox using the same repository but different tags
// - Create an image based on the previous image (images_ps_filter_test2)
// - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
// - Filter them out :P
func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
// Build images
imageName1 := "images_ps_filter_test1"
imageID1, err := buildImage(imageName1,
`FROM busybox
LABEL match me 1`, true) |
83b03b00 |
c.Assert(err, checker.IsNil) |
c1af0ac0 |
imageName1Tagged := "images_ps_filter_test1:tag"
imageID1Tagged, err := buildImage(imageName1Tagged,
`FROM busybox
LABEL match me 1 tagged`, true) |
83b03b00 |
c.Assert(err, checker.IsNil) |
c1af0ac0 |
imageName2 := "images_ps_filter_test2"
imageID2, err := buildImage(imageName2,
fmt.Sprintf(`FROM %s
LABEL match me 2`, imageName1), true) |
83b03b00 |
c.Assert(err, checker.IsNil) |
c1af0ac0 |
// start containers |
e4468913 |
dockerCmd(c, "run", "--name=first", "busybox", "echo", "hello")
firstID, err := getIDByName("first")
c.Assert(err, check.IsNil) |
c1af0ac0 |
// start another container |
e4468913 |
dockerCmd(c, "run", "--name=second", "busybox", "echo", "hello")
secondID, err := getIDByName("second")
c.Assert(err, check.IsNil) |
c1af0ac0 |
// start third container |
e4468913 |
dockerCmd(c, "run", "--name=third", imageName1, "echo", "hello")
thirdID, err := getIDByName("third")
c.Assert(err, check.IsNil) |
c1af0ac0 |
// start fourth container |
e4468913 |
dockerCmd(c, "run", "--name=fourth", imageName1Tagged, "echo", "hello")
fourthID, err := getIDByName("fourth")
c.Assert(err, check.IsNil) |
c1af0ac0 |
// start fifth container |
e4468913 |
dockerCmd(c, "run", "--name=fifth", imageName2, "echo", "hello")
fifthID, err := getIDByName("fifth")
c.Assert(err, check.IsNil) |
c1af0ac0 |
var filterTestSuite = []struct {
filterName string
expectedIDs []string
}{
// non existent stuff
{"nonexistent", []string{}},
{"nonexistent:tag", []string{}},
// image
{"busybox", []string{firstID, secondID, thirdID, fourthID, fifthID}},
{imageName1, []string{thirdID, fifthID}},
{imageName2, []string{fifthID}},
// image:tag
{fmt.Sprintf("%s:latest", imageName1), []string{thirdID, fifthID}},
{imageName1Tagged, []string{fourthID}},
// short-id
{stringid.TruncateID(imageID1), []string{thirdID, fifthID}},
{stringid.TruncateID(imageID2), []string{fifthID}},
// full-id
{imageID1, []string{thirdID, fifthID}},
{imageID1Tagged, []string{fourthID}},
{imageID2, []string{fifthID}},
}
|
e4468913 |
var out string |
c1af0ac0 |
for _, filter := range filterTestSuite {
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
}
// Multiple ancestor filter
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
}
func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
actualIDs := []string{}
if out != "" {
actualIDs = strings.Split(out[:len(out)-1], "\n")
}
sort.Strings(actualIDs)
sort.Strings(expectedIDs)
|
83b03b00 |
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)) |
c1af0ac0 |
if len(expectedIDs) > 0 {
same := true
for i := range expectedIDs {
if actualIDs[i] != expectedIDs[i] {
c.Logf("%s, %s", actualIDs[i], expectedIDs[i])
same = false
break
}
} |
83b03b00 |
c.Assert(same, checker.Equals, true, check.Commentf("Expected filtered container(s) for %s ancestor filter to be %v, got %v", filterName, expectedIDs, actualIDs)) |
c1af0ac0 |
}
}
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) { |
abb5e9a0 |
// start container |
e4468913 |
dockerCmd(c, "run", "--name=first", "-l", "match=me", "-l", "second=tag", "busybox")
firstID, err := getIDByName("first")
c.Assert(err, check.IsNil) |
abb5e9a0 |
// start another container |
e4468913 |
dockerCmd(c, "run", "--name=second", "-l", "match=me too", "busybox")
secondID, err := getIDByName("second")
c.Assert(err, check.IsNil) |
abb5e9a0 |
// start third container |
e4468913 |
dockerCmd(c, "run", "--name=third", "-l", "nomatch=me", "busybox")
thirdID, err := getIDByName("third")
c.Assert(err, check.IsNil) |
abb5e9a0 |
// filter containers by exact match |
e4468913 |
out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me") |
abb5e9a0 |
containerOut := strings.TrimSpace(out) |
83b03b00 |
c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)) |
abb5e9a0 |
|
5de1298f |
// filter containers by two labels |
a2787469 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag") |
5de1298f |
containerOut = strings.TrimSpace(out) |
83b03b00 |
c.Assert(containerOut, checker.Equals, firstID, check.Commentf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)) |
5de1298f |
// filter containers by two labels, but expect not found because of AND behavior |
a2787469 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no") |
5de1298f |
containerOut = strings.TrimSpace(out) |
83b03b00 |
c.Assert(containerOut, checker.Equals, "", check.Commentf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)) |
5de1298f |
|
abb5e9a0 |
// filter containers by exact key |
a2787469 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match") |
abb5e9a0 |
containerOut = strings.TrimSpace(out) |
83b03b00 |
c.Assert(containerOut, checker.Contains, firstID)
c.Assert(containerOut, checker.Contains, secondID)
c.Assert(containerOut, checker.Not(checker.Contains), thirdID) |
abb5e9a0 |
}
|
dc944ea7 |
func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) { |
777ee34b |
runSleepingContainer(c, "--name=sleep") |
81f84023 |
|
a2787469 |
dockerCmd(c, "run", "--name", "zero1", "busybox", "true") |
03ea2166 |
firstZero, err := getIDByName("zero1") |
83b03b00 |
c.Assert(err, checker.IsNil) |
03ea2166 |
|
a2787469 |
dockerCmd(c, "run", "--name", "zero2", "busybox", "true") |
03ea2166 |
secondZero, err := getIDByName("zero2") |
83b03b00 |
c.Assert(err, checker.IsNil) |
03ea2166 |
|
83b03b00 |
out, _, err := dockerCmdWithError("run", "--name", "nonzero1", "busybox", "false")
c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err)) |
a2787469 |
|
03ea2166 |
firstNonZero, err := getIDByName("nonzero1") |
83b03b00 |
c.Assert(err, checker.IsNil) |
03ea2166 |
|
83b03b00 |
out, _, err = dockerCmdWithError("run", "--name", "nonzero2", "busybox", "false")
c.Assert(err, checker.NotNil, check.Commentf("Should fail.", out, err)) |
03ea2166 |
secondNonZero, err := getIDByName("nonzero2") |
83b03b00 |
c.Assert(err, checker.IsNil) |
03ea2166 |
// filter containers by exited=0 |
83b03b00 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0") |
03ea2166 |
ids := strings.Split(strings.TrimSpace(out), "\n") |
83b03b00 |
c.Assert(ids, checker.HasLen, 2, check.Commentf("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])) |
03ea2166 |
|
a2787469 |
out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1") |
03ea2166 |
ids = strings.Split(strings.TrimSpace(out), "\n") |
83b03b00 |
c.Assert(ids, checker.HasLen, 2, check.Commentf("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])) |
81f84023 |
|
03ea2166 |
} |
e45deceb |
|
dc944ea7 |
func (s *DockerSuite) TestPsRightTagName(c *check.C) { |
75d10745 |
// TODO Investigate further why this fails on Windows to Windows CI |
f9a3558a |
testRequires(c, DaemonIsLinux) |
e45deceb |
tag := "asybox:shmatest" |
5c295460 |
dockerCmd(c, "tag", "busybox", tag) |
e45deceb |
var id1 string |
777ee34b |
out, _ := runSleepingContainer(c) |
5c295460 |
id1 = strings.TrimSpace(string(out)) |
e45deceb |
var id2 string |
777ee34b |
out, _ = runSleepingContainerInImage(c, tag) |
5c295460 |
id2 = strings.TrimSpace(string(out)) |
645c020f |
var imageID string |
62a856e9 |
out = inspectField(c, "busybox", "Id") |
5c295460 |
imageID = strings.TrimSpace(string(out)) |
645c020f |
var id3 string |
777ee34b |
out, _ = runSleepingContainerInImage(c, imageID) |
5c295460 |
id3 = strings.TrimSpace(string(out)) |
645c020f |
|
5c295460 |
out, _ = dockerCmd(c, "ps", "--no-trunc") |
e45deceb |
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
// skip header
lines = lines[1:] |
83b03b00 |
c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines))) |
e45deceb |
for _, line := range lines {
f := strings.Fields(line)
switch f[0] {
case id1: |
83b03b00 |
c.Assert(f[1], checker.Equals, "busybox", check.Commentf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])) |
e45deceb |
case id2: |
83b03b00 |
c.Assert(f[1], checker.Equals, tag, check.Commentf("Expected %s tag for id %s, got %s", tag, id2, f[1])) |
645c020f |
case id3: |
83b03b00 |
c.Assert(f[1], checker.Equals, imageID, check.Commentf("Expected %s imageID for id %s, got %s", tag, id3, f[1])) |
e45deceb |
default: |
dc944ea7 |
c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3) |
e45deceb |
}
}
} |
2a3225eb |
|
dc944ea7 |
func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) { |
75d10745 |
// Problematic on Windows as it doesn't support links as of Jan 2016 |
f9a3558a |
testRequires(c, DaemonIsLinux) |
777ee34b |
runSleepingContainer(c, "--name=first")
runSleepingContainer(c, "--name=second", "--link=first:first") |
5c295460 |
out, _ := dockerCmd(c, "ps", "--no-trunc") |
2a3225eb |
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
// strip header
lines = lines[1:]
expected := []string{"second", "first,second/first"}
var names []string
for _, l := range lines {
fields := strings.Fields(l)
names = append(names, fields[len(fields)-1])
} |
83b03b00 |
c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array: %v, got: %v", expected, names)) |
2a3225eb |
} |
ba99a73c |
|
dc944ea7 |
func (s *DockerSuite) TestPsGroupPortRange(c *check.C) { |
75d10745 |
// Problematic on Windows as it doesn't support port ranges as of Jan 2016 |
f9a3558a |
testRequires(c, DaemonIsLinux) |
bdb354f1 |
portRange := "3850-3900" |
a2787469 |
dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top") |
ba99a73c |
|
a2787469 |
out, _ := dockerCmd(c, "ps") |
ba99a73c |
|
83b03b00 |
c.Assert(string(out), checker.Contains, portRange, check.Commentf("docker ps output should have had the port range %q: %s", portRange, string(out))) |
ba99a73c |
} |
d0de2b1e |
func (s *DockerSuite) TestPsWithSize(c *check.C) { |
75d10745 |
// Problematic on Windows as it doesn't report the size correctly @swernli |
f9a3558a |
testRequires(c, DaemonIsLinux) |
5c295460 |
dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top")
out, _ := dockerCmd(c, "ps", "--size") |
83b03b00 |
c.Assert(out, checker.Contains, "virtual", check.Commentf("docker ps with --size should show virtual size of container")) |
d0de2b1e |
} |
dc697b1c |
func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
// create a container |
5c295460 |
out, _ := dockerCmd(c, "create", "busybox") |
dc697b1c |
cID := strings.TrimSpace(out)
shortCID := cID[:12]
// Make sure it DOESN'T show up w/o a '-a' for normal 'ps' |
5c295460 |
out, _ = dockerCmd(c, "ps", "-q") |
83b03b00 |
c.Assert(out, checker.Not(checker.Contains), shortCID, check.Commentf("Should have not seen '%s' in ps output:\n%s", shortCID, out)) |
dc697b1c |
// Make sure it DOES show up as 'Created' for 'ps -a' |
5c295460 |
out, _ = dockerCmd(c, "ps", "-a") |
dc697b1c |
hits := 0
for _, line := range strings.Split(out, "\n") {
if !strings.Contains(line, shortCID) {
continue
}
hits++ |
83b03b00 |
c.Assert(line, checker.Contains, "Created", check.Commentf("Missing 'Created' on '%s'", line)) |
dc697b1c |
}
|
83b03b00 |
c.Assert(hits, checker.Equals, 1, check.Commentf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out)) |
dc697b1c |
// filter containers by 'create' - note, no -a needed |
5c295460 |
out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created") |
dc697b1c |
containerOut := strings.TrimSpace(out) |
83b03b00 |
c.Assert(cID, checker.HasPrefix, containerOut) |
dc697b1c |
} |
542b58d8 |
func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) { |
75d10745 |
// Problematic on Windows as it doesn't support link as of Jan 2016 |
f9a3558a |
testRequires(c, DaemonIsLinux) |
542b58d8 |
//create 2 containers and link them
dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
//use the new format capabilities to only list the names and --no-trunc to get all names
out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
expected := []string{"parent", "child,parent/linkedone"}
var names []string
for _, l := range lines {
names = append(names, l)
} |
83b03b00 |
c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with non-truncated names: %v, got: %v", expected, names)) |
542b58d8 |
//now list without turning off truncation and make sure we only get the non-link names
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
expected = []string{"parent", "child"}
var truncNames []string
for _, l := range lines {
truncNames = append(truncNames, l)
} |
83b03b00 |
c.Assert(expected, checker.DeepEquals, truncNames, check.Commentf("Expected array with truncated names: %v, got: %v", expected, truncNames)) |
68ade49a |
}
// Test for GitHub issue #21772
func (s *DockerSuite) TestPsNamesMultipleTime(c *check.C) {
runSleepingContainer(c, "--name=test1")
runSleepingContainer(c, "--name=test2") |
542b58d8 |
|
68ade49a |
//use the new format capabilities to list the names twice
out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Names}}")
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
expected := []string{"test2 test2", "test1 test1"}
var names []string
for _, l := range lines {
names = append(names, l)
}
c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with names displayed twice: %v, got: %v", expected, names)) |
542b58d8 |
} |
f57fc03e |
func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
// make sure no-container "docker ps" still prints the header row
out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}") |
83b03b00 |
c.Assert(out, checker.Equals, "CONTAINER ID\n", check.Commentf(`Expected 'CONTAINER ID\n', got %v`, out)) |
f57fc03e |
// verify that "docker ps" with a container still prints the header row also |
777ee34b |
runSleepingContainer(c, "--name=test") |
f57fc03e |
out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}") |
83b03b00 |
c.Assert(out, checker.Equals, "NAMES\ntest\n", check.Commentf(`Expected 'NAMES\ntest\n', got %v`, out)) |
f57fc03e |
} |
df46bfdd |
func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
config := `{ |
34a3c3ca |
"psFormat": "default {{ .ID }}" |
df46bfdd |
}`
d, err := ioutil.TempDir("", "integration-cli-") |
83b03b00 |
c.Assert(err, checker.IsNil) |
df46bfdd |
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644) |
83b03b00 |
c.Assert(err, checker.IsNil) |
df46bfdd |
|
777ee34b |
out, _ := runSleepingContainer(c, "--name=test") |
df46bfdd |
id := strings.TrimSpace(out)
out, _ = dockerCmd(c, "--config", d, "ps", "-q") |
83b03b00 |
c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out)) |
df46bfdd |
} |
2167f40a |
// Test for GitHub issue #12595
func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) { |
75d10745 |
// TODO: Investigate why this fails on Windows to Windows CI further. |
f9a3558a |
testRequires(c, DaemonIsLinux) |
2167f40a |
originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
runCmd := exec.Command(dockerBinary, "tag", "busybox:latest", originalImageName)
out, _, err := runCommandWithOutput(runCmd) |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
originalImageID, err := getIDByName(originalImageName) |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
|
777ee34b |
runCmd = exec.Command(dockerBinary, append([]string{"run", "-d", originalImageName}, defaultSleepCommand...)...) |
2167f40a |
out, _, err = runCommandWithOutput(runCmd) |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
containerID := strings.TrimSpace(out)
linesOut, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
lines := strings.Split(strings.TrimSpace(string(linesOut)), "\n")
// skip header
lines = lines[1:] |
83b03b00 |
c.Assert(len(lines), checker.Equals, 1) |
2167f40a |
for _, line := range lines {
f := strings.Fields(line) |
83b03b00 |
c.Assert(f[1], checker.Equals, originalImageName) |
2167f40a |
}
runCmd = exec.Command(dockerBinary, "commit", containerID, updatedImageName)
out, _, err = runCommandWithOutput(runCmd) |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
|
4455ec14 |
runCmd = exec.Command(dockerBinary, "tag", updatedImageName, originalImageName) |
2167f40a |
out, _, err = runCommandWithOutput(runCmd) |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
linesOut, err = exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput() |
83b03b00 |
c.Assert(err, checker.IsNil) |
2167f40a |
lines = strings.Split(strings.TrimSpace(string(linesOut)), "\n")
// skip header
lines = lines[1:] |
83b03b00 |
c.Assert(len(lines), checker.Equals, 1) |
2167f40a |
for _, line := range lines {
f := strings.Fields(line) |
83b03b00 |
c.Assert(f[1], checker.Equals, originalImageID) |
2167f40a |
}
} |
6025517b |
func (s *DockerSuite) TestPsNotShowPortsOfStoppedContainer(c *check.C) { |
845dc1b2 |
testRequires(c, DaemonIsLinux) |
6025517b |
dockerCmd(c, "run", "--name=foo", "-d", "-p", "5000:5000", "busybox", "top")
c.Assert(waitRun("foo"), checker.IsNil)
out, _ := dockerCmd(c, "ps")
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
expected := "0.0.0.0:5000->5000/tcp"
fields := strings.Fields(lines[1])
c.Assert(fields[len(fields)-2], checker.Equals, expected, check.Commentf("Expected: %v, got: %v", expected, fields[len(fields)-2]))
dockerCmd(c, "kill", "foo")
dockerCmd(c, "wait", "foo")
out, _ = dockerCmd(c, "ps", "-l")
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
fields = strings.Fields(lines[1])
c.Assert(fields[len(fields)-2], checker.Not(checker.Equals), expected, check.Commentf("Should not got %v", expected))
} |
bd4fb00f |
func (s *DockerSuite) TestPsShowMounts(c *check.C) {
prefix, slash := getPrefixAndSlashFromDaemonPlatform()
mp := prefix + slash + "test"
dockerCmd(c, "volume", "create", "--name", "ps-volume-test") |
cdd7fddf |
// volume mount containers |
bd4fb00f |
runSleepingContainer(c, "--name=volume-test-1", "--volume", "ps-volume-test:"+mp)
c.Assert(waitRun("volume-test-1"), checker.IsNil)
runSleepingContainer(c, "--name=volume-test-2", "--volume", mp)
c.Assert(waitRun("volume-test-2"), checker.IsNil) |
cdd7fddf |
// bind mount container
var bindMountSource string
var bindMountDestination string
if DaemonIsWindows.Condition() {
bindMountSource = "c:\\"
bindMountDestination = "c:\\t"
} else {
bindMountSource = "/tmp"
bindMountDestination = "/t"
}
runSleepingContainer(c, "--name=bind-mount-test", "-v", bindMountSource+":"+bindMountDestination)
c.Assert(waitRun("bind-mount-test"), checker.IsNil) |
bd4fb00f |
out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}")
lines := strings.Split(strings.TrimSpace(string(out)), "\n") |
cdd7fddf |
c.Assert(lines, checker.HasLen, 3) |
bd4fb00f |
fields := strings.Fields(lines[0])
c.Assert(fields, checker.HasLen, 2) |
cdd7fddf |
c.Assert(fields[0], checker.Equals, "bind-mount-test")
c.Assert(fields[1], checker.Equals, bindMountSource)
fields = strings.Fields(lines[1])
c.Assert(fields, checker.HasLen, 2) |
bd4fb00f |
annonymounsVolumeID := fields[1]
|
cdd7fddf |
fields = strings.Fields(lines[2]) |
bd4fb00f |
c.Assert(fields[1], checker.Equals, "ps-volume-test")
// filter by volume name
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=ps-volume-test")
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
c.Assert(lines, checker.HasLen, 1)
fields = strings.Fields(lines[0])
c.Assert(fields[1], checker.Equals, "ps-volume-test")
// empty results filtering by unknown volume
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=this-volume-should-not-exist")
c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
// filter by mount destination
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+mp)
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
c.Assert(lines, checker.HasLen, 2)
fields = strings.Fields(lines[0])
c.Assert(fields[1], checker.Equals, annonymounsVolumeID)
fields = strings.Fields(lines[1])
c.Assert(fields[1], checker.Equals, "ps-volume-test")
|
cdd7fddf |
// filter by bind mount source
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountSource)
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
c.Assert(lines, checker.HasLen, 1)
fields = strings.Fields(lines[0])
c.Assert(fields, checker.HasLen, 2)
c.Assert(fields[0], checker.Equals, "bind-mount-test")
c.Assert(fields[1], checker.Equals, bindMountSource)
// filter by bind mount destination
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountDestination)
lines = strings.Split(strings.TrimSpace(string(out)), "\n")
c.Assert(lines, checker.HasLen, 1)
fields = strings.Fields(lines[0])
c.Assert(fields, checker.HasLen, 2)
c.Assert(fields[0], checker.Equals, "bind-mount-test")
c.Assert(fields[1], checker.Equals, bindMountSource)
|
bd4fb00f |
// empty results filtering by unknown mount point
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+prefix+slash+"this-path-was-never-mounted")
c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
} |
55cdb6dc |
func (s *DockerSuite) TestPsFormatSize(c *check.C) {
testRequires(c, DaemonIsLinux)
runSleepingContainer(c)
out, _ := dockerCmd(c, "ps", "--format", "table {{.Size}}")
lines := strings.Split(out, "\n")
c.Assert(lines[1], checker.Not(checker.Equals), "0 B", check.Commentf("Should not display a size of 0 B"))
out, _ = dockerCmd(c, "ps", "--size", "--format", "table {{.Size}}")
lines = strings.Split(out, "\n")
c.Assert(lines[0], checker.Equals, "SIZE", check.Commentf("Should only have one size column"))
out, _ = dockerCmd(c, "ps", "--size", "--format", "raw")
lines = strings.Split(out, "\n")
c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
} |
912af1ae |
func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) { |
7c46ba02 |
// TODO default network on Windows is not called "bridge", and creating a
// custom network fails on Windows fails with "Error response from daemon: plugin not found")
testRequires(c, DaemonIsLinux)
// create some containers
runSleepingContainer(c, "--net=bridge", "--name=onbridgenetwork")
runSleepingContainer(c, "--net=none", "--name=onnonenetwork")
// Filter docker ps on non existing network
out, _ := dockerCmd(c, "ps", "--filter", "network=doesnotexist")
containerOut := strings.TrimSpace(string(out))
lines := strings.Split(containerOut, "\n")
// skip header
lines = lines[1:]
// ps output should have no containers
c.Assert(lines, checker.HasLen, 0) |
912af1ae |
// Filter docker ps on network bridge
out, _ = dockerCmd(c, "ps", "--filter", "network=bridge") |
7c46ba02 |
containerOut = strings.TrimSpace(string(out)) |
912af1ae |
|
7c46ba02 |
lines = strings.Split(containerOut, "\n") |
912af1ae |
// skip header
lines = lines[1:]
// ps output should have only one container
c.Assert(lines, checker.HasLen, 1)
// Making sure onbridgenetwork is on the output |
7c46ba02 |
c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n")) |
912af1ae |
// Filter docker ps on networks bridge and none
out, _ = dockerCmd(c, "ps", "--filter", "network=bridge", "--filter", "network=none")
containerOut = strings.TrimSpace(string(out))
lines = strings.Split(containerOut, "\n")
// skip header
lines = lines[1:]
//ps output should have both the containers
c.Assert(lines, checker.HasLen, 2)
// Making sure onbridgenetwork and onnonenetwork is on the output |
7c46ba02 |
c.Assert(containerOut, checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on bridge network\n"))
nwID, _ := dockerCmd(c, "network", "inspect", "--format", "{{.ID}}", "bridge")
// Filter by network ID
out, _ = dockerCmd(c, "ps", "--filter", "network="+nwID)
containerOut = strings.TrimSpace(string(out))
c.Assert(containerOut, checker.Contains, "onbridgenetwork") |
912af1ae |
} |
8b9d96d2 |
func (s *DockerSuite) TestPsByOrder(c *check.C) {
name1 := "xyz-abc"
out, err := runSleepingContainer(c, "--name", name1)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
container1 := strings.TrimSpace(out)
name2 := "xyz-123"
out, err = runSleepingContainer(c, "--name", name2)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
container2 := strings.TrimSpace(out)
name3 := "789-abc"
out, err = runSleepingContainer(c, "--name", name3)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
name4 := "789-123"
out, err = runSleepingContainer(c, "--name", name4)
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Not(checker.Equals), "")
// Run multiple time should have the same result
out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz")
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1))
// Run multiple time should have the same result
out, err = dockerCmd(c, "ps", "--no-trunc", "-q", "-f", "name=xyz")
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Equals, fmt.Sprintf("%s\n%s", container2, container1))
} |