test_compute_service.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import ipaddress
  2. import six
  3. from cloudbridge.cloud.factory import ProviderList
  4. from cloudbridge.cloud.interfaces import InstanceState
  5. from cloudbridge.cloud.interfaces import InvalidConfigurationException
  6. from cloudbridge.cloud.interfaces.exceptions import WaitStateException
  7. from cloudbridge.cloud.interfaces.resources import Instance
  8. from cloudbridge.cloud.interfaces.resources import SnapshotState
  9. from cloudbridge.cloud.interfaces.resources import VMType
  10. from test import helpers
  11. from test.helpers import ProviderTestBase
  12. from test.helpers import standard_interface_tests as sit
  13. class CloudComputeServiceTestCase(ProviderTestBase):
  14. _multiprocess_can_split_ = True
  15. @helpers.skipIfNoService(['compute.instances', 'networking.networks'])
  16. def test_crud_instance(self):
  17. label = "cb-instcrud-{0}".format(helpers.get_uuid())
  18. # Declare these variables and late binding will allow
  19. # the cleanup method access to the most current values
  20. subnet = None
  21. def create_inst(label):
  22. # Also test whether sending in an empty_dict for user_data
  23. # results in an automatic conversion to string.
  24. return helpers.get_test_instance(self.provider, label,
  25. subnet=subnet, user_data={})
  26. def cleanup_inst(inst):
  27. if inst:
  28. inst.delete()
  29. inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN])
  30. inst.refresh()
  31. self.assertTrue(
  32. inst.state == InstanceState.UNKNOWN,
  33. "Instance.state must be unknown when refreshing after a "
  34. "delete but got %s"
  35. % inst.state)
  36. def check_deleted(inst):
  37. deleted_inst = self.provider.compute.instances.get(
  38. inst.id)
  39. self.assertTrue(
  40. deleted_inst is None or deleted_inst.state in (
  41. InstanceState.DELETED,
  42. InstanceState.UNKNOWN),
  43. "Instance %s should have been deleted but still exists." %
  44. label)
  45. subnet = helpers.get_or_create_default_subnet(self.provider)
  46. sit.check_crud(self, self.provider.compute.instances, Instance,
  47. "cb-instcrud", create_inst, cleanup_inst,
  48. custom_check_delete=check_deleted)
  49. def _is_valid_ip(self, address):
  50. try:
  51. ipaddress.ip_address(address)
  52. except ValueError:
  53. return False
  54. return True
  55. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  56. 'security.vm_firewalls',
  57. 'security.key_pairs'])
  58. def test_instance_properties(self):
  59. label = "cb-inst-props-{0}".format(helpers.get_uuid())
  60. # Declare these variables and late binding will allow
  61. # the cleanup method access to the most current values
  62. test_instance = None
  63. fw = None
  64. kp = None
  65. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  66. test_instance, fw, kp)):
  67. subnet = helpers.get_or_create_default_subnet(self.provider)
  68. net = subnet.network
  69. kp = self.provider.security.key_pairs.create(name=label)
  70. fw = self.provider.security.vm_firewalls.create(
  71. label=label, description=label, network_id=net.id)
  72. test_instance = helpers.get_test_instance(self.provider,
  73. label, key_pair=kp,
  74. vm_firewalls=[fw],
  75. subnet=subnet)
  76. self.assertEqual(
  77. test_instance.label, label,
  78. "Instance label {0} is not equal to the expected label"
  79. " {1}".format(test_instance.label, label))
  80. image_id = helpers.get_provider_test_data(self.provider, "image")
  81. self.assertEqual(test_instance.image_id, image_id,
  82. "Image id {0} is not equal to the expected id"
  83. " {1}".format(test_instance.image_id, image_id))
  84. self.assertIsInstance(test_instance.zone_id,
  85. six.string_types)
  86. self.assertEqual(
  87. test_instance.image_id,
  88. helpers.get_provider_test_data(self.provider, "image"))
  89. self.assertIsInstance(test_instance.public_ips, list)
  90. if test_instance.public_ips:
  91. self.assertTrue(
  92. test_instance.public_ips[0], "public ip should contain a"
  93. " valid value if a list of public_ips exist")
  94. self.assertIsInstance(test_instance.private_ips, list)
  95. self.assertTrue(test_instance.private_ips[0], "private ip should"
  96. " contain a valid value")
  97. self.assertEqual(
  98. test_instance.key_pair_id,
  99. kp.id)
  100. self.assertIsInstance(test_instance.vm_firewalls, list)
  101. self.assertEqual(
  102. test_instance.vm_firewalls[0],
  103. fw)
  104. self.assertIsInstance(test_instance.vm_firewall_ids, list)
  105. self.assertEqual(
  106. test_instance.vm_firewall_ids[0],
  107. fw.id)
  108. # Must have either a public or a private ip
  109. ip_private = test_instance.private_ips[0] \
  110. if test_instance.private_ips else None
  111. ip_address = test_instance.public_ips[0] \
  112. if test_instance.public_ips and test_instance.public_ips[0] \
  113. else ip_private
  114. # Convert to unicode for py27 compatibility with ipaddress()
  115. ip_address = u"{}".format(ip_address)
  116. self.assertIsNotNone(
  117. ip_address,
  118. "Instance must have either a public IP or a private IP")
  119. self.assertTrue(
  120. self._is_valid_ip(ip_address),
  121. "Instance must have a valid IP address. Got: %s" % ip_address)
  122. self.assertIsInstance(test_instance.vm_type_id,
  123. six.string_types)
  124. vm_type = self.provider.compute.vm_types.get(
  125. test_instance.vm_type_id)
  126. self.assertEqual(
  127. vm_type, test_instance.vm_type,
  128. "VM type {0} does not match expected type {1}".format(
  129. vm_type.name, test_instance.vm_type))
  130. self.assertIsInstance(vm_type, VMType)
  131. expected_type = helpers.get_provider_test_data(self.provider,
  132. 'vm_type')
  133. self.assertEqual(
  134. vm_type.name, expected_type,
  135. "VM type {0} does not match expected type {1}".format(
  136. vm_type.name, expected_type))
  137. find_zone = [zone for zone in
  138. self.provider.compute.regions.current.zones
  139. if zone.id == test_instance.zone_id]
  140. self.assertEqual(len(find_zone), 1,
  141. "Instance's placement zone could not be "
  142. " found in zones list")
  143. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  144. 'compute.vm_types'])
  145. def test_block_device_mapping_launch_config(self):
  146. lc = self.provider.compute.instances.create_launch_config()
  147. # specifying an invalid size should raise
  148. # an exception
  149. with self.assertRaises(InvalidConfigurationException):
  150. lc.add_volume_device(size=-1)
  151. # Attempting to add a blank volume without specifying a size
  152. # should raise an exception
  153. with self.assertRaises(InvalidConfigurationException):
  154. lc.add_volume_device(source=None)
  155. # block_devices should be empty so far
  156. self.assertListEqual(
  157. lc.block_devices, [], "No block devices should have been"
  158. " added to mappings list since the configuration was"
  159. " invalid")
  160. # Add a new volume
  161. lc.add_volume_device(size=1, delete_on_terminate=True)
  162. # Override root volume size
  163. image_id = helpers.get_provider_test_data(self.provider, "image")
  164. img = self.provider.compute.images.get(image_id)
  165. # The size should be greater then the ami size
  166. # and therefore, img.min_disk is used.
  167. lc.add_volume_device(
  168. is_root=True,
  169. source=img,
  170. size=img.min_disk if img and img.min_disk else 30,
  171. delete_on_terminate=True)
  172. # Attempting to add more than one root volume should raise an
  173. # exception.
  174. with self.assertRaises(InvalidConfigurationException):
  175. lc.add_volume_device(size=1, is_root=True)
  176. # Attempting to add an incorrect source should raise an exception
  177. with self.assertRaises(InvalidConfigurationException):
  178. lc.add_volume_device(
  179. source="invalid_source",
  180. delete_on_terminate=True)
  181. # Add all available ephemeral devices
  182. vm_type_name = helpers.get_provider_test_data(
  183. self.provider,
  184. "vm_type")
  185. vm_type = self.provider.compute.vm_types.find(
  186. name=vm_type_name)[0]
  187. for _ in range(vm_type.num_ephemeral_disks):
  188. lc.add_ephemeral_device()
  189. # block_devices should be populated
  190. self.assertTrue(
  191. len(lc.block_devices) == 2 + vm_type.num_ephemeral_disks,
  192. "Expected %d total block devices bit found %d" %
  193. (2 + vm_type.num_ephemeral_disks, len(lc.block_devices)))
  194. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  195. 'compute.vm_types', 'storage.volumes'])
  196. def test_block_device_mapping_attachments(self):
  197. label = "cb-blkattch-{0}".format(helpers.get_uuid())
  198. if self.provider.PROVIDER_ID == ProviderList.OPENSTACK:
  199. raise self.skipTest("Not running BDM tests because OpenStack is"
  200. " not stable enough yet")
  201. test_vol = self.provider.storage.volumes.create(
  202. label, 1,
  203. helpers.get_provider_test_data(self.provider,
  204. "placement"))
  205. with helpers.cleanup_action(lambda: test_vol.delete()):
  206. test_vol.wait_till_ready()
  207. test_snap = test_vol.create_snapshot(label=label,
  208. description=label)
  209. def cleanup_snap(snap):
  210. if snap:
  211. snap.delete()
  212. snap.wait_for([SnapshotState.UNKNOWN],
  213. terminal_states=[SnapshotState.ERROR])
  214. with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  215. test_snap.wait_till_ready()
  216. lc = self.provider.compute.instances.create_launch_config()
  217. # Add a new blank volume
  218. lc.add_volume_device(size=1, delete_on_terminate=True)
  219. # Attach an existing volume
  220. lc.add_volume_device(size=1, source=test_vol,
  221. delete_on_terminate=True)
  222. # Add a new volume based on a snapshot
  223. lc.add_volume_device(size=1, source=test_snap,
  224. delete_on_terminate=True)
  225. # Override root volume size
  226. image_id = helpers.get_provider_test_data(
  227. self.provider,
  228. "image")
  229. img = self.provider.compute.images.get(image_id)
  230. # The size should be greater then the ami size
  231. # and therefore, img.min_disk is used.
  232. lc.add_volume_device(
  233. is_root=True,
  234. source=img,
  235. size=img.min_disk if img and img.min_disk else 30,
  236. delete_on_terminate=True)
  237. # Add all available ephemeral devices
  238. vm_type_name = helpers.get_provider_test_data(
  239. self.provider,
  240. "vm_type")
  241. vm_type = self.provider.compute.vm_types.find(
  242. name=vm_type_name)[0]
  243. for _ in range(vm_type.num_ephemeral_disks):
  244. lc.add_ephemeral_device()
  245. subnet = helpers.get_or_create_default_subnet(
  246. self.provider)
  247. inst = helpers.create_test_instance(
  248. self.provider,
  249. label,
  250. subnet=subnet,
  251. launch_config=lc)
  252. with helpers.cleanup_action(lambda:
  253. helpers.delete_test_instance(
  254. inst)):
  255. try:
  256. inst.wait_till_ready()
  257. except WaitStateException as e:
  258. self.fail("The block device mapped launch did not "
  259. " complete successfully: %s" % e)
  260. # TODO: Check instance attachments and make sure they
  261. # correspond to requested mappings
  262. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  263. 'security.vm_firewalls'])
  264. def test_instance_methods(self):
  265. label = "cb-instmethods-{0}".format(helpers.get_uuid())
  266. # Declare these variables and late binding will allow
  267. # the cleanup method access to the most current values
  268. net = None
  269. test_inst = None
  270. fw = None
  271. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  272. instance=test_inst, vm_firewall=fw, network=net)):
  273. net = self.provider.networking.networks.create(
  274. label=label, cidr_block='10.0.0.0/16')
  275. cidr = '10.0.1.0/24'
  276. subnet = net.create_subnet(label=label, cidr_block=cidr,
  277. zone=helpers.get_provider_test_data(
  278. self.provider,
  279. 'placement'))
  280. test_inst = helpers.get_test_instance(self.provider, label,
  281. subnet=subnet)
  282. fw = self.provider.security.vm_firewalls.create(
  283. label=label, description=label, network_id=net.id)
  284. # Check adding a VM firewall to a running instance
  285. test_inst.add_vm_firewall(fw)
  286. test_inst.refresh()
  287. self.assertTrue(
  288. fw in test_inst.vm_firewalls, "Expected VM firewall '%s'"
  289. " to be among instance vm_firewalls: [%s]" %
  290. (fw, test_inst.vm_firewalls))
  291. # Check removing a VM firewall from a running instance
  292. test_inst.remove_vm_firewall(fw)
  293. test_inst.refresh()
  294. self.assertTrue(
  295. fw not in test_inst.vm_firewalls, "Expected VM firewall"
  296. " '%s' to be removed from instance vm_firewalls: [%s]" %
  297. (fw, test_inst.vm_firewalls))
  298. # check floating ips
  299. router = self.provider.networking.routers.create(label, net)
  300. gateway = net.gateways.get_or_create_inet_gateway()
  301. def cleanup_router(router, gateway):
  302. with helpers.cleanup_action(lambda: router.delete()):
  303. with helpers.cleanup_action(lambda: gateway.delete()):
  304. router.detach_subnet(subnet)
  305. router.detach_gateway(gateway)
  306. with helpers.cleanup_action(lambda: cleanup_router(router,
  307. gateway)):
  308. router.attach_subnet(subnet)
  309. router.attach_gateway(gateway)
  310. # check whether adding an elastic ip works
  311. fip = gateway.floating_ips.create()
  312. self.assertFalse(
  313. fip.in_use,
  314. "Newly created floating IP address should not be in use.")
  315. with helpers.cleanup_action(lambda: fip.delete()):
  316. with helpers.cleanup_action(
  317. lambda: test_inst.remove_floating_ip(fip)):
  318. test_inst.add_floating_ip(fip)
  319. test_inst.refresh()
  320. # On Devstack, FloatingIP is listed under private_ips.
  321. self.assertIn(fip.public_ip, test_inst.public_ips +
  322. test_inst.private_ips)
  323. fip.refresh()
  324. self.assertTrue(
  325. fip.in_use,
  326. "Attached floating IP address should be in use.")
  327. test_inst.refresh()
  328. test_inst.reboot()
  329. test_inst.wait_till_ready()
  330. self.assertNotIn(
  331. fip.public_ip,
  332. test_inst.public_ips + test_inst.private_ips)