test_compute_service.py 14 KB

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