github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/integration-cli/docker_cli_top_test.go (about)

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