test_compute_service.py 16 KB

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