docker_cli_top_test.go 3.2 KB

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