gopkg.in/docker/docker.v20@v20.10.27/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  func (s *DockerSuite) TestTopMultipleArgs(c *testing.T) {
    12  	out := runSleepingContainer(c, "-d")
    13  	cleanedContainerID := strings.TrimSpace(out)
    14  
    15  	var expected icmd.Expected
    16  	switch testEnv.OSType {
    17  	case "windows":
    18  		expected = icmd.Expected{ExitCode: 1, Err: "Windows does not support arguments to top"}
    19  	default:
    20  		expected = icmd.Expected{Out: "PID"}
    21  	}
    22  	result := dockerCmdWithResult("top", cleanedContainerID, "-o", "pid")
    23  	result.Assert(c, expected)
    24  }
    25  
    26  func (s *DockerSuite) TestTopNonPrivileged(c *testing.T) {
    27  	out := runSleepingContainer(c, "-d")
    28  	cleanedContainerID := strings.TrimSpace(out)
    29  
    30  	out1, _ := dockerCmd(c, "top", cleanedContainerID)
    31  	out2, _ := dockerCmd(c, "top", cleanedContainerID)
    32  	dockerCmd(c, "kill", cleanedContainerID)
    33  
    34  	// Windows will list the name of the launched executable which in this case is busybox.exe, without the parameters.
    35  	// Linux will display the command executed in the container
    36  	var lookingFor string
    37  	if testEnv.OSType == "windows" {
    38  		lookingFor = "busybox.exe"
    39  	} else {
    40  		lookingFor = "top"
    41  	}
    42  
    43  	assert.Assert(c, strings.Contains(out1, lookingFor), "top should've listed `%s` in the process list, but failed the first time", lookingFor)
    44  	assert.Assert(c, strings.Contains(out2, lookingFor), "top should've listed `%s` in the process list, but failed the second time", lookingFor)
    45  }
    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 *DockerSuite) 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  
    61  func (s *DockerSuite) TestTopPrivileged(c *testing.T) {
    62  	// Windows does not support --privileged
    63  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    64  	out, _ := dockerCmd(c, "run", "--privileged", "-i", "-d", "busybox", "top")
    65  	cleanedContainerID := strings.TrimSpace(out)
    66  
    67  	out1, _ := dockerCmd(c, "top", cleanedContainerID)
    68  	out2, _ := dockerCmd(c, "top", cleanedContainerID)
    69  	dockerCmd(c, "kill", cleanedContainerID)
    70  
    71  	assert.Assert(c, strings.Contains(out1, "top"), "top should've listed `top` in the process list, but failed the first time")
    72  	assert.Assert(c, strings.Contains(out2, "top"), "top should've listed `top` in the process list, but failed the second time")
    73  }