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