test_compute_service.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import ipaddress
  2. from test import helpers
  3. from test.helpers import ProviderTestBase
  4. from test.helpers import standard_interface_tests as sit
  5. from cloudbridge.cloud.factory import ProviderList
  6. from cloudbridge.cloud.interfaces import InstanceState
  7. from cloudbridge.cloud.interfaces import InvalidConfigurationException
  8. from cloudbridge.cloud.interfaces.exceptions import WaitStateException
  9. from cloudbridge.cloud.interfaces.resources import Instance
  10. from cloudbridge.cloud.interfaces.resources import SnapshotState
  11. from cloudbridge.cloud.interfaces.resources import VMType
  12. import six
  13. class CloudComputeServiceTestCase(ProviderTestBase):
  14. _multiprocess_can_split_ = True
  15. @helpers.skipIfNoService(['compute.instances', 'networking.networks'])
  16. def test_crud_instance(self):
  17. name = "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(name):
  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, name,
  26. subnet=subnet, user_data={})
  27. def cleanup_inst(inst):
  28. inst.delete()
  29. inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN])
  30. def check_deleted(inst):
  31. deleted_inst = self.provider.compute.instances.get(
  32. inst.id)
  33. self.assertTrue(
  34. deleted_inst is None or deleted_inst.state in (
  35. InstanceState.DELETED,
  36. InstanceState.UNKNOWN),
  37. "Instance %s should have been deleted but still exists." %
  38. name)
  39. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  40. network=net)):
  41. net, subnet = helpers.create_test_network(self.provider, name)
  42. sit.check_crud(self, self.provider.compute.instances, Instance,
  43. "cb_instcrud", create_inst, cleanup_inst,
  44. custom_check_delete=check_deleted)
  45. def _is_valid_ip(self, address):
  46. try:
  47. ipaddress.ip_address(address)
  48. except ValueError:
  49. return False
  50. return True
  51. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  52. 'security.vm_firewalls',
  53. 'security.key_pairs'])
  54. def test_instance_properties(self):
  55. name = "cb_inst_props-{0}".format(helpers.get_uuid())
  56. # Declare these variables and late binding will allow
  57. # the cleanup method access to the most current values
  58. test_instance = None
  59. net = None
  60. fw = None
  61. kp = None
  62. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  63. test_instance, net, fw, kp)):
  64. net, subnet = helpers.create_test_network(self.provider, name)
  65. kp = self.provider.security.key_pairs.create(name=name)
  66. fw = self.provider.security.vm_firewalls.create(
  67. name=name, description=name, network_id=net.id)
  68. test_instance = helpers.get_test_instance(self.provider,
  69. name, key_pair=kp,
  70. vm_firewalls=[fw],
  71. subnet=subnet)
  72. self.assertEqual(
  73. test_instance.name, name,
  74. "Instance name {0} is not equal to the expected name"
  75. " {1}".format(test_instance.name, name))
  76. image_id = helpers.get_provider_test_data(self.provider, "image")
  77. self.assertEqual(test_instance.image_id, image_id,
  78. "Image id {0} is not equal to the expected id"
  79. " {1}".format(test_instance.image_id, image_id))
  80. self.assertIsInstance(test_instance.zone_id,
  81. six.string_types)
  82. self.assertEqual(
  83. test_instance.image_id,
  84. helpers.get_provider_test_data(self.provider, "image"))
  85. self.assertIsInstance(test_instance.public_ips, list)
  86. if test_instance.public_ips:
  87. self.assertTrue(
  88. test_instance.public_ips[0], "public ip should contain a"
  89. " valid value if a list of public_ips exist")
  90. self.assertIsInstance(test_instance.private_ips, list)
  91. self.assertTrue(test_instance.private_ips[0], "private ip should"
  92. " contain a valid value")
  93. self.assertEqual(
  94. test_instance.key_pair_name,
  95. kp.name)
  96. self.assertIsInstance(test_instance.vm_firewalls, list)
  97. self.assertEqual(
  98. test_instance.vm_firewalls[0],
  99. fw)
  100. self.assertIsInstance(test_instance.vm_firewall_ids, list)
  101. self.assertEqual(
  102. test_instance.vm_firewall_ids[0],
  103. fw.id)
  104. # Must have either a public or a private ip
  105. ip_private = test_instance.private_ips[0] \
  106. if test_instance.private_ips else None
  107. ip_address = test_instance.public_ips[0] \
  108. if test_instance.public_ips and test_instance.public_ips[0] \
  109. else ip_private
  110. # Convert to unicode for py27 compatibility with ipaddress()
  111. ip_address = u"{}".format(ip_address)
  112. self.assertIsNotNone(
  113. ip_address,
  114. "Instance must have either a public IP or a private IP")
  115. self.assertTrue(
  116. self._is_valid_ip(ip_address),
  117. "Instance must have a valid IP address. Got: %s" % ip_address)
  118. self.assertIsInstance(test_instance.vm_type_id,
  119. six.string_types)
  120. vm_type = self.provider.compute.vm_types.get(
  121. test_instance.vm_type_id)
  122. self.assertEqual(
  123. vm_type, test_instance.vm_type,
  124. "VM type {0} does not match expected type {1}".format(
  125. vm_type.name, test_instance.vm_type))
  126. self.assertIsInstance(vm_type, VMType)
  127. expected_type = helpers.get_provider_test_data(self.provider,
  128. 'vm_type')
  129. self.assertEqual(
  130. vm_type.name, expected_type,
  131. "VM type {0} does not match expected type {1}".format(
  132. vm_type.name, expected_type))
  133. find_zone = [zone for zone in
  134. self.provider.compute.regions.current.zones
  135. if zone.id == test_instance.zone_id]
  136. self.assertEqual(len(find_zone), 1,
  137. "Instance's placement zone could not be "
  138. " found in zones list")
  139. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  140. 'compute.vm_types'])
  141. def test_block_device_mapping_launch_config(self):
  142. lc = self.provider.compute.instances.create_launch_config()
  143. # specifying an invalid size should raise
  144. # an exception
  145. with self.assertRaises(InvalidConfigurationException):
  146. lc.add_volume_device(size=-1)
  147. # Attempting to add a blank volume without specifying a size
  148. # should raise an exception
  149. with self.assertRaises(InvalidConfigurationException):
  150. lc.add_volume_device(source=None)
  151. # block_devices should be empty so far
  152. self.assertListEqual(
  153. lc.block_devices, [], "No block devices should have been"
  154. " added to mappings list since the configuration was"
  155. " invalid")
  156. # Add a new volume
  157. lc.add_volume_device(size=1, delete_on_terminate=True)
  158. # Override root volume size
  159. image_id = helpers.get_provider_test_data(self.provider, "image")
  160. img = self.provider.compute.images.get(image_id)
  161. # The size should be greater then the ami size
  162. # and therefore, img.min_disk is used.
  163. lc.add_volume_device(
  164. is_root=True,
  165. source=img,
  166. size=img.min_disk if img and img.min_disk > 30 else 30,
  167. delete_on_terminate=True)
  168. # Attempting to add more than one root volume should raise an
  169. # exception.
  170. with self.assertRaises(InvalidConfigurationException):
  171. lc.add_volume_device(size=1, is_root=True)
  172. # Attempting to add an incorrect source should raise an exception
  173. with self.assertRaises(InvalidConfigurationException):
  174. lc.add_volume_device(
  175. source="invalid_source",
  176. delete_on_terminate=True)
  177. # Add all available ephemeral devices
  178. vm_type_name = helpers.get_provider_test_data(
  179. self.provider,
  180. "vm_type")
  181. vm_type = self.provider.compute.vm_types.find(
  182. name=vm_type_name)[0]
  183. for _ in range(vm_type.num_ephemeral_disks):
  184. lc.add_ephemeral_device()
  185. # block_devices should be populated
  186. self.assertTrue(
  187. len(lc.block_devices) == 2 + vm_type.num_ephemeral_disks,
  188. "Expected %d total block devices bit found %d" %
  189. (2 + vm_type.num_ephemeral_disks, len(lc.block_devices)))
  190. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  191. 'compute.vm_types', 'storage.volumes'])
  192. def test_block_device_mapping_attachments(self):
  193. name = "cb_blkattch-{0}".format(helpers.get_uuid())
  194. if self.provider.PROVIDER_ID == ProviderList.OPENSTACK:
  195. raise self.skipTest("Not running BDM tests because OpenStack is"
  196. " not stable enough yet")
  197. test_vol = self.provider.storage.volumes.create(
  198. name,
  199. 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(name=name,
  205. description=name)
  206. def cleanup_snap(snap):
  207. snap.delete()
  208. snap.wait_for([SnapshotState.UNKNOWN],
  209. terminal_states=[SnapshotState.ERROR])
  210. with helpers.cleanup_action(lambda:
  211. 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, name)
  243. with helpers.cleanup_action(lambda:
  244. helpers.delete_test_network(net)):
  245. inst = helpers.create_test_instance(
  246. self.provider,
  247. name,
  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. name = "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, name)
  272. test_inst = helpers.get_test_instance(self.provider, name,
  273. subnet=subnet)
  274. fw = self.provider.security.vm_firewalls.create(
  275. name=name, description=name, 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(name, 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)
  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)