gitee.com/mysnapcore/mysnapd@v0.1.0/cmd/snap-update-ns/user_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/testutil"
    34  )
    35  
    36  type userSuite struct{}
    37  
    38  var _ = Suite(&userSuite{})
    39  
    40  func (s *userSuite) TestLock(c *C) {
    41  	dirs.SetRootDir(c.MkDir())
    42  	defer dirs.SetRootDir("/")
    43  	c.Assert(os.MkdirAll(dirs.FeaturesDir, 0755), IsNil)
    44  
    45  	upCtx := update.NewUserProfileUpdateContext("foo", false, 1234)
    46  
    47  	// Locking is a no-op.
    48  	unlock, err := upCtx.Lock()
    49  	c.Assert(err, IsNil)
    50  	c.Check(unlock, NotNil)
    51  	unlock()
    52  }
    53  
    54  func (s *userSuite) TestAssumptions(c *C) {
    55  	upCtx := update.NewUserProfileUpdateContext("foo", false, 1234)
    56  	as := upCtx.Assumptions()
    57  	c.Check(as.UnrestrictedPaths(), IsNil)
    58  }
    59  
    60  func (s *userSuite) TestLoadDesiredProfile(c *C) {
    61  	// Mock directories but to simplify testing use the real value for XDG.
    62  	dirs.SetRootDir(c.MkDir())
    63  	defer dirs.SetRootDir("/")
    64  	dirs.XdgRuntimeDirBase = "/run/user"
    65  
    66  	upCtx := update.NewUserProfileUpdateContext("foo", false, 1234)
    67  
    68  	input := "$XDG_RUNTIME_DIR/doc/by-app/snap.foo $XDG_RUNTIME_DIR/doc none bind,rw 0 0\n"
    69  	output := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"
    70  
    71  	// Write a desired user mount profile for snap "foo".
    72  	path := update.DesiredUserProfilePath("foo")
    73  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
    74  	c.Assert(ioutil.WriteFile(path, []byte(input), 0644), IsNil)
    75  
    76  	// Ask the user profile update helper to read the desired profile.
    77  	profile, err := upCtx.LoadDesiredProfile()
    78  	c.Assert(err, IsNil)
    79  	builder := &bytes.Buffer{}
    80  	profile.WriteTo(builder)
    81  
    82  	// Note that the profile read back contains expanded $XDG_RUNTIME_DIR.
    83  	c.Check(builder.String(), Equals, output)
    84  }
    85  
    86  func (s *userSuite) TestLoadCurrentProfile(c *C) {
    87  	// Mock directories.
    88  	dirs.SetRootDir(c.MkDir())
    89  	defer dirs.SetRootDir("/")
    90  
    91  	upCtx := update.NewUserProfileUpdateContext("foo", false, 1234)
    92  
    93  	// Write a current user mount profile for snap "foo".
    94  	text := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"
    95  	path := update.CurrentUserProfilePath(upCtx.InstanceName(), upCtx.UID())
    96  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
    97  	c.Assert(ioutil.WriteFile(path, []byte(text), 0644), IsNil)
    98  
    99  	// Ask the user profile update helper to read the current profile.
   100  	profile, err := upCtx.LoadCurrentProfile()
   101  	c.Assert(err, IsNil)
   102  	builder := &bytes.Buffer{}
   103  	profile.WriteTo(builder)
   104  
   105  	// Note that the profile is empty.
   106  	// Currently user profiles are not persisted so the presence of a profile on-disk is ignored.
   107  	c.Check(builder.String(), Equals, "")
   108  }
   109  
   110  func (s *userSuite) TestSaveCurrentProfile(c *C) {
   111  	// Mock directories and create directory runtime mount profiles.
   112  	dirs.SetRootDir(c.MkDir())
   113  	defer dirs.SetRootDir("/")
   114  	c.Assert(os.MkdirAll(dirs.SnapRunNsDir, 0755), IsNil)
   115  
   116  	upCtx := update.NewUserProfileUpdateContext("foo", false, 1234)
   117  
   118  	// Prepare a mount profile to be saved.
   119  	text := "/run/user/1234/doc/by-app/snap.foo /run/user/1234/doc none bind,rw 0 0\n"
   120  	profile, err := osutil.LoadMountProfileText(text)
   121  	c.Assert(err, IsNil)
   122  
   123  	// Write a fake current user mount profile for snap "foo".
   124  	path := update.CurrentUserProfilePath("foo", 1234)
   125  	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
   126  	c.Assert(ioutil.WriteFile(path, []byte("banana"), 0644), IsNil)
   127  
   128  	// Ask the user profile update helper to write the current profile.
   129  	err = upCtx.SaveCurrentProfile(profile)
   130  	c.Assert(err, IsNil)
   131  
   132  	// Note that the profile was not modified.
   133  	// Currently user profiles are not persisted.
   134  	c.Check(path, testutil.FileEquals, "banana")
   135  }
   136  
   137  func (s *userSuite) TestDesiredUserProfilePath(c *C) {
   138  	c.Check(update.DesiredUserProfilePath("foo"), Equals, "/var/lib/snapd/mount/snap.foo.user-fstab")
   139  }
   140  
   141  func (s *userSuite) TestCurrentUserProfilePath(c *C) {
   142  	c.Check(update.CurrentUserProfilePath("foo", 12345), Equals, "/run/snapd/ns/snap.foo.12345.user-fstab")
   143  }