gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-update-ns/system_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  
    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 "gitee.com/mysnapcore/mysnapd/cmd/snap-update-ns"
    31  	"gitee.com/mysnapcore/mysnapd/dirs"
    32  	"gitee.com/mysnapcore/mysnapd/osutil"
    33  	"gitee.com/mysnapcore/mysnapd/sandbox/cgroup"
    34  	"gitee.com/mysnapcore/mysnapd/testutil"
    35  )
    36  
    37  type systemSuite struct{}
    38  
    39  var _ = Suite(&systemSuite{})
    40  
    41  func (s *systemSuite) TestLockCgroup(c *C) {
    42  	dirs.SetRootDir(c.MkDir())
    43  	defer dirs.SetRootDir("/")
    44  
    45  	restore := cgroup.MockVersion(cgroup.V1, nil)
    46  	defer restore()
    47  
    48  	var frozen []string
    49  	var thawed []string
    50  	happyFreeze := func(snapName string) error {
    51  		frozen = append(frozen, snapName)
    52  		return nil
    53  	}
    54  	happyThaw := func(snapName string) error {
    55  		thawed = append(thawed, snapName)
    56  		return nil
    57  	}
    58  	cgroup.MockFreezing(happyFreeze, happyThaw)
    59  
    60  	upCtx := update.NewSystemProfileUpdateContext("foo", false)
    61  	unlock, err := upCtx.Lock()
    62  	c.Assert(err, IsNil)
    63  	c.Check(unlock, NotNil)
    64  
    65  	c.Check(frozen, DeepEquals, []string{"foo"})
    66  	c.Check(thawed, HasLen, 0)
    67  
    68  	unlock()
    69  	c.Check(frozen, DeepEquals, []string{"foo"})
    70  	c.Check(thawed, DeepEquals, []string{"foo"})
    71  }
    72  
    73  func (s *systemSuite) TestAssumptions(c *C) {
    74  	// Non-instances can access /tmp, /var/snap and /snap/$SNAP_NAME
    75  	upCtx := update.NewSystemProfileUpdateContext("foo", false)
    76  	as := upCtx.Assumptions()
    77  	c.Check(as.UnrestrictedPaths(), DeepEquals, []string{"/tmp", "/var/snap", "/snap/foo", "/dev/shm", "/run/systemd", "/var/lib/snapd/hostfs/tmp"})
    78  	c.Check(as.ModeForPath("/stuff"), Equals, os.FileMode(0755))
    79  	c.Check(as.ModeForPath("/tmp"), Equals, os.FileMode(0755))
    80  	c.Check(as.ModeForPath("/var/lib/snapd/hostfs/tmp"), Equals, os.FileMode(0755))
    81  	c.Check(as.ModeForPath("/var/lib/snapd/hostfs/tmp/snap.x11-server"), Equals, os.FileMode(0700))
    82  	c.Check(as.ModeForPath("/var/lib/snapd/hostfs/tmp/snap.x11-server/tmp"), Equals, os.FileMode(0777)|os.ModeSticky)
    83  	c.Check(as.ModeForPath("/var/lib/snapd/hostfs/tmp/snap.x11-server/foo"), Equals, os.FileMode(0755))
    84  	c.Check(as.ModeForPath("/var/lib/snapd/hostfs/tmp/snap.x11-server/tmp/.X11-unix"), Equals, os.FileMode(0777)|os.ModeSticky)
    85  	c.Check(as.ModeForPath("/dev/shm/snap.some-snap"), Equals, os.FileMode(0777)|os.ModeSticky)
    86  
    87  	// Instances can, in addition, access /snap/$SNAP_INSTANCE_NAME
    88  	upCtx = update.NewSystemProfileUpdateContext("foo_instance", false)
    89  	as = upCtx.Assumptions()
    90  	c.Check(as.UnrestrictedPaths(), DeepEquals, []string{"/tmp", "/var/snap", "/snap/foo_instance", "/dev/shm", "/run/systemd", "/snap/foo", "/var/lib/snapd/hostfs/tmp"})
    91  }
    92  
    93  func (s *systemSuite) TestLoadDesiredProfile(c *C) {
    94  	// Mock directories.
    95  	dirs.SetRootDir(c.MkDir())
    96  	defer dirs.SetRootDir("/")
    97  
    98  	upCtx := update.NewSystemProfileUpdateContext("foo", false)
    99  	text := "/snap/foo/42/dir /snap/bar/13/dir none bind,rw 0 0\n"
   100  
   101  	// Write a desired system mount profile for snap "foo".
   102  	path := update.DesiredSystemProfilePath(upCtx.InstanceName())
   103  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   104  	c.Assert(ioutil.WriteFile(path, []byte(text), 0644), IsNil)
   105  
   106  	// Ask the system profile update helper to read the desired profile.
   107  	profile, err := upCtx.LoadDesiredProfile()
   108  	c.Assert(err, IsNil)
   109  	builder := &bytes.Buffer{}
   110  	profile.WriteTo(builder)
   111  
   112  	c.Check(builder.String(), Equals, text)
   113  }
   114  
   115  func (s *systemSuite) TestLoadCurrentProfile(c *C) {
   116  	// Mock directories.
   117  	dirs.SetRootDir(c.MkDir())
   118  	defer dirs.SetRootDir("/")
   119  
   120  	upCtx := update.NewSystemProfileUpdateContext("foo", false)
   121  	text := "/snap/foo/42/dir /snap/bar/13/dir none bind,rw 0 0\n"
   122  
   123  	// Write a current system mount profile for snap "foo".
   124  	path := update.CurrentSystemProfilePath(upCtx.InstanceName())
   125  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   126  	c.Assert(ioutil.WriteFile(path, []byte(text), 0644), IsNil)
   127  
   128  	// Ask the system profile update helper to read the current profile.
   129  	profile, err := upCtx.LoadCurrentProfile()
   130  	c.Assert(err, IsNil)
   131  	builder := &bytes.Buffer{}
   132  	profile.WriteTo(builder)
   133  
   134  	// The profile is returned unchanged.
   135  	c.Check(builder.String(), Equals, text)
   136  }
   137  
   138  func (s *systemSuite) TestSaveCurrentProfile(c *C) {
   139  	// Mock directories and create directory for runtime mount profiles.
   140  	dirs.SetRootDir(c.MkDir())
   141  	defer dirs.SetRootDir("/")
   142  	c.Assert(os.MkdirAll(dirs.SnapRunNsDir, 0755), IsNil)
   143  
   144  	upCtx := update.NewSystemProfileUpdateContext("foo", false)
   145  	text := "/snap/foo/42/dir /snap/bar/13/dir none bind,rw 0 0\n"
   146  
   147  	// Prepare a mount profile to be saved.
   148  	profile, err := osutil.LoadMountProfileText(text)
   149  	c.Assert(err, IsNil)
   150  
   151  	// Ask the system profile update to write the current profile.
   152  	c.Assert(upCtx.SaveCurrentProfile(profile), IsNil)
   153  	c.Check(update.CurrentSystemProfilePath(upCtx.InstanceName()), testutil.FileEquals, text)
   154  }
   155  
   156  func (s *systemSuite) TestDesiredSystemProfilePath(c *C) {
   157  	c.Check(update.DesiredSystemProfilePath("foo"), Equals, "/var/lib/snapd/mount/snap.foo.fstab")
   158  }
   159  
   160  func (s *systemSuite) TestCurrentSystemProfilePath(c *C) {
   161  	c.Check(update.CurrentSystemProfilePath("foo"), Equals, "/run/snapd/ns/snap.foo.fstab")
   162  }