github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/interfaces/mount/ns_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-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 mount_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/interfaces/mount"
    31  	"github.com/snapcore/snapd/release"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type nsSuite struct {
    36  	testutil.BaseTest
    37  }
    38  
    39  var _ = Suite(&nsSuite{})
    40  
    41  func (s *nsSuite) SetUpTest(c *C) {
    42  	s.BaseTest.SetUpTest(c)
    43  	dirs.SetRootDir(c.MkDir())
    44  	s.AddCleanup(func() { dirs.SetRootDir("") })
    45  	s.AddCleanup(release.MockOnClassic(true))
    46  	// Anything that just gives us no-reexec.
    47  	s.AddCleanup(release.MockReleaseInfo(&release.OS{ID: "fedora"}))
    48  }
    49  
    50  func (s *nsSuite) TestDiscardNamespaceMnt(c *C) {
    51  	for _, t := range []struct {
    52  		cmd    string
    53  		mnt    bool
    54  		errStr string
    55  		res    [][]string
    56  	}{
    57  		// The mnt file present so we use snap-discard-ns;
    58  		// The command doesn't fail and there's no error.
    59  		{cmd: "", mnt: true, errStr: "", res: [][]string{{"snap-discard-ns", "snap-name"}}},
    60  		// The mnt file is not present so we don't do anything.
    61  		{cmd: "", mnt: false, errStr: "", res: nil},
    62  		// The mnt file is present so we use snap-discard-ns;
    63  		// The command fails and we forward the error along with the output.
    64  		{
    65  			cmd:    "echo failure; exit 1;",
    66  			mnt:    true,
    67  			errStr: `cannot discard preserved namespace of snap "snap-name": failure`,
    68  			res:    [][]string{{"snap-discard-ns", "snap-name"}}},
    69  		// The mnt file is present so we use snap-discard-ns;
    70  		// The command fails silently and we forward this fact using a generic message.
    71  		{
    72  			cmd:    "exit 1;",
    73  			mnt:    true,
    74  			errStr: `cannot discard preserved namespace of snap "snap-name": exit status 1`,
    75  			res:    [][]string{{"snap-discard-ns", "snap-name"}}},
    76  	} {
    77  		cmd := testutil.MockCommand(c, "snap-discard-ns", t.cmd)
    78  		dirs.DistroLibExecDir = cmd.BinDir()
    79  		defer cmd.Restore()
    80  
    81  		if t.mnt {
    82  			c.Assert(os.MkdirAll(dirs.SnapRunNsDir, 0755), IsNil)
    83  			c.Assert(ioutil.WriteFile(filepath.Join(dirs.SnapRunNsDir, "snap-name.mnt"), nil, 0644), IsNil)
    84  		} else {
    85  			c.Assert(os.RemoveAll(dirs.SnapRunNsDir), IsNil)
    86  		}
    87  
    88  		err := mount.DiscardSnapNamespace("snap-name")
    89  		if t.errStr != "" {
    90  			c.Check(err, ErrorMatches, t.errStr)
    91  		} else {
    92  			c.Check(err, IsNil)
    93  			c.Check(cmd.Calls(), DeepEquals, t.res)
    94  		}
    95  	}
    96  }
    97  
    98  func (s *nsSuite) TestUpdateNamespaceMnt(c *C) {
    99  	for _, t := range []struct {
   100  		cmd    string
   101  		mnt    bool
   102  		errStr string
   103  		res    [][]string
   104  	}{
   105  		// The mnt file present so we use snap-update-ns;
   106  		// The command doesn't fail and there's no error.
   107  		{cmd: "", mnt: true, errStr: "", res: [][]string{{"snap-update-ns", "snap-name"}}},
   108  		// The mnt file is not present so we don't do anything.
   109  		{cmd: "", mnt: false, errStr: "", res: nil},
   110  		// The mnt file is present so we use snap-update-ns;
   111  		// The command fails and we forward the error along with the output.
   112  		{
   113  			cmd:    "echo failure; exit 1;",
   114  			mnt:    true,
   115  			errStr: `cannot update preserved namespace of snap "snap-name": failure`,
   116  			res:    [][]string{{"snap-update-ns", "snap-name"}}},
   117  		// The mnt file is present so we use snap-update-ns;
   118  		// The command fails silently and we forward this fact using a generic message.
   119  		{
   120  			cmd:    "exit 1;",
   121  			mnt:    true,
   122  			errStr: `cannot update preserved namespace of snap "snap-name": exit status 1`,
   123  			res:    [][]string{{"snap-update-ns", "snap-name"}}},
   124  	} {
   125  		cmd := testutil.MockCommand(c, "snap-update-ns", t.cmd)
   126  		dirs.DistroLibExecDir = cmd.BinDir()
   127  		defer cmd.Restore()
   128  
   129  		if t.mnt {
   130  			c.Assert(os.MkdirAll(dirs.SnapRunNsDir, 0755), IsNil)
   131  			c.Assert(ioutil.WriteFile(filepath.Join(dirs.SnapRunNsDir, "snap-name.mnt"), nil, 0644), IsNil)
   132  		} else {
   133  			c.Assert(os.RemoveAll(dirs.SnapRunNsDir), IsNil)
   134  		}
   135  
   136  		err := mount.UpdateSnapNamespace("snap-name")
   137  		if t.errStr != "" {
   138  			c.Check(err, ErrorMatches, t.errStr)
   139  		} else {
   140  			c.Check(err, IsNil)
   141  			c.Check(cmd.Calls(), DeepEquals, t.res)
   142  		}
   143  	}
   144  }