github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/provider/vsphere/environ_instance_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build !gccgo 5 6 package vsphere_test 7 8 import ( 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/instance" 14 "github.com/juju/juju/provider/vsphere" 15 ) 16 17 type environInstanceSuite struct { 18 vsphere.BaseSuite 19 namespace instance.Namespace 20 } 21 22 var _ = gc.Suite(&environInstanceSuite{}) 23 24 func (s *environInstanceSuite) SetUpTest(c *gc.C) { 25 s.BaseSuite.SetUpTest(c) 26 namespace, err := instance.NewNamespace(s.Env.Config().UUID()) 27 c.Assert(err, jc.ErrorIsNil) 28 s.namespace = namespace 29 } 30 31 func (s *environInstanceSuite) machineName(c *gc.C, id string) string { 32 name, err := s.namespace.Hostname(id) 33 c.Assert(err, jc.ErrorIsNil) 34 return name 35 } 36 37 func (s *environInstanceSuite) TestInstances(c *gc.C) { 38 client := vsphere.ExposeEnvFakeClient(s.Env) 39 client.SetPropertyProxyHandler("FakeDatacenter", vsphere.RetrieveDatacenterProperties) 40 vmName1 := s.machineName(c, "1") 41 vmName2 := s.machineName(c, "2") 42 s.FakeInstancesWithResourcePool(client, vsphere.InstRp{Inst: vmName1, Rp: "rp1"}, vsphere.InstRp{Inst: vmName2, Rp: "rp2"}) 43 44 instances, err := s.Env.Instances([]instance.Id{instance.Id(vmName1), instance.Id(vmName2)}) 45 46 c.Assert(err, jc.ErrorIsNil) 47 c.Assert(len(instances), gc.Equals, 2) 48 c.Assert(string(instances[0].Id()), gc.Equals, vmName1) 49 c.Assert(string(instances[1].Id()), gc.Equals, vmName2) 50 } 51 52 func (s *environInstanceSuite) TestInstancesReturnNoInstances(c *gc.C) { 53 client := vsphere.ExposeEnvFakeClient(s.Env) 54 client.SetPropertyProxyHandler("FakeDatacenter", vsphere.RetrieveDatacenterProperties) 55 s.FakeInstancesWithResourcePool(client, vsphere.InstRp{Inst: "Some name that don't match naming convention", Rp: "rp1"}) 56 57 _, err := s.Env.Instances([]instance.Id{instance.Id("Some other name")}) 58 59 c.Assert(err, gc.Equals, environs.ErrNoInstances) 60 } 61 62 func (s *environInstanceSuite) TestInstancesReturnPartialInstances(c *gc.C) { 63 client := vsphere.ExposeEnvFakeClient(s.Env) 64 client.SetPropertyProxyHandler("FakeDatacenter", vsphere.RetrieveDatacenterProperties) 65 vmName1 := s.machineName(c, "1") 66 vmName2 := s.machineName(c, "2") 67 s.FakeInstancesWithResourcePool(client, vsphere.InstRp{Inst: vmName1, Rp: "rp1"}, vsphere.InstRp{Inst: "Some inst", Rp: "rp2"}) 68 69 _, err := s.Env.Instances([]instance.Id{instance.Id(vmName1), instance.Id(vmName2)}) 70 71 c.Assert(err, gc.Equals, environs.ErrPartialInstances) 72 }