github.com/rigado/snapd@v2.42.5-go-mod+incompatible/sandbox/cgroup/cgroup_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 package cgroup_test 20 21 import ( 22 "errors" 23 "path/filepath" 24 "testing" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/sandbox/cgroup" 30 ) 31 32 type cgroupSuite struct{} 33 34 var _ = Suite(&cgroupSuite{}) 35 36 func TestCgroup(t *testing.T) { TestingT(t) } 37 38 func (s *cgroupSuite) TearDownTest(c *C) { 39 dirs.SetRootDir("/") 40 } 41 42 func (s *cgroupSuite) TestIsUnified(c *C) { 43 restore := cgroup.MockVersion(cgroup.V2, nil) 44 defer restore() 45 c.Assert(cgroup.IsUnified(), Equals, true) 46 47 restore = cgroup.MockVersion(cgroup.V1, nil) 48 defer restore() 49 c.Assert(cgroup.IsUnified(), Equals, false) 50 51 restore = cgroup.MockVersion(cgroup.Unknown, nil) 52 defer restore() 53 c.Assert(cgroup.IsUnified(), Equals, false) 54 } 55 56 func (s *cgroupSuite) TestProbeVersion2(c *C) { 57 restore := cgroup.MockFsTypeForPath(func(p string) (int64, error) { 58 c.Assert(p, Equals, filepath.Join(dirs.GlobalRootDir, "/sys/fs/cgroup")) 59 return int64(cgroup.Cgroup2SuperMagic), nil 60 }) 61 defer restore() 62 v, err := cgroup.ProbeCgroupVersion() 63 c.Assert(err, IsNil) 64 c.Assert(v, Equals, cgroup.V2) 65 } 66 67 func (s *cgroupSuite) TestProbeVersion1(c *C) { 68 const TMPFS_MAGIC = 0x1021994 69 restore := cgroup.MockFsTypeForPath(func(p string) (int64, error) { 70 c.Assert(p, Equals, filepath.Join(dirs.GlobalRootDir, "/sys/fs/cgroup")) 71 return TMPFS_MAGIC, nil 72 }) 73 defer restore() 74 v, err := cgroup.ProbeCgroupVersion() 75 c.Assert(err, IsNil) 76 c.Assert(v, Equals, cgroup.V1) 77 } 78 79 func (s *cgroupSuite) TestProbeVersionUnhappy(c *C) { 80 restore := cgroup.MockFsTypeForPath(func(p string) (int64, error) { 81 c.Assert(p, Equals, filepath.Join(dirs.GlobalRootDir, "/sys/fs/cgroup")) 82 return 0, errors.New("statfs fail") 83 }) 84 defer restore() 85 v, err := cgroup.ProbeCgroupVersion() 86 c.Assert(err, ErrorMatches, "cannot determine filesystem type: statfs fail") 87 c.Assert(v, Equals, cgroup.Unknown) 88 } 89 90 func (s *cgroupSuite) TestVersion(c *C) { 91 restore := cgroup.MockVersion(cgroup.V2, nil) 92 defer restore() 93 v, err := cgroup.Version() 94 c.Assert(v, Equals, cgroup.V2) 95 c.Assert(err, IsNil) 96 97 restore = cgroup.MockVersion(cgroup.V1, nil) 98 defer restore() 99 v, err = cgroup.Version() 100 c.Assert(v, Equals, cgroup.V1) 101 c.Assert(err, IsNil) 102 103 restore = cgroup.MockVersion(cgroup.Unknown, nil) 104 defer restore() 105 v, err = cgroup.Version() 106 c.Assert(v, Equals, cgroup.Unknown) 107 c.Assert(err, IsNil) 108 109 restore = cgroup.MockVersion(cgroup.Unknown, errors.New("foo")) 110 defer restore() 111 v, err = cgroup.Version() 112 c.Assert(v, Equals, cgroup.Unknown) 113 c.Assert(err, ErrorMatches, "foo") 114 } 115 116 func (s *cgroupSuite) TestProcPidPath(c *C) { 117 c.Assert(cgroup.ProcPidPath(1), Equals, filepath.Join(dirs.GlobalRootDir, "/proc/1/cgroup")) 118 c.Assert(cgroup.ProcPidPath(1234), Equals, filepath.Join(dirs.GlobalRootDir, "/proc/1234/cgroup")) 119 } 120 121 func (s *cgroupSuite) TestControllerPathV1(c *C) { 122 c.Assert(cgroup.ControllerPathV1("freezer"), Equals, filepath.Join(dirs.GlobalRootDir, "/sys/fs/cgroup/freezer")) 123 c.Assert(cgroup.ControllerPathV1("memory"), Equals, filepath.Join(dirs.GlobalRootDir, "/sys/fs/cgroup/memory")) 124 }