docker_cli_top_test.go 3.4 KB

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