github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  	coretesting "github.com/juju/juju/testing"
    18  	"github.com/juju/juju/tools/lxdclient"
    19  )
    20  
    21  type environInstSuite struct {
    22  	lxd.BaseSuite
    23  }
    24  
    25  var _ = gc.Suite(&environInstSuite{})
    26  
    27  func (s *environInstSuite) TestInstancesOkay(c *gc.C) {
    28  	ids := []instance.Id{"spam", "eggs", "ham"}
    29  	var raw []lxdclient.Instance
    30  	var expected []instance.Instance
    31  	for _, id := range ids {
    32  		raw = append(raw, *s.NewRawInstance(c, string(id)))
    33  		expected = append(expected, s.NewInstance(c, string(id)))
    34  	}
    35  	s.Client.Insts = raw
    36  
    37  	insts, err := s.Env.Instances(ids)
    38  	c.Assert(err, jc.ErrorIsNil)
    39  
    40  	c.Check(insts, jc.DeepEquals, expected)
    41  }
    42  
    43  func (s *environInstSuite) TestInstancesAPI(c *gc.C) {
    44  	ids := []instance.Id{"spam", "eggs", "ham"}
    45  	s.Env.Instances(ids)
    46  
    47  	s.Stub.CheckCalls(c, []gitjujutesting.StubCall{{
    48  		FuncName: "Instances",
    49  		Args: []interface{}{
    50  			s.Prefix(),
    51  			lxdclient.AliveStatuses,
    52  		},
    53  	}})
    54  }
    55  
    56  func (s *environInstSuite) TestInstancesEmptyArg(c *gc.C) {
    57  	insts, err := s.Env.Instances(nil)
    58  
    59  	c.Check(insts, gc.HasLen, 0)
    60  	c.Check(errors.Cause(err), gc.Equals, environs.ErrNoInstances)
    61  }
    62  
    63  func (s *environInstSuite) TestInstancesInstancesFailed(c *gc.C) {
    64  	failure := errors.New("<unknown>")
    65  	s.Stub.SetErrors(failure)
    66  
    67  	ids := []instance.Id{"spam"}
    68  	insts, err := s.Env.Instances(ids)
    69  
    70  	c.Check(insts, jc.DeepEquals, []instance.Instance{nil})
    71  	c.Check(errors.Cause(err), gc.Equals, failure)
    72  }
    73  
    74  func (s *environInstSuite) TestInstancesPartialMatch(c *gc.C) {
    75  	raw := s.NewRawInstance(c, "spam")
    76  	expected := s.NewInstance(c, "spam")
    77  	s.Client.Insts = []lxdclient.Instance{*raw}
    78  
    79  	ids := []instance.Id{"spam", "eggs"}
    80  	insts, err := s.Env.Instances(ids)
    81  
    82  	c.Check(insts, jc.DeepEquals, []instance.Instance{expected, nil})
    83  	c.Check(errors.Cause(err), gc.Equals, environs.ErrPartialInstances)
    84  }
    85  
    86  func (s *environInstSuite) TestInstancesNoMatch(c *gc.C) {
    87  	raw := s.NewRawInstance(c, "spam")
    88  	s.Client.Insts = []lxdclient.Instance{*raw}
    89  
    90  	ids := []instance.Id{"eggs"}
    91  	insts, err := s.Env.Instances(ids)
    92  
    93  	c.Check(insts, jc.DeepEquals, []instance.Instance{nil})
    94  	c.Check(errors.Cause(err), gc.Equals, environs.ErrNoInstances)
    95  }
    96  
    97  func (s *environInstSuite) TestControllerInstancesOkay(c *gc.C) {
    98  	s.Client.Insts = []lxdclient.Instance{*s.RawInstance}
    99  
   100  	ids, err := s.Env.ControllerInstances(coretesting.ControllerTag.Id())
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	c.Check(ids, jc.DeepEquals, []instance.Id{"spam"})
   104  	s.BaseSuite.Client.CheckCallNames(c, "Instances")
   105  	s.BaseSuite.Client.CheckCall(
   106  		c, 0, "Instances",
   107  		"juju-",
   108  		[]string{"Starting", "Started", "Running", "Stopping", "Stopped"},
   109  	)
   110  }
   111  
   112  func (s *environInstSuite) TestControllerInstancesNotBootstrapped(c *gc.C) {
   113  	_, err := s.Env.ControllerInstances("not-used")
   114  
   115  	c.Check(err, gc.Equals, environs.ErrNotBootstrapped)
   116  }
   117  
   118  func (s *environInstSuite) TestControllerInstancesMixed(c *gc.C) {
   119  	other := lxdclient.NewInstance(lxdclient.InstanceSummary{}, nil)
   120  	s.Client.Insts = []lxdclient.Instance{*s.RawInstance, *other}
   121  
   122  	ids, err := s.Env.ControllerInstances(coretesting.ControllerTag.Id())
   123  	c.Assert(err, jc.ErrorIsNil)
   124  
   125  	c.Check(ids, jc.DeepEquals, []instance.Id{"spam"})
   126  }