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