github.com/containers/podman/v5@v5.1.0-rc1/test/e2e/run_cpu_test.go (about) 1 package integration 2 3 import ( 4 "os" 5 6 . "github.com/containers/podman/v5/test/utils" 7 . "github.com/onsi/ginkgo/v2" 8 . "github.com/onsi/gomega" 9 . "github.com/onsi/gomega/gexec" 10 ) 11 12 var _ = Describe("Podman run cpu", func() { 13 14 BeforeEach(func() { 15 SkipIfRootlessCgroupsV1("Setting CPU not supported on cgroupv1 for rootless users") 16 17 if CGROUPSV2 { 18 if err := os.WriteFile("/sys/fs/cgroup/cgroup.subtree_control", []byte("+cpuset"), 0644); err != nil { 19 Skip("cpuset controller not available on the current kernel") 20 } 21 } 22 }) 23 24 It("podman run cpu-period", func() { 25 var result *PodmanSessionIntegration 26 if CGROUPSV2 { 27 result = podmanTest.Podman([]string{"run", "--rm", "--cpu-period=5000", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpu.max"}) 28 } else { 29 result = podmanTest.Podman([]string{"run", "--rm", "--cpu-period=5000", ALPINE, "cat", "/sys/fs/cgroup/cpu/cpu.cfs_period_us"}) 30 } 31 result.WaitWithDefaultTimeout() 32 Expect(result).Should(ExitCleanly()) 33 Expect(result.OutputToString()).To(ContainSubstring("5000")) 34 }) 35 36 It("podman run cpu-quota", func() { 37 var result *PodmanSessionIntegration 38 39 if CGROUPSV2 { 40 result = podmanTest.Podman([]string{"run", "--rm", "--cpu-quota=5000", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpu.max"}) 41 } else { 42 result = podmanTest.Podman([]string{"run", "--rm", "--cpu-quota=5000", ALPINE, "cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"}) 43 } 44 result.WaitWithDefaultTimeout() 45 Expect(result).Should(ExitCleanly()) 46 Expect(result.OutputToString()).To(ContainSubstring("5000")) 47 }) 48 49 It("podman run cpus", func() { 50 if CGROUPSV2 { 51 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-quota=5000", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpu.max"}) 52 result.WaitWithDefaultTimeout() 53 Expect(result).Should(ExitCleanly()) 54 Expect(result.OutputToString()).To(Equal("5000 100000")) 55 } else { 56 result := podmanTest.Podman([]string{"run", "--rm", "--cpus=0.5", ALPINE, "cat", "/sys/fs/cgroup/cpu/cpu.cfs_period_us"}) 57 result.WaitWithDefaultTimeout() 58 Expect(result).Should(ExitCleanly()) 59 Expect(result.OutputToString()).To(Equal("100000")) 60 61 result = podmanTest.Podman([]string{"run", "--rm", "--cpus=0.5", ALPINE, "cat", "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"}) 62 result.WaitWithDefaultTimeout() 63 Expect(result).Should(ExitCleanly()) 64 Expect(result.OutputToString()).To(Equal("50000")) 65 } 66 }) 67 68 It("podman run cpu-shares", func() { 69 if CGROUPSV2 { 70 // [2-262144] is mapped to [1-10000] 71 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-shares=262144", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpu.weight"}) 72 result.WaitWithDefaultTimeout() 73 Expect(result).Should(ExitCleanly()) 74 Expect(result.OutputToString()).To(Equal("10000")) 75 } else { 76 result := podmanTest.Podman([]string{"run", "--rm", "-c", "2", ALPINE, "cat", "/sys/fs/cgroup/cpu/cpu.shares"}) 77 result.WaitWithDefaultTimeout() 78 Expect(result).Should(ExitCleanly()) 79 Expect(result.OutputToString()).To(Equal("2")) 80 } 81 }) 82 83 It("podman run cpuset-cpus", func() { 84 var result *PodmanSessionIntegration 85 86 if CGROUPSV2 { 87 result = podmanTest.Podman([]string{"run", "--rm", "--cpuset-cpus=0", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpuset.cpus.effective"}) 88 } else { 89 result = podmanTest.Podman([]string{"run", "--rm", "--cpuset-cpus=0", ALPINE, "cat", "/sys/fs/cgroup/cpuset/cpuset.cpus"}) 90 } 91 result.WaitWithDefaultTimeout() 92 Expect(result).Should(ExitCleanly()) 93 Expect(result.OutputToString()).To(Equal("0")) 94 }) 95 96 It("podman run cpuset-mems", func() { 97 var result *PodmanSessionIntegration 98 99 if CGROUPSV2 { 100 result = podmanTest.Podman([]string{"run", "--rm", "--cpuset-mems=0", ALPINE, "sh", "-c", "cat /sys/fs/cgroup/$(sed -e 's|0::||' < /proc/self/cgroup)/cpuset.mems.effective"}) 101 } else { 102 result = podmanTest.Podman([]string{"run", "--rm", "--cpuset-mems=0", ALPINE, "cat", "/sys/fs/cgroup/cpuset/cpuset.mems"}) 103 } 104 result.WaitWithDefaultTimeout() 105 Expect(result).Should(ExitCleanly()) 106 Expect(result.OutputToString()).To(Equal("0")) 107 }) 108 109 It("podman run cpus and cpu-period", func() { 110 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-period=5000", "--cpus=0.5", ALPINE, "ls"}) 111 result.WaitWithDefaultTimeout() 112 Expect(result).To(ExitWithError(125, "--cpu-period and --cpus cannot be set together")) 113 }) 114 115 It("podman run cpus and cpu-quota", func() { 116 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-quota=5000", "--cpus=0.5", ALPINE, "ls"}) 117 result.WaitWithDefaultTimeout() 118 Expect(result).To(ExitWithError(125, "--cpu-quota and --cpus cannot be set together")) 119 }) 120 121 It("podman run invalid cpu-rt-period with cgroupsv2", func() { 122 SkipIfCgroupV1("testing options that only work in cgroup v2") 123 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-rt-period=5000", ALPINE, "ls"}) 124 result.WaitWithDefaultTimeout() 125 Expect(result).Should(Exit(0)) 126 Expect(result.ErrorToString()).To(ContainSubstring("Realtime period not supported on cgroups V2 systems")) 127 }) 128 129 It("podman run invalid cpu-rt-runtime with cgroupsv2", func() { 130 SkipIfCgroupV1("testing options that only work in cgroup v2") 131 result := podmanTest.Podman([]string{"run", "--rm", "--cpu-rt-runtime=5000", ALPINE, "ls"}) 132 result.WaitWithDefaultTimeout() 133 Expect(result).Should(Exit(0)) 134 Expect(result.ErrorToString()).To(ContainSubstring("Realtime runtime not supported on cgroups V2 systems")) 135 }) 136 })