launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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 gc "launchpad.net/gocheck" 12 13 "launchpad.net/juju-core/instance" 14 "launchpad.net/juju-core/testing/testbase" 15 ) 16 17 type prereqsSuite struct { 18 testbase.LoggingSuite 19 tmpdir string 20 oldpath string 21 } 22 23 var _ = gc.Suite(&prereqsSuite{}) 24 25 const lsbrelease = `#!/bin/sh 26 echo $JUJUTEST_LSB_RELEASE_ID 27 ` 28 29 func init() { 30 // Set the paths to mongod and lxc-ls to 31 // something we know exists. This allows 32 // all of the non-prereqs tests to pass 33 // even when mongodb and lxc-ls can't be 34 // found. 35 mongodPath = "/bin/true" 36 lxclsPath = "/bin/true" 37 } 38 39 func (s *prereqsSuite) SetUpTest(c *gc.C) { 40 s.LoggingSuite.SetUpTest(c) 41 s.tmpdir = c.MkDir() 42 s.oldpath = os.Getenv("PATH") 43 mongodPath = filepath.Join(s.tmpdir, "mongod") 44 lxclsPath = filepath.Join(s.tmpdir, "lxc-ls") 45 os.Setenv("PATH", s.tmpdir) 46 os.Setenv("JUJUTEST_LSB_RELEASE_ID", "Ubuntu") 47 err := ioutil.WriteFile(filepath.Join(s.tmpdir, "lsb_release"), []byte(lsbrelease), 0777) 48 c.Assert(err, gc.IsNil) 49 } 50 51 func (s *prereqsSuite) TearDownTest(c *gc.C) { 52 os.Setenv("PATH", s.oldpath) 53 mongodPath = "/bin/true" 54 lxclsPath = "/bin/true" 55 s.LoggingSuite.TearDownTest(c) 56 } 57 58 func (*prereqsSuite) TestSupportedOS(c *gc.C) { 59 defer func(old string) { 60 goos = old 61 }(goos) 62 goos = "windows" 63 err := VerifyPrerequisites(instance.LXC) 64 c.Assert(err, gc.ErrorMatches, "Unsupported operating system: windows(.|\n)*") 65 } 66 67 func (s *prereqsSuite) TestMongoPrereq(c *gc.C) { 68 err := VerifyPrerequisites(instance.LXC) 69 c.Assert(err, gc.ErrorMatches, "(.|\n)*MongoDB server must be installed(.|\n)*") 70 c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install mongodb-server(.|\n)*") 71 72 os.Setenv("JUJUTEST_LSB_RELEASE_ID", "NotUbuntu") 73 err = VerifyPrerequisites(instance.LXC) 74 c.Assert(err, gc.ErrorMatches, "(.|\n)*MongoDB server must be installed(.|\n)*") 75 c.Assert(err, gc.Not(gc.ErrorMatches), "(.|\n)*apt-get install(.|\n)*") 76 77 err = ioutil.WriteFile(mongodPath, nil, 0777) 78 c.Assert(err, gc.IsNil) 79 err = ioutil.WriteFile(filepath.Join(s.tmpdir, "lxc-ls"), nil, 0777) 80 c.Assert(err, gc.IsNil) 81 err = VerifyPrerequisites(instance.LXC) 82 c.Assert(err, gc.IsNil) 83 } 84 85 func (s *prereqsSuite) TestLxcPrereq(c *gc.C) { 86 err := ioutil.WriteFile(mongodPath, nil, 0777) 87 c.Assert(err, gc.IsNil) 88 89 err = VerifyPrerequisites(instance.LXC) 90 c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be\ninstalled(.|\n)*") 91 c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install lxc(.|\n)*") 92 93 os.Setenv("JUJUTEST_LSB_RELEASE_ID", "NotUbuntu") 94 err = VerifyPrerequisites(instance.LXC) 95 c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be installed(.|\n)*") 96 c.Assert(err, gc.Not(gc.ErrorMatches), "(.|\n)*apt-get install(.|\n)*") 97 98 err = ioutil.WriteFile(lxclsPath, nil, 0777) 99 c.Assert(err, gc.IsNil) 100 err = VerifyPrerequisites(instance.LXC) 101 c.Assert(err, gc.IsNil) 102 }