gitee.com/mysnapcore/mysnapd@v0.1.0/sandbox/cgroup/memory_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2022 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  	"io/ioutil"
    23  	"path/filepath"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"gitee.com/mysnapcore/mysnapd/sandbox/cgroup"
    28  	"gitee.com/mysnapcore/mysnapd/testutil"
    29  )
    30  
    31  type memorySuite struct {
    32  	testutil.BaseTest
    33  
    34  	mockCgroupsFile string
    35  }
    36  
    37  var _ = Suite(&memorySuite{})
    38  
    39  var (
    40  	cgroupContentCommon = `#subsys_name	hierarchy	num_cgroups	enabled
    41  cpuset	6	3	1
    42  cpu	3	133	1
    43  devices	10	135	1`
    44  )
    45  
    46  func (s *memorySuite) SetUpTest(c *C) {
    47  	s.BaseTest.SetUpTest(c)
    48  
    49  	s.mockCgroupsFile = filepath.Join(c.MkDir(), "cgroups")
    50  	s.AddCleanup(cgroup.MockCgroupsFilePath(s.mockCgroupsFile))
    51  }
    52  
    53  func (s *memorySuite) TestCheckMemoryCgroupHappy(c *C) {
    54  	extra := "memory	2	223	1"
    55  	content := cgroupContentCommon + "\n" + extra
    56  
    57  	err := ioutil.WriteFile(s.mockCgroupsFile, []byte(content), 0644)
    58  	c.Assert(err, IsNil)
    59  	err = cgroup.CheckMemoryCgroup()
    60  	c.Assert(err, IsNil)
    61  }
    62  
    63  func (s *memorySuite) TestCheckMemoryCgroupMissing(c *C) {
    64  	// note there is no file created for s.mockCgroupsFile
    65  
    66  	err := cgroup.CheckMemoryCgroup()
    67  	c.Assert(err, ErrorMatches, "cannot open cgroups file: open .*/cgroups: no such file or directory")
    68  }
    69  
    70  func (s *memorySuite) TestCheckMemoryCgroupNoMemoryEntry(c *C) {
    71  	content := cgroupContentCommon
    72  
    73  	err := ioutil.WriteFile(s.mockCgroupsFile, []byte(content), 0644)
    74  	c.Assert(err, IsNil)
    75  	err = cgroup.CheckMemoryCgroup()
    76  	c.Assert(err, ErrorMatches, "cannot find memory cgroup in .*/cgroups")
    77  }
    78  
    79  func (s *memorySuite) TestCheckMemoryCgroupInvalidMemoryEntry(c *C) {
    80  	extra := "memory	invalid line"
    81  	content := cgroupContentCommon + "\n" + extra
    82  
    83  	err := ioutil.WriteFile(s.mockCgroupsFile, []byte(content), 0644)
    84  	c.Assert(err, IsNil)
    85  	err = cgroup.CheckMemoryCgroup()
    86  	c.Assert(err, ErrorMatches, `cannot parse cgroups file: invalid line "memory\\tinvalid line"`)
    87  }
    88  
    89  func (s *memorySuite) TestCheckMemoryCgroupDisabled(c *C) {
    90  	extra := "memory	2	223	0"
    91  	content := cgroupContentCommon + "\n" + extra
    92  
    93  	err := ioutil.WriteFile(s.mockCgroupsFile, []byte(content), 0644)
    94  	c.Assert(err, IsNil)
    95  	err = cgroup.CheckMemoryCgroup()
    96  	c.Assert(err, ErrorMatches, "memory cgroup is disabled on this system")
    97  }