docker_cli_top_test.go 3.0 KB

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