test_compute_service.py 13 KB

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