test_compute_service.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import ipaddress
  2. import uuid
  3. import six
  4. from cloudbridge.cloud.interfaces import InvalidConfigurationException
  5. from cloudbridge.cloud.interfaces import InstanceState
  6. from cloudbridge.cloud.interfaces.resources import InstanceType
  7. from cloudbridge.cloud.interfaces.exceptions import WaitStateException
  8. from test.helpers import ProviderTestBase
  9. import test.helpers as helpers
  10. class CloudComputeServiceTestCase(ProviderTestBase):
  11. def __init__(self, methodName, provider):
  12. super(CloudComputeServiceTestCase, self).__init__(
  13. methodName=methodName, provider=provider)
  14. @helpers.skipIfNoService(['compute.instances', 'network'])
  15. def test_crud_instance(self):
  16. name = "CBInstCrud-{0}-{1}".format(
  17. self.provider.name,
  18. uuid.uuid4())
  19. net, subnet = helpers.create_test_network(self.provider, name)
  20. inst = helpers.get_test_instance(self.provider, name, subnet=subnet)
  21. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  22. inst, net)):
  23. all_instances = self.provider.compute.instances.list()
  24. list_instances = [i for i in all_instances if i.name == name]
  25. self.assertTrue(
  26. len(list_instances) == 1,
  27. "List instances does not return the expected instance %s" %
  28. name)
  29. # check iteration
  30. iter_instances = [i for i in self.provider.compute.instances
  31. if i.name == name]
  32. self.assertTrue(
  33. len(iter_instances) == 1,
  34. "Iter instances does not return the expected instance %s" %
  35. name)
  36. # check find
  37. find_instances = self.provider.compute.instances.find(name=name)
  38. self.assertTrue(
  39. len(find_instances) == 1,
  40. "Find instances does not return the expected instance %s" %
  41. name)
  42. # check non-existent find
  43. find_instances = self.provider.compute.instances.find(
  44. name="non_existent")
  45. self.assertTrue(
  46. len(find_instances) == 0,
  47. "Find() for a non-existent image returned %s" % find_instances)
  48. get_inst = self.provider.compute.instances.get(
  49. inst.id)
  50. self.assertTrue(
  51. list_instances[0] ==
  52. get_inst == inst,
  53. "Objects returned by list: {0} and get: {1} are not as "
  54. " expected: {2}" .format(list_instances[0].id,
  55. get_inst.id,
  56. inst.id))
  57. self.assertTrue(
  58. list_instances[0].name ==
  59. get_inst.name == inst.name,
  60. "Names returned by list: {0} and get: {1} are not as "
  61. " expected: {2}" .format(list_instances[0].name,
  62. get_inst.name,
  63. inst.name))
  64. deleted_inst = self.provider.compute.instances.get(
  65. inst.id)
  66. self.assertTrue(
  67. deleted_inst is None or deleted_inst.state in (
  68. InstanceState.TERMINATED,
  69. InstanceState.UNKNOWN),
  70. "Instance %s should have been deleted but still exists." %
  71. name)
  72. def _is_valid_ip(self, address):
  73. try:
  74. ipaddress.ip_address(address)
  75. except ValueError:
  76. return False
  77. return True
  78. @helpers.skipIfNoService(['compute.instances', 'network',
  79. 'security.security_groups',
  80. 'security.key_pairs'])
  81. def test_instance_properties(self):
  82. name = "CBInstProps-{0}-{1}".format(
  83. self.provider.name,
  84. uuid.uuid4())
  85. net, subnet = helpers.create_test_network(self.provider, name)
  86. kp = self.provider.security.key_pairs.create(name=name)
  87. sg = self.provider.security.security_groups.create(
  88. name=name, description=name, network_id=net.id)
  89. test_instance = helpers.get_test_instance(self.provider,
  90. name, key_pair=kp,
  91. security_groups=[sg],
  92. subnet=subnet)
  93. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  94. test_instance, net, sg, kp)):
  95. self.assertTrue(
  96. test_instance.id in repr(test_instance),
  97. "repr(obj) should contain the object id so that the object"
  98. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  99. self.assertEqual(
  100. test_instance.name, name,
  101. "Instance name {0} is not equal to the expected name"
  102. " {1}".format(test_instance.name, name))
  103. image_id = helpers.get_provider_test_data(self.provider, "image")
  104. self.assertEqual(test_instance.image_id, image_id,
  105. "Image id {0} is not equal to the expected id"
  106. " {1}".format(test_instance.image_id, image_id))
  107. self.assertIsInstance(test_instance.zone_id,
  108. six.string_types)
  109. # FIXME: Moto is not returning the instance's placement zone
  110. # find_zone = [zone for zone in
  111. # self.provider.compute.regions.current.zones
  112. # if zone.id == test_instance.zone_id]
  113. # self.assertEqual(len(find_zone), 1,
  114. # "Instance's placement zone could not be "
  115. # " found in zones list")
  116. self.assertEqual(
  117. test_instance.image_id,
  118. helpers.get_provider_test_data(self.provider, "image"))
  119. self.assertIsInstance(test_instance.public_ips, list)
  120. self.assertIsInstance(test_instance.private_ips, list)
  121. self.assertEqual(
  122. test_instance.key_pair_name,
  123. kp.name)
  124. self.assertIsInstance(test_instance.security_groups, list)
  125. self.assertEqual(
  126. test_instance.security_groups[0],
  127. sg)
  128. self.assertIsInstance(test_instance.security_group_ids, list)
  129. self.assertEqual(
  130. test_instance.security_group_ids[0],
  131. sg.id)
  132. # Must have either a public or a private ip
  133. ip_private = test_instance.private_ips[0] \
  134. if test_instance.private_ips else None
  135. ip_address = test_instance.public_ips[0] \
  136. if test_instance.public_ips and test_instance.public_ips[0] \
  137. else ip_private
  138. self.assertIsNotNone(
  139. ip_address,
  140. "Instance must have either a public IP or a private IP")
  141. self.assertTrue(
  142. self._is_valid_ip(ip_address),
  143. "Instance must have a valid IP address")
  144. self.assertIsInstance(test_instance.instance_type_id,
  145. six.string_types)
  146. itype = self.provider.compute.instance_types.get(
  147. test_instance.instance_type_id)
  148. self.assertEqual(
  149. itype, test_instance.instance_type,
  150. "Instance type {0} does not match expected type {1}".format(
  151. itype.name, test_instance.instance_type))
  152. self.assertIsInstance(itype, InstanceType)
  153. expected_type = helpers.get_provider_test_data(self.provider,
  154. 'instance_type')
  155. self.assertEqual(
  156. itype.name, expected_type,
  157. "Instance type {0} does not match expected type {1}".format(
  158. itype.name, expected_type))
  159. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  160. 'compute.instance_types'])
  161. def test_block_device_mapping_launch_config(self):
  162. lc = self.provider.compute.instances.create_launch_config()
  163. # specifying an invalid size should raise
  164. # an exception
  165. with self.assertRaises(InvalidConfigurationException):
  166. lc.add_volume_device(size=-1)
  167. # Attempting to add a blank volume without specifying a size
  168. # should raise an exception
  169. with self.assertRaises(InvalidConfigurationException):
  170. lc.add_volume_device(source=None)
  171. # block_devices should be empty so far
  172. self.assertListEqual(
  173. lc.block_devices, [], "No block devices should have been"
  174. " added to mappings list since the configuration was"
  175. " invalid")
  176. # Add a new volume
  177. lc.add_volume_device(size=1, delete_on_terminate=True)
  178. # Override root volume size
  179. image_id = helpers.get_provider_test_data(self.provider, "image")
  180. img = self.provider.compute.images.get(image_id)
  181. lc.add_volume_device(
  182. is_root=True,
  183. source=img,
  184. # TODO: This should be greater than the ami size or tests will fail
  185. # on actual infrastructure. Needs an image.size method
  186. size=2,
  187. delete_on_terminate=True)
  188. # Attempting to add more than one root volume should raise an
  189. # exception.
  190. with self.assertRaises(InvalidConfigurationException):
  191. lc.add_volume_device(size=1, is_root=True)
  192. # Attempting to add an incorrect source should raise an exception
  193. with self.assertRaises(InvalidConfigurationException):
  194. lc.add_volume_device(
  195. source="invalid_source",
  196. delete_on_terminate=True)
  197. # Add all available ephemeral devices
  198. instance_type_name = helpers.get_provider_test_data(
  199. self.provider,
  200. "instance_type")
  201. inst_type = self.provider.compute.instance_types.find(
  202. name=instance_type_name)[0]
  203. for _ in range(inst_type.num_ephemeral_disks):
  204. lc.add_ephemeral_device()
  205. # block_devices should be populated
  206. self.assertTrue(
  207. len(lc.block_devices) == 2 + inst_type.num_ephemeral_disks,
  208. "Expected %d total block devices bit found %d" %
  209. (2 + inst_type.num_ephemeral_disks, len(lc.block_devices)))
  210. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  211. 'compute.instance_types', 'block_store.volumes'])
  212. def test_block_device_mapping_attachments(self):
  213. name = "CBInstBlkAttch-{0}-{1}".format(
  214. self.provider.name,
  215. uuid.uuid4())
  216. # test_vol = self.provider.block_store.volumes.create(
  217. # name,
  218. # 1,
  219. # helpers.get_provider_test_data(self.provider, "placement"))
  220. # with helpers.cleanup_action(lambda: test_vol.delete()):
  221. # test_vol.wait_till_ready()
  222. # test_snap = test_vol.create_snapshot(name=name,
  223. # description=name)
  224. #
  225. # def cleanup_snap(snap):
  226. # snap.delete()
  227. # snap.wait_for(
  228. # [SnapshotState.UNKNOWN],
  229. # terminal_states=[SnapshotState.ERROR])
  230. #
  231. # with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  232. # test_snap.wait_till_ready()
  233. lc = self.provider.compute.instances.create_launch_config()
  234. # Add a new blank volume
  235. # lc.add_volume_device(size=1, delete_on_terminate=True)
  236. # Attach an existing volume
  237. # lc.add_volume_device(size=1, source=test_vol,
  238. # delete_on_terminate=True)
  239. # Add a new volume based on a snapshot
  240. # lc.add_volume_device(size=1, source=test_snap,
  241. # delete_on_terminate=True)
  242. # Override root volume size
  243. image_id = helpers.get_provider_test_data(
  244. self.provider,
  245. "image")
  246. img = self.provider.compute.images.get(image_id)
  247. lc.add_volume_device(
  248. is_root=True,
  249. source=img,
  250. # TODO: This should be greater than the ami size or tests
  251. # will fail on actual infrastructure. Needs an image.size
  252. # method
  253. size=8,
  254. delete_on_terminate=True)
  255. # Add all available ephemeral devices
  256. instance_type_name = helpers.get_provider_test_data(
  257. self.provider,
  258. "instance_type")
  259. inst_type = self.provider.compute.instance_types.find(
  260. name=instance_type_name)[0]
  261. for _ in range(inst_type.num_ephemeral_disks):
  262. lc.add_ephemeral_device()
  263. net, subnet = helpers.create_test_network(self.provider, name)
  264. inst = helpers.create_test_instance(
  265. self.provider,
  266. name,
  267. subnet=subnet,
  268. zone=helpers.get_provider_test_data(self.provider, 'placement'),
  269. launch_config=lc)
  270. def cleanup(instance, net):
  271. instance.terminate()
  272. instance.wait_for(
  273. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  274. terminal_states=[InstanceState.ERROR])
  275. helpers.delete_test_network(net)
  276. with helpers.cleanup_action(lambda: cleanup(inst, net)):
  277. try:
  278. inst.wait_till_ready()
  279. except WaitStateException as e:
  280. self.fail("The block device mapped launch did not "
  281. " complete successfully: %s" % e)
  282. # TODO: Check instance attachments and make sure they
  283. # correspond to requested mappings