github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/container/lxc/initialisation_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxc
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/utils/packaging/commands"
     9  	"github.com/juju/utils/packaging/manager"
    10  	"github.com/juju/utils/series"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/testing"
    14  )
    15  
    16  type InitialiserSuite struct {
    17  	testing.BaseSuite
    18  	calledCmds []string
    19  }
    20  
    21  var _ = gc.Suite(&InitialiserSuite{})
    22  
    23  // getMockRunCommandWithRetry is a helper function which returns a function
    24  // with an identical signature to manager.RunCommandWithRetry which saves each
    25  // command it recieves in a slice and always returns no output, error code 0
    26  // and a nil error.
    27  func getMockRunCommandWithRetry(calledCmds *[]string) func(string, func(string) error) (string, int, error) {
    28  	return func(cmd string, fatalError func(string) error) (string, int, error) {
    29  		*calledCmds = append(*calledCmds, cmd)
    30  		return "", 0, nil
    31  	}
    32  }
    33  
    34  func (s *InitialiserSuite) SetUpTest(c *gc.C) {
    35  	s.BaseSuite.SetUpTest(c)
    36  	s.calledCmds = []string{}
    37  	s.PatchValue(&manager.RunCommandWithRetry, getMockRunCommandWithRetry(&s.calledCmds))
    38  }
    39  
    40  func (s *InitialiserSuite) TestLTSSeriesPackages(c *gc.C) {
    41  	// Momentarily, the only series with a dedicated cloud archive is precise,
    42  	// which we will use for the following test:
    43  	paccmder, err := commands.NewPackageCommander("precise")
    44  	c.Assert(err, jc.ErrorIsNil)
    45  
    46  	s.PatchValue(&series.HostSeries, func() string { return "precise" })
    47  	container := NewContainerInitialiser("precise")
    48  
    49  	err = container.Initialise()
    50  	c.Assert(err, jc.ErrorIsNil)
    51  
    52  	c.Assert(s.calledCmds, gc.DeepEquals, []string{
    53  		paccmder.InstallCmd("--target-release", "precise-updates/cloud-tools", "lxc"),
    54  		paccmder.InstallCmd("--target-release", "precise-updates/cloud-tools", "cloud-image-utils"),
    55  	})
    56  }
    57  
    58  func (s *InitialiserSuite) TestNoSeriesPackages(c *gc.C) {
    59  	// Here we want to test for any other series whilst avoiding the
    60  	// possibility of hitting a cloud archive-requiring release.
    61  	// As such, we simply pass an empty series.
    62  	paccmder, err := commands.NewPackageCommander("")
    63  	c.Assert(err, jc.ErrorIsNil)
    64  
    65  	container := NewContainerInitialiser("")
    66  
    67  	err = container.Initialise()
    68  	c.Assert(err, jc.ErrorIsNil)
    69  
    70  	c.Assert(s.calledCmds, gc.DeepEquals, []string{
    71  		paccmder.InstallCmd("lxc"),
    72  		paccmder.InstallCmd("cloud-image-utils"),
    73  	})
    74  }