github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/top_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"os"
     5  	"os/user"
     6  
     7  	. "github.com/containers/podman/v5/test/utils"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	. "github.com/onsi/gomega/gexec"
    11  )
    12  
    13  var _ = Describe("Podman top", func() {
    14  
    15  	It("podman top without container name or id", func() {
    16  		result := podmanTest.Podman([]string{"top"})
    17  		result.WaitWithDefaultTimeout()
    18  		Expect(result).Should(Exit(125))
    19  	})
    20  
    21  	It("podman top on bogus container", func() {
    22  		result := podmanTest.Podman([]string{"top", "1234"})
    23  		result.WaitWithDefaultTimeout()
    24  		Expect(result).Should(Exit(125))
    25  	})
    26  
    27  	It("podman top on non-running container", func() {
    28  		_, ec, cid := podmanTest.RunLsContainer("")
    29  		Expect(ec).To(Equal(0))
    30  		result := podmanTest.Podman([]string{"top", cid})
    31  		result.WaitWithDefaultTimeout()
    32  		Expect(result).Should(Exit(125))
    33  	})
    34  
    35  	It("podman top on container", func() {
    36  		session := podmanTest.Podman([]string{"run", "--name", "test", "-d", ALPINE, "top", "-d", "2"})
    37  		session.WaitWithDefaultTimeout()
    38  		Expect(session).Should(ExitCleanly())
    39  
    40  		result := podmanTest.Podman([]string{"top", "test"})
    41  		result.WaitWithDefaultTimeout()
    42  		Expect(result).Should(ExitCleanly())
    43  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    44  	})
    45  
    46  	It("podman container top on container", func() {
    47  		session := podmanTest.Podman([]string{"container", "run", "--name", "test", "-d", ALPINE, "top", "-d", "2"})
    48  		session.WaitWithDefaultTimeout()
    49  		Expect(session).Should(ExitCleanly())
    50  
    51  		result := podmanTest.Podman([]string{"container", "top", "test"})
    52  		result.WaitWithDefaultTimeout()
    53  		Expect(result).Should(ExitCleanly())
    54  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    55  
    56  		// Just a smoke test since groups may change over time.
    57  		result = podmanTest.Podman([]string{"container", "top", "test", "groups", "hgroups"})
    58  		result.WaitWithDefaultTimeout()
    59  		Expect(result).Should(ExitCleanly())
    60  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    61  	})
    62  
    63  	It("podman top with options", func() {
    64  		session := podmanTest.Podman([]string{"run", "-d", ALPINE, "top", "-d", "2"})
    65  		session.WaitWithDefaultTimeout()
    66  		Expect(session).Should(ExitCleanly())
    67  
    68  		result := podmanTest.Podman([]string{"top", session.OutputToString(), "pid", "%C", "args"})
    69  		result.WaitWithDefaultTimeout()
    70  		Expect(result).Should(ExitCleanly())
    71  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    72  
    73  		result = podmanTest.Podman([]string{"container", "top", session.OutputToString(), "uid"})
    74  		result.WaitWithDefaultTimeout()
    75  		Expect(result).Should(ExitCleanly())
    76  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    77  		Expect(result.OutputToStringArray()[1]).To(Equal("0"))
    78  
    79  		user, err := user.Current()
    80  		if err != nil {
    81  			os.Exit(1)
    82  		}
    83  
    84  		result = podmanTest.Podman([]string{"container", "top", session.OutputToString(), "huid"})
    85  		result.WaitWithDefaultTimeout()
    86  		Expect(result).Should(ExitCleanly())
    87  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
    88  		Expect(result.OutputToStringArray()[1]).To(Equal(user.Uid))
    89  	})
    90  
    91  	It("podman top with ps(1) options", func() {
    92  		session := podmanTest.Podman([]string{"run", "-d", fedoraMinimal, "sleep", "inf"})
    93  		session.WaitWithDefaultTimeout()
    94  		Expect(session).Should(ExitCleanly())
    95  
    96  		result := podmanTest.Podman([]string{"top", session.OutputToString(), "aux"})
    97  		result.WaitWithDefaultTimeout()
    98  		Expect(result).Should(ExitCleanly())
    99  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
   100  
   101  		result = podmanTest.Podman([]string{"top", session.OutputToString(), "ax -o args"})
   102  		result.WaitWithDefaultTimeout()
   103  		Expect(result).Should(ExitCleanly())
   104  
   105  		result = podmanTest.Podman([]string{"top", session.OutputToString(), "ax", "-o", "args"})
   106  		result.WaitWithDefaultTimeout()
   107  		Expect(result).Should(ExitCleanly())
   108  		Expect(result.OutputToStringArray()).To(Equal([]string{"COMMAND", "sleep inf"}))
   109  
   110  		// Now make sure we use ps in the container with CAP_SYS_PTRACE
   111  		session = podmanTest.Podman([]string{"run", "-d", "--cap-add=SYS_PTRACE", fedoraMinimal, "sleep", "inf"})
   112  		session.WaitWithDefaultTimeout()
   113  		Expect(session).Should(ExitCleanly())
   114  
   115  		// Because the image does not contain this must fail and we know we use the correct podman exec fallback.
   116  		exec := podmanTest.Podman([]string{"top", session.OutputToString(), "aux"})
   117  		exec.WaitWithDefaultTimeout()
   118  		Expect(exec).Should(Exit(125))
   119  		Expect(exec.ErrorToString()).Should(ContainSubstring("OCI runtime attempted to invoke a command that was not found"))
   120  	})
   121  
   122  	It("podman top with comma-separated options", func() {
   123  		session := podmanTest.Podman([]string{"run", "-d", ALPINE, "top", "-d", "2"})
   124  		session.WaitWithDefaultTimeout()
   125  		Expect(session).Should(ExitCleanly())
   126  
   127  		result := podmanTest.Podman([]string{"top", session.OutputToString(), "user,pid,comm"})
   128  		result.WaitWithDefaultTimeout()
   129  		Expect(result).Should(ExitCleanly())
   130  		Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
   131  	})
   132  
   133  	It("podman top on container invalid options", func() {
   134  		top := podmanTest.RunTopContainer("")
   135  		top.WaitWithDefaultTimeout()
   136  		Expect(top).Should(ExitCleanly())
   137  		cid := top.OutputToString()
   138  
   139  		// We need to pass -eo to force executing ps in the Alpine container.
   140  		// Alpines stripped down ps(1) is accepting any kind of weird input in
   141  		// contrast to others, such that a `ps invalid` will silently ignore
   142  		// the wrong input and still print the -ef output instead.
   143  		result := podmanTest.Podman([]string{"top", cid, "-eo", "invalid"})
   144  		result.WaitWithDefaultTimeout()
   145  		Expect(result).Should(Exit(125))
   146  	})
   147  
   148  	It("podman top on privileged container", func() {
   149  		session := podmanTest.Podman([]string{"run", "--privileged", "-d", ALPINE, "top"})
   150  		session.WaitWithDefaultTimeout()
   151  		Expect(session).Should(ExitCleanly())
   152  		cid := session.OutputToString()
   153  
   154  		result := podmanTest.Podman([]string{"top", cid, "capeff"})
   155  		result.WaitWithDefaultTimeout()
   156  		Expect(result).Should(ExitCleanly())
   157  		Expect(result.OutputToStringArray()).To(Equal([]string{"EFFECTIVE CAPS", "full"}))
   158  	})
   159  })