test_compute_service.py 11 KB

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