github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/maas/maas2instance_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 10 "github.com/juju/gomaasapi" 11 "github.com/juju/juju/core/instance" 12 "github.com/juju/juju/core/status" 13 "github.com/juju/juju/network" 14 ) 15 16 type maas2InstanceSuite struct { 17 maas2Suite 18 } 19 20 var _ = gc.Suite(&maas2InstanceSuite{}) 21 22 func (s *maas2InstanceSuite) TestString(c *gc.C) { 23 machine := &fakeMachine{hostname: "peewee", systemID: "herman"} 24 instance := &maas2Instance{machine: machine} 25 c.Assert(instance.String(), gc.Equals, "peewee:herman") 26 } 27 28 func (s *maas2InstanceSuite) TestID(c *gc.C) { 29 machine := &fakeMachine{systemID: "herman"} 30 thing := &maas2Instance{machine: machine} 31 c.Assert(thing.Id(), gc.Equals, instance.Id("herman")) 32 } 33 34 func (s *maas2InstanceSuite) TestAddresses(c *gc.C) { 35 vlan := fakeVLAN{vid: 66} 36 subnet := fakeSubnet{ 37 id: 99, 38 vlan: vlan, 39 cidr: "192.168.10.0/24", 40 space: "freckles", 41 } 42 machine := &fakeMachine{ 43 systemID: "1", 44 interfaceSet: []gomaasapi.Interface{ 45 &fakeInterface{ 46 id: 91, 47 name: "eth0", 48 type_: "physical", 49 enabled: true, 50 macAddress: "52:54:00:70:9b:fe", 51 vlan: vlan, 52 links: []gomaasapi.Link{ 53 &fakeLink{ 54 id: 436, 55 subnet: &subnet, 56 ipAddress: "192.168.10.1", 57 mode: "static", 58 }, 59 }, 60 parents: []string{}, 61 children: []string{}, 62 }, 63 }, 64 } 65 controller := &fakeController{ 66 spaces: []gomaasapi.Space{ 67 fakeSpace{ 68 name: "freckles", 69 id: 4567, 70 subnets: []gomaasapi.Subnet{subnet}, 71 }, 72 }, 73 machines: []gomaasapi.Machine{machine}, 74 } 75 instance := &maas2Instance{machine: machine, environ: s.makeEnviron(c, controller)} 76 addresses, err := instance.Addresses(s.callCtx) 77 78 expectedAddresses := []network.Address{ 79 newAddressOnSpaceWithId("freckles", network.Id("4567"), "192.168.10.1"), 80 } 81 82 c.Assert(err, jc.ErrorIsNil) 83 c.Assert(addresses, jc.SameContents, expectedAddresses) 84 } 85 86 func (s *maas2InstanceSuite) TestZone(c *gc.C) { 87 machine := &fakeMachine{zoneName: "inflatable"} 88 instance := &maas2Instance{machine: machine} 89 zone, err := instance.zone() 90 c.Assert(err, jc.ErrorIsNil) 91 c.Assert(zone, gc.Equals, "inflatable") 92 } 93 94 func (s *maas2InstanceSuite) TestStatusSuccess(c *gc.C) { 95 machine := &fakeMachine{statusMessage: "Wexler", statusName: "Deploying"} 96 thing := &maas2Instance{machine: machine} 97 result := thing.Status(s.callCtx) 98 c.Assert(result, jc.DeepEquals, instance.Status{status.Allocating, "Deploying: Wexler"}) 99 } 100 101 func (s *maas2InstanceSuite) TestStatusError(c *gc.C) { 102 machine := &fakeMachine{statusMessage: "", statusName: ""} 103 thing := &maas2Instance{machine: machine} 104 result := thing.Status(s.callCtx) 105 c.Assert(result, jc.DeepEquals, instance.Status{"", "error in getting status"}) 106 } 107 108 func (s *maas2InstanceSuite) TestHostname(c *gc.C) { 109 machine := &fakeMachine{hostname: "saul-goodman"} 110 thing := &maas2Instance{machine: machine} 111 result, err := thing.hostname() 112 c.Assert(err, jc.ErrorIsNil) 113 c.Assert(result, gc.Equals, "saul-goodman") 114 } 115 116 func (s *maas2InstanceSuite) TestHardwareCharacteristics(c *gc.C) { 117 machine := &fakeMachine{ 118 cpuCount: 3, 119 memory: 4, 120 architecture: "foo/bam", 121 zoneName: "bar", 122 tags: []string{"foo", "bar"}, 123 } 124 thing := &maas2Instance{machine: machine} 125 arch := "foo" 126 cpu := uint64(3) 127 mem := uint64(4) 128 zone := "bar" 129 tags := []string{"foo", "bar"} 130 expected := &instance.HardwareCharacteristics{ 131 Arch: &arch, 132 CpuCores: &cpu, 133 Mem: &mem, 134 AvailabilityZone: &zone, 135 Tags: &tags, 136 } 137 result, err := thing.hardwareCharacteristics() 138 c.Assert(err, jc.ErrorIsNil) 139 c.Assert(result, jc.DeepEquals, expected) 140 }