github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/cmd/snap-update-ns/common_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  	"bytes"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	update "github.com/snapcore/snapd/cmd/snap-update-ns"
    31  	"github.com/snapcore/snapd/cmd/snaplock"
    32  	"github.com/snapcore/snapd/dirs"
    33  	"github.com/snapcore/snapd/osutil"
    34  	"github.com/snapcore/snapd/sandbox/cgroup"
    35  	"github.com/snapcore/snapd/testutil"
    36  )
    37  
    38  type commonSuite struct {
    39  	dir   string
    40  	upCtx *update.CommonProfileUpdateContext
    41  }
    42  
    43  var _ = Suite(&commonSuite{})
    44  
    45  func (s *commonSuite) SetUpTest(c *C) {
    46  	s.dir = c.MkDir()
    47  	s.upCtx = update.NewCommonProfileUpdateContext("foo", false,
    48  		filepath.Join(s.dir, "current.fstab"),
    49  		filepath.Join(s.dir, "desired.fstab"))
    50  }
    51  
    52  func (s *commonSuite) TestInstanceName(c *C) {
    53  	c.Check(s.upCtx.InstanceName(), Equals, "foo")
    54  }
    55  
    56  func (s *commonSuite) TestLock(c *C) {
    57  	// Mock away real freezer code, allowing test code to return an error when freezing.
    58  	var freezingError error
    59  	restore := cgroup.MockFreezing(func(string) error { return freezingError }, func(string) error { return nil })
    60  	defer restore()
    61  	// Mock system directories, we use the lock directory.
    62  	dirs.SetRootDir(s.dir)
    63  	defer dirs.SetRootDir("")
    64  
    65  	// We will use 2nd lock for our testing.
    66  	testLock, err := snaplock.OpenLock(s.upCtx.InstanceName())
    67  	c.Assert(err, IsNil)
    68  	defer testLock.Close()
    69  
    70  	// When fromSnapConfine is false we acquire our own lock.
    71  	s.upCtx.SetFromSnapConfine(false)
    72  	c.Check(s.upCtx.FromSnapConfine(), Equals, false)
    73  	unlock, err := s.upCtx.Lock()
    74  	c.Assert(err, IsNil)
    75  	// The lock is acquired now. We should not be able to get another lock.
    76  	c.Check(testLock.TryLock(), Equals, osutil.ErrAlreadyLocked)
    77  	// We can release the original lock now and see our test lock working.
    78  	unlock()
    79  	c.Assert(testLock.TryLock(), IsNil)
    80  
    81  	// When fromSnapConfine is true we test existing lock but don't grab one.
    82  	s.upCtx.SetFromSnapConfine(true)
    83  	c.Check(s.upCtx.FromSnapConfine(), Equals, true)
    84  	err = testLock.Lock()
    85  	c.Assert(err, IsNil)
    86  	unlock, err = s.upCtx.Lock()
    87  	c.Assert(err, IsNil)
    88  	unlock()
    89  
    90  	// When the test lock is unlocked the common update helper reports an error
    91  	// since it was expecting the lock to be held. Oh, and the lock is not leaked.
    92  	testLock.Unlock()
    93  	unlock, err = s.upCtx.Lock()
    94  	c.Check(err, ErrorMatches, `mount namespace of snap "foo" is not locked but --from-snap-confine was used`)
    95  	c.Check(unlock, IsNil)
    96  	c.Assert(testLock.TryLock(), IsNil)
    97  
    98  	// When freezing fails the lock acquired internally is not leaked.
    99  	freezingError = errTesting
   100  	s.upCtx.SetFromSnapConfine(false)
   101  	c.Check(s.upCtx.FromSnapConfine(), Equals, false)
   102  	testLock.Unlock()
   103  	unlock, err = s.upCtx.Lock()
   104  	c.Check(err, Equals, errTesting)
   105  	c.Check(unlock, IsNil)
   106  	c.Check(testLock.TryLock(), IsNil)
   107  }
   108  
   109  func (s *commonSuite) TestLoadDesiredProfile(c *C) {
   110  	upCtx := s.upCtx
   111  	text := "tmpfs /tmp tmpfs defaults 0 0\n"
   112  
   113  	// Ask the common profile update helper to read the desired profile.
   114  	profile, err := upCtx.LoadCurrentProfile()
   115  	c.Assert(err, IsNil)
   116  
   117  	// A profile that is not present on disk just reads as a valid empty profile.
   118  	c.Check(profile.Entries, HasLen, 0)
   119  
   120  	// Write a desired user mount profile for snap "foo".
   121  	path := upCtx.DesiredProfilePath()
   122  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   123  	c.Assert(ioutil.WriteFile(path, []byte(text), 0644), IsNil)
   124  
   125  	// Ask the common profile update helper to read the desired profile.
   126  	profile, err = upCtx.LoadDesiredProfile()
   127  	c.Assert(err, IsNil)
   128  	builder := &bytes.Buffer{}
   129  	profile.WriteTo(builder)
   130  
   131  	// The profile is returned unchanged.
   132  	c.Check(builder.String(), Equals, text)
   133  }
   134  
   135  func (s *commonSuite) TestLoadCurrentProfile(c *C) {
   136  	upCtx := s.upCtx
   137  	text := "tmpfs /tmp tmpfs defaults 0 0\n"
   138  
   139  	// Ask the common profile update helper to read the current profile.
   140  	profile, err := upCtx.LoadCurrentProfile()
   141  	c.Assert(err, IsNil)
   142  
   143  	// A profile that is not present on disk just reads as a valid empty profile.
   144  	c.Check(profile.Entries, HasLen, 0)
   145  
   146  	// Write a current user mount profile for snap "foo".
   147  	path := upCtx.CurrentProfilePath()
   148  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   149  	c.Assert(ioutil.WriteFile(path, []byte(text), 0644), IsNil)
   150  
   151  	// Ask the common profile update helper to read the current profile.
   152  	profile, err = upCtx.LoadCurrentProfile()
   153  	c.Assert(err, IsNil)
   154  	builder := &bytes.Buffer{}
   155  	profile.WriteTo(builder)
   156  
   157  	// The profile is returned unchanged.
   158  	c.Check(builder.String(), Equals, text)
   159  }
   160  
   161  func (s *commonSuite) TestSaveCurrentProfile(c *C) {
   162  	upCtx := s.upCtx
   163  	text := "tmpfs /tmp tmpfs defaults 0 0\n"
   164  
   165  	// Prepare a mount profile to be saved.
   166  	profile, err := osutil.LoadMountProfileText(text)
   167  	c.Assert(err, IsNil)
   168  
   169  	// Prepare the directory for saving the profile.
   170  	path := upCtx.CurrentProfilePath()
   171  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   172  
   173  	// Ask the common profile update to write the current profile.
   174  	c.Assert(upCtx.SaveCurrentProfile(profile), IsNil)
   175  	c.Check(path, testutil.FileEquals, text)
   176  }