github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/provider/maas/instance_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package maas 5 6 import ( 7 "fmt" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/network" 13 ) 14 15 type instanceTest struct { 16 providerSuite 17 } 18 19 var _ = gc.Suite(&instanceTest{}) 20 21 func (s *instanceTest) TestId(c *gc.C) { 22 jsonValue := `{"system_id": "system_id", "test": "test"}` 23 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 24 resourceURI, _ := obj.GetField("resource_uri") 25 instance := maasInstance{&obj} 26 27 c.Check(string(instance.Id()), gc.Equals, resourceURI) 28 } 29 30 func (s *instanceTest) TestString(c *gc.C) { 31 jsonValue := `{"hostname": "thethingintheplace", "system_id": "system_id", "test": "test"}` 32 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 33 instance := &maasInstance{&obj} 34 hostname, err := instance.hostname() 35 c.Assert(err, jc.ErrorIsNil) 36 expected := hostname + ":" + string(instance.Id()) 37 c.Assert(fmt.Sprint(instance), gc.Equals, expected) 38 } 39 40 func (s *instanceTest) TestStringWithoutHostname(c *gc.C) { 41 // For good measure, test what happens if we don't have a hostname. 42 jsonValue := `{"system_id": "system_id", "test": "test"}` 43 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 44 instance := &maasInstance{&obj} 45 _, err := instance.hostname() 46 c.Assert(err, gc.NotNil) 47 expected := fmt.Sprintf("<DNSName failed: %q>", err) + ":" + string(instance.Id()) 48 c.Assert(fmt.Sprint(instance), gc.Equals, expected) 49 } 50 51 func (s *instanceTest) TestAddresses(c *gc.C) { 52 jsonValue := `{ 53 "hostname": "testing.invalid", 54 "system_id": "system_id", 55 "ip_addresses": [ "1.2.3.4", "fe80::d806:dbff:fe23:1199" ] 56 }` 57 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 58 inst := maasInstance{&obj} 59 60 expected := []network.Address{ 61 network.NewScopedAddress("testing.invalid", network.ScopePublic), 62 network.NewScopedAddress("testing.invalid", network.ScopeCloudLocal), 63 network.NewAddress("1.2.3.4"), 64 network.NewAddress("fe80::d806:dbff:fe23:1199"), 65 } 66 67 addr, err := inst.Addresses() 68 69 c.Assert(err, jc.ErrorIsNil) 70 c.Check(addr, gc.DeepEquals, expected) 71 } 72 73 func (s *instanceTest) TestAddressesMissing(c *gc.C) { 74 // Older MAAS versions do not have ip_addresses returned, for these 75 // just the DNS name should be returned without error. 76 jsonValue := `{ 77 "hostname": "testing.invalid", 78 "system_id": "system_id" 79 }` 80 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 81 inst := maasInstance{&obj} 82 83 addr, err := inst.Addresses() 84 c.Assert(err, jc.ErrorIsNil) 85 c.Check(addr, gc.DeepEquals, []network.Address{ 86 {Value: "testing.invalid", Type: network.HostName, Scope: network.ScopePublic}, 87 {Value: "testing.invalid", Type: network.HostName, Scope: network.ScopeCloudLocal}, 88 }) 89 } 90 91 func (s *instanceTest) TestAddressesInvalid(c *gc.C) { 92 jsonValue := `{ 93 "hostname": "testing.invalid", 94 "system_id": "system_id", 95 "ip_addresses": "incompatible" 96 }` 97 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 98 inst := maasInstance{&obj} 99 100 _, err := inst.Addresses() 101 c.Assert(err, gc.NotNil) 102 } 103 104 func (s *instanceTest) TestAddressesInvalidContents(c *gc.C) { 105 jsonValue := `{ 106 "hostname": "testing.invalid", 107 "system_id": "system_id", 108 "ip_addresses": [42] 109 }` 110 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 111 inst := maasInstance{&obj} 112 113 _, err := inst.Addresses() 114 c.Assert(err, gc.NotNil) 115 } 116 117 func (s *instanceTest) TestHardwareCharacteristics(c *gc.C) { 118 jsonValue := `{ 119 "system_id": "system_id", 120 "architecture": "amd64/generic", 121 "cpu_count": 6, 122 "memory": 16384 123 }` 124 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 125 inst := maasInstance{&obj} 126 hc, err := inst.hardwareCharacteristics() 127 c.Assert(err, jc.ErrorIsNil) 128 c.Assert(hc, gc.NotNil) 129 c.Assert(hc.String(), gc.Equals, `arch=amd64 cpu-cores=6 mem=16384M`) 130 } 131 132 func (s *instanceTest) TestHardwareCharacteristicsWithTags(c *gc.C) { 133 jsonValue := `{ 134 "system_id": "system_id", 135 "architecture": "amd64/generic", 136 "cpu_count": 6, 137 "memory": 16384, 138 "tag_names": ["a", "b"] 139 }` 140 obj := s.testMAASObject.TestServer.NewNode(jsonValue) 141 inst := maasInstance{&obj} 142 hc, err := inst.hardwareCharacteristics() 143 c.Assert(err, jc.ErrorIsNil) 144 c.Assert(hc, gc.NotNil) 145 c.Assert(hc.String(), gc.Equals, `arch=amd64 cpu-cores=6 mem=16384M tags=a,b`) 146 } 147 148 func (s *instanceTest) TestHardwareCharacteristicsMissing(c *gc.C) { 149 s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "cpu_count": 6, "memory": 16384}`, 150 `error determining architecture: Requested string, got <nil>.`) 151 s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "amd64", "memory": 16384}`, 152 `error determining cpu count: Requested float64, got <nil>.`) 153 s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "armhf", "cpu_count": 6}`, 154 `error determining available memory: Requested float64, got <nil>.`) 155 s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "armhf", "cpu_count": 6, "memory": 1, "tag_names": "wot"}`, 156 `error determining tag names: Requested array, got string.`) 157 } 158 159 func (s *instanceTest) testHardwareCharacteristicsMissing(c *gc.C, json, expect string) { 160 obj := s.testMAASObject.TestServer.NewNode(json) 161 inst := maasInstance{&obj} 162 _, err := inst.hardwareCharacteristics() 163 c.Assert(err, gc.ErrorMatches, expect) 164 }