github.com/circular-dark/docker@v1.7.0/integration-cli/docker_cli_ps_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"reflect"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/go-check/check"
    12  )
    13  
    14  func (s *DockerSuite) TestPsListContainers(c *check.C) {
    15  
    16  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    17  	out, _, err := runCommandWithOutput(runCmd)
    18  	if err != nil {
    19  		c.Fatal(out, err)
    20  	}
    21  	firstID := strings.TrimSpace(out)
    22  
    23  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    24  	out, _, err = runCommandWithOutput(runCmd)
    25  	if err != nil {
    26  		c.Fatal(out, err)
    27  	}
    28  	secondID := strings.TrimSpace(out)
    29  
    30  	// not long running
    31  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
    32  	out, _, err = runCommandWithOutput(runCmd)
    33  	if err != nil {
    34  		c.Fatal(out, err)
    35  	}
    36  	thirdID := strings.TrimSpace(out)
    37  
    38  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    39  	out, _, err = runCommandWithOutput(runCmd)
    40  	if err != nil {
    41  		c.Fatal(out, err)
    42  	}
    43  	fourthID := strings.TrimSpace(out)
    44  
    45  	// make sure the second is running
    46  	if err := waitRun(secondID); err != nil {
    47  		c.Fatalf("waiting for container failed: %v", err)
    48  	}
    49  
    50  	// make sure third one is not running
    51  	runCmd = exec.Command(dockerBinary, "wait", thirdID)
    52  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    53  		c.Fatal(out, err)
    54  	}
    55  
    56  	// make sure the forth is running
    57  	if err := waitRun(fourthID); err != nil {
    58  		c.Fatalf("waiting for container failed: %v", err)
    59  	}
    60  
    61  	// all
    62  	runCmd = exec.Command(dockerBinary, "ps", "-a")
    63  	out, _, err = runCommandWithOutput(runCmd)
    64  	if err != nil {
    65  		c.Fatal(out, err)
    66  	}
    67  
    68  	if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
    69  		c.Errorf("Container list is not in the correct order: %s", out)
    70  	}
    71  
    72  	// running
    73  	runCmd = exec.Command(dockerBinary, "ps")
    74  	out, _, err = runCommandWithOutput(runCmd)
    75  	if err != nil {
    76  		c.Fatal(out, err)
    77  	}
    78  
    79  	if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
    80  		c.Errorf("Container list is not in the correct order: %s", out)
    81  	}
    82  
    83  	// from here all flag '-a' is ignored
    84  
    85  	// limit
    86  	runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
    87  	out, _, err = runCommandWithOutput(runCmd)
    88  	if err != nil {
    89  		c.Fatal(out, err)
    90  	}
    91  	expected := []string{fourthID, thirdID}
    92  
    93  	if !assertContainerList(out, expected) {
    94  		c.Errorf("Container list is not in the correct order: %s", out)
    95  	}
    96  
    97  	runCmd = exec.Command(dockerBinary, "ps", "-n=2")
    98  	out, _, err = runCommandWithOutput(runCmd)
    99  	if err != nil {
   100  		c.Fatal(out, err)
   101  	}
   102  
   103  	if !assertContainerList(out, expected) {
   104  		c.Errorf("Container list is not in the correct order: %s", out)
   105  	}
   106  
   107  	// since
   108  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
   109  	out, _, err = runCommandWithOutput(runCmd)
   110  	if err != nil {
   111  		c.Fatal(out, err)
   112  	}
   113  	expected = []string{fourthID, thirdID, secondID}
   114  
   115  	if !assertContainerList(out, expected) {
   116  		c.Errorf("Container list is not in the correct order: %s", out)
   117  	}
   118  
   119  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
   120  	out, _, err = runCommandWithOutput(runCmd)
   121  	if err != nil {
   122  		c.Fatal(out, err)
   123  	}
   124  
   125  	if !assertContainerList(out, expected) {
   126  		c.Errorf("Container list is not in the correct order: %s", out)
   127  	}
   128  
   129  	// before
   130  	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
   131  	out, _, err = runCommandWithOutput(runCmd)
   132  	if err != nil {
   133  		c.Fatal(out, err)
   134  	}
   135  	expected = []string{secondID, firstID}
   136  
   137  	if !assertContainerList(out, expected) {
   138  		c.Errorf("Container list is not in the correct order: %s", out)
   139  	}
   140  
   141  	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
   142  	out, _, err = runCommandWithOutput(runCmd)
   143  	if err != nil {
   144  		c.Fatal(out, err)
   145  	}
   146  
   147  	if !assertContainerList(out, expected) {
   148  		c.Errorf("Container list is not in the correct order: %s", out)
   149  	}
   150  
   151  	// since & before
   152  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
   153  	out, _, err = runCommandWithOutput(runCmd)
   154  	if err != nil {
   155  		c.Fatal(out, err)
   156  	}
   157  	expected = []string{thirdID, secondID}
   158  
   159  	if !assertContainerList(out, expected) {
   160  		c.Errorf("Container list is not in the correct order: %s", out)
   161  	}
   162  
   163  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
   164  	out, _, err = runCommandWithOutput(runCmd)
   165  	if err != nil {
   166  		c.Fatal(out, err)
   167  	}
   168  	if !assertContainerList(out, expected) {
   169  		c.Errorf("Container list is not in the correct order: %s", out)
   170  	}
   171  
   172  	// since & limit
   173  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
   174  	out, _, err = runCommandWithOutput(runCmd)
   175  	if err != nil {
   176  		c.Fatal(out, err)
   177  	}
   178  	expected = []string{fourthID, thirdID}
   179  
   180  	if !assertContainerList(out, expected) {
   181  		c.Errorf("Container list is not in the correct order: %s", out)
   182  	}
   183  
   184  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
   185  	out, _, err = runCommandWithOutput(runCmd)
   186  	if err != nil {
   187  		c.Fatal(out, err)
   188  	}
   189  
   190  	if !assertContainerList(out, expected) {
   191  		c.Errorf("Container list is not in the correct order: %s", out)
   192  	}
   193  
   194  	// before & limit
   195  	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
   196  	out, _, err = runCommandWithOutput(runCmd)
   197  	if err != nil {
   198  		c.Fatal(out, err)
   199  	}
   200  	expected = []string{thirdID}
   201  
   202  	if !assertContainerList(out, expected) {
   203  		c.Errorf("Container list is not in the correct order: %s", out)
   204  	}
   205  
   206  	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
   207  	out, _, err = runCommandWithOutput(runCmd)
   208  	if err != nil {
   209  		c.Fatal(out, err)
   210  	}
   211  
   212  	if !assertContainerList(out, expected) {
   213  		c.Errorf("Container list is not in the correct order: %s", out)
   214  	}
   215  
   216  	// since & before & limit
   217  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
   218  	out, _, err = runCommandWithOutput(runCmd)
   219  	if err != nil {
   220  		c.Fatal(out, err)
   221  	}
   222  	expected = []string{thirdID}
   223  
   224  	if !assertContainerList(out, expected) {
   225  		c.Errorf("Container list is not in the correct order: %s", out)
   226  	}
   227  
   228  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
   229  	out, _, err = runCommandWithOutput(runCmd)
   230  	if err != nil {
   231  		c.Fatal(out, err)
   232  	}
   233  
   234  	if !assertContainerList(out, expected) {
   235  		c.Errorf("Container list is not in the correct order: %s", out)
   236  	}
   237  
   238  }
   239  
   240  func assertContainerList(out string, expected []string) bool {
   241  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   242  	if len(lines)-1 != len(expected) {
   243  		return false
   244  	}
   245  
   246  	containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
   247  	for i := 0; i < len(expected); i++ {
   248  		foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
   249  		if foundID != expected[i][:12] {
   250  			return false
   251  		}
   252  	}
   253  
   254  	return true
   255  }
   256  
   257  func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
   258  	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
   259  	runCommandWithOutput(cmd)
   260  	cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
   261  	baseOut, _, err := runCommandWithOutput(cmd)
   262  	baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
   263  	baseSizeIndex := strings.Index(baseLines[0], "SIZE")
   264  	baseFoundsize := baseLines[1][baseSizeIndex:]
   265  	baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
   266  	if err != nil {
   267  		c.Fatal(err)
   268  	}
   269  
   270  	name := "test_size"
   271  	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
   272  	out, _, err := runCommandWithOutput(runCmd)
   273  	if err != nil {
   274  		c.Fatal(out, err)
   275  	}
   276  	id, err := getIDByName(name)
   277  	if err != nil {
   278  		c.Fatal(err)
   279  	}
   280  
   281  	runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
   282  	wait := make(chan struct{})
   283  	go func() {
   284  		out, _, err = runCommandWithOutput(runCmd)
   285  		close(wait)
   286  	}()
   287  	select {
   288  	case <-wait:
   289  	case <-time.After(3 * time.Second):
   290  		c.Fatalf("Calling \"docker ps -s\" timed out!")
   291  	}
   292  	if err != nil {
   293  		c.Fatal(out, err)
   294  	}
   295  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   296  	if len(lines) != 2 {
   297  		c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
   298  	}
   299  	sizeIndex := strings.Index(lines[0], "SIZE")
   300  	idIndex := strings.Index(lines[0], "CONTAINER ID")
   301  	foundID := lines[1][idIndex : idIndex+12]
   302  	if foundID != id[:12] {
   303  		c.Fatalf("Expected id %s, got %s", id[:12], foundID)
   304  	}
   305  	expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
   306  	foundSize := lines[1][sizeIndex:]
   307  	if !strings.Contains(foundSize, expectedSize) {
   308  		c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
   309  	}
   310  
   311  }
   312  
   313  func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
   314  	// FIXME: this should test paused, but it makes things hang and its wonky
   315  	// this is because paused containers can't be controlled by signals
   316  
   317  	// start exited container
   318  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   319  	out, _, err := runCommandWithOutput(runCmd)
   320  	if err != nil {
   321  		c.Fatal(out, err)
   322  	}
   323  	firstID := strings.TrimSpace(out)
   324  
   325  	// make sure the exited cintainer is not running
   326  	runCmd = exec.Command(dockerBinary, "wait", firstID)
   327  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   328  		c.Fatal(out, err)
   329  	}
   330  
   331  	// start running container
   332  	runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
   333  	out, _, err = runCommandWithOutput(runCmd)
   334  	if err != nil {
   335  		c.Fatal(out, err)
   336  	}
   337  	secondID := strings.TrimSpace(out)
   338  
   339  	// filter containers by exited
   340  	runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
   341  	out, _, err = runCommandWithOutput(runCmd)
   342  	if err != nil {
   343  		c.Fatal(out, err)
   344  	}
   345  	containerOut := strings.TrimSpace(out)
   346  	if containerOut != firstID[:12] {
   347  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   348  	}
   349  
   350  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
   351  	out, _, err = runCommandWithOutput(runCmd)
   352  	if err != nil {
   353  		c.Fatal(out, err)
   354  	}
   355  	containerOut = strings.TrimSpace(out)
   356  	if containerOut != secondID[:12] {
   357  		c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
   358  	}
   359  
   360  }
   361  
   362  func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
   363  
   364  	// start container
   365  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   366  	out, _, err := runCommandWithOutput(runCmd)
   367  	if err != nil {
   368  		c.Fatal(out, err)
   369  	}
   370  	firstID := strings.TrimSpace(out)
   371  
   372  	// start another container
   373  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
   374  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   375  		c.Fatal(out, err)
   376  	}
   377  
   378  	// filter containers by id
   379  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
   380  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   381  		c.Fatal(out, err)
   382  	}
   383  	containerOut := strings.TrimSpace(out)
   384  	if containerOut != firstID[:12] {
   385  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   386  	}
   387  
   388  }
   389  
   390  func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
   391  
   392  	// start container
   393  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
   394  	out, _, err := runCommandWithOutput(runCmd)
   395  	if err != nil {
   396  		c.Fatal(out, err)
   397  	}
   398  	firstID := strings.TrimSpace(out)
   399  
   400  	// start another container
   401  	runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "top")
   402  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   403  		c.Fatal(out, err)
   404  	}
   405  
   406  	// filter containers by name
   407  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
   408  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   409  		c.Fatal(out, err)
   410  	}
   411  	containerOut := strings.TrimSpace(out)
   412  	if containerOut != firstID[:12] {
   413  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   414  	}
   415  
   416  }
   417  
   418  func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
   419  	// start container
   420  	runCmd := exec.Command(dockerBinary, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
   421  	out, _, err := runCommandWithOutput(runCmd)
   422  	if err != nil {
   423  		c.Fatal(out, err)
   424  	}
   425  	firstID := strings.TrimSpace(out)
   426  
   427  	// start another container
   428  	runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "match=me too", "busybox")
   429  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   430  		c.Fatal(out, err)
   431  	}
   432  	secondID := strings.TrimSpace(out)
   433  
   434  	// start third container
   435  	runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "nomatch=me", "busybox")
   436  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   437  		c.Fatal(out, err)
   438  	}
   439  	thirdID := strings.TrimSpace(out)
   440  
   441  	// filter containers by exact match
   442  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
   443  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   444  		c.Fatal(out, err)
   445  	}
   446  	containerOut := strings.TrimSpace(out)
   447  	if containerOut != firstID {
   448  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
   449  	}
   450  
   451  	// filter containers by two labels
   452  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
   453  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   454  		c.Fatal(out, err)
   455  	}
   456  	containerOut = strings.TrimSpace(out)
   457  	if containerOut != firstID {
   458  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
   459  	}
   460  
   461  	// filter containers by two labels, but expect not found because of AND behavior
   462  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
   463  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   464  		c.Fatal(out, err)
   465  	}
   466  	containerOut = strings.TrimSpace(out)
   467  	if containerOut != "" {
   468  		c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
   469  	}
   470  
   471  	// filter containers by exact key
   472  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
   473  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   474  		c.Fatal(out, err)
   475  	}
   476  	containerOut = strings.TrimSpace(out)
   477  	if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
   478  		c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
   479  	}
   480  }
   481  
   482  func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
   483  
   484  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
   485  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   486  		c.Fatal(out, err)
   487  	}
   488  
   489  	runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
   490  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   491  		c.Fatal(out, err)
   492  	}
   493  	firstZero, err := getIDByName("zero1")
   494  	if err != nil {
   495  		c.Fatal(err)
   496  	}
   497  
   498  	runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
   499  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   500  		c.Fatal(out, err)
   501  	}
   502  	secondZero, err := getIDByName("zero2")
   503  	if err != nil {
   504  		c.Fatal(err)
   505  	}
   506  
   507  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
   508  	if out, _, err := runCommandWithOutput(runCmd); err == nil {
   509  		c.Fatal("Should fail.", out, err)
   510  	}
   511  	firstNonZero, err := getIDByName("nonzero1")
   512  	if err != nil {
   513  		c.Fatal(err)
   514  	}
   515  
   516  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
   517  	if out, _, err := runCommandWithOutput(runCmd); err == nil {
   518  		c.Fatal("Should fail.", out, err)
   519  	}
   520  	secondNonZero, err := getIDByName("nonzero2")
   521  	if err != nil {
   522  		c.Fatal(err)
   523  	}
   524  
   525  	// filter containers by exited=0
   526  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
   527  	out, _, err := runCommandWithOutput(runCmd)
   528  	if err != nil {
   529  		c.Fatal(out, err)
   530  	}
   531  	ids := strings.Split(strings.TrimSpace(out), "\n")
   532  	if len(ids) != 2 {
   533  		c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
   534  	}
   535  	if ids[0] != secondZero {
   536  		c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
   537  	}
   538  	if ids[1] != firstZero {
   539  		c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
   540  	}
   541  
   542  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
   543  	out, _, err = runCommandWithOutput(runCmd)
   544  	if err != nil {
   545  		c.Fatal(out, err)
   546  	}
   547  	ids = strings.Split(strings.TrimSpace(out), "\n")
   548  	if len(ids) != 2 {
   549  		c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
   550  	}
   551  	if ids[0] != secondNonZero {
   552  		c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
   553  	}
   554  	if ids[1] != firstNonZero {
   555  		c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
   556  	}
   557  
   558  }
   559  
   560  func (s *DockerSuite) TestPsRightTagName(c *check.C) {
   561  	tag := "asybox:shmatest"
   562  	if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
   563  		c.Fatalf("Failed to tag image: %s, out: %q", err, out)
   564  	}
   565  
   566  	var id1 string
   567  	if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
   568  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   569  	} else {
   570  		id1 = strings.TrimSpace(string(out))
   571  	}
   572  
   573  	var id2 string
   574  	if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
   575  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   576  	} else {
   577  		id2 = strings.TrimSpace(string(out))
   578  	}
   579  
   580  	var imageID string
   581  	if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
   582  		c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
   583  	} else {
   584  		imageID = strings.TrimSpace(string(out))
   585  	}
   586  
   587  	var id3 string
   588  	if out, err := exec.Command(dockerBinary, "run", "-d", imageID, "top").CombinedOutput(); err != nil {
   589  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   590  	} else {
   591  		id3 = strings.TrimSpace(string(out))
   592  	}
   593  
   594  	out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
   595  	if err != nil {
   596  		c.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
   597  	}
   598  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   599  	// skip header
   600  	lines = lines[1:]
   601  	if len(lines) != 3 {
   602  		c.Fatalf("There should be 3 running container, got %d", len(lines))
   603  	}
   604  	for _, line := range lines {
   605  		f := strings.Fields(line)
   606  		switch f[0] {
   607  		case id1:
   608  			if f[1] != "busybox" {
   609  				c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
   610  			}
   611  		case id2:
   612  			if f[1] != tag {
   613  				c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
   614  			}
   615  		case id3:
   616  			if f[1] != imageID {
   617  				c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
   618  			}
   619  		default:
   620  			c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
   621  		}
   622  	}
   623  }
   624  
   625  func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
   626  	if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
   627  		c.Fatalf("Output: %s, err: %s", out, err)
   628  	}
   629  	if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
   630  		c.Fatalf("Output: %s, err: %s", out, err)
   631  	}
   632  	out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
   633  	if err != nil {
   634  		c.Fatalf("Output: %s, err: %s", out, err)
   635  	}
   636  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   637  	// strip header
   638  	lines = lines[1:]
   639  	expected := []string{"second", "first,second/first"}
   640  	var names []string
   641  	for _, l := range lines {
   642  		fields := strings.Fields(l)
   643  		names = append(names, fields[len(fields)-1])
   644  	}
   645  	if !reflect.DeepEqual(expected, names) {
   646  		c.Fatalf("Expected array: %v, got: %v", expected, names)
   647  	}
   648  }
   649  
   650  func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
   651  
   652  	portRange := "3800-3900"
   653  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top"))
   654  	if err != nil {
   655  		c.Fatal(out, err)
   656  	}
   657  
   658  	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps"))
   659  	if err != nil {
   660  		c.Fatal(out, err)
   661  	}
   662  
   663  	// check that the port range is in the output
   664  	if !strings.Contains(string(out), portRange) {
   665  		c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
   666  	}
   667  
   668  }
   669  
   670  func (s *DockerSuite) TestPsWithSize(c *check.C) {
   671  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "sizetest", "busybox", "top"))
   672  	if err != nil {
   673  		c.Fatal(out, err)
   674  	}
   675  	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps", "--size"))
   676  	if err != nil {
   677  		c.Fatal(out, err)
   678  	}
   679  	if !strings.Contains(out, "virtual") {
   680  		c.Fatalf("docker ps with --size should show virtual size of container")
   681  	}
   682  }