github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/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 After=zfs-mount.service 92 93 [Mount] 94 What=/var/lib/snapd/snaps/foo_13.snap 95 Where=%s/foo/13 96 Type=squashfs 97 Options=nodev,ro,x-gdu.hide,x-gvfs-hide 98 LazyUnmount=yes 99 100 [Install] 101 WantedBy=multi-user.target 102 `[1:], dirs.StripRootDir(dirs.SnapMountDir))) 103 } 104 105 func (s *mountunitSuite) TestRemoveMountUnit(c *C) { 106 info := &snap.Info{ 107 SideInfo: snap.SideInfo{ 108 RealName: "foo", 109 Revision: snap.R(13), 110 }, 111 Version: "1.1", 112 Architectures: []string{"all"}, 113 } 114 115 err := backend.AddMountUnit(info, false, progress.Null) 116 c.Assert(err, IsNil) 117 118 // ensure we have the files 119 un := fmt.Sprintf("%s.mount", systemd.EscapeUnitNamePath(filepath.Join(dirs.StripRootDir(dirs.SnapMountDir), "foo", "13"))) 120 p := filepath.Join(dirs.SnapServicesDir, un) 121 c.Assert(osutil.FileExists(p), Equals, true) 122 123 // now call remove and ensure they are gone 124 err = backend.RemoveMountUnit(info.MountDir(), progress.Null) 125 c.Assert(err, IsNil) 126 p = filepath.Join(dirs.SnapServicesDir, un) 127 c.Assert(osutil.FileExists(p), Equals, false) 128 }