docker_cli_top_test.go 3.0 KB

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