test_compute_service.py 13 KB

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