test_compute_service.py 11 KB

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