github.com/crquan/docker@v1.8.1/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 out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish") 240 if !strings.Contains(out, "Unrecognised filter value for status") { 241 c.Fatalf("Expected error response due to invalid status filter output: %q", out) 242 } 243 244 } 245 246 func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) { 247 248 // start container 249 out, _ := dockerCmd(c, "run", "-d", "busybox") 250 firstID := strings.TrimSpace(out) 251 252 // start another container 253 dockerCmd(c, "run", "-d", "busybox", "top") 254 255 // filter containers by id 256 out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=id="+firstID) 257 containerOut := strings.TrimSpace(out) 258 if containerOut != firstID[:12] { 259 c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out) 260 } 261 262 } 263 264 func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) { 265 266 // start container 267 out, _ := dockerCmd(c, "run", "-d", "--name=a_name_to_match", "busybox") 268 firstID := strings.TrimSpace(out) 269 270 // start another container 271 dockerCmd(c, "run", "-d", "--name=b_name_to_match", "busybox", "top") 272 273 // filter containers by name 274 out, _ = dockerCmd(c, "ps", "-a", "-q", "--filter=name=a_name_to_match") 275 containerOut := strings.TrimSpace(out) 276 if containerOut != firstID[:12] { 277 c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out) 278 } 279 280 } 281 282 func (s *DockerSuite) TestPsListContainersFilterLabel(c *check.C) { 283 // start container 284 out, _ := dockerCmd(c, "run", "-d", "-l", "match=me", "-l", "second=tag", "busybox") 285 firstID := strings.TrimSpace(out) 286 287 // start another container 288 out, _ = dockerCmd(c, "run", "-d", "-l", "match=me too", "busybox") 289 secondID := strings.TrimSpace(out) 290 291 // start third container 292 out, _ = dockerCmd(c, "run", "-d", "-l", "nomatch=me", "busybox") 293 thirdID := strings.TrimSpace(out) 294 295 // filter containers by exact match 296 out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me") 297 containerOut := strings.TrimSpace(out) 298 if containerOut != firstID { 299 c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out) 300 } 301 302 // filter containers by two labels 303 out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag") 304 containerOut = strings.TrimSpace(out) 305 if containerOut != firstID { 306 c.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID, containerOut, out) 307 } 308 309 // filter containers by two labels, but expect not found because of AND behavior 310 out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match=me", "--filter=label=second=tag-no") 311 containerOut = strings.TrimSpace(out) 312 if containerOut != "" { 313 c.Fatalf("Expected nothing, got %s for exited filter, output: %q", containerOut, out) 314 } 315 316 // filter containers by exact key 317 out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=label=match") 318 containerOut = strings.TrimSpace(out) 319 if (!strings.Contains(containerOut, firstID) || !strings.Contains(containerOut, secondID)) || strings.Contains(containerOut, thirdID) { 320 c.Fatalf("Expected ids %s,%s, got %s for exited filter, output: %q", firstID, secondID, containerOut, out) 321 } 322 } 323 324 func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) { 325 326 dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top") 327 328 dockerCmd(c, "run", "--name", "zero1", "busybox", "true") 329 firstZero, err := getIDByName("zero1") 330 if err != nil { 331 c.Fatal(err) 332 } 333 334 dockerCmd(c, "run", "--name", "zero2", "busybox", "true") 335 secondZero, err := getIDByName("zero2") 336 if err != nil { 337 c.Fatal(err) 338 } 339 340 if out, _, err := dockerCmdWithError(c, "run", "--name", "nonzero1", "busybox", "false"); err == nil { 341 c.Fatal("Should fail.", out, err) 342 } 343 344 firstNonZero, err := getIDByName("nonzero1") 345 if err != nil { 346 c.Fatal(err) 347 } 348 349 if out, _, err := dockerCmdWithError(c, "run", "--name", "nonzero2", "busybox", "false"); err == nil { 350 c.Fatal("Should fail.", out, err) 351 } 352 secondNonZero, err := getIDByName("nonzero2") 353 if err != nil { 354 c.Fatal(err) 355 } 356 357 // filter containers by exited=0 358 out, _ := dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=0") 359 ids := strings.Split(strings.TrimSpace(out), "\n") 360 if len(ids) != 2 { 361 c.Fatalf("Should be 2 zero exited containers got %d: %s", len(ids), out) 362 } 363 if ids[0] != secondZero { 364 c.Fatalf("First in list should be %q, got %q", secondZero, ids[0]) 365 } 366 if ids[1] != firstZero { 367 c.Fatalf("Second in list should be %q, got %q", firstZero, ids[1]) 368 } 369 370 out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=exited=1") 371 ids = strings.Split(strings.TrimSpace(out), "\n") 372 if len(ids) != 2 { 373 c.Fatalf("Should be 2 zero exited containers got %d", len(ids)) 374 } 375 if ids[0] != secondNonZero { 376 c.Fatalf("First in list should be %q, got %q", secondNonZero, ids[0]) 377 } 378 if ids[1] != firstNonZero { 379 c.Fatalf("Second in list should be %q, got %q", firstNonZero, ids[1]) 380 } 381 382 } 383 384 func (s *DockerSuite) TestPsRightTagName(c *check.C) { 385 tag := "asybox:shmatest" 386 dockerCmd(c, "tag", "busybox", tag) 387 388 var id1 string 389 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 390 id1 = strings.TrimSpace(string(out)) 391 392 var id2 string 393 out, _ = dockerCmd(c, "run", "-d", tag, "top") 394 id2 = strings.TrimSpace(string(out)) 395 396 var imageID string 397 out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox") 398 imageID = strings.TrimSpace(string(out)) 399 400 var id3 string 401 out, _ = dockerCmd(c, "run", "-d", imageID, "top") 402 id3 = strings.TrimSpace(string(out)) 403 404 out, _ = dockerCmd(c, "ps", "--no-trunc") 405 lines := strings.Split(strings.TrimSpace(string(out)), "\n") 406 // skip header 407 lines = lines[1:] 408 if len(lines) != 3 { 409 c.Fatalf("There should be 3 running container, got %d", len(lines)) 410 } 411 for _, line := range lines { 412 f := strings.Fields(line) 413 switch f[0] { 414 case id1: 415 if f[1] != "busybox" { 416 c.Fatalf("Expected %s tag for id %s, got %s", "busybox", id1, f[1]) 417 } 418 case id2: 419 if f[1] != tag { 420 c.Fatalf("Expected %s tag for id %s, got %s", tag, id2, f[1]) 421 } 422 case id3: 423 if f[1] != imageID { 424 c.Fatalf("Expected %s imageID for id %s, got %s", tag, id3, f[1]) 425 } 426 default: 427 c.Fatalf("Unexpected id %s, expected %s and %s and %s", f[0], id1, id2, id3) 428 } 429 } 430 } 431 432 func (s *DockerSuite) TestPsLinkedWithNoTrunc(c *check.C) { 433 dockerCmd(c, "run", "--name=first", "-d", "busybox", "top") 434 dockerCmd(c, "run", "--name=second", "--link=first:first", "-d", "busybox", "top") 435 436 out, _ := dockerCmd(c, "ps", "--no-trunc") 437 lines := strings.Split(strings.TrimSpace(string(out)), "\n") 438 // strip header 439 lines = lines[1:] 440 expected := []string{"second", "first,second/first"} 441 var names []string 442 for _, l := range lines { 443 fields := strings.Fields(l) 444 names = append(names, fields[len(fields)-1]) 445 } 446 if !reflect.DeepEqual(expected, names) { 447 c.Fatalf("Expected array: %v, got: %v", expected, names) 448 } 449 } 450 451 func (s *DockerSuite) TestPsGroupPortRange(c *check.C) { 452 453 portRange := "3800-3900" 454 dockerCmd(c, "run", "-d", "--name", "porttest", "-p", portRange+":"+portRange, "busybox", "top") 455 456 out, _ := dockerCmd(c, "ps") 457 458 // check that the port range is in the output 459 if !strings.Contains(string(out), portRange) { 460 c.Fatalf("docker ps output should have had the port range %q: %s", portRange, string(out)) 461 } 462 463 } 464 465 func (s *DockerSuite) TestPsWithSize(c *check.C) { 466 dockerCmd(c, "run", "-d", "--name", "sizetest", "busybox", "top") 467 468 out, _ := dockerCmd(c, "ps", "--size") 469 if !strings.Contains(out, "virtual") { 470 c.Fatalf("docker ps with --size should show virtual size of container") 471 } 472 } 473 474 func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) { 475 // create a container 476 out, _ := dockerCmd(c, "create", "busybox") 477 cID := strings.TrimSpace(out) 478 shortCID := cID[:12] 479 480 // Make sure it DOESN'T show up w/o a '-a' for normal 'ps' 481 out, _ = dockerCmd(c, "ps", "-q") 482 if strings.Contains(out, shortCID) { 483 c.Fatalf("Should have not seen '%s' in ps output:\n%s", shortCID, out) 484 } 485 486 // Make sure it DOES show up as 'Created' for 'ps -a' 487 out, _ = dockerCmd(c, "ps", "-a") 488 489 hits := 0 490 for _, line := range strings.Split(out, "\n") { 491 if !strings.Contains(line, shortCID) { 492 continue 493 } 494 hits++ 495 if !strings.Contains(line, "Created") { 496 c.Fatalf("Missing 'Created' on '%s'", line) 497 } 498 } 499 500 if hits != 1 { 501 c.Fatalf("Should have seen '%s' in ps -a output once:%d\n%s", shortCID, hits, out) 502 } 503 504 // filter containers by 'create' - note, no -a needed 505 out, _ = dockerCmd(c, "ps", "-q", "-f", "status=created") 506 containerOut := strings.TrimSpace(out) 507 if !strings.HasPrefix(cID, containerOut) { 508 c.Fatalf("Expected id %s, got %s for filter, out: %s", cID, containerOut, out) 509 } 510 } 511 512 func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) { 513 //create 2 containers and link them 514 dockerCmd(c, "run", "--name=child", "-d", "busybox", "top") 515 dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top") 516 517 //use the new format capabilities to only list the names and --no-trunc to get all names 518 out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc") 519 lines := strings.Split(strings.TrimSpace(string(out)), "\n") 520 expected := []string{"parent", "child,parent/linkedone"} 521 var names []string 522 for _, l := range lines { 523 names = append(names, l) 524 } 525 if !reflect.DeepEqual(expected, names) { 526 c.Fatalf("Expected array with non-truncated names: %v, got: %v", expected, names) 527 } 528 529 //now list without turning off truncation and make sure we only get the non-link names 530 out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}") 531 lines = strings.Split(strings.TrimSpace(string(out)), "\n") 532 expected = []string{"parent", "child"} 533 var truncNames []string 534 for _, l := range lines { 535 truncNames = append(truncNames, l) 536 } 537 if !reflect.DeepEqual(expected, truncNames) { 538 c.Fatalf("Expected array with truncated names: %v, got: %v", expected, truncNames) 539 } 540 541 } 542 543 func (s *DockerSuite) TestPsFormatHeaders(c *check.C) { 544 // make sure no-container "docker ps" still prints the header row 545 out, _ := dockerCmd(c, "ps", "--format", "table {{.ID}}") 546 if out != "CONTAINER ID\n" { 547 c.Fatalf(`Expected 'CONTAINER ID\n', got %v`, out) 548 } 549 550 // verify that "docker ps" with a container still prints the header row also 551 dockerCmd(c, "run", "--name=test", "-d", "busybox", "top") 552 out, _ = dockerCmd(c, "ps", "--format", "table {{.Names}}") 553 if out != "NAMES\ntest\n" { 554 c.Fatalf(`Expected 'NAMES\ntest\n', got %v`, out) 555 } 556 }