test_compute_service.py 17 KB

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