test_compute_service.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 = None
  88. # sg = self.provider.security.security_groups.create(
  89. # name=name, description=name)
  90. test_instance = helpers.get_test_instance(self.provider,
  91. name, key_pair=kp)
  92. # security_groups=[sg])
  93. def cleanup(inst, kp, sg):
  94. inst.terminate()
  95. inst.wait_for([InstanceState.TERMINATED, InstanceState.UNKNOWN],
  96. terminal_states=[InstanceState.ERROR])
  97. kp.delete()
  98. # sg.delete()
  99. with helpers.cleanup_action(lambda: cleanup(test_instance, kp, sg)):
  100. self.assertTrue(
  101. test_instance.id in repr(test_instance),
  102. "repr(obj) should contain the object id so that the object"
  103. " can be reconstructed, but does not. eval(repr(obj)) == obj")
  104. self.assertEqual(
  105. test_instance.name, name,
  106. "Instance name {0} is not equal to the expected name"
  107. " {1}".format(test_instance.name, name))
  108. image_id = helpers.get_provider_test_data(self.provider, "image")
  109. self.assertEqual(test_instance.image_id, image_id,
  110. "Image id {0} is not equal to the expected id"
  111. " {1}".format(test_instance.image_id, image_id))
  112. self.assertIsInstance(test_instance.zone_id,
  113. six.string_types)
  114. # FIXME: Moto is not returning the instance's placement zone
  115. # find_zone = [zone for zone in
  116. # self.provider.compute.regions.current.zones
  117. # if zone.id == test_instance.zone_id]
  118. # self.assertEqual(len(find_zone), 1,
  119. # "Instance's placement zone could not be "
  120. # " found in zones list")
  121. self.assertEqual(
  122. test_instance.image_id,
  123. helpers.get_provider_test_data(self.provider, "image"))
  124. self.assertIsInstance(test_instance.public_ips, list)
  125. self.assertIsInstance(test_instance.private_ips, list)
  126. self.assertEqual(
  127. test_instance.key_pair_name,
  128. kp.name)
  129. self.assertIsInstance(test_instance.security_groups, list)
  130. # self.assertEqual(
  131. # test_instance.security_groups[0],
  132. # sg)
  133. # self.assertIsInstance(test_instance.security_group_ids, list)
  134. # self.assertEqual(
  135. # test_instance.security_group_ids[0],
  136. # sg.id)
  137. # Must have either a public or a private ip
  138. ip_private = test_instance.private_ips[0] \
  139. if test_instance.private_ips else None
  140. ip_address = test_instance.public_ips[0] \
  141. if test_instance.public_ips else ip_private
  142. self.assertIsNotNone(
  143. ip_address,
  144. "Instance must have either a public IP or a private IP")
  145. self.assertTrue(
  146. self._is_valid_ip(ip_address),
  147. "Instance must have a valid IP address")
  148. self.assertIsInstance(test_instance.instance_type_id,
  149. six.string_types)
  150. itype = self.provider.compute.instance_types.get(
  151. test_instance.instance_type_id)
  152. self.assertEqual(
  153. itype, test_instance.instance_type,
  154. "Instance type {0} does not match expected type {1}".format(
  155. itype.name, test_instance.instance_type))
  156. self.assertIsInstance(itype, InstanceType)
  157. expected_type = helpers.get_provider_test_data(self.provider,
  158. 'instance_type')
  159. self.assertEqual(
  160. itype.name, expected_type,
  161. "Instance type {0} does not match expected type {1}".format(
  162. itype.name, expected_type))
  163. def test_block_device_mapping_launch_config(self):
  164. lc = self.provider.compute.instances.create_launch_config()
  165. # specifying an invalid size should raise
  166. # an exception
  167. with self.assertRaises(InvalidConfigurationException):
  168. lc.add_volume_device(size=-1)
  169. # Attempting to add a blank volume without specifying a size
  170. # should raise an exception
  171. with self.assertRaises(InvalidConfigurationException):
  172. lc.add_volume_device(source=None)
  173. # block_devices should be empty so far
  174. self.assertListEqual(
  175. lc.block_devices, [], "No block devices should have been"
  176. " added to mappings list since the configuration was"
  177. " invalid")
  178. # Add a new volume
  179. lc.add_volume_device(size=1, delete_on_terminate=True)
  180. # Override root volume size
  181. image_id = helpers.get_provider_test_data(self.provider, "image")
  182. img = self.provider.compute.images.get(image_id)
  183. lc.add_volume_device(
  184. is_root=True,
  185. source=img,
  186. # TODO: This should be greater than the ami size or tests will fail
  187. # on actual infrastructure. Needs an image.size method
  188. size=2,
  189. delete_on_terminate=True)
  190. # Attempting to add more than one root volume should raise an
  191. # exception.
  192. with self.assertRaises(InvalidConfigurationException):
  193. lc.add_volume_device(size=1, is_root=True)
  194. # Attempting to add an incorrect source should raise an exception
  195. with self.assertRaises(InvalidConfigurationException):
  196. lc.add_volume_device(
  197. source="invalid_source",
  198. delete_on_terminate=True)
  199. # Add all available ephemeral devices
  200. instance_type_name = helpers.get_provider_test_data(
  201. self.provider,
  202. "instance_type")
  203. inst_type = self.provider.compute.instance_types.find(
  204. name=instance_type_name)[0]
  205. for _ in range(inst_type.num_ephemeral_disks):
  206. lc.add_ephemeral_device()
  207. # block_devices should be populated
  208. self.assertTrue(
  209. len(lc.block_devices) == 2 + inst_type.num_ephemeral_disks,
  210. "Expected %d total block devices bit found %d" %
  211. (2 + inst_type.num_ephemeral_disks, len(lc.block_devices)))
  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=2,
  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. inst = helpers.create_test_instance(
  264. self.provider,
  265. name,
  266. zone=helpers.get_provider_test_data(
  267. self.provider,
  268. 'placement'),
  269. launch_config=lc)
  270. def cleanup(instance):
  271. instance.terminate()
  272. instance.wait_for(
  273. [InstanceState.TERMINATED, InstanceState.UNKNOWN],
  274. terminal_states=[InstanceState.ERROR])
  275. with helpers.cleanup_action(lambda: cleanup(inst)):
  276. try:
  277. inst.wait_till_ready()
  278. except WaitStateException as e:
  279. self.fail("The block device mapped launch did not "
  280. " complete successfully: %s" % e)
  281. # TODO: Check instance attachments and make sure they
  282. # correspond to requested mappings