github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/overlord/snapstate/backend/mountunit_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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 backend_test
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/osutil"
    31  	"github.com/snapcore/snapd/osutil/squashfs"
    32  	"github.com/snapcore/snapd/overlord/snapstate/backend"
    33  	"github.com/snapcore/snapd/progress"
    34  	"github.com/snapcore/snapd/snap"
    35  	"github.com/snapcore/snapd/systemd"
    36  	"github.com/snapcore/snapd/testutil"
    37  )
    38  
    39  type mountunitSuite struct {
    40  	umount *testutil.MockCmd
    41  
    42  	systemctlRestorer func()
    43  
    44  	testutil.BaseTest
    45  }
    46  
    47  var _ = Suite(&mountunitSuite{})
    48  
    49  func (s *mountunitSuite) SetUpTest(c *C) {
    50  	dirs.SetRootDir(c.MkDir())
    51  
    52  	// needed for system key generation
    53  	s.AddCleanup(osutil.MockMountInfo(""))
    54  
    55  	err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc", "systemd", "system", "multi-user.target.wants"), 0755)
    56  	c.Assert(err, IsNil)
    57  
    58  	s.systemctlRestorer = systemd.MockSystemctl(func(cmd ...string) ([]byte, error) {
    59  		return []byte("ActiveState=inactive\n"), nil
    60  	})
    61  	s.umount = testutil.MockCommand(c, "umount", "")
    62  }
    63  
    64  func (s *mountunitSuite) TearDownTest(c *C) {
    65  	dirs.SetRootDir("")
    66  	s.umount.Restore()
    67  	s.systemctlRestorer()
    68  }
    69  
    70  func (s *mountunitSuite) TestAddMountUnit(c *C) {
    71  	restore := squashfs.MockNeedsFuse(false)
    72  	defer restore()
    73  
    74  	info := &snap.Info{
    75  		SideInfo: snap.SideInfo{
    76  			RealName: "foo",
    77  			Revision: snap.R(13),
    78  		},
    79  		Version:       "1.1",
    80  		Architectures: []string{"all"},
    81  	}
    82  	err := backend.AddMountUnit(info, false, progress.Null)
    83  	c.Assert(err, IsNil)
    84  
    85  	// ensure correct mount unit
    86  	un := fmt.Sprintf("%s.mount", systemd.EscapeUnitNamePath(filepath.Join(dirs.StripRootDir(dirs.SnapMountDir), "foo", "13")))
    87  	c.Assert(filepath.Join(dirs.SnapServicesDir, un), testutil.FileEquals, fmt.Sprintf(`
    88  [Unit]
    89  Description=Mount unit for foo, revision 13
    90  Before=snapd.service
    91  
    92  [Mount]
    93  What=/var/lib/snapd/snaps/foo_13.snap
    94  Where=%s/foo/13
    95  Type=squashfs
    96  Options=nodev,ro,x-gdu.hide
    97  LazyUnmount=yes
    98  
    99  [Install]
   100  WantedBy=multi-user.target
   101  `[1:], dirs.StripRootDir(dirs.SnapMountDir)))
   102  }
   103  
   104  func (s *mountunitSuite) TestRemoveMountUnit(c *C) {
   105  	info := &snap.Info{
   106  		SideInfo: snap.SideInfo{
   107  			RealName: "foo",
   108  			Revision: snap.R(13),
   109  		},
   110  		Version:       "1.1",
   111  		Architectures: []string{"all"},
   112  	}
   113  
   114  	err := backend.AddMountUnit(info, false, progress.Null)
   115  	c.Assert(err, IsNil)
   116  
   117  	// ensure we have the files
   118  	un := fmt.Sprintf("%s.mount", systemd.EscapeUnitNamePath(filepath.Join(dirs.StripRootDir(dirs.SnapMountDir), "foo", "13")))
   119  	p := filepath.Join(dirs.SnapServicesDir, un)
   120  	c.Assert(osutil.FileExists(p), Equals, true)
   121  
   122  	// now call remove and ensure they are gone
   123  	err = backend.RemoveMountUnit(info.MountDir(), progress.Null)
   124  	c.Assert(err, IsNil)
   125  	p = filepath.Join(dirs.SnapServicesDir, un)
   126  	c.Assert(osutil.FileExists(p), Equals, false)
   127  }