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

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  
     6  	. "github.com/containers/podman/v5/test/utils"
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("Podman run memory", func() {
    12  
    13  	BeforeEach(func() {
    14  		SkipIfRootlessCgroupsV1("Setting Memory not supported on cgroupv1 for rootless users")
    15  	})
    16  
    17  	It("podman run memory test", func() {
    18  		var session *PodmanSessionIntegration
    19  
    20  		if CGROUPSV2 {
    21  			session = podmanTest.Podman([]string{"run", "--memory=40m", "--net=none", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/memory.max"})
    22  		} else {
    23  			session = podmanTest.Podman([]string{"run", "--memory=40m", ALPINE, "cat", "/sys/fs/cgroup/memory/memory.limit_in_bytes"})
    24  		}
    25  		session.WaitWithDefaultTimeout()
    26  		Expect(session).Should(ExitCleanly())
    27  		Expect(session.OutputToString()).To(Equal("41943040"))
    28  	})
    29  
    30  	It("podman run memory-reservation test", func() {
    31  		var session *PodmanSessionIntegration
    32  
    33  		if CGROUPSV2 {
    34  			session = podmanTest.Podman([]string{"run", "--memory-reservation=40m", "--net=none", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/memory.low"})
    35  		} else {
    36  			session = podmanTest.Podman([]string{"run", "--memory-reservation=40m", ALPINE, "cat", "/sys/fs/cgroup/memory/memory.soft_limit_in_bytes"})
    37  		}
    38  
    39  		session.WaitWithDefaultTimeout()
    40  		Expect(session).Should(ExitCleanly())
    41  		Expect(session.OutputToString()).To(Equal("41943040"))
    42  	})
    43  
    44  	It("podman run memory-swap test", func() {
    45  		var (
    46  			session *PodmanSessionIntegration
    47  			expect  string
    48  		)
    49  
    50  		if CGROUPSV2 {
    51  			session = podmanTest.Podman([]string{"run", "--memory=20m", "--memory-swap=30M", "--net=none", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/memory.swap.max"})
    52  			expect = "10485760"
    53  		} else {
    54  			session = podmanTest.Podman([]string{"run", "--memory=20m", "--memory-swap=30M", ALPINE, "cat", "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"})
    55  			expect = "31457280"
    56  		}
    57  		session.WaitWithDefaultTimeout()
    58  		Expect(session).Should(ExitCleanly())
    59  		Expect(session.OutputToString()).To(Equal(expect))
    60  	})
    61  
    62  	for _, limit := range []string{"0", "15", "100"} {
    63  		limit := limit // Keep this value in a proper scope
    64  		testName := fmt.Sprintf("podman run memory-swappiness test(%s)", limit)
    65  		It(testName, func() {
    66  			SkipIfCgroupV2("memory-swappiness not supported on cgroupV2")
    67  			session := podmanTest.Podman([]string{"run", fmt.Sprintf("--memory-swappiness=%s", limit), ALPINE, "cat", "/sys/fs/cgroup/memory/memory.swappiness"})
    68  			session.WaitWithDefaultTimeout()
    69  			Expect(session).Should(ExitCleanly())
    70  			Expect(session.OutputToString()).To(Equal(limit))
    71  		})
    72  	}
    73  
    74  	It("podman run memory test on oomkilled container", func() {
    75  		mem := SystemExec("cat", []string{"/proc/sys/vm/overcommit_memory"})
    76  		mem.WaitWithDefaultTimeout()
    77  		if mem.OutputToString() != "0" {
    78  			Skip("overcommit memory is not set to 0")
    79  		}
    80  
    81  		ctrName := "oomkilled-ctr"
    82  		// create a container that gets oomkilled
    83  		session := podmanTest.Podman([]string{"run", "--name", ctrName, "--read-only", "--memory-swap=20m", "--memory=20m", "--oom-score-adj=1000", ALPINE, "sort", "/dev/urandom"})
    84  		session.WaitWithDefaultTimeout()
    85  		Expect(session).Should(ExitWithError(137, ""))
    86  
    87  		inspect := podmanTest.Podman(([]string{"inspect", "--format", "{{.State.OOMKilled}} {{.State.ExitCode}}", ctrName}))
    88  		inspect.WaitWithDefaultTimeout()
    89  		Expect(inspect).Should(ExitCleanly())
    90  		// Check oomkilled and exit code values
    91  		Expect(inspect.OutputToString()).Should(Equal("true 137"))
    92  	})
    93  
    94  	It("podman run memory test on successfully exited container", func() {
    95  		ctrName := "success-ctr"
    96  		session := podmanTest.Podman([]string{"run", "--name", ctrName, "--memory=40m", ALPINE, "echo", "hello"})
    97  		session.WaitWithDefaultTimeout()
    98  		Expect(session).Should(ExitCleanly())
    99  
   100  		inspect := podmanTest.Podman(([]string{"inspect", "--format", "{{.State.OOMKilled}} {{.State.ExitCode}}", ctrName}))
   101  		inspect.WaitWithDefaultTimeout()
   102  		Expect(inspect).Should(ExitCleanly())
   103  		// Check oomkilled and exit code values
   104  		Expect(inspect.OutputToString()).Should(Equal("false 0"))
   105  	})
   106  })