github.com/AbhinandanKurakure/podman/v3@v3.4.10/test/e2e/stats_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  	"time"
     8  
     9  	. "github.com/containers/podman/v3/test/utils"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	. "github.com/onsi/gomega/gexec"
    13  )
    14  
    15  // TODO: we need to check the output. Currently, we only check the exit codes
    16  // which is not enough.
    17  var _ = Describe("Podman stats", func() {
    18  	var (
    19  		tempdir    string
    20  		podmanTest *PodmanTestIntegration
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		SkipIfRootlessCgroupsV1("stats not supported on cgroupv1 for rootless users")
    25  		if isContainerized() {
    26  			SkipIfCgroupV1("stats not supported inside cgroupv1 container environment")
    27  		}
    28  		var err error
    29  		tempdir, err = CreateTempDirInTempDir()
    30  		if err != nil {
    31  			os.Exit(1)
    32  		}
    33  		podmanTest = PodmanTestCreate(tempdir)
    34  		podmanTest.Setup()
    35  		podmanTest.SeedImages()
    36  	})
    37  
    38  	AfterEach(func() {
    39  		podmanTest.Cleanup()
    40  		f := CurrentGinkgoTestDescription()
    41  		processTestResult(f)
    42  
    43  	})
    44  
    45  	It("podman stats with bogus container", func() {
    46  		session := podmanTest.Podman([]string{"stats", "--no-stream", "123"})
    47  		session.WaitWithDefaultTimeout()
    48  		Expect(session).Should(Exit(125))
    49  	})
    50  
    51  	It("podman stats on a running container", func() {
    52  		session := podmanTest.RunTopContainer("")
    53  		session.WaitWithDefaultTimeout()
    54  		Expect(session).Should(Exit(0))
    55  		cid := session.OutputToString()
    56  		session = podmanTest.Podman([]string{"stats", "--no-stream", cid})
    57  		session.WaitWithDefaultTimeout()
    58  		Expect(session).Should(Exit(0))
    59  	})
    60  
    61  	It("podman stats on all containers", func() {
    62  		session := podmanTest.RunTopContainer("")
    63  		session.WaitWithDefaultTimeout()
    64  		Expect(session).Should(Exit(0))
    65  		session = podmanTest.Podman([]string{"stats", "--no-stream", "-a"})
    66  		session.WaitWithDefaultTimeout()
    67  		Expect(session).Should(Exit(0))
    68  	})
    69  
    70  	It("podman stats on all running containers", func() {
    71  		session := podmanTest.RunTopContainer("")
    72  		session.WaitWithDefaultTimeout()
    73  		Expect(session).Should(Exit(0))
    74  		session = podmanTest.Podman([]string{"stats", "--no-stream"})
    75  		session.WaitWithDefaultTimeout()
    76  		Expect(session).Should(Exit(0))
    77  	})
    78  
    79  	It("podman stats only output cids", func() {
    80  		session := podmanTest.RunTopContainer("")
    81  		session.WaitWithDefaultTimeout()
    82  		Expect(session).Should(Exit(0))
    83  		session = podmanTest.Podman([]string{"stats", "--all", "--no-stream", "--format", "\"{{.ID}}\""})
    84  		session.WaitWithDefaultTimeout()
    85  		Expect(session).Should(Exit(0))
    86  	})
    87  
    88  	It("podman stats with GO template", func() {
    89  		session := podmanTest.RunTopContainer("")
    90  		session.WaitWithDefaultTimeout()
    91  		Expect(session).Should(Exit(0))
    92  		stats := podmanTest.Podman([]string{"stats", "-a", "--no-reset", "--no-stream", "--format", "table {{.ID}} {{.AVGCPU}} {{.MemUsage}} {{.CPU}} {{.NetIO}} {{.BlockIO}} {{.PIDS}}"})
    93  		stats.WaitWithDefaultTimeout()
    94  		Expect(stats).To(Exit(0))
    95  	})
    96  
    97  	It("podman stats with invalid GO template", func() {
    98  		session := podmanTest.RunTopContainer("")
    99  		session.WaitWithDefaultTimeout()
   100  		Expect(session).Should(Exit(0))
   101  		stats := podmanTest.Podman([]string{"stats", "-a", "--no-reset", "--no-stream", "--format", "\"table {{.ID}} {{.NoSuchField}} \""})
   102  		stats.WaitWithDefaultTimeout()
   103  		Expect(stats).To(ExitWithError())
   104  	})
   105  
   106  	It("podman stats with negative interval", func() {
   107  		session := podmanTest.RunTopContainer("")
   108  		session.WaitWithDefaultTimeout()
   109  		Expect(session).Should(Exit(0))
   110  		stats := podmanTest.Podman([]string{"stats", "-a", "--no-reset", "--no-stream", "--interval=-1"})
   111  		stats.WaitWithDefaultTimeout()
   112  		Expect(stats).To(ExitWithError())
   113  	})
   114  
   115  	It("podman stats with zero interval", func() {
   116  		session := podmanTest.RunTopContainer("")
   117  		session.WaitWithDefaultTimeout()
   118  		Expect(session).Should(Exit(0))
   119  		stats := podmanTest.Podman([]string{"stats", "-a", "--no-reset", "--no-stream", "--interval=0"})
   120  		stats.WaitWithDefaultTimeout()
   121  		Expect(stats).To(ExitWithError())
   122  	})
   123  
   124  	It("podman stats with interval", func() {
   125  		session := podmanTest.RunTopContainer("")
   126  		session.WaitWithDefaultTimeout()
   127  		Expect(session).Should(Exit(0))
   128  		stats := podmanTest.Podman([]string{"stats", "-a", "--no-reset", "--no-stream", "--interval=5"})
   129  		stats.WaitWithDefaultTimeout()
   130  		Expect(stats).Should(Exit(0))
   131  	})
   132  
   133  	It("podman stats with json output", func() {
   134  		var found bool
   135  		session := podmanTest.RunTopContainer("")
   136  		session.WaitWithDefaultTimeout()
   137  		Expect(session).Should(Exit(0))
   138  		for i := 0; i < 5; i++ {
   139  			ps := podmanTest.Podman([]string{"ps", "-q"})
   140  			ps.WaitWithDefaultTimeout()
   141  			if len(ps.OutputToStringArray()) == 1 {
   142  				found = true
   143  				break
   144  			}
   145  			time.Sleep(time.Second)
   146  		}
   147  		Expect(found).To(BeTrue())
   148  		stats := podmanTest.Podman([]string{"stats", "--all", "--no-stream", "--format", "json"})
   149  		stats.WaitWithDefaultTimeout()
   150  		Expect(stats).Should(Exit(0))
   151  		Expect(stats.IsJSONOutputValid()).To(BeTrue())
   152  	})
   153  
   154  	It("podman stats on a container with no net ns", func() {
   155  		session := podmanTest.Podman([]string{"run", "-d", "--net", "none", ALPINE, "top"})
   156  		session.WaitWithDefaultTimeout()
   157  		Expect(session).Should(Exit(0))
   158  		session = podmanTest.Podman([]string{"stats", "--no-stream", "-a"})
   159  		session.WaitWithDefaultTimeout()
   160  		Expect(session).Should(Exit(0))
   161  	})
   162  
   163  	It("podman stats on a container that joined another's net ns", func() {
   164  		session := podmanTest.RunTopContainer("")
   165  		session.WaitWithDefaultTimeout()
   166  		Expect(session).Should(Exit(0))
   167  		cid := session.OutputToString()
   168  
   169  		session = podmanTest.Podman([]string{"run", "-d", "--net", fmt.Sprintf("container:%s", cid), ALPINE, "top"})
   170  		session.WaitWithDefaultTimeout()
   171  		Expect(session).Should(Exit(0))
   172  
   173  		session = podmanTest.Podman([]string{"stats", "--no-stream", "-a"})
   174  		session.WaitWithDefaultTimeout()
   175  		Expect(session).Should(Exit(0))
   176  	})
   177  
   178  	It("podman stats on container with forced slirp4netns", func() {
   179  		// This will force the slirp4netns net mode to be tested as root
   180  		session := podmanTest.Podman([]string{"run", "-d", "--net", "slirp4netns", ALPINE, "top"})
   181  		session.WaitWithDefaultTimeout()
   182  		Expect(session).Should(Exit(0))
   183  		session = podmanTest.Podman([]string{"stats", "--no-stream", "-a"})
   184  		session.WaitWithDefaultTimeout()
   185  		Expect(session).Should(Exit(0))
   186  	})
   187  
   188  	// Regression test for #8265
   189  	It("podman stats with custom memory limits", func() {
   190  		// Run three containers. One with a memory limit.  Make sure
   191  		// that the limits are different and the limited one has a
   192  		// lower limit.
   193  		ctrNoLimit0 := "no-limit-0"
   194  		ctrNoLimit1 := "no-limit-1"
   195  		ctrWithLimit := "with-limit"
   196  
   197  		session := podmanTest.Podman([]string{"run", "-d", "--name", ctrNoLimit0, ALPINE, "top"})
   198  		session.WaitWithDefaultTimeout()
   199  		Expect(session).Should(Exit(0))
   200  
   201  		session = podmanTest.Podman([]string{"run", "-d", "--name", ctrNoLimit1, ALPINE, "top"})
   202  		session.WaitWithDefaultTimeout()
   203  		Expect(session).Should(Exit(0))
   204  
   205  		session = podmanTest.Podman([]string{"run", "-d", "--name", ctrWithLimit, "--memory", "50m", ALPINE, "top"})
   206  		session.WaitWithDefaultTimeout()
   207  		Expect(session).Should(Exit(0))
   208  
   209  		session = podmanTest.Podman([]string{"stats", "--no-stream", "--format", "{{.MemLimit}}", ctrNoLimit0, ctrNoLimit1, ctrWithLimit})
   210  		session.WaitWithDefaultTimeout()
   211  		Expect(session).Should(Exit(0))
   212  
   213  		// We have three containers.  The unlimited ones need to have
   214  		// the same limit, the limited one a lower one.
   215  		limits := session.OutputToStringArray()
   216  		Expect(len(limits)).To(BeNumerically("==", 3))
   217  		Expect(limits[0]).To(Equal(limits[1]))
   218  		Expect(limits[0]).ToNot(Equal(limits[2]))
   219  
   220  		defaultLimit, err := strconv.Atoi(limits[0])
   221  		Expect(err).To(BeNil())
   222  		customLimit, err := strconv.Atoi(limits[2])
   223  		Expect(err).To(BeNil())
   224  
   225  		Expect(customLimit).To(BeNumerically("<", defaultLimit))
   226  	})
   227  })