github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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/series"
    14  	"github.com/juju/utils/symlink"
    15  	gc "gopkg.in/check.v1"
    16  
    17  	"github.com/juju/juju/instance"
    18  	"github.com/juju/juju/mongo"
    19  	"github.com/juju/juju/testing"
    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  	pm, err := testing.GetPackageManager()
    64  	c.Assert(err, jc.ErrorIsNil)
    65  
    66  	err = symlink.New("/bin/true", filepath.Join(s.tmpdir, pm.PackageQuery))
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	s.PatchValue(&isPackageInstalled, func(pack string) bool {
    69  		pacman, err := manager.NewPackageManager(series.HostSeries())
    70  		c.Assert(err, jc.ErrorIsNil)
    71  		return pacman.IsInstalled(pack)
    72  	})
    73  }
    74  
    75  func (*prereqsSuite) TestSupportedOS(c *gc.C) {
    76  	defer func(old string) {
    77  		goos = old
    78  	}(goos)
    79  	goos = "windows"
    80  	err := VerifyPrerequisites(instance.LXC)
    81  	c.Assert(err, gc.ErrorMatches, "Unsupported operating system: windows(.|\n)*")
    82  }
    83  
    84  const fakeMongoFmt = `#!/bin/sh
    85  echo db version v%d.%d.%d
    86  echo Thu Feb 13 15:53:58.210 git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
    87  `
    88  
    89  func (s *prereqsSuite) TestLxcPrereq(c *gc.C) {
    90  	s.PatchValue(&lxclsPath, filepath.Join(s.tmpdir, "non-existent"))
    91  
    92  	err := VerifyPrerequisites(instance.LXC)
    93  	c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be\ninstalled(.|\n)*")
    94  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install lxc(.|\n)*")
    95  
    96  	os.Setenv("JUJUTEST_LSB_RELEASE_ID", "NotUbuntu")
    97  	err = VerifyPrerequisites(instance.LXC)
    98  	c.Assert(err, gc.ErrorMatches, "(.|\n)*Linux Containers \\(LXC\\) userspace tools must be installed(.|\n)*")
    99  	c.Assert(err, gc.Not(gc.ErrorMatches), "(.|\n)*apt-get install(.|\n)*")
   100  
   101  	err = ioutil.WriteFile(lxclsPath, nil, 0777)
   102  	c.Assert(err, jc.ErrorIsNil)
   103  	err = VerifyPrerequisites(instance.LXC)
   104  	c.Assert(err, jc.ErrorIsNil)
   105  }
   106  
   107  func (s *prereqsSuite) TestCloudImageUtilsPrereq(c *gc.C) {
   108  	s.PatchValue(&isPackageInstalled, func(pack string) bool {
   109  		if pack == "juju-local" {
   110  			return true
   111  		}
   112  		return false
   113  	})
   114  
   115  	err := VerifyPrerequisites(instance.LXC)
   116  	c.Assert(err, gc.ErrorMatches, "(.|\n)*cloud-image-utils must be installed(.|\n)*")
   117  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install cloud-image-utils(.|\n)*")
   118  }
   119  
   120  func (s *prereqsSuite) TestJujuLocalPrereq(c *gc.C) {
   121  
   122  	pm, err := testing.GetPackageManager()
   123  	c.Assert(err, jc.ErrorIsNil)
   124  
   125  	err = os.Remove(filepath.Join(s.tmpdir, pm.PackageQuery))
   126  	c.Assert(err, jc.ErrorIsNil)
   127  	err = symlink.New("/bin/false", filepath.Join(s.tmpdir, pm.PackageQuery))
   128  	c.Assert(err, jc.ErrorIsNil)
   129  
   130  	err = VerifyPrerequisites(instance.LXC)
   131  	c.Assert(err, gc.ErrorMatches, "(.|\n)*juju-local must be installed to enable the local provider(.|\n)*")
   132  	c.Assert(err, gc.ErrorMatches, "(.|\n)*apt-get install juju-local(.|\n)*")
   133  }