test_compute_service.py 16 KB

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