gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/wrappers/binaries_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 wrappers_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "testing" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/dirs" 31 "github.com/snapcore/snapd/osutil" 32 "github.com/snapcore/snapd/snap" 33 "github.com/snapcore/snapd/snap/snaptest" 34 "github.com/snapcore/snapd/wrappers" 35 ) 36 37 func TestWrappers(t *testing.T) { TestingT(t) } 38 39 type binariesTestSuite struct { 40 tempdir string 41 base string 42 } 43 44 // silly wrappers to get better failure messages 45 type noBaseBinariesSuite struct{ binariesTestSuite } 46 type withBaseBinariesSuite struct{ binariesTestSuite } 47 type withSnapdBinariesSuite struct{ binariesTestSuite } 48 49 var _ = Suite(&noBaseBinariesSuite{}) 50 var _ = Suite(&withBaseBinariesSuite{binariesTestSuite{base: "core99"}}) 51 var _ = Suite(&withSnapdBinariesSuite{binariesTestSuite{base: "core-with-snapd"}}) 52 53 func (s *binariesTestSuite) SetUpTest(c *C) { 54 s.tempdir = c.MkDir() 55 dirs.SetRootDir(s.tempdir) 56 } 57 58 func (s *binariesTestSuite) TearDownTest(c *C) { 59 dirs.SetRootDir("") 60 } 61 62 const packageHelloNoSrv = `name: hello-snap 63 version: 1.10 64 summary: hello 65 description: Hello... 66 apps: 67 hello: 68 command: bin/hello 69 world: 70 command: bin/world 71 completer: world-completer.sh 72 ` 73 74 const packageHello = packageHelloNoSrv + ` 75 svc1: 76 command: bin/hello 77 stop-command: bin/goodbye 78 post-stop-command: bin/missya 79 daemon: forking 80 ` 81 82 func (s *binariesTestSuite) TestAddSnapBinariesAndRemove(c *C) { 83 // no completers support -> no problem \o/ 84 c.Assert(osutil.FileExists(dirs.CompletersDir), Equals, false) 85 86 s.testAddSnapBinariesAndRemove(c) 87 } 88 89 func (s *binariesTestSuite) TestAddSnapBinariesAndRemoveWithCompleters(c *C) { 90 c.Assert(os.MkdirAll(dirs.CompletersDir, 0755), IsNil) 91 if s.base == "core-with-snapd" { 92 c.Check(os.MkdirAll(filepath.Join(dirs.SnapMountDir, "snapd/current/usr/lib/snapd"), 0755), IsNil) 93 } 94 c.Assert(os.MkdirAll(filepath.Dir(dirs.CompleteShPath(s.base)), 0755), IsNil) 95 c.Assert(ioutil.WriteFile(dirs.CompleteShPath(s.base), nil, 0644), IsNil) 96 // full completers support -> we get completers \o/ 97 98 s.testAddSnapBinariesAndRemove(c) 99 } 100 101 func (s *binariesTestSuite) TestAddSnapBinariesAndRemoveWithExistingCompleters(c *C) { 102 c.Assert(os.MkdirAll(dirs.CompletersDir, 0755), IsNil) 103 if s.base == "core-with-snapd" { 104 c.Check(os.MkdirAll(filepath.Join(dirs.SnapMountDir, "snapd/current/usr/lib/snapd"), 0755), IsNil) 105 } 106 c.Assert(os.MkdirAll(filepath.Dir(dirs.CompleteShPath(s.base)), 0755), IsNil) 107 c.Assert(ioutil.WriteFile(dirs.CompleteShPath(s.base), nil, 0644), IsNil) 108 // existing completers -> they're left alone \o/ 109 c.Assert(ioutil.WriteFile(filepath.Join(dirs.CompletersDir, "hello-snap.world"), nil, 0644), IsNil) 110 111 s.testAddSnapBinariesAndRemove(c) 112 } 113 114 func (s *binariesTestSuite) testAddSnapBinariesAndRemove(c *C) { 115 info := snaptest.MockSnap(c, packageHello+"base: "+s.base+"\n", &snap.SideInfo{Revision: snap.R(11)}) 116 completer := filepath.Join(dirs.CompletersDir, "hello-snap.world") 117 completerExisted := osutil.FileExists(completer) 118 119 err := wrappers.AddSnapBinaries(info) 120 c.Assert(err, IsNil) 121 122 bins := []string{"hello-snap.hello", "hello-snap.world"} 123 124 for _, bin := range bins { 125 link := filepath.Join(dirs.SnapBinariesDir, bin) 126 target, err := os.Readlink(link) 127 c.Assert(err, IsNil, Commentf(bin)) 128 c.Check(target, Equals, "/usr/bin/snap", Commentf(bin)) 129 } 130 131 if osutil.FileExists(dirs.CompletersDir) { 132 if completerExisted { 133 // there was a completer there before, so it should _not_ be a symlink to our complete.sh 134 c.Assert(osutil.IsSymlink(completer), Equals, false) 135 } else { 136 target, err := os.Readlink(completer) 137 c.Assert(err, IsNil) 138 c.Check(target, Equals, dirs.CompleteShPath(s.base)) 139 } 140 } 141 142 err = wrappers.RemoveSnapBinaries(info) 143 c.Assert(err, IsNil) 144 145 for _, bin := range bins { 146 link := filepath.Join(dirs.SnapBinariesDir, bin) 147 c.Check(osutil.FileExists(link), Equals, false, Commentf(bin)) 148 } 149 150 // we left the existing completer alone, but removed it otherwise 151 c.Check(osutil.FileExists(completer), Equals, completerExisted) 152 } 153 154 func (s *binariesTestSuite) TestAddSnapBinariesCleansUpOnFailure(c *C) { 155 link := filepath.Join(dirs.SnapBinariesDir, "hello-snap.hello") 156 c.Assert(osutil.FileExists(link), Equals, false) 157 c.Assert(os.MkdirAll(filepath.Join(dirs.SnapBinariesDir, "hello-snap.bye", "potato"), 0755), IsNil) 158 159 info := snaptest.MockSnap(c, packageHello+` 160 bye: 161 command: bin/bye 162 `, &snap.SideInfo{Revision: snap.R(11)}) 163 164 err := wrappers.AddSnapBinaries(info) 165 c.Assert(err, NotNil) 166 167 c.Check(osutil.FileExists(link), Equals, false) 168 }