github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/cloudsigma/instance_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cloudsigma 5 6 import ( 7 "strings" 8 9 "github.com/altoros/gosigma" 10 "github.com/altoros/gosigma/data" 11 "github.com/altoros/gosigma/mock" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/core/instance" 15 "github.com/juju/juju/environs/context" 16 "github.com/juju/juju/network" 17 "github.com/juju/juju/testing" 18 ) 19 20 type instanceSuite struct { 21 testing.BaseSuite 22 inst *sigmaInstance 23 instWithoutIP *sigmaInstance 24 25 callCtx context.ProviderCallContext 26 } 27 28 var _ = gc.Suite(&instanceSuite{}) 29 30 func (s *instanceSuite) SetUpSuite(c *gc.C) { 31 s.BaseSuite.SetUpSuite(c) 32 mock.Start() 33 } 34 35 func (s *instanceSuite) TearDownSuite(c *gc.C) { 36 mock.Stop() 37 s.BaseSuite.TearDownSuite(c) 38 } 39 40 func (s *instanceSuite) SetUpTest(c *gc.C) { 41 s.BaseSuite.SetUpTest(c) 42 43 cli, err := gosigma.NewClient(mock.Endpoint(""), mock.TestUser, mock.TestPassword, nil) 44 c.Assert(err, gc.IsNil) 45 46 mock.ResetServers() 47 48 ds, err := data.ReadServer(strings.NewReader(jsonInstanceData)) 49 c.Assert(err, gc.IsNil) 50 mock.AddServer(ds) 51 52 mock.AddServer(&data.Server{ 53 Resource: data.Resource{URI: "uri", UUID: "uuid-no-ip"}, 54 }) 55 56 server, err := cli.Server("f4ec5097-121e-44a7-a207-75bc02163260") 57 c.Assert(err, gc.IsNil) 58 c.Assert(server, gc.NotNil) 59 s.inst = &sigmaInstance{server} 60 61 server, err = cli.Server("uuid-no-ip") 62 c.Assert(err, gc.IsNil) 63 c.Assert(server, gc.NotNil) 64 s.instWithoutIP = &sigmaInstance{server} 65 66 s.callCtx = context.NewCloudCallContext() 67 } 68 69 func (s *instanceSuite) TearDownTest(c *gc.C) { 70 mock.ResetServers() 71 s.BaseSuite.TearDownTest(c) 72 } 73 74 func (s *instanceSuite) TestInstanceId(c *gc.C) { 75 c.Check(s.inst.Id(), gc.Equals, instance.Id("f4ec5097-121e-44a7-a207-75bc02163260")) 76 } 77 78 func (s *instanceSuite) TestInstanceStatus(c *gc.C) { 79 c.Check(s.inst.Status(s.callCtx).Message, gc.Equals, "running") 80 } 81 82 func (s *instanceSuite) TestInstanceAddresses(c *gc.C) { 83 addrs, err := s.inst.Addresses(s.callCtx) 84 c.Check(addrs, gc.HasLen, 1) 85 c.Check(err, gc.IsNil) 86 a := addrs[0] 87 c.Check(a.Value, gc.Equals, "178.22.70.33") 88 c.Check(a.Type, gc.Equals, network.IPv4Address) 89 c.Check(a.Scope, gc.Equals, network.ScopePublic) 90 91 addrs, err = s.instWithoutIP.Addresses(s.callCtx) 92 c.Check(err, gc.IsNil) 93 c.Check(len(addrs), gc.Equals, 0) 94 } 95 96 func (s *instanceSuite) TestIngressRules(c *gc.C) { 97 c.Check(s.inst.OpenPorts(s.callCtx, "", nil), gc.ErrorMatches, "OpenPorts not implemented") 98 c.Check(s.inst.ClosePorts(s.callCtx, "", nil), gc.ErrorMatches, "ClosePorts not implemented") 99 100 _, err := s.inst.IngressRules(s.callCtx, "") 101 c.Check(err, gc.ErrorMatches, "InstanceRules not implemented") 102 } 103 104 func (s *instanceSuite) TestInstanceHardware(c *gc.C) { 105 hw, err := s.inst.hardware("64", 1000000) 106 c.Assert(err, gc.IsNil) 107 c.Assert(hw, gc.NotNil) 108 109 c.Check(*hw.Arch, gc.Equals, "64") 110 111 c.Check(hw.Mem, gc.NotNil) 112 if hw.Mem != nil { 113 c.Check(*hw.Mem, gc.Equals, uint64(2048)) 114 } 115 116 c.Check(hw.RootDisk, gc.IsNil) 117 118 c.Check(hw.CpuCores, gc.NotNil) 119 if hw.CpuCores != nil { 120 c.Check(*hw.CpuCores, gc.Equals, uint64(1)) 121 } 122 123 c.Check(hw.CpuPower, gc.NotNil) 124 c.Check(*hw.CpuPower, gc.Equals, uint64(2000)) 125 126 c.Check(hw.Tags, gc.IsNil) 127 } 128 129 const jsonInstanceData = `{ 130 "context": true, 131 "cpu": 2000, 132 "cpu_model": null, 133 "cpus_instead_of_cores": false, 134 "drives": [ 135 { 136 "boot_order": 1, 137 "dev_channel": "0:0", 138 "device": "virtio", 139 "drive": { 140 "resource_uri": "/api/2.0/drives/f968dc48-25a0-4d46-8f16-e12e073e1fe6/", 141 "uuid": "f968dc48-25a0-4d46-8f16-e12e073e1fe6" 142 }, 143 "runtime": { 144 "io": { 145 "bytes_read": 82980352, 146 "bytes_written": 189440, 147 "count_flush": 0, 148 "count_read": 3952, 149 "count_written": 19, 150 "total_time_ns_flush": 0, 151 "total_time_ns_read": 4435322816, 152 "total_time_ns_write": 123240430 153 } 154 } 155 } 156 ], 157 "enable_numa": false, 158 "hv_relaxed": false, 159 "hv_tsc": false, 160 "jobs": [], 161 "mem": 2147483648, 162 "meta": { 163 "description": "test-description", 164 "ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDDiwTGBsmFKBYHcKaVy5IgsYBR4XVYLS6KP/NKClE7gONlIGURE3+/45BX8TfHJHM5WTN8NBqJejKDHqwfyueR1f2VGoPkJxODGt/X/ZDNftLZLYwPd2DfDBs27ahOadZCk4Cl5l7mU0aoE74UnIcQoNPl6w7axkIFTIXr8+0HMk8DFB0iviBSJK118p1RGwhsoA1Hudn1CsgqARGPmNn6mxwvmQfQY7hZxZoOH9WMcvkNZ7rAFrwS/BuvEpEXkoC95K/JDPvmQVVJk7we+WeHfTYSmApkDFcSaypyjL2HOV8pvE+VntcIIhZccHiOubyjsBAx5aoTI+ueCsoz5AL1 maxim.perenesenko@altoros.com" 165 }, 166 "name": "LiveTest-srv-17-54-51-999999999", 167 "nics": [ 168 { 169 "boot_order": null, 170 "firewall_policy": null, 171 "ip_v4_conf": { 172 "conf": "dhcp", 173 "ip": null 174 }, 175 "ip_v6_conf": null, 176 "mac": "22:ab:bf:26:e1:be", 177 "model": "virtio", 178 "runtime": { 179 "interface_type": "public", 180 "io": { 181 "bytes_recv": 0, 182 "bytes_sent": 17540, 183 "packets_recv": 0, 184 "packets_sent": 256 185 }, 186 "ip_v4": { 187 "resource_uri": "/api/2.0/ips/178.22.70.33/", 188 "uuid": "178.22.70.33" 189 }, 190 "ip_v6": null 191 }, 192 "vlan": null 193 }, 194 { 195 "boot_order": null, 196 "firewall_policy": null, 197 "ip_v4_conf": null, 198 "ip_v6_conf": null, 199 "mac": "22:9e:e7:d7:86:94", 200 "model": "virtio", 201 "runtime": { 202 "interface_type": "private", 203 "io": { 204 "bytes_recv": 0, 205 "bytes_sent": 1046, 206 "packets_recv": 0, 207 "packets_sent": 13 208 }, 209 "ip_v4": null, 210 "ip_v6": null 211 }, 212 "vlan": { 213 "resource_uri": "/api/2.0/vlans/5bc05e7e-6555-4f40-add8-3b8e91447702/", 214 "uuid": "5bc05e7e-6555-4f40-add8-3b8e91447702" 215 } 216 } 217 ], 218 "owner": { 219 "resource_uri": "/api/2.0/user/c25eb0ed-161f-44f4-ac1d-d584ce3a5312/", 220 "uuid": "c25eb0ed-161f-44f4-ac1d-d584ce3a5312" 221 }, 222 "requirements": [], 223 "resource_uri": "/api/2.0/servers/f4ec5097-121e-44a7-a207-75bc02163260/", 224 "runtime": { 225 "active_since": "2014-04-24T14:56:58+00:00", 226 "nics": [ 227 { 228 "interface_type": "public", 229 "io": { 230 "bytes_recv": 0, 231 "bytes_sent": 17540, 232 "packets_recv": 0, 233 "packets_sent": 256 234 }, 235 "ip_v4": { 236 "resource_uri": "/api/2.0/ips/178.22.70.33/", 237 "uuid": "178.22.70.33" 238 }, 239 "ip_v6": null, 240 "mac": "22:ab:bf:26:e1:be" 241 }, 242 { 243 "interface_type": "private", 244 "io": { 245 "bytes_recv": 0, 246 "bytes_sent": 1046, 247 "packets_recv": 0, 248 "packets_sent": 13 249 }, 250 "ip_v4": null, 251 "ip_v6": null, 252 "mac": "22:9e:e7:d7:86:94" 253 } 254 ] 255 }, 256 "smp": 1, 257 "status": "running", 258 "tags": [], 259 "uuid": "f4ec5097-121e-44a7-a207-75bc02163260", 260 "vnc_password": "test-vnc-password" 261 }`