test_compute_service.py 16 KB

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