github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/integration/lifecycle/rlimit_test.go (about) 1 package lifecycle_test 2 3 import ( 4 "io" 5 6 "github.com/cloudfoundry-incubator/garden" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 "github.com/onsi/gomega/gbytes" 10 ) 11 12 var _ = Describe("Resource limits", func() { 13 var ( 14 container garden.Container 15 privilegedContainer bool 16 ) 17 18 JustBeforeEach(func() { 19 var err error 20 21 client = startGarden() 22 23 container, err = client.Create(garden.ContainerSpec{ 24 Privileged: privilegedContainer, 25 }) 26 Expect(err).ToNot(HaveOccurred()) 27 }) 28 29 AfterEach(func() { 30 err := client.Destroy(container.Handle()) 31 Expect(err).ToNot(HaveOccurred()) 32 }) 33 34 Describe("Specific resource limits", func() { 35 Context("CPU rlimit", func() { 36 Context("with a privileged container", func() { 37 BeforeEach(func() { 38 privilegedContainer = true 39 }) 40 41 It("rlimits can be set", func() { 42 var cpu uint64 = 9000 43 stdout := gbytes.NewBuffer() 44 45 process, err := container.Run(garden.ProcessSpec{ 46 Path: "sh", 47 User: "root", 48 Args: []string{"-c", "ulimit -t"}, 49 Limits: garden.ResourceLimits{ 50 Cpu: &cpu, 51 }, 52 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 53 Expect(err).ToNot(HaveOccurred()) 54 55 Eventually(stdout).Should(gbytes.Say("9000")) 56 Expect(process.Wait()).To(Equal(0)) 57 }) 58 }) 59 60 Context("with a non-privileged container", func() { 61 BeforeEach(func() { 62 privilegedContainer = false 63 }) 64 65 It("rlimits can be set", func() { 66 var cpu uint64 = 9000 67 stdout := gbytes.NewBuffer() 68 69 process, err := container.Run(garden.ProcessSpec{ 70 Path: "sh", 71 User: "root", 72 Args: []string{"-c", "ulimit -t"}, 73 Limits: garden.ResourceLimits{ 74 Cpu: &cpu, 75 }, 76 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 77 Expect(err).ToNot(HaveOccurred()) 78 79 Eventually(stdout).Should(gbytes.Say("9000")) 80 Expect(process.Wait()).To(Equal(0)) 81 }) 82 }) 83 }) 84 85 Context("FSIZE rlimit", func() { 86 Context("with a privileged container", func() { 87 BeforeEach(func() { 88 privilegedContainer = true 89 }) 90 91 It("rlimits can be set", func() { 92 var fsize uint64 = 4194304 93 stdout := gbytes.NewBuffer() 94 95 process, err := container.Run(garden.ProcessSpec{ 96 Path: "sh", 97 User: "root", 98 Args: []string{"-c", "ulimit -f"}, 99 Limits: garden.ResourceLimits{ 100 Fsize: &fsize, 101 }, 102 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 103 Expect(err).ToNot(HaveOccurred()) 104 105 Eventually(stdout).Should(gbytes.Say("8192")) 106 Expect(process.Wait()).To(Equal(0)) 107 }) 108 }) 109 110 Context("with a non-privileged container", func() { 111 BeforeEach(func() { 112 privilegedContainer = false 113 }) 114 115 It("rlimits can be set", func() { 116 var fsize uint64 = 4194304 117 stdout := gbytes.NewBuffer() 118 119 process, err := container.Run(garden.ProcessSpec{ 120 Path: "sh", 121 User: "root", 122 Args: []string{"-c", "ulimit -f"}, 123 Limits: garden.ResourceLimits{ 124 Fsize: &fsize, 125 }, 126 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 127 Expect(err).ToNot(HaveOccurred()) 128 129 Eventually(stdout).Should(gbytes.Say("8192")) 130 Expect(process.Wait()).To(Equal(0)) 131 }) 132 }) 133 }) 134 135 Context("NOFILE rlimit", func() { 136 Context("with a privileged container", func() { 137 BeforeEach(func() { 138 privilegedContainer = true 139 }) 140 141 It("rlimits can be set", func() { 142 var nofile uint64 = 524288 143 stdout := gbytes.NewBuffer() 144 145 process, err := container.Run(garden.ProcessSpec{ 146 Path: "sh", 147 User: "root", 148 Args: []string{"-c", "ulimit -n"}, 149 Limits: garden.ResourceLimits{ 150 Nofile: &nofile, 151 }, 152 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 153 Expect(err).ToNot(HaveOccurred()) 154 155 Eventually(stdout).Should(gbytes.Say("524288")) 156 Expect(process.Wait()).To(Equal(0)) 157 }) 158 }) 159 160 Context("with a non-privileged container", func() { 161 BeforeEach(func() { 162 privilegedContainer = false 163 }) 164 165 It("rlimits can be set", func() { 166 var nofile uint64 = 524288 167 stdout := gbytes.NewBuffer() 168 process, err := container.Run(garden.ProcessSpec{ 169 Path: "sh", 170 User: "root", 171 Args: []string{"-c", "ulimit -n"}, 172 Limits: garden.ResourceLimits{ 173 Nofile: &nofile, 174 }, 175 }, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter}) 176 Expect(err).ToNot(HaveOccurred()) 177 178 Eventually(stdout).Should(gbytes.Say("524288")) 179 Expect(process.Wait()).To(Equal(0)) 180 }) 181 }) 182 }) 183 }) 184 })