github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/sandbox/cgroup/freezer_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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  
    20  package cgroup_test
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/sandbox/cgroup"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type freezerSuite struct{}
    35  
    36  var _ = Suite(&freezerSuite{})
    37  
    38  func (s *freezerSuite) TestFreezeSnapProcesses(c *C) {
    39  	dirs.SetRootDir(c.MkDir())
    40  	defer dirs.SetRootDir("")
    41  
    42  	n := "foo"                                                             // snap name
    43  	p := filepath.Join(cgroup.FreezerCgroupDir, fmt.Sprintf("snap.%s", n)) // snap freezer cgroup
    44  	f := filepath.Join(p, "freezer.state")                                 // freezer.state file of the cgroup
    45  
    46  	// When the freezer cgroup filesystem doesn't exist we do nothing at all.
    47  	c.Assert(cgroup.FreezeSnapProcesses(n), IsNil)
    48  	_, err := os.Stat(f)
    49  	c.Assert(os.IsNotExist(err), Equals, true)
    50  
    51  	// When the freezer cgroup filesystem exists but the particular cgroup
    52  	// doesn't exist we don nothing at all.
    53  	c.Assert(os.MkdirAll(cgroup.FreezerCgroupDir, 0755), IsNil)
    54  	c.Assert(cgroup.FreezeSnapProcesses(n), IsNil)
    55  	_, err = os.Stat(f)
    56  	c.Assert(os.IsNotExist(err), Equals, true)
    57  
    58  	// When the cgroup exists we write FROZEN the freezer.state file.
    59  	c.Assert(os.MkdirAll(p, 0755), IsNil)
    60  	c.Assert(cgroup.FreezeSnapProcesses(n), IsNil)
    61  	_, err = os.Stat(f)
    62  	c.Assert(err, IsNil)
    63  	c.Assert(f, testutil.FileEquals, `FROZEN`)
    64  }
    65  
    66  func (s *freezerSuite) TestThawSnapProcesses(c *C) {
    67  	dirs.SetRootDir(c.MkDir())
    68  	defer dirs.SetRootDir("")
    69  
    70  	n := "foo"                                                             // snap name
    71  	p := filepath.Join(cgroup.FreezerCgroupDir, fmt.Sprintf("snap.%s", n)) // snap freezer cgroup
    72  	f := filepath.Join(p, "freezer.state")                                 // freezer.state file of the cgroup
    73  
    74  	// When the freezer cgroup filesystem doesn't exist we do nothing at all.
    75  	c.Assert(cgroup.ThawSnapProcesses(n), IsNil)
    76  	_, err := os.Stat(f)
    77  	c.Assert(os.IsNotExist(err), Equals, true)
    78  
    79  	// When the freezer cgroup filesystem exists but the particular cgroup
    80  	// doesn't exist we don nothing at all.
    81  	c.Assert(os.MkdirAll(cgroup.FreezerCgroupDir, 0755), IsNil)
    82  	c.Assert(cgroup.ThawSnapProcesses(n), IsNil)
    83  	_, err = os.Stat(f)
    84  	c.Assert(os.IsNotExist(err), Equals, true)
    85  
    86  	// When the cgroup exists we write THAWED the freezer.state file.
    87  	c.Assert(os.MkdirAll(p, 0755), IsNil)
    88  	c.Assert(cgroup.ThawSnapProcesses(n), IsNil)
    89  	_, err = os.Stat(f)
    90  	c.Assert(err, IsNil)
    91  	c.Assert(f, testutil.FileEquals, `THAWED`)
    92  }