github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_cli_top_test.go (about)

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