test_compute_service.py 16 KB

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