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. @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. if test_instance.public_ips:
  86. self.assertTrue(
  87. test_instance.public_ips[0], "public ip should contain a"
  88. " valid value if a list of public_ips exist")
  89. self.assertIsInstance(test_instance.private_ips, list)
  90. self.assertTrue(test_instance.private_ips[0], "private ip should"
  91. " contain a valid value")
  92. self.assertEqual(
  93. test_instance.key_pair_name,
  94. kp.name)
  95. self.assertIsInstance(test_instance.vm_firewalls, list)
  96. self.assertEqual(
  97. test_instance.vm_firewalls[0],
  98. fw)
  99. self.assertIsInstance(test_instance.vm_firewall_ids, list)
  100. self.assertEqual(
  101. test_instance.vm_firewall_ids[0],
  102. fw.id)
  103. # Must have either a public or a private ip
  104. ip_private = test_instance.private_ips[0] \
  105. if test_instance.private_ips else None
  106. ip_address = test_instance.public_ips[0] \
  107. if test_instance.public_ips and test_instance.public_ips[0] \
  108. else ip_private
  109. # Convert to unicode for py27 compatibility with ipaddress()
  110. ip_address = u"{}".format(ip_address)
  111. self.assertIsNotNone(
  112. ip_address,
  113. "Instance must have either a public IP or a private IP")
  114. self.assertTrue(
  115. self._is_valid_ip(ip_address),
  116. "Instance must have a valid IP address. Got: %s" % ip_address)
  117. self.assertIsInstance(test_instance.vm_type_id,
  118. six.string_types)
  119. vm_type = self.provider.compute.vm_types.get(
  120. test_instance.vm_type_id)
  121. self.assertEqual(
  122. vm_type, test_instance.vm_type,
  123. "VM type {0} does not match expected type {1}".format(
  124. vm_type.name, test_instance.vm_type))
  125. self.assertIsInstance(vm_type, VMType)
  126. expected_type = helpers.get_provider_test_data(self.provider,
  127. 'vm_type')
  128. self.assertEqual(
  129. vm_type.name, expected_type,
  130. "VM type {0} does not match expected type {1}".format(
  131. vm_type.name, expected_type))
  132. find_zone = [zone for zone in
  133. self.provider.compute.regions.current.zones
  134. if zone.id == test_instance.zone_id]
  135. self.assertEqual(len(find_zone), 1,
  136. "Instance's placement zone could not be "
  137. " found in zones list")
  138. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  139. 'compute.vm_types'])
  140. def test_block_device_mapping_launch_config(self):
  141. lc = self.provider.compute.instances.create_launch_config()
  142. # specifying an invalid size should raise
  143. # an exception
  144. with self.assertRaises(InvalidConfigurationException):
  145. lc.add_volume_device(size=-1)
  146. # Attempting to add a blank volume without specifying a size
  147. # should raise an exception
  148. with self.assertRaises(InvalidConfigurationException):
  149. lc.add_volume_device(source=None)
  150. # block_devices should be empty so far
  151. self.assertListEqual(
  152. lc.block_devices, [], "No block devices should have been"
  153. " added to mappings list since the configuration was"
  154. " invalid")
  155. # Add a new volume
  156. lc.add_volume_device(size=1, delete_on_terminate=True)
  157. # Override root volume size
  158. image_id = helpers.get_provider_test_data(self.provider, "image")
  159. img = self.provider.compute.images.get(image_id)
  160. # The size should be greater then the ami size
  161. # and therefore, img.min_disk is used.
  162. lc.add_volume_device(
  163. is_root=True,
  164. source=img,
  165. size=img.min_disk if img and img.min_disk else 30,
  166. delete_on_terminate=True)
  167. # Attempting to add more than one root volume should raise an
  168. # exception.
  169. with self.assertRaises(InvalidConfigurationException):
  170. lc.add_volume_device(size=1, is_root=True)
  171. # Attempting to add an incorrect source should raise an exception
  172. with self.assertRaises(InvalidConfigurationException):
  173. lc.add_volume_device(
  174. source="invalid_source",
  175. delete_on_terminate=True)
  176. # Add all available ephemeral devices
  177. vm_type_name = helpers.get_provider_test_data(
  178. self.provider,
  179. "vm_type")
  180. vm_type = self.provider.compute.vm_types.find(
  181. name=vm_type_name)[0]
  182. for _ in range(vm_type.num_ephemeral_disks):
  183. lc.add_ephemeral_device()
  184. # block_devices should be populated
  185. self.assertTrue(
  186. len(lc.block_devices) == 2 + vm_type.num_ephemeral_disks,
  187. "Expected %d total block devices bit found %d" %
  188. (2 + vm_type.num_ephemeral_disks, len(lc.block_devices)))
  189. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  190. 'compute.vm_types', 'storage.volumes'])
  191. def test_block_device_mapping_attachments(self):
  192. name = "cb_blkattch-{0}".format(helpers.get_uuid())
  193. if self.provider.PROVIDER_ID == ProviderList.OPENSTACK:
  194. raise self.skipTest("Not running BDM tests because OpenStack is"
  195. " not stable enough yet")
  196. test_vol = self.provider.storage.volumes.create(
  197. name,
  198. 1,
  199. helpers.get_provider_test_data(self.provider,
  200. "placement"))
  201. with helpers.cleanup_action(lambda: test_vol.delete()):
  202. test_vol.wait_till_ready()
  203. test_snap = test_vol.create_snapshot(name=name,
  204. description=name)
  205. def cleanup_snap(snap):
  206. snap.delete()
  207. snap.wait_for([SnapshotState.UNKNOWN],
  208. terminal_states=[SnapshotState.ERROR])
  209. with helpers.cleanup_action(lambda:
  210. cleanup_snap(test_snap)):
  211. test_snap.wait_till_ready()
  212. lc = self.provider.compute.instances.create_launch_config()
  213. # Add a new blank volume
  214. lc.add_volume_device(size=1, delete_on_terminate=True)
  215. # Attach an existing volume
  216. lc.add_volume_device(size=1, source=test_vol,
  217. delete_on_terminate=True)
  218. # Add a new volume based on a snapshot
  219. lc.add_volume_device(size=1, source=test_snap,
  220. delete_on_terminate=True)
  221. # Override root volume size
  222. image_id = helpers.get_provider_test_data(
  223. self.provider,
  224. "image")
  225. img = self.provider.compute.images.get(image_id)
  226. # The size should be greater then the ami size
  227. # and therefore, img.min_disk is used.
  228. lc.add_volume_device(
  229. is_root=True,
  230. source=img,
  231. size=img.min_disk if img and img.min_disk else 30,
  232. delete_on_terminate=True)
  233. # Add all available ephemeral devices
  234. vm_type_name = helpers.get_provider_test_data(
  235. self.provider,
  236. "vm_type")
  237. vm_type = self.provider.compute.vm_types.find(
  238. name=vm_type_name)[0]
  239. for _ in range(vm_type.num_ephemeral_disks):
  240. lc.add_ephemeral_device()
  241. net, subnet = helpers.create_test_network(self.provider, name)
  242. with helpers.cleanup_action(lambda:
  243. helpers.delete_test_network(net)):
  244. inst = helpers.create_test_instance(
  245. self.provider,
  246. name,
  247. subnet=subnet,
  248. launch_config=lc)
  249. with helpers.cleanup_action(lambda:
  250. helpers.delete_test_instance(
  251. inst)):
  252. try:
  253. inst.wait_till_ready()
  254. except WaitStateException as e:
  255. self.fail("The block device mapped launch did not "
  256. " complete successfully: %s" % e)
  257. # TODO: Check instance attachments and make sure they
  258. # correspond to requested mappings
  259. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  260. 'networking.floating_ips',
  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 = (self.provider.networking.gateways
  302. .get_or_create_inet_gateway(net, name))
  303. router.attach_gateway(gateway)
  304. # check whether adding an elastic ip works
  305. fip = self.provider.networking.floating_ips.create()
  306. self.assertFalse(
  307. fip.in_use,
  308. "Newly created floating IP address should not be in use.")
  309. with helpers.cleanup_action(lambda: fip.delete()):
  310. with helpers.cleanup_action(
  311. lambda: test_inst.remove_floating_ip(fip)):
  312. test_inst.add_floating_ip(fip)
  313. test_inst.refresh()
  314. # On Devstack, FloatingIP is listed under private_ips.
  315. self.assertIn(fip.public_ip, test_inst.public_ips +
  316. test_inst.private_ips)
  317. fip.refresh()
  318. self.assertTrue(
  319. fip.in_use,
  320. "Attached floating IP address should be in use.")
  321. test_inst.refresh()
  322. self.assertNotIn(
  323. fip.public_ip,
  324. test_inst.public_ips + test_inst.private_ips)