test_compute_service.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import ipaddress
  2. import six
  3. from cloudbridge.cloud.base.resources import BaseNetwork
  4. from cloudbridge.cloud.factory import ProviderList
  5. from cloudbridge.cloud.interfaces import InstanceState
  6. from cloudbridge.cloud.interfaces import InvalidConfigurationException
  7. from cloudbridge.cloud.interfaces.exceptions import WaitStateException
  8. from cloudbridge.cloud.interfaces.resources import Instance
  9. from cloudbridge.cloud.interfaces.resources import SnapshotState
  10. from cloudbridge.cloud.interfaces.resources import VMType
  11. from test import helpers
  12. from test.helpers import ProviderTestBase
  13. from test.helpers import standard_interface_tests as sit
  14. class CloudComputeServiceTestCase(ProviderTestBase):
  15. _multiprocess_can_split_ = True
  16. @helpers.skipIfNoService(['compute.instances', 'networking.networks'])
  17. def test_crud_instance(self):
  18. label = "cb-instcrud-{0}".format(helpers.get_uuid())
  19. # Declare these variables and late binding will allow
  20. # the cleanup method access to the most current values
  21. subnet = None
  22. def create_inst(label):
  23. # Also test whether sending in an empty_dict for user_data
  24. # results in an automatic conversion to string.
  25. return helpers.get_test_instance(self.provider, label,
  26. subnet=subnet, user_data={})
  27. def cleanup_inst(inst):
  28. if inst:
  29. inst.delete()
  30. inst.wait_for([InstanceState.DELETED, InstanceState.UNKNOWN])
  31. inst.refresh()
  32. self.assertTrue(
  33. inst.state == InstanceState.UNKNOWN,
  34. "Instance.state must be unknown when refreshing after a "
  35. "delete but got %s"
  36. % inst.state)
  37. def check_deleted(inst):
  38. deleted_inst = self.provider.compute.instances.get(
  39. inst.id)
  40. self.assertTrue(
  41. deleted_inst is None or deleted_inst.state in (
  42. InstanceState.DELETED,
  43. InstanceState.UNKNOWN),
  44. "Instance %s should have been deleted but still exists." %
  45. label)
  46. subnet = helpers.get_or_create_default_subnet(self.provider)
  47. sit.check_crud(self, self.provider.compute.instances, Instance,
  48. "cb-instcrud", create_inst, cleanup_inst,
  49. custom_check_delete=check_deleted)
  50. def _is_valid_ip(self, address):
  51. try:
  52. ipaddress.ip_address(address)
  53. except ValueError:
  54. return False
  55. return True
  56. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  57. 'security.vm_firewalls',
  58. 'security.key_pairs'])
  59. def test_instance_properties(self):
  60. label = "cb-inst-props-{0}".format(helpers.get_uuid())
  61. # Declare these variables and late binding will allow
  62. # the cleanup method access to the most current values
  63. test_instance = None
  64. fw = None
  65. kp = None
  66. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  67. test_instance, fw, kp)):
  68. subnet = helpers.get_or_create_default_subnet(self.provider)
  69. net = subnet.network
  70. kp = self.provider.security.key_pairs.create(name=label)
  71. fw = self.provider.security.vm_firewalls.create(
  72. label=label, description=label, network=net.id)
  73. test_instance = helpers.get_test_instance(self.provider,
  74. label, key_pair=kp,
  75. vm_firewalls=[fw],
  76. subnet=subnet)
  77. self.assertEqual(
  78. test_instance.label, label,
  79. "Instance label {0} is not equal to the expected label"
  80. " {1}".format(test_instance.label, label))
  81. image_id = helpers.get_provider_test_data(self.provider, "image")
  82. self.assertEqual(test_instance.image_id, image_id,
  83. "Image id {0} is not equal to the expected id"
  84. " {1}".format(test_instance.image_id, image_id))
  85. self.assertIsInstance(test_instance.zone_id,
  86. six.string_types)
  87. self.assertEqual(
  88. test_instance.image_id,
  89. helpers.get_provider_test_data(self.provider, "image"))
  90. self.assertIsInstance(test_instance.public_ips, list)
  91. if test_instance.public_ips:
  92. self.assertTrue(
  93. test_instance.public_ips[0], "public ip should contain a"
  94. " valid value if a list of public_ips exist")
  95. self.assertIsInstance(test_instance.private_ips, list)
  96. self.assertTrue(test_instance.private_ips[0], "private ip should"
  97. " contain a valid value")
  98. self.assertEqual(
  99. test_instance.key_pair_id,
  100. kp.id)
  101. self.assertIsInstance(test_instance.vm_firewalls, list)
  102. self.assertEqual(
  103. test_instance.vm_firewalls[0],
  104. fw)
  105. self.assertIsInstance(test_instance.vm_firewall_ids, list)
  106. self.assertEqual(
  107. test_instance.vm_firewall_ids[0],
  108. fw.id)
  109. # Must have either a public or a private ip
  110. ip_private = test_instance.private_ips[0] \
  111. if test_instance.private_ips else None
  112. ip_address = test_instance.public_ips[0] \
  113. if test_instance.public_ips and test_instance.public_ips[0] \
  114. else ip_private
  115. # Convert to unicode for py27 compatibility with ipaddress()
  116. ip_address = u"{}".format(ip_address)
  117. self.assertIsNotNone(
  118. ip_address,
  119. "Instance must have either a public IP or a private IP")
  120. self.assertTrue(
  121. self._is_valid_ip(ip_address),
  122. "Instance must have a valid IP address. Got: %s" % ip_address)
  123. self.assertIsInstance(test_instance.vm_type_id,
  124. six.string_types)
  125. vm_type = self.provider.compute.vm_types.get(
  126. test_instance.vm_type_id)
  127. self.assertEqual(
  128. vm_type, test_instance.vm_type,
  129. "VM type {0} does not match expected type {1}".format(
  130. vm_type.name, test_instance.vm_type))
  131. self.assertIsInstance(vm_type, VMType)
  132. expected_type = helpers.get_provider_test_data(self.provider,
  133. 'vm_type')
  134. self.assertEqual(
  135. vm_type.name, expected_type,
  136. "VM type {0} does not match expected type {1}".format(
  137. vm_type.name, expected_type))
  138. find_zone = [zone for zone in
  139. self.provider.compute.regions.current.zones
  140. if zone.id == test_instance.zone_id]
  141. self.assertEqual(len(find_zone), 1,
  142. "Instance's placement zone could not be "
  143. " found in zones list")
  144. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  145. 'compute.vm_types'])
  146. def test_block_device_mapping_launch_config(self):
  147. lc = self.provider.compute.instances.create_launch_config()
  148. # specifying an invalid size should raise
  149. # an exception
  150. with self.assertRaises(InvalidConfigurationException):
  151. lc.add_volume_device(size=-1)
  152. # Attempting to add a blank volume without specifying a size
  153. # should raise an exception
  154. with self.assertRaises(InvalidConfigurationException):
  155. lc.add_volume_device(source=None)
  156. # block_devices should be empty so far
  157. self.assertListEqual(
  158. lc.block_devices, [], "No block devices should have been"
  159. " added to mappings list since the configuration was"
  160. " invalid")
  161. # Add a new volume
  162. lc.add_volume_device(size=1, delete_on_terminate=True)
  163. # Override root volume size
  164. image_id = helpers.get_provider_test_data(self.provider, "image")
  165. img = self.provider.compute.images.get(image_id)
  166. # The size should be greater then the ami size
  167. # and therefore, img.min_disk is used.
  168. lc.add_volume_device(
  169. is_root=True,
  170. source=img,
  171. size=img.min_disk if img and img.min_disk else 30,
  172. delete_on_terminate=True)
  173. # Attempting to add more than one root volume should raise an
  174. # exception.
  175. with self.assertRaises(InvalidConfigurationException):
  176. lc.add_volume_device(size=1, is_root=True)
  177. # Attempting to add an incorrect source should raise an exception
  178. with self.assertRaises(InvalidConfigurationException):
  179. lc.add_volume_device(
  180. source="invalid_source",
  181. delete_on_terminate=True)
  182. # Add all available ephemeral devices
  183. vm_type_name = helpers.get_provider_test_data(
  184. self.provider,
  185. "vm_type")
  186. vm_type = self.provider.compute.vm_types.find(
  187. name=vm_type_name)[0]
  188. for _ in range(vm_type.num_ephemeral_disks):
  189. lc.add_ephemeral_device()
  190. # block_devices should be populated
  191. self.assertTrue(
  192. len(lc.block_devices) == 2 + vm_type.num_ephemeral_disks,
  193. "Expected %d total block devices bit found %d" %
  194. (2 + vm_type.num_ephemeral_disks, len(lc.block_devices)))
  195. @helpers.skipIfNoService(['compute.instances', 'compute.images',
  196. 'compute.vm_types', 'storage.volumes'])
  197. def test_block_device_mapping_attachments(self):
  198. label = "cb-blkattch-{0}".format(helpers.get_uuid())
  199. if self.provider.PROVIDER_ID == ProviderList.OPENSTACK:
  200. raise self.skipTest("Not running BDM tests because OpenStack is"
  201. " not stable enough yet")
  202. test_vol = self.provider.storage.volumes.create(
  203. label, 1,
  204. helpers.get_provider_test_data(self.provider,
  205. "placement"))
  206. with helpers.cleanup_action(lambda: test_vol.delete()):
  207. test_vol.wait_till_ready()
  208. test_snap = test_vol.create_snapshot(label=label,
  209. description=label)
  210. def cleanup_snap(snap):
  211. if snap:
  212. snap.delete()
  213. snap.wait_for([SnapshotState.UNKNOWN],
  214. terminal_states=[SnapshotState.ERROR])
  215. with helpers.cleanup_action(lambda: cleanup_snap(test_snap)):
  216. test_snap.wait_till_ready()
  217. lc = self.provider.compute.instances.create_launch_config()
  218. # Add a new blank volume
  219. lc.add_volume_device(size=1, delete_on_terminate=True)
  220. # Attach an existing volume
  221. lc.add_volume_device(size=1, source=test_vol,
  222. delete_on_terminate=True)
  223. # Add a new volume based on a snapshot
  224. lc.add_volume_device(size=1, source=test_snap,
  225. delete_on_terminate=True)
  226. # Override root volume size
  227. image_id = helpers.get_provider_test_data(
  228. self.provider,
  229. "image")
  230. img = self.provider.compute.images.get(image_id)
  231. # The size should be greater then the ami size
  232. # and therefore, img.min_disk is used.
  233. lc.add_volume_device(
  234. is_root=True,
  235. source=img,
  236. size=img.min_disk if img and img.min_disk else 30,
  237. delete_on_terminate=True)
  238. # Add all available ephemeral devices
  239. vm_type_name = helpers.get_provider_test_data(
  240. self.provider,
  241. "vm_type")
  242. vm_type = self.provider.compute.vm_types.find(
  243. name=vm_type_name)[0]
  244. # Some providers, e.g. GCP, has a limit on total number of
  245. # attached disks; it does not matter how many of them are
  246. # ephemeral or persistent. So, wee keep in mind that we have
  247. # attached 4 disks already, and add ephemeral disks accordingly
  248. # to not exceed the limit.
  249. for _ in range(vm_type.num_ephemeral_disks - 4):
  250. lc.add_ephemeral_device()
  251. subnet = helpers.get_or_create_default_subnet(
  252. self.provider)
  253. inst = None
  254. with helpers.cleanup_action(
  255. lambda: helpers.delete_instance(inst)):
  256. inst = helpers.create_test_instance(
  257. self.provider,
  258. label,
  259. subnet=subnet,
  260. launch_config=lc)
  261. try:
  262. inst.wait_till_ready()
  263. except WaitStateException as e:
  264. self.fail("The block device mapped launch did not "
  265. " complete successfully: %s" % e)
  266. # TODO: Check instance attachments and make sure they
  267. # correspond to requested mappings
  268. @helpers.skipIfNoService(['compute.instances', 'networking.networks',
  269. 'security.vm_firewalls'])
  270. def test_instance_methods(self):
  271. label = "cb-instmethods-{0}".format(helpers.get_uuid())
  272. # Declare these variables and late binding will allow
  273. # the cleanup method access to the most current values
  274. net = None
  275. test_inst = None
  276. fw = None
  277. with helpers.cleanup_action(lambda: helpers.cleanup_test_resources(
  278. instance=test_inst, vm_firewall=fw, network=net)):
  279. net = self.provider.networking.networks.create(
  280. label=label, cidr_block=BaseNetwork.CB_DEFAULT_IPV4RANGE)
  281. cidr = '10.0.1.0/24'
  282. subnet = net.create_subnet(label=label, cidr_block=cidr,
  283. zone=helpers.get_provider_test_data(
  284. self.provider,
  285. 'placement'))
  286. test_inst = helpers.get_test_instance(self.provider, label,
  287. subnet=subnet)
  288. fw = self.provider.security.vm_firewalls.create(
  289. label=label, description=label, network=net.id)
  290. # Check adding a VM firewall to a running instance
  291. test_inst.add_vm_firewall(fw)
  292. test_inst.refresh()
  293. self.assertTrue(
  294. fw in test_inst.vm_firewalls, "Expected VM firewall '%s'"
  295. " to be among instance vm_firewalls: [%s]" %
  296. (fw, test_inst.vm_firewalls))
  297. # Check removing a VM firewall from a running instance
  298. test_inst.remove_vm_firewall(fw)
  299. test_inst.refresh()
  300. self.assertTrue(
  301. fw not in test_inst.vm_firewalls, "Expected VM firewall"
  302. " '%s' to be removed from instance vm_firewalls: [%s]" %
  303. (fw, test_inst.vm_firewalls))
  304. # check floating ips
  305. router = self.provider.networking.routers.create(label, net)
  306. gateway = net.gateways.get_or_create_inet_gateway()
  307. def cleanup_router(router, gateway):
  308. with helpers.cleanup_action(lambda: router.delete()):
  309. with helpers.cleanup_action(lambda: gateway.delete()):
  310. router.detach_subnet(subnet)
  311. router.detach_gateway(gateway)
  312. with helpers.cleanup_action(lambda: cleanup_router(router,
  313. gateway)):
  314. router.attach_subnet(subnet)
  315. router.attach_gateway(gateway)
  316. fip = None
  317. with helpers.cleanup_action(lambda: helpers.cleanup_fip(fip)):
  318. # check whether adding an elastic ip works
  319. fip = gateway.floating_ips.create()
  320. self.assertFalse(
  321. fip.in_use,
  322. "Newly created floating IP %s should not be in use." %
  323. fip.public_ip)
  324. with helpers.cleanup_action(
  325. lambda: test_inst.remove_floating_ip(fip)):
  326. test_inst.add_floating_ip(fip)
  327. test_inst.refresh()
  328. # On Devstack, FloatingIP is listed under private_ips.
  329. self.assertIn(fip.public_ip, test_inst.public_ips +
  330. test_inst.private_ips)
  331. fip.refresh()
  332. self.assertTrue(
  333. fip.in_use,
  334. "Attached floating IP %s address should be in use."
  335. % fip.public_ip)
  336. test_inst.refresh()
  337. test_inst.reboot()
  338. test_inst.wait_till_ready()
  339. self.assertNotIn(
  340. fip.public_ip,
  341. test_inst.public_ips + test_inst.private_ips)