many: abstract path to /bin/{true,false} #3096

Merged
merged 12 commits into from Apr 5, 2017
@@ -49,23 +49,26 @@ type ErrtrackerTestSuite struct {
var _ = Suite(&ErrtrackerTestSuite{})
+var truePath = osutil.LookPathDefault("true", "/bin/true")
+var falsePath = osutil.LookPathDefault("false", "/bin/false")
+
func (s *ErrtrackerTestSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
p := filepath.Join(c.MkDir(), "machine-id")
err := ioutil.WriteFile(p, []byte("bbb1a6a5bcdb418380056a2d759c3f7c"), 0644)
c.Assert(err, IsNil)
s.AddCleanup(errtracker.MockMachineIDPath(p))
- s.AddCleanup(errtracker.MockHostSnapd("/bin/true"))
- s.AddCleanup(errtracker.MockCoreSnapd("/bin/false"))
+ s.AddCleanup(errtracker.MockHostSnapd(truePath))
+ s.AddCleanup(errtracker.MockCoreSnapd(falsePath))
}
func (s *ErrtrackerTestSuite) TestReport(c *C) {
n := 0
identifier := ""
- hostBuildID, err := osutil.ReadBuildID("/bin/true")
+ hostBuildID, err := osutil.ReadBuildID(truePath)
c.Assert(err, IsNil)
- coreBuildID, err := osutil.ReadBuildID("/bin/false")
+ coreBuildID, err := osutil.ReadBuildID(falsePath)
c.Assert(err, IsNil)
prev := errtracker.SnapdVersion
View
@@ -34,6 +34,10 @@ type buildIDSuite struct{}
var _ = Suite(&buildIDSuite{})
+var truePath = osutil.LookPathDefault("true", "/bin/true")
+var falsePath = osutil.LookPathDefault("false", "/bin/false")
+var gccPath = osutil.LookPathDefault("gcc", "/usr/bin/gcc")
+
func buildID(c *C, fname string) string {
output, err := exec.Command("file", fname).CombinedOutput()
c.Assert(err, IsNil)
@@ -46,7 +50,7 @@ func buildID(c *C, fname string) string {
}
func (s *buildIDSuite) TestReadBuildID(c *C) {
- for _, fname := range []string{"/bin/true", "/bin/false"} {
+ for _, fname := range []string{truePath, falsePath} {
id, err := osutil.ReadBuildID(fname)
c.Assert(err, IsNil)
@@ -56,7 +60,7 @@ func (s *buildIDSuite) TestReadBuildID(c *C) {
func (s *buildIDSuite) TestReadBuildIDNoID(c *C) {
stripedTruth := filepath.Join(c.MkDir(), "true")
- osutil.CopyFile("/bin/true", stripedTruth, 0)
+ osutil.CopyFile(truePath, stripedTruth, 0)
output, err := exec.Command("strip", "-R", ".note.gnu.build-id", stripedTruth).CombinedOutput()
c.Assert(string(output), Equals, "")
c.Assert(err, IsNil)
@@ -67,14 +71,14 @@ func (s *buildIDSuite) TestReadBuildIDNoID(c *C) {
}
func (s *buildIDSuite) TestReadBuildIDmd5(c *C) {
- if !osutil.FileExists("/usr/bin/gcc") {
+ if !osutil.FileExists(gccPath) {
c.Skip("No gcc found")
}
md5Truth := filepath.Join(c.MkDir(), "true")
err := ioutil.WriteFile(md5Truth+".c", []byte(`int main(){return 0;}`), 0644)
c.Assert(err, IsNil)
- output, err := exec.Command("gcc", "-Wl,-build-id=md5", "-xc", md5Truth+".c", "-o", md5Truth).CombinedOutput()
+ output, err := exec.Command(gccPath, "-Wl,-build-id=md5", "-xc", md5Truth+".c", "-o", md5Truth).CombinedOutput()
c.Assert(string(output), Equals, "")
c.Assert(err, IsNil)
View
@@ -71,11 +71,11 @@ func (s *commandFromCoreSuite) TestCommandFromCore(c *C) {
root := filepath.Join(dirs.SnapMountDir, "/core/current")
os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755)
- osutil.CopyFile("/bin/true", filepath.Join(root, "/usr/bin/xdelta3"), 0)
+ osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0)
cmd, err := osutil.CommandFromCore("/usr/bin/xdelta3", "--some-xdelta-arg")
c.Assert(err, IsNil)
- out, err := exec.Command("/bin/sh", "-c", "readelf -l /bin/true |grep interpreter:|cut -f2 -d:|cut -f1 -d]").Output()
+ out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("readelf -l %s |grep interpreter:|cut -f2 -d:|cut -f1 -d]", truePath)).Output()
c.Assert(err, IsNil)
interp := strings.TrimSpace(string(out))
@@ -93,7 +93,7 @@ func (s *commandFromCoreSuite) TestCommandFromCoreSymlinkCycle(c *C) {
root := filepath.Join(dirs.SnapMountDir, "/core/current")
os.MkdirAll(filepath.Join(root, "/usr/bin"), 0755)
- osutil.CopyFile("/bin/true", filepath.Join(root, "/usr/bin/xdelta3"), 0)
+ osutil.CopyFile(truePath, filepath.Join(root, "/usr/bin/xdelta3"), 0)
out, err := exec.Command("/bin/sh", "-c", "readelf -l /bin/true |grep interpreter:|cut -f2 -d:|cut -f1 -d]").Output()
c.Assert(err, IsNil)
View
@@ -63,3 +63,16 @@ func ExecutableExists(name string) bool {
return err == nil
}
+
+var lookPath func(name string) (string, error) = exec.LookPath
+
+// LookPathDefault searches for a given command name in all directories
+// listed in the environment variable PATH and returns the found path or the
+// provided default path.
+func LookPathDefault(name string, defaultPath string) string {
+ p, err := lookPath(name)
+ if err != nil {
+ return defaultPath
+ }
+ return p
+}
View
@@ -20,6 +20,7 @@
package osutil
import (
+ "fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -89,3 +90,13 @@ func (ts *StatTestSuite) TestExecutableExists(c *C) {
c.Assert(os.Chmod(fname, 0755), IsNil)
c.Check(ExecutableExists("xyzzy"), Equals, true)
}
+
+func (s *StatTestSuite) TestLookPathDefaultGivesCorrectPath(c *C) {
+ lookPath = func(name string) (string, error) { return "/bin/true", nil }
+ c.Assert(LookPathDefault("true", "/bin/foo"), Equals, "/bin/true")
+}
+
+func (s *StatTestSuite) TestLookPathDefaultReturnsDefaultWhenNotFound(c *C) {
+ lookPath = func(name string) (string, error) { return "", fmt.Errorf("Not found") }
+ c.Assert(LookPathDefault("bar", "/bin/bla"), Equals, "/bin/bla")
+}