docker_cli_top_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  17. _, err = runCommand(killCmd)
  18. errorOut(err, t, fmt.Sprintf("failed to kill container: %v", err))
  19. deleteContainer(cleanedContainerID)
  20. if !strings.Contains(out, "sleep 20") {
  21. t.Fatal("top should've listed sleep 20 in the process list")
  22. }
  23. logDone("top - sleep process should be listed in non privileged mode")
  24. }
  25. func TestTopPrivileged(t *testing.T) {
  26. runCmd := exec.Command(dockerBinary, "run", "--privileged", "-i", "-d", "busybox", "sleep", "20")
  27. out, _, err := runCommandWithOutput(runCmd)
  28. errorOut(err, t, fmt.Sprintf("failed to start the container: %v", err))
  29. cleanedContainerID := stripTrailingCharacters(out)
  30. topCmd := exec.Command(dockerBinary, "top", cleanedContainerID)
  31. out, _, err = runCommandWithOutput(topCmd)
  32. errorOut(err, t, fmt.Sprintf("failed to run top: %v %v", out, err))
  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") {
  38. t.Fatal("top should've listed sleep 20 in the process list")
  39. }
  40. logDone("top - sleep process should be listed in privileged mode")
  41. }