github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/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  	jc "github.com/juju/testing/checkers"
    12  	"github.com/juju/utils/packaging/manager"
    13  	"github.com/juju/utils/symlink"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/instance"
    17  	"github.com/juju/juju/mongo"
    18  	"github.com/juju/juju/testing"
    19  	"github.com/juju/juju/version"
    20  )
    21  
    22  type prereqsSuite struct {
    23  	testing.BaseSuite
    24  	tmpdir         string
    25  	testMongodPath string
    26  }
    27  
    28  var _ = gc.Suite(&prereqsSuite{})
    29  
    30  const lsbrelease = `#!/bin/sh
    31  echo $JUJUTEST_LSB_RELEASE_ID
    32  `
    33  
    34  func init() {
    35  	// Set the paths to mongod and lxc-ls to
    36  	// something we know exists. This allows
    37  	// all of the non-prereqs tests to pass
    38  	// even when mongodb and lxc-ls can't be
    39  	// found.
    40  	lxclsPath = "/bin/true"
    41  
    42  	// Allow non-prereq tests to pass by default.
    43  	isPackageInstalled = func(packageName string) bool {
    44  		return true
    45  	}
    46  }
    47  
    48  func (s *prereqsSuite) SetUpTest(c *gc.C) {
    49  	s.BaseSuite.SetUpTest(c)
    50  	s.tmpdir = c.MkDir()
    51  	s.testMongodPath = filepath.Join(s.tmpdir, "mongod")
    52  
    53  	s.PatchEnvironment("PATH", s.tmpdir)
    54  
    55  	s.PatchValue(&mongo.JujuMongodPath, "/somewhere/that/wont/exist")
    56  
    57  	os.Setenv("JUJUTEST_LSB_RELEASE_ID", "Ubuntu")
    58  	err := ioutil.WriteFile(filepath.Join(s.tmpdir, "lsb_release"), []byte(lsbrelease), 0777)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	// symlink $temp/dpkg-query to /bin/true, to
    62  	// simulate package installation query responses.
    63  	err = symlink.New("/bin/true", filepath.Join(s.tmpdir, "dpkg-query"))
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	s.PatchValue(&isPackageInstalled, func(pack string) bool {
    66  		pacman, _ := manager.NewPackageManager(version.Current.Series)
    67  		return pacman.IsInstalled(pack)
    68  	})
    69  }
    70  
    71  func (*prereqsSuite) TestSupportedOS(c *gc.C) {
    72  	defer func(old string) {
    73  		goos = old
    74  	}(goos)
    75  	goos = "windows"
    76  	err := VerifyPrerequisites(instance.LXC)
    77  	c.Assert(err, gc.ErrorMatches, "Unsupported operating system: windows(.|\n)*")
    78  }
    79  
    80  const fakeMongoFmt = `#!/bin/sh
    81  echo db version v%d.%d.%d
    82  echo Thu Feb 13 15:53:58.210 git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
    83  `
    84  
    85  func (s *prereqsSuite) TestLxcPrereq(c *gc.C) {
    86  	s.PatchValue(&lxclsPath, filepath.Join(s.tmpdir, "non-existent"))
    87  
    88  	err := VerifyPrerequisites(instance.LXC)
    89  	c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be\ninstalled(.|\n)*")
    90  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install lxc(.|\n)*")
    91  
    92  	os.Setenv("JUJUTEST_LSB_RELEASE_ID", "NotUbuntu")
    93  	err = VerifyPrerequisites(instance.LXC)
    94  	c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be installed(.|\n)*")
    95  	c.Assert(err, gc.Not(gc.ErrorMatches), "(.|\n)*apt-get install(.|\n)*")
    96  
    97  	err = ioutil.WriteFile(lxclsPath, nil, 0777)
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	err = VerifyPrerequisites(instance.LXC)
   100  	c.Assert(err, jc.ErrorIsNil)
   101  }
   102  
   103  const jujuLocalInstalled = `#!/bin/sh
   104  if [ "$2" = "juju-local" ]; then return 0; else return 1; fi
   105  `
   106  
   107  func (s *prereqsSuite) TestCloudImageUtilsPrereq(c *gc.C) {
   108  	err := os.Remove(filepath.Join(s.tmpdir, "dpkg-query"))
   109  	c.Assert(err, jc.ErrorIsNil)
   110  	err = ioutil.WriteFile(filepath.Join(s.tmpdir, "dpkg-query"), []byte(jujuLocalInstalled), 0777)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	err = VerifyPrerequisites(instance.LXC)
   114  	c.Assert(err, gc.ErrorMatches, "(.|\n)*cloud-image-utils must be installed(.|\n)*")
   115  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install cloud-image-utils(.|\n)*")
   116  }
   117  
   118  func (s *prereqsSuite) TestJujuLocalPrereq(c *gc.C) {
   119  	err := os.Remove(filepath.Join(s.tmpdir, "dpkg-query"))
   120  	c.Assert(err, jc.ErrorIsNil)
   121  	err = symlink.New("/bin/false", filepath.Join(s.tmpdir, "dpkg-query"))
   122  	c.Assert(err, jc.ErrorIsNil)
   123  
   124  	err = VerifyPrerequisites(instance.LXC)
   125  	c.Assert(err, gc.ErrorMatches, "(.|\n)*juju-local must be installed to enable the local provider(.|\n)*")
   126  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install juju-local(.|\n)*")
   127  }