test_compute_service.py 16 KB

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