github.com/titanous/docker@v1.4.1/integration-cli/docker_cli_ps_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestPsListContainers(t *testing.T) {
    11  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    12  	out, _, err := runCommandWithOutput(runCmd)
    13  	if err != nil {
    14  		t.Fatal(out, err)
    15  	}
    16  	firstID := stripTrailingCharacters(out)
    17  
    18  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    19  	out, _, err = runCommandWithOutput(runCmd)
    20  	if err != nil {
    21  		t.Fatal(out, err)
    22  	}
    23  	secondID := stripTrailingCharacters(out)
    24  
    25  	// not long running
    26  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "true")
    27  	out, _, err = runCommandWithOutput(runCmd)
    28  	if err != nil {
    29  		t.Fatal(out, err)
    30  	}
    31  	thirdID := stripTrailingCharacters(out)
    32  
    33  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "top")
    34  	out, _, err = runCommandWithOutput(runCmd)
    35  	if err != nil {
    36  		t.Fatal(out, err)
    37  	}
    38  	fourthID := stripTrailingCharacters(out)
    39  
    40  	// make sure third one is not running
    41  	runCmd = exec.Command(dockerBinary, "wait", thirdID)
    42  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    43  		t.Fatal(out, err)
    44  	}
    45  
    46  	// all
    47  	runCmd = exec.Command(dockerBinary, "ps", "-a")
    48  	out, _, err = runCommandWithOutput(runCmd)
    49  	if err != nil {
    50  		t.Fatal(out, err)
    51  	}
    52  
    53  	if !assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}) {
    54  		t.Error("Container list is not in the correct order")
    55  	}
    56  
    57  	// running
    58  	runCmd = exec.Command(dockerBinary, "ps")
    59  	out, _, err = runCommandWithOutput(runCmd)
    60  	if err != nil {
    61  		t.Fatal(out, err)
    62  	}
    63  
    64  	if !assertContainerList(out, []string{fourthID, secondID, firstID}) {
    65  		t.Error("Container list is not in the correct order")
    66  	}
    67  
    68  	// from here all flag '-a' is ignored
    69  
    70  	// limit
    71  	runCmd = exec.Command(dockerBinary, "ps", "-n=2", "-a")
    72  	out, _, err = runCommandWithOutput(runCmd)
    73  	if err != nil {
    74  		t.Fatal(out, err)
    75  	}
    76  	expected := []string{fourthID, thirdID}
    77  
    78  	if !assertContainerList(out, expected) {
    79  		t.Error("Container list is not in the correct order")
    80  	}
    81  
    82  	runCmd = exec.Command(dockerBinary, "ps", "-n=2")
    83  	out, _, err = runCommandWithOutput(runCmd)
    84  	if err != nil {
    85  		t.Fatal(out, err)
    86  	}
    87  
    88  	if !assertContainerList(out, expected) {
    89  		t.Error("Container list is not in the correct order")
    90  	}
    91  
    92  	// since
    93  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-a")
    94  	out, _, err = runCommandWithOutput(runCmd)
    95  	if err != nil {
    96  		t.Fatal(out, err)
    97  	}
    98  	expected = []string{fourthID, thirdID, secondID}
    99  
   100  	if !assertContainerList(out, expected) {
   101  		t.Error("Container list is not in the correct order")
   102  	}
   103  
   104  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID)
   105  	out, _, err = runCommandWithOutput(runCmd)
   106  	if err != nil {
   107  		t.Fatal(out, err)
   108  	}
   109  
   110  	if !assertContainerList(out, expected) {
   111  		t.Error("Container list is not in the correct order")
   112  	}
   113  
   114  	// before
   115  	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID, "-a")
   116  	out, _, err = runCommandWithOutput(runCmd)
   117  	if err != nil {
   118  		t.Fatal(out, err)
   119  	}
   120  	expected = []string{secondID, firstID}
   121  
   122  	if !assertContainerList(out, expected) {
   123  		t.Error("Container list is not in the correct order")
   124  	}
   125  
   126  	runCmd = exec.Command(dockerBinary, "ps", "--before", thirdID)
   127  	out, _, err = runCommandWithOutput(runCmd)
   128  	if err != nil {
   129  		t.Fatal(out, err)
   130  	}
   131  
   132  	if !assertContainerList(out, expected) {
   133  		t.Error("Container list is not in the correct order")
   134  	}
   135  
   136  	// since & before
   137  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-a")
   138  	out, _, err = runCommandWithOutput(runCmd)
   139  	if err != nil {
   140  		t.Fatal(out, err)
   141  	}
   142  	expected = []string{thirdID, secondID}
   143  
   144  	if !assertContainerList(out, expected) {
   145  		t.Error("Container list is not in the correct order")
   146  	}
   147  
   148  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID)
   149  	out, _, err = runCommandWithOutput(runCmd)
   150  	if err != nil {
   151  		t.Fatal(out, err)
   152  	}
   153  	if !assertContainerList(out, expected) {
   154  		t.Error("Container list is not in the correct order")
   155  	}
   156  
   157  	// since & limit
   158  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2", "-a")
   159  	out, _, err = runCommandWithOutput(runCmd)
   160  	if err != nil {
   161  		t.Fatal(out, err)
   162  	}
   163  	expected = []string{fourthID, thirdID}
   164  
   165  	if !assertContainerList(out, expected) {
   166  		t.Error("Container list is not in the correct order")
   167  	}
   168  
   169  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "-n=2")
   170  	out, _, err = runCommandWithOutput(runCmd)
   171  	if err != nil {
   172  		t.Fatal(out, err)
   173  	}
   174  
   175  	if !assertContainerList(out, expected) {
   176  		t.Error("Container list is not in the correct order")
   177  	}
   178  
   179  	// before & limit
   180  	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1", "-a")
   181  	out, _, err = runCommandWithOutput(runCmd)
   182  	if err != nil {
   183  		t.Fatal(out, err)
   184  	}
   185  	expected = []string{thirdID}
   186  
   187  	if !assertContainerList(out, expected) {
   188  		t.Error("Container list is not in the correct order")
   189  	}
   190  
   191  	runCmd = exec.Command(dockerBinary, "ps", "--before", fourthID, "-n=1")
   192  	out, _, err = runCommandWithOutput(runCmd)
   193  	if err != nil {
   194  		t.Fatal(out, err)
   195  	}
   196  
   197  	if !assertContainerList(out, expected) {
   198  		t.Error("Container list is not in the correct order")
   199  	}
   200  
   201  	// since & before & limit
   202  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1", "-a")
   203  	out, _, err = runCommandWithOutput(runCmd)
   204  	if err != nil {
   205  		t.Fatal(out, err)
   206  	}
   207  	expected = []string{thirdID}
   208  
   209  	if !assertContainerList(out, expected) {
   210  		t.Error("Container list is not in the correct order")
   211  	}
   212  
   213  	runCmd = exec.Command(dockerBinary, "ps", "--since", firstID, "--before", fourthID, "-n=1")
   214  	out, _, err = runCommandWithOutput(runCmd)
   215  	if err != nil {
   216  		t.Fatal(out, err)
   217  	}
   218  
   219  	if !assertContainerList(out, expected) {
   220  		t.Error("Container list is not in the correct order")
   221  	}
   222  
   223  	deleteAllContainers()
   224  
   225  	logDone("ps - test ps options")
   226  }
   227  
   228  func assertContainerList(out string, expected []string) bool {
   229  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   230  	if len(lines)-1 != len(expected) {
   231  		return false
   232  	}
   233  
   234  	containerIDIndex := strings.Index(lines[0], "CONTAINER ID")
   235  	for i := 0; i < len(expected); i++ {
   236  		foundID := lines[i+1][containerIDIndex : containerIDIndex+12]
   237  		if foundID != expected[i][:12] {
   238  			return false
   239  		}
   240  	}
   241  
   242  	return true
   243  }
   244  
   245  func TestPsListContainersSize(t *testing.T) {
   246  	name := "test_size"
   247  	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "sh", "-c", "echo 1 > test")
   248  	out, _, err := runCommandWithOutput(runCmd)
   249  	if err != nil {
   250  		t.Fatal(out, err)
   251  	}
   252  	id, err := getIDByName(name)
   253  	if err != nil {
   254  		t.Fatal(err)
   255  	}
   256  
   257  	runCmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
   258  	wait := make(chan struct{})
   259  	go func() {
   260  		out, _, err = runCommandWithOutput(runCmd)
   261  		close(wait)
   262  	}()
   263  	select {
   264  	case <-wait:
   265  	case <-time.After(3 * time.Second):
   266  		t.Fatalf("Calling \"docker ps -s\" timed out!")
   267  	}
   268  	if err != nil {
   269  		t.Fatal(out, err)
   270  	}
   271  	lines := strings.Split(strings.Trim(out, "\n "), "\n")
   272  	sizeIndex := strings.Index(lines[0], "SIZE")
   273  	idIndex := strings.Index(lines[0], "CONTAINER ID")
   274  	foundID := lines[1][idIndex : idIndex+12]
   275  	if foundID != id[:12] {
   276  		t.Fatalf("Expected id %s, got %s", id[:12], foundID)
   277  	}
   278  	expectedSize := "2 B"
   279  	foundSize := lines[1][sizeIndex:]
   280  	if foundSize != expectedSize {
   281  		t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
   282  	}
   283  
   284  	deleteAllContainers()
   285  	logDone("ps - test ps size")
   286  }
   287  
   288  func TestPsListContainersFilterStatus(t *testing.T) {
   289  	// FIXME: this should test paused, but it makes things hang and its wonky
   290  	// this is because paused containers can't be controlled by signals
   291  
   292  	// start exited container
   293  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   294  	out, _, err := runCommandWithOutput(runCmd)
   295  	if err != nil {
   296  		t.Fatal(out, err)
   297  	}
   298  	firstID := stripTrailingCharacters(out)
   299  
   300  	// make sure the exited cintainer is not running
   301  	runCmd = exec.Command(dockerBinary, "wait", firstID)
   302  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   303  		t.Fatal(out, err)
   304  	}
   305  
   306  	// start running container
   307  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 360")
   308  	out, _, err = runCommandWithOutput(runCmd)
   309  	if err != nil {
   310  		t.Fatal(out, err)
   311  	}
   312  	secondID := stripTrailingCharacters(out)
   313  
   314  	// filter containers by exited
   315  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=exited")
   316  	out, _, err = runCommandWithOutput(runCmd)
   317  	if err != nil {
   318  		t.Fatal(out, err)
   319  	}
   320  	containerOut := strings.TrimSpace(out)
   321  	if containerOut != firstID[:12] {
   322  		t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   323  	}
   324  
   325  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=status=running")
   326  	out, _, err = runCommandWithOutput(runCmd)
   327  	if err != nil {
   328  		t.Fatal(out, err)
   329  	}
   330  	containerOut = strings.TrimSpace(out)
   331  	if containerOut != secondID[:12] {
   332  		t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
   333  	}
   334  
   335  	deleteAllContainers()
   336  
   337  	logDone("ps - test ps filter status")
   338  }
   339  
   340  func TestPsListContainersFilterID(t *testing.T) {
   341  	// start container
   342  	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
   343  	out, _, err := runCommandWithOutput(runCmd)
   344  	if err != nil {
   345  		t.Fatal(out, err)
   346  	}
   347  	firstID := stripTrailingCharacters(out)
   348  
   349  	// start another container
   350  	runCmd = exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 360")
   351  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   352  		t.Fatal(out, err)
   353  	}
   354  
   355  	// filter containers by id
   356  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=id="+firstID)
   357  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   358  		t.Fatal(out, err)
   359  	}
   360  	containerOut := strings.TrimSpace(out)
   361  	if containerOut != firstID[:12] {
   362  		t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   363  	}
   364  
   365  	deleteAllContainers()
   366  
   367  	logDone("ps - test ps filter id")
   368  }
   369  
   370  func TestPsListContainersFilterName(t *testing.T) {
   371  	// start container
   372  	runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
   373  	out, _, err := runCommandWithOutput(runCmd)
   374  	if err != nil {
   375  		t.Fatal(out, err)
   376  	}
   377  	firstID := stripTrailingCharacters(out)
   378  
   379  	// start another container
   380  	runCmd = exec.Command(dockerBinary, "run", "-d", "--name=b_name_to_match", "busybox", "sh", "-c", "sleep 360")
   381  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   382  		t.Fatal(out, err)
   383  	}
   384  
   385  	// filter containers by name
   386  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--filter=name=a_name_to_match")
   387  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
   388  		t.Fatal(out, err)
   389  	}
   390  	containerOut := strings.TrimSpace(out)
   391  	if containerOut != firstID[:12] {
   392  		t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
   393  	}
   394  
   395  	deleteAllContainers()
   396  
   397  	logDone("ps - test ps filter name")
   398  }
   399  
   400  func TestPsListContainersFilterExited(t *testing.T) {
   401  	deleteAllContainers()
   402  	defer deleteAllContainers()
   403  	runCmd := exec.Command(dockerBinary, "run", "--name", "zero1", "busybox", "true")
   404  	out, _, err := runCommandWithOutput(runCmd)
   405  	if err != nil {
   406  		t.Fatal(out, err)
   407  	}
   408  	firstZero, err := getIDByName("zero1")
   409  	if err != nil {
   410  		t.Fatal(err)
   411  	}
   412  
   413  	runCmd = exec.Command(dockerBinary, "run", "--name", "zero2", "busybox", "true")
   414  	out, _, err = runCommandWithOutput(runCmd)
   415  	if err != nil {
   416  		t.Fatal(out, err)
   417  	}
   418  	secondZero, err := getIDByName("zero2")
   419  	if err != nil {
   420  		t.Fatal(err)
   421  	}
   422  
   423  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero1", "busybox", "false")
   424  	out, _, err = runCommandWithOutput(runCmd)
   425  	if err == nil {
   426  		t.Fatal("Should fail.", out, err)
   427  	}
   428  	firstNonZero, err := getIDByName("nonzero1")
   429  	if err != nil {
   430  		t.Fatal(err)
   431  	}
   432  
   433  	runCmd = exec.Command(dockerBinary, "run", "--name", "nonzero2", "busybox", "false")
   434  	out, _, err = runCommandWithOutput(runCmd)
   435  	if err == nil {
   436  		t.Fatal("Should fail.", out, err)
   437  	}
   438  	secondNonZero, err := getIDByName("nonzero2")
   439  	if err != nil {
   440  		t.Fatal(err)
   441  	}
   442  
   443  	// filter containers by exited=0
   444  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0")
   445  	out, _, err = runCommandWithOutput(runCmd)
   446  	if err != nil {
   447  		t.Fatal(out, err)
   448  	}
   449  	ids := strings.Split(strings.TrimSpace(out), "\n")
   450  	if len(ids) != 2 {
   451  		t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
   452  	}
   453  	if ids[0] != secondZero {
   454  		t.Fatalf("First in list should be %q, got %q", secondZero, ids[0])
   455  	}
   456  	if ids[1] != firstZero {
   457  		t.Fatalf("Second in list should be %q, got %q", firstZero, ids[1])
   458  	}
   459  
   460  	runCmd = exec.Command(dockerBinary, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1")
   461  	out, _, err = runCommandWithOutput(runCmd)
   462  	if err != nil {
   463  		t.Fatal(out, err)
   464  	}
   465  	ids = strings.Split(strings.TrimSpace(out), "\n")
   466  	if len(ids) != 2 {
   467  		t.Fatalf("Should be 2 zero exited containerst got %d", len(ids))
   468  	}
   469  	if ids[0] != secondNonZero {
   470  		t.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0])
   471  	}
   472  	if ids[1] != firstNonZero {
   473  		t.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1])
   474  	}
   475  	logDone("ps - test ps filter exited")
   476  }