docker_cli_top_test.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "strings"
  4. "github.com/docker/docker/integration-cli/checker"
  5. icmd "github.com/docker/docker/pkg/testutil/cmd"
  6. "github.com/go-check/check"
  7. )
  8. func (s *DockerSuite) TestTopMultipleArgs(c *check.C) {
  9. out := runSleepingContainer(c, "-d")
  10. cleanedContainerID := strings.TrimSpace(out)
  11. var expected icmd.Expected
  12. switch testEnv.DaemonPlatform() {
  13. case "windows":
  14. expected = icmd.Expected{ExitCode: 1, Err: "Windows does not support arguments to top"}
  15. default:
  16. expected = icmd.Expected{Out: "PID"}
  17. }
  18. result := dockerCmdWithResult("top", cleanedContainerID, "-o", "pid")
  19. c.Assert(result, icmd.Matches, expected)
  20. }
  21. func (s *DockerSuite) TestTopNonPrivileged(c *check.C) {
  22. out := runSleepingContainer(c, "-d")
  23. cleanedContainerID := strings.TrimSpace(out)
  24. out1, _ := dockerCmd(c, "top", cleanedContainerID)
  25. out2, _ := dockerCmd(c, "top", cleanedContainerID)
  26. dockerCmd(c, "kill", cleanedContainerID)
  27. // Windows will list the name of the launched executable which in this case is busybox.exe, without the parameters.
  28. // Linux will display the command executed in the container
  29. var lookingFor string
  30. if testEnv.DaemonPlatform() == "windows" {
  31. lookingFor = "busybox.exe"
  32. } else {
  33. lookingFor = "top"
  34. }
  35. c.Assert(out1, checker.Contains, lookingFor, check.Commentf("top should've listed `%s` in the process list, but failed the first time", lookingFor))
  36. c.Assert(out2, checker.Contains, lookingFor, check.Commentf("top should've listed `%s` in the process list, but failed the second time", lookingFor))
  37. }
  38. // TestTopWindowsCoreProcesses validates that there are lines for the critical
  39. // processes which are found in a Windows container. Note Windows is architecturally
  40. // very different to Linux in this regard.
  41. func (s *DockerSuite) TestTopWindowsCoreProcesses(c *check.C) {
  42. testRequires(c, DaemonIsWindows)
  43. out := runSleepingContainer(c, "-d")
  44. cleanedContainerID := strings.TrimSpace(out)
  45. out1, _ := dockerCmd(c, "top", cleanedContainerID)
  46. lookingFor := []string{"smss.exe", "csrss.exe", "wininit.exe", "services.exe", "lsass.exe", "CExecSvc.exe"}
  47. for i, s := range lookingFor {
  48. c.Assert(out1, checker.Contains, s, check.Commentf("top should've listed `%s` in the process list, but failed. Test case %d", s, i))
  49. }
  50. }
  51. func (s *DockerSuite) TestTopPrivileged(c *check.C) {
  52. // Windows does not support --privileged
  53. testRequires(c, DaemonIsLinux, NotUserNamespace)
  54. out, _ := dockerCmd(c, "run", "--privileged", "-i", "-d", "busybox", "top")
  55. cleanedContainerID := strings.TrimSpace(out)
  56. out1, _ := dockerCmd(c, "top", cleanedContainerID)
  57. out2, _ := dockerCmd(c, "top", cleanedContainerID)
  58. dockerCmd(c, "kill", cleanedContainerID)
  59. c.Assert(out1, checker.Contains, "top", check.Commentf("top should've listed `top` in the process list, but failed the first time"))
  60. c.Assert(out2, checker.Contains, "top", check.Commentf("top should've listed `top` in the process list, but failed the second time"))
  61. }