test_compute_service.py 14 KB

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