github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/provider/lxd/environ_instance_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxd_test
     7  
     8  import (
     9  	"github.com/juju/errors"
    10  	gitjujutesting "github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/environs"
    15  	"github.com/juju/juju/instance"
    16  	"github.com/juju/juju/provider/lxd"
    17  	"github.com/juju/juju/tools/lxdclient"
    18  )
    19  
    20  type environInstSuite struct {
    21  	lxd.BaseSuite
    22  }
    23  
    24  var _ = gc.Suite(&environInstSuite{})
    25  
    26  func (s *environInstSuite) TestInstancesOkay(c *gc.C) {
    27  	ids := []instance.Id{"spam", "eggs", "ham"}
    28  	var raw []lxdclient.Instance
    29  	var expected []instance.Instance
    30  	for _, id := range ids {
    31  		raw = append(raw, *s.NewRawInstance(c, string(id)))
    32  		expected = append(expected, s.NewInstance(c, string(id)))
    33  	}
    34  	s.Client.Insts = raw
    35  
    36  	insts, err := s.Env.Instances(ids)
    37  	c.Assert(err, jc.ErrorIsNil)
    38  
    39  	c.Check(insts, jc.DeepEquals, expected)
    40  }
    41  
    42  func (s *environInstSuite) TestInstancesAPI(c *gc.C) {
    43  	ids := []instance.Id{"spam", "eggs", "ham"}
    44  	s.Env.Instances(ids)
    45  
    46  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
    47  		FuncName: "Instances",
    48  		Args: []interface{}{
    49  			s.Prefix + "machine-",
    50  			lxdclient.AliveStatuses,
    51  		},
    52  	}})
    53  }
    54  
    55  func (s *environInstSuite) TestInstancesEmptyArg(c *gc.C) {
    56  	insts, err := s.Env.Instances(nil)
    57  
    58  	c.Check(insts, gc.HasLen, 0)
    59  	c.Check(errors.Cause(err), gc.Equals, environs.ErrNoInstances)
    60  }
    61  
    62  func (s *environInstSuite) TestInstancesInstancesFailed(c *gc.C) {
    63  	failure := errors.New("<unknown>")
    64  	s.Stub.SetErrors(failure)
    65  
    66  	ids := []instance.Id{"spam"}
    67  	insts, err := s.Env.Instances(ids)
    68  
    69  	c.Check(insts, jc.DeepEquals, []instance.Instance{nil})
    70  	c.Check(errors.Cause(err), gc.Equals, failure)
    71  }
    72  
    73  func (s *environInstSuite) TestInstancesPartialMatch(c *gc.C) {
    74  	raw := s.NewRawInstance(c, "spam")
    75  	expected := s.NewInstance(c, "spam")
    76  	s.Client.Insts = []lxdclient.Instance{*raw}
    77  
    78  	ids := []instance.Id{"spam", "eggs"}
    79  	insts, err := s.Env.Instances(ids)
    80  
    81  	c.Check(insts, jc.DeepEquals, []instance.Instance{expected, nil})
    82  	c.Check(errors.Cause(err), gc.Equals, environs.ErrPartialInstances)
    83  }
    84  
    85  func (s *environInstSuite) TestInstancesNoMatch(c *gc.C) {
    86  	raw := s.NewRawInstance(c, "spam")
    87  	s.Client.Insts = []lxdclient.Instance{*raw}
    88  
    89  	ids := []instance.Id{"eggs"}
    90  	insts, err := s.Env.Instances(ids)
    91  
    92  	c.Check(insts, jc.DeepEquals, []instance.Instance{nil})
    93  	c.Check(errors.Cause(err), gc.Equals, environs.ErrNoInstances)
    94  }
    95  
    96  func (s *environInstSuite) TestControllerInstancesOkay(c *gc.C) {
    97  	s.Client.Insts = []lxdclient.Instance{*s.RawInstance}
    98  
    99  	ids, err := s.Env.ControllerInstances()
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	c.Check(ids, jc.DeepEquals, []instance.Id{"spam"})
   103  	s.BaseSuite.Client.CheckCallNames(c, "Instances")
   104  	s.BaseSuite.Client.CheckCall(
   105  		c, 0, "Instances",
   106  		"juju-"+s.Env.Config().ControllerUUID()+"-machine-",
   107  		[]string{"Starting", "Started", "Running", "Stopping", "Stopped"},
   108  	)
   109  }
   110  
   111  func (s *environInstSuite) TestControllerInstancesNotBootstrapped(c *gc.C) {
   112  	_, err := s.Env.ControllerInstances()
   113  
   114  	c.Check(err, gc.Equals, environs.ErrNotBootstrapped)
   115  }
   116  
   117  func (s *environInstSuite) TestControllerInstancesMixed(c *gc.C) {
   118  	other := lxdclient.NewInstance(lxdclient.InstanceSummary{}, nil)
   119  	s.Client.Insts = []lxdclient.Instance{*s.RawInstance, *other}
   120  
   121  	ids, err := s.Env.ControllerInstances()
   122  	c.Assert(err, jc.ErrorIsNil)
   123  
   124  	c.Check(ids, jc.DeepEquals, []instance.Id{"spam"})
   125  }