github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  
   259  	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
   260  	runCommandWithOutput(cmd)
   261  	cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
   262  	baseOut, _, err := runCommandWithOutput(cmd)
   263  	baseLines := strings.Split(strings.Trim(baseOut, "\n "), "\n")
   264  	baseSizeIndex := strings.Index(baseLines[0], "SIZE")
   265  	baseFoundsize := baseLines[1][baseSizeIndex:]
   266  	baseBytes, err := strconv.Atoi(strings.Split(baseFoundsize, " ")[0])
   267  	if err != nil {
   268  		c.Fatal(err)
   269  	}
   270  
   271  	name := "test_size"
   272  	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
   273  	out, _, err := runCommandWithOutput(runCmd)
   274  	if err != nil {
   275  		c.Fatal(out, err)
   276  	}
   277  	id, err := getIDByName(name)
   278  	if err != nil {
   279  		c.Fatal(err)
   280  	}
   281  
   282  	runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
   283  	wait := make(chan struct{})
   284  	go func() {
   285  		out, _, err = runCommandWithOutput(runCmd)
   286  		close(wait)
   287  	}()
   288  	select {
   289  	case <-wait:
   290  	case <-time.After(3 * time.Second):
   291  		c.Fatalf("Calling \"docker ps -s\" timed out!")
   292  	}
   293  	if err != nil {
   294  		c.Fatal(out, err)
   295  	}
   296  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   297  	if len(lines) != 2 {
   298  		c.Fatalf("Expected 2 lines for 'ps -s -n=1' output, got %d", len(lines))
   299  	}
   300  	sizeIndex := strings.Index(lines[0], "SIZE")
   301  	idIndex := strings.Index(lines[0], "CONTAINER ID")
   302  	foundID := lines[1][idIndex : idIndex+12]
   303  	if foundID != id[:12] {
   304  		c.Fatalf("Expected id %s, got %s", id[:12], foundID)
   305  	}
   306  	expectedSize := fmt.Sprintf("%d B", (2 + baseBytes))
   307  	foundSize := lines[1][sizeIndex:]
   308  	if foundSize != expectedSize {
   309  		c.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
   310  	}
   311  
   312  }
   313  
   314  func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
   315  	// FIXME: this should test paused, but it makes things hang and its wonky
   316  	// this is because paused containers can't be controlled by signals
   317  
   318  	// start exited container
   319  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   320  	out, _, err := runCommandWithOutput(runCmd)
   321  	if err != nil {
   322  		c.Fatal(out, err)
   323  	}
   324  	firstID := strings.TrimSpace(out)
   325  
   326  	// make sure the exited cintainer is not running
   327  	runCmd = exec.Command(dockerBinary, "wait", firstID)
   328  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   329  		c.Fatal(out, err)
   330  	}
   331  
   332  	// start running container
   333  	runCmd = exec.Command(dockerBinary, "run", "-itd", "busybox")
   334  	out, _, err = runCommandWithOutput(runCmd)
   335  	if err != nil {
   336  		c.Fatal(out, err)
   337  	}
   338  	secondID := strings.TrimSpace(out)
   339  
   340  	// filter containers by exited
   341  	runCmd = exec.Command(dockerBinary, "ps", "-q", "--filter=status=exited")
   342  	out, _, err = runCommandWithOutput(runCmd)
   343  	if err != nil {
   344  		c.Fatal(out, err)
   345  	}
   346  	containerOut := strings.TrimSpace(out)
   347  	if containerOut != firstID[:12] {
   348  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   349  	}
   350  
   351  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
   352  	out, _, err = runCommandWithOutput(runCmd)
   353  	if err != nil {
   354  		c.Fatal(out, err)
   355  	}
   356  	containerOut = strings.TrimSpace(out)
   357  	if containerOut != secondID[:12] {
   358  		c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
   359  	}
   360  
   361  }
   362  
   363  func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {
   364  
   365  	// start container
   366  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   367  	out, _, err := runCommandWithOutput(runCmd)
   368  	if err != nil {
   369  		c.Fatal(out, err)
   370  	}
   371  	firstID := strings.TrimSpace(out)
   372  
   373  	// start another container
   374  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
   375  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   376  		c.Fatal(out, err)
   377  	}
   378  
   379  	// filter containers by id
   380  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
   381  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   382  		c.Fatal(out, err)
   383  	}
   384  	containerOut := strings.TrimSpace(out)
   385  	if containerOut != firstID[:12] {
   386  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   387  	}
   388  
   389  }
   390  
   391  func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
   392  
   393  	// start container
   394  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
   395  	out, _, err := runCommandWithOutput(runCmd)
   396  	if err != nil {
   397  		c.Fatal(out, err)
   398  	}
   399  	firstID := strings.TrimSpace(out)
   400  
   401  	// start another container
   402  	runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "top")
   403  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   404  		c.Fatal(out, err)
   405  	}
   406  
   407  	// filter containers by name
   408  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
   409  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   410  		c.Fatal(out, err)
   411  	}
   412  	containerOut := strings.TrimSpace(out)
   413  	if containerOut != firstID[:12] {
   414  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   415  	}
   416  
   417  }
   418  
   419  func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) {
   420  	// start container
   421  	runCmd := exec.Command(dockerBinary, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox")
   422  	out, _, err := runCommandWithOutput(runCmd)
   423  	if err != nil {
   424  		c.Fatal(out, err)
   425  	}
   426  	firstID := strings.TrimSpace(out)
   427  
   428  	// start another container
   429  	runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "match=me too", "busybox")
   430  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   431  		c.Fatal(out, err)
   432  	}
   433  	secondID := strings.TrimSpace(out)
   434  
   435  	// start third container
   436  	runCmd = exec.Command(dockerBinary, "run", "-d", "-l", "nomatch=me", "busybox")
   437  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   438  		c.Fatal(out, err)
   439  	}
   440  	thirdID := strings.TrimSpace(out)
   441  
   442  	// filter containers by exact match
   443  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me")
   444  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   445  		c.Fatal(out, err)
   446  	}
   447  	containerOut := strings.TrimSpace(out)
   448  	if containerOut != firstID {
   449  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
   450  	}
   451  
   452  	// filter containers by two labels
   453  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag")
   454  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   455  		c.Fatal(out, err)
   456  	}
   457  	containerOut = strings.TrimSpace(out)
   458  	if containerOut != firstID {
   459  		c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out)
   460  	}
   461  
   462  	// filter containers by two labels, but expect not found because of AND behavior
   463  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no")
   464  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   465  		c.Fatal(out, err)
   466  	}
   467  	containerOut = strings.TrimSpace(out)
   468  	if containerOut != "" {
   469  		c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out)
   470  	}
   471  
   472  	// filter containers by exact key
   473  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=label=match")
   474  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   475  		c.Fatal(out, err)
   476  	}
   477  	containerOut = strings.TrimSpace(out)
   478  	if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) {
   479  		c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out)
   480  	}
   481  }
   482  
   483  func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
   484  
   485  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
   486  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   487  		c.Fatal(out, err)
   488  	}
   489  
   490  	runCmd = exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
   491  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   492  		c.Fatal(out, err)
   493  	}
   494  	firstZero, err := getIDByName("zero1")
   495  	if err != nil {
   496  		c.Fatal(err)
   497  	}
   498  
   499  	runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
   500  	if out, _, err := runCommandWithOutput(runCmd); err != nil {
   501  		c.Fatal(out, err)
   502  	}
   503  	secondZero, err := getIDByName("zero2")
   504  	if err != nil {
   505  		c.Fatal(err)
   506  	}
   507  
   508  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
   509  	if out, _, err := runCommandWithOutput(runCmd); err == nil {
   510  		c.Fatal("Should fail.", out, err)
   511  	}
   512  	firstNonZero, err := getIDByName("nonzero1")
   513  	if err != nil {
   514  		c.Fatal(err)
   515  	}
   516  
   517  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
   518  	if out, _, err := runCommandWithOutput(runCmd); err == nil {
   519  		c.Fatal("Should fail.", out, err)
   520  	}
   521  	secondNonZero, err := getIDByName("nonzero2")
   522  	if err != nil {
   523  		c.Fatal(err)
   524  	}
   525  
   526  	// filter containers by exited=0
   527  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
   528  	out, _, err := runCommandWithOutput(runCmd)
   529  	if err != nil {
   530  		c.Fatal(out, err)
   531  	}
   532  	ids := strings.Split(strings.TrimSpace(out), "\n")
   533  	if len(ids) != 2 {
   534  		c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out)
   535  	}
   536  	if ids[0] != secondZero {
   537  		c.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
   538  	}
   539  	if ids[1] != firstZero {
   540  		c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
   541  	}
   542  
   543  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
   544  	out, _, err = runCommandWithOutput(runCmd)
   545  	if err != nil {
   546  		c.Fatal(out, err)
   547  	}
   548  	ids = strings.Split(strings.TrimSpace(out), "\n")
   549  	if len(ids) != 2 {
   550  		c.Fatalf("Should be 2 zero exited containers got %d", len(ids))
   551  	}
   552  	if ids[0] != secondNonZero {
   553  		c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
   554  	}
   555  	if ids[1] != firstNonZero {
   556  		c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
   557  	}
   558  
   559  }
   560  
   561  func (s *DockerSuite) TestPsRightTagName(c *check.C) {
   562  	tag := "asybox:shmatest"
   563  	if out, err := exec.Command(dockerBinary, "tag", "busybox", tag).CombinedOutput(); err != nil {
   564  		c.Fatalf("Failed to tag image: %s, out: %q", err, out)
   565  	}
   566  
   567  	var id1 string
   568  	if out, err := exec.Command(dockerBinary, "run", "-d", "busybox", "top").CombinedOutput(); err != nil {
   569  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   570  	} else {
   571  		id1 = strings.TrimSpace(string(out))
   572  	}
   573  
   574  	var id2 string
   575  	if out, err := exec.Command(dockerBinary, "run", "-d", tag, "top").CombinedOutput(); err != nil {
   576  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   577  	} else {
   578  		id2 = strings.TrimSpace(string(out))
   579  	}
   580  
   581  	var imageID string
   582  	if out, err := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox").CombinedOutput(); err != nil {
   583  		c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
   584  	} else {
   585  		imageID = strings.TrimSpace(string(out))
   586  	}
   587  
   588  	var id3 string
   589  	if out, err := exec.Command(dockerBinary, "run", "-d", imageID, "top").CombinedOutput(); err != nil {
   590  		c.Fatalf("Failed to run container: %s, out: %q", err, out)
   591  	} else {
   592  		id3 = strings.TrimSpace(string(out))
   593  	}
   594  
   595  	out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
   596  	if err != nil {
   597  		c.Fatalf("Failed to run 'ps': %s, out: %q", err, out)
   598  	}
   599  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   600  	// skip header
   601  	lines = lines[1:]
   602  	if len(lines) != 3 {
   603  		c.Fatalf("There should be 3 running container, got %d", len(lines))
   604  	}
   605  	for _, line := range lines {
   606  		f := strings.Fields(line)
   607  		switch f[0] {
   608  		case id1:
   609  			if f[1] != "busybox" {
   610  				c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1])
   611  			}
   612  		case id2:
   613  			if f[1] != tag {
   614  				c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1])
   615  			}
   616  		case id3:
   617  			if f[1] != imageID {
   618  				c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1])
   619  			}
   620  		default:
   621  			c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3)
   622  		}
   623  	}
   624  }
   625  
   626  func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) {
   627  	if out, err := exec.Command(dockerBinary, "run", "--name=first", "-d", "busybox", "top").CombinedOutput(); err != nil {
   628  		c.Fatalf("Output: %s, err: %s", out, err)
   629  	}
   630  	if out, err := exec.Command(dockerBinary, "run", "--name=second", "--link=first:first", "-d", "busybox", "top").CombinedOutput(); err != nil {
   631  		c.Fatalf("Output: %s, err: %s", out, err)
   632  	}
   633  	out, err := exec.Command(dockerBinary, "ps", "--no-trunc").CombinedOutput()
   634  	if err != nil {
   635  		c.Fatalf("Output: %s, err: %s", out, err)
   636  	}
   637  	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
   638  	// strip header
   639  	lines = lines[1:]
   640  	expected := []string{"second", "first,second/first"}
   641  	var names []string
   642  	for _, l := range lines {
   643  		fields := strings.Fields(l)
   644  		names = append(names, fields[len(fields)-1])
   645  	}
   646  	if !reflect.DeepEqual(expected, names) {
   647  		c.Fatalf("Expected array: %v, got: %v", expected, names)
   648  	}
   649  }
   650  
   651  func (s *DockerSuite) TestPsGroupPortRange(c *check.C) {
   652  
   653  	portRange := "3800-3900"
   654  	out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top"))
   655  	if err != nil {
   656  		c.Fatal(out, err)
   657  	}
   658  
   659  	out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "ps"))
   660  	if err != nil {
   661  		c.Fatal(out, err)
   662  	}
   663  
   664  	// check that the port range is in the output
   665  	if !strings.Contains(string(out), portRange) {
   666  		c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out))
   667  	}
   668  
   669  }