docker_cli_top_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestTopNonPrivileged(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. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
  14. out, _, err = runCommandWithOutput(topCmd)
  15. errorOut(err, t, fmt.Sprintf("failed to run top: %v %v", out, err))
  16. topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
  17. out2, _, err2 := runCommandWithOutput(topCmd)
  18. errorOut(err, t, fmt.Sprintf("failed to run top: %v %v", out2, err2))
  19. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  20. _, err = runCommand(killCmd)
  21. errorOut(err, t, fmt.Sprintf("failed to kill container: %v", err))
  22. deleteContainer(cleanedContainerID)
  23. if !strings.Contains(out, "sleep 20") && !strings.Contains(out2, "sleep 20") {
  24. t.Fatal("top should've listed `sleep 20` in the process list, but failed twice")
  25. } else if !strings.Contains(out, "sleep 20") {
  26. t.Fatal("top should've listed `sleep 20` in the process list, but failed the first time")
  27. } else if !strings.Contains(out2, "sleep 20") {
  28. t.Fatal("top should've listed `sleep 20` in the process list, but failed the second itime")
  29. }
  30. logDone("top - sleep process should be listed in non privileged mode")
  31. }
  32. func TestTopPrivileged(t *testing.T) {
  33. runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "sleep", "20")
  34. out, _, err := runCommandWithOutput(runCmd)
  35. errorOut(err, t, fmt.Sprintf("failed to start the container: %v", err))
  36. cleanedContainerID := stripTrailingCharacters(out)
  37. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
  38. out, _, err = runCommandWithOutput(topCmd)
  39. errorOut(err, t, fmt.Sprintf("failed to run top: %v %v", out, err))
  40. topCmd = exec.Command(dockerBinary, "top", cleanedContainerID)
  41. out2, _, err2 := runCommandWithOutput(topCmd)
  42. errorOut(err, t, fmt.Sprintf("failed to run top: %v %v", out2, err2))
  43. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  44. _, err = runCommand(killCmd)
  45. errorOut(err, t, fmt.Sprintf("failed to kill container: %v", err))
  46. deleteContainer(cleanedContainerID)
  47. if !strings.Contains(out, "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(out, "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 privileged mode")
  55. }