test_compute_service.py 13 KB

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