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