github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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 45 var _ = Suite(&mountunitSuite{}) 46 47 func (s *mountunitSuite) SetUpTest(c *C) { 48 dirs.SetRootDir(c.MkDir()) 49 50 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc", "systemd", "system", "multi-user.target.wants"), 0755) 51 c.Assert(err, IsNil) 52 53 s.systemctlRestorer = systemd.MockSystemctl(func(cmd ...string) ([]byte, error) { 54 return []byte("ActiveState=inactive\n"), nil 55 }) 56 s.umount = testutil.MockCommand(c, "umount", "") 57 } 58 59 func (s *mountunitSuite) TearDownTest(c *C) { 60 dirs.SetRootDir("") 61 s.umount.Restore() 62 s.systemctlRestorer() 63 } 64 65 func (s *mountunitSuite) TestAddMountUnit(c *C) { 66 restore := squashfs.MockNeedsFuse(false) 67 defer restore() 68 69 info := &snap.Info{ 70 SideInfo: snap.SideInfo{ 71 RealName: "foo", 72 Revision: snap.R(13), 73 }, 74 Version: "1.1", 75 Architectures: []string{"all"}, 76 } 77 err := backend.AddMountUnit(info, progress.Null) 78 c.Assert(err, IsNil) 79 80 // ensure correct mount unit 81 un := fmt.Sprintf("%s.mount", systemd.EscapeUnitNamePath(filepath.Join(dirs.StripRootDir(dirs.SnapMountDir), "foo", "13"))) 82 c.Assert(filepath.Join(dirs.SnapServicesDir, un), testutil.FileEquals, fmt.Sprintf(` 83 [Unit] 84 Description=Mount unit for foo, revision 13 85 Before=snapd.service 86 87 [Mount] 88 What=/var/lib/snapd/snaps/foo_13.snap 89 Where=%s/foo/13 90 Type=squashfs 91 Options=nodev,ro,x-gdu.hide 92 LazyUnmount=yes 93 94 [Install] 95 WantedBy=multi-user.target 96 `[1:], dirs.StripRootDir(dirs.SnapMountDir))) 97 } 98 99 func (s *mountunitSuite) TestRemoveMountUnit(c *C) { 100 info := &snap.Info{ 101 SideInfo: snap.SideInfo{ 102 RealName: "foo", 103 Revision: snap.R(13), 104 }, 105 Version: "1.1", 106 Architectures: []string{"all"}, 107 } 108 109 err := backend.AddMountUnit(info, progress.Null) 110 c.Assert(err, IsNil) 111 112 // ensure we have the files 113 un := fmt.Sprintf("%s.mount", systemd.EscapeUnitNamePath(filepath.Join(dirs.StripRootDir(dirs.SnapMountDir), "foo", "13"))) 114 p := filepath.Join(dirs.SnapServicesDir, un) 115 c.Assert(osutil.FileExists(p), Equals, true) 116 117 // now call remove and ensure they are gone 118 err = backend.RemoveMountUnit(info.MountDir(), progress.Null) 119 c.Assert(err, IsNil) 120 p = filepath.Join(dirs.SnapServicesDir, un) 121 c.Assert(osutil.FileExists(p), Equals, false) 122 }