github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/cgroups_manager/container_cgroups_manager_test.go (about) 1 package cgroups_manager_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 11 "github.com/cloudfoundry-incubator/garden-linux/old/cgroups_manager" 12 ) 13 14 var _ = Describe("Container cgroups", func() { 15 var cgroupsPath string 16 var cgroupsManager *cgroups_manager.ContainerCgroupsManager 17 18 BeforeEach(func() { 19 tmpdir, err := ioutil.TempDir(os.TempDir(), "some-cgroups") 20 Expect(err).ToNot(HaveOccurred()) 21 22 cgroupsPath = tmpdir 23 24 cgroupsManager = cgroups_manager.New(cgroupsPath, "some-container-id") 25 }) 26 27 Describe("setting", func() { 28 It("writes the value to the name under the subsytem", func() { 29 containerMemoryCgroupsPath := path.Join(cgroupsPath, "memory", "instance-some-container-id") 30 err := os.MkdirAll(containerMemoryCgroupsPath, 0755) 31 Expect(err).ToNot(HaveOccurred()) 32 33 err = cgroupsManager.Set("memory", "memory.limit_in_bytes", "42") 34 Expect(err).ToNot(HaveOccurred()) 35 36 value, err := ioutil.ReadFile(path.Join(containerMemoryCgroupsPath, "memory.limit_in_bytes")) 37 Expect(err).ToNot(HaveOccurred()) 38 Expect(string(value)).To(Equal("42")) 39 }) 40 41 Context("when the cgroups directory does not exist", func() { 42 BeforeEach(func() { 43 err := os.RemoveAll(cgroupsPath) 44 Expect(err).ToNot(HaveOccurred()) 45 }) 46 47 It("returns an error", func() { 48 err := cgroupsManager.Set("memory", "memory.limit_in_bytes", "42") 49 Expect(err).To(HaveOccurred()) 50 }) 51 }) 52 }) 53 54 Describe("getting", func() { 55 It("reads the current value from the name under the subsystem", func() { 56 containerMemoryCgroupsPath := path.Join(cgroupsPath, "memory", "instance-some-container-id") 57 58 err := os.MkdirAll(containerMemoryCgroupsPath, 0755) 59 Expect(err).ToNot(HaveOccurred()) 60 61 err = ioutil.WriteFile(path.Join(containerMemoryCgroupsPath, "memory.limit_in_bytes"), []byte("123\n"), 0644) 62 Expect(err).ToNot(HaveOccurred()) 63 64 val, err := cgroupsManager.Get("memory", "memory.limit_in_bytes") 65 Expect(err).ToNot(HaveOccurred()) 66 Expect(val).To(Equal("123")) 67 }) 68 }) 69 70 Describe("retrieving a subsystem path", func() { 71 It("returns <path>/<subsytem>/instance-<container-id>", func() { 72 Expect(cgroupsManager.SubsystemPath("memory")).To(Equal( 73 path.Join(cgroupsPath, "memory", "instance-some-container-id"), 74 )) 75 76 }) 77 }) 78 })