github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/cmdutil/cmdutil_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 cmdutil_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "os/exec" 27 "path/filepath" 28 "strings" 29 "testing" 30 31 . "gopkg.in/check.v1" 32 33 "github.com/snapcore/snapd/cmd/cmdutil" 34 "github.com/snapcore/snapd/dirs" 35 "github.com/snapcore/snapd/osutil" 36 ) 37 38 // Hook up check.v1 into the "go test" runner 39 func Test(t *testing.T) { TestingT(t) } 40 41 var truePath = osutil.LookPathDefault("true", "/bin/true") 42 43 type cmdutilSuite struct{} 44 45 var _ = Suite(&cmdutilSuite{}) 46 47 func (s *cmdutilSuite) SetUpTest(c *C) { 48 dirs.SetRootDir(c.MkDir()) 49 } 50 51 func (s *cmdutilSuite) TearDownTest(c *C) { 52 dirs.SetRootDir("") 53 } 54 55 func (s *cmdutilSuite) makeMockLdSoConf(c *C, root string) { 56 ldSoConf := filepath.Join(root, "/etc/ld.so.conf") 57 ldSoConfD := ldSoConf + ".d" 58 59 err := os.MkdirAll(filepath.Dir(ldSoConf), 0755) 60 c.Assert(err, IsNil) 61 err = os.MkdirAll(ldSoConfD, 0755) 62 c.Assert(err, IsNil) 63 64 err = ioutil.WriteFile(ldSoConf, []byte("include /etc/ld.so.conf.d/*.conf"), 0644) 65 c.Assert(err, IsNil) 66 67 ldSoConf1 := filepath.Join(ldSoConfD, "x86_64-linux-gnu.conf") 68 69 err = ioutil.WriteFile(ldSoConf1, []byte(` 70 # Multiarch support 71 /lib/x86_64-linux-gnu 72 /usr/lib/x86_64-linux-gnu`), 0644) 73 c.Assert(err, IsNil) 74 } 75 76 func (s *cmdutilSuite) TestCommandFromSystemSnap(c *C) { 77 for _, snap := range []string{"core", "snapd"} { 78 79 root := filepath.Join(dirs.SnapMountDir, snap, "current") 80 s.makeMockLdSoConf(c, root) 81 82 os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755) 83 osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0) 84 cmd, err := cmdutil.CommandFromSystemSnap("/usr/bin/xdelta3", "--some-xdelta-arg") 85 c.Assert(err, IsNil) 86 87 out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("readelf -l %s |grep interpreter:|cut -f2 -d:|cut -f1 -d]", truePath)).Output() 88 c.Assert(err, IsNil) 89 interp := strings.TrimSpace(string(out)) 90 91 c.Check(cmd.Args, DeepEquals, []string{ 92 filepath.Join(root, interp), 93 "--library-path", 94 fmt.Sprintf("%s/lib/x86_64-linux-gnu:%s/usr/lib/x86_64-linux-gnu", root, root), 95 filepath.Join(root, "/usr/bin/xdelta3"), 96 "--some-xdelta-arg", 97 }) 98 } 99 } 100 101 func (s *cmdutilSuite) TestCommandFromCoreSymlinkCycle(c *C) { 102 root := filepath.Join(dirs.SnapMountDir, "/core/current") 103 s.makeMockLdSoConf(c, root) 104 105 os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755) 106 osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0) 107 108 out, err := exec.Command("/bin/sh", "-c", "readelf -l /bin/true |grep interpreter:|cut -f2 -d:|cut -f1 -d]").Output() 109 c.Assert(err, IsNil) 110 interp := strings.TrimSpace(string(out)) 111 112 coreInterp := filepath.Join(root, interp) 113 c.Assert(os.MkdirAll(filepath.Dir(coreInterp), 0755), IsNil) 114 c.Assert(os.Symlink(filepath.Base(coreInterp), coreInterp), IsNil) 115 116 _, err = cmdutil.CommandFromSystemSnap("/usr/bin/xdelta3", "--some-xdelta-arg") 117 c.Assert(err, ErrorMatches, "cannot run command from core: symlink cycle found") 118 }