github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/provider/local/prereqs_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package local 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 "github.com/juju/utils/apt" 12 gc "launchpad.net/gocheck" 13 14 "github.com/juju/juju/agent/mongo" 15 "github.com/juju/juju/instance" 16 "github.com/juju/juju/testing" 17 ) 18 19 type prereqsSuite struct { 20 testing.BaseSuite 21 tmpdir string 22 testMongodPath string 23 } 24 25 var _ = gc.Suite(&prereqsSuite{}) 26 27 const lsbrelease = `#!/bin/sh 28 echo $JUJUTEST_LSB_RELEASE_ID 29 ` 30 31 func init() { 32 // Set the paths to mongod and lxc-ls to 33 // something we know exists. This allows 34 // all of the non-prereqs tests to pass 35 // even when mongodb and lxc-ls can't be 36 // found. 37 lxclsPath = "/bin/true" 38 39 // Allow non-prereq tests to pass by default. 40 isPackageInstalled = func(packageName string) bool { 41 return true 42 } 43 } 44 45 func (s *prereqsSuite) SetUpTest(c *gc.C) { 46 s.BaseSuite.SetUpTest(c) 47 s.tmpdir = c.MkDir() 48 s.testMongodPath = filepath.Join(s.tmpdir, "mongod") 49 50 s.PatchEnvironment("PATH", s.tmpdir) 51 52 s.PatchValue(&mongo.JujuMongodPath, "/somewhere/that/wont/exist") 53 54 os.Setenv("JUJUTEST_LSB_RELEASE_ID", "Ubuntu") 55 err := ioutil.WriteFile(filepath.Join(s.tmpdir, "lsb_release"), []byte(lsbrelease), 0777) 56 c.Assert(err, gc.IsNil) 57 58 // symlink $temp/dpkg-query to /bin/true, to 59 // simulate package installation query responses. 60 err = os.Symlink("/bin/true", filepath.Join(s.tmpdir, "dpkg-query")) 61 c.Assert(err, gc.IsNil) 62 s.PatchValue(&isPackageInstalled, apt.IsPackageInstalled) 63 } 64 65 func (*prereqsSuite) TestSupportedOS(c *gc.C) { 66 defer func(old string) { 67 goos = old 68 }(goos) 69 goos = "windows" 70 err := VerifyPrerequisites(instance.LXC) 71 c.Assert(err, gc.ErrorMatches, "Unsupported operating system: windows(.|\n)*") 72 } 73 74 const fakeMongoFmt = `#!/bin/sh 75 echo db version v%d.%d.%d 76 echo Thu Feb 13 15:53:58.210 git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673 77 ` 78 79 func (s *prereqsSuite) TestLxcPrereq(c *gc.C) { 80 s.PatchValue(&lxclsPath, filepath.Join(s.tmpdir, "non-existent")) 81 82 err := VerifyPrerequisites(instance.LXC) 83 c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be\ninstalled(.|\n)*") 84 c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install lxc(.|\n)*") 85 86 os.Setenv("JUJUTEST_LSB_RELEASE_ID", "NotUbuntu") 87 err = VerifyPrerequisites(instance.LXC) 88 c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be installed(.|\n)*") 89 c.Assert(err, gc.Not(gc.ErrorMatches), "(.|\n)*apt-get install(.|\n)*") 90 91 err = ioutil.WriteFile(lxclsPath, nil, 0777) 92 c.Assert(err, gc.IsNil) 93 err = VerifyPrerequisites(instance.LXC) 94 c.Assert(err, gc.IsNil) 95 } 96 97 func (s *prereqsSuite) TestJujuLocalPrereq(c *gc.C) { 98 err := os.Remove(filepath.Join(s.tmpdir, "dpkg-query")) 99 c.Assert(err, gc.IsNil) 100 err = os.Symlink("/bin/false", filepath.Join(s.tmpdir, "dpkg-query")) 101 c.Assert(err, gc.IsNil) 102 103 err = VerifyPrerequisites(instance.LXC) 104 c.Assert(err, gc.ErrorMatches, "(.|\n)*juju-local must be installed to enable the local provider(.|\n)*") 105 c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install juju-local(.|\n)*") 106 }