docker_cli_top_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "os/exec"
  4. "strings"
  5. "github.com/go-check/check"
  6. )
  7. func (s *DockerSuite) TestTopMultipleArgs(c *check.C) {
  8. runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "top")
  9. out, _, err := runCommandWithOutput(runCmd)
  10. if err != nil {
  11. c.Fatalf("failed to start the container: %s, %v", out, err)
  12. }
  13. cleanedContainerID := strings.TrimSpace(out)
  14. defer deleteContainer(cleanedContainerID)
  15. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID, "-o", "pid")
  16. out, _, err = runCommandWithOutput(topCmd)
  17. if err != nil {
  18. c.Fatalf("failed to run top: %s, %v", out, err)
  19. }
  20. if !strings.Contains(out, "PID") {
  21. c.Fatalf("did not see PID after top -o pid: %s", out)
  22. }
  23. }
  24. func (s *DockerSuite) TestTopNonPrivileged(c *check.C) {
  25. runCmd := exec.Command(dockerBinary, "run", "-i", "-d", "busybox", "top")
  26. out, _, err := runCommandWithOutput(runCmd)
  27. if err != nil {
  28. c.Fatalf("failed to start the container: %s, %v", out, err)
  29. }
  30. cleanedContainerID := strings.TrimSpace(out)
  31. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
  32. out1, _, err := runCommandWithOutput(topCmd)
  33. if err != nil {
  34. c.Fatalf("failed to run top: %s, %v", out1, err)
  35. }
  36. topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
  37. out2, _, err := runCommandWithOutput(topCmd)
  38. if err != nil {
  39. c.Fatalf("failed to run top: %s, %v", out2, err)
  40. }
  41. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  42. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  43. c.Fatalf("failed to kill container: %s, %v", out, err)
  44. }
  45. deleteContainer(cleanedContainerID)
  46. if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") {
  47. c.Fatal("top should've listed `top` in the process list, but failed twice")
  48. } else if !strings.Contains(out1, "top") {
  49. c.Fatal("top should've listed `top` in the process list, but failed the first time")
  50. } else if !strings.Contains(out2, "top") {
  51. c.Fatal("top should've listed `top` in the process list, but failed the second itime")
  52. }
  53. }
  54. func (s *DockerSuite) TestTopPrivileged(c *check.C) {
  55. runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "top")
  56. out, _, err := runCommandWithOutput(runCmd)
  57. if err != nil {
  58. c.Fatalf("failed to start the container: %s, %v", out, err)
  59. }
  60. cleanedContainerID := strings.TrimSpace(out)
  61. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
  62. out1, _, err := runCommandWithOutput(topCmd)
  63. if err != nil {
  64. c.Fatalf("failed to run top: %s, %v", out1, err)
  65. }
  66. topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
  67. out2, _, err := runCommandWithOutput(topCmd)
  68. if err != nil {
  69. c.Fatalf("failed to run top: %s, %v", out2, err)
  70. }
  71. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  72. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  73. c.Fatalf("failed to kill container: %s, %v", out, err)
  74. }
  75. deleteContainer(cleanedContainerID)
  76. if !strings.Contains(out1, "top") && !strings.Contains(out2, "top") {
  77. c.Fatal("top should've listed `top` in the process list, but failed twice")
  78. } else if !strings.Contains(out1, "top") {
  79. c.Fatal("top should've listed `top` in the process list, but failed the first time")
  80. } else if !strings.Contains(out2, "top") {
  81. c.Fatal("top should've listed `top` in the process list, but failed the second itime")
  82. }
  83. }