docker_cli_top_test.go 3.5 KB

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