OpenStack Compute (Nova)
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

898 líneas
37KB

  1. # Copyright 2011 Justin Santa Barbara
  2. # All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """
  16. Provides common functionality for integrated unit tests
  17. """
  18. import collections
  19. import random
  20. import six
  21. import string
  22. import time
  23. import os_traits
  24. from oslo_log import log as logging
  25. from oslo_utils.fixture import uuidsentinel as uuids
  26. import nova.conf
  27. from nova import context
  28. from nova.db import api as db
  29. import nova.image.glance
  30. from nova import objects
  31. from nova import test
  32. from nova.tests import fixtures as nova_fixtures
  33. from nova.tests.functional.api import client as api_client
  34. from nova.tests.functional import fixtures as func_fixtures
  35. from nova.tests.unit import cast_as_call
  36. from nova.tests.unit import fake_notifier
  37. import nova.tests.unit.image.fake
  38. from nova.tests.unit import policy_fixture
  39. from nova import utils
  40. CONF = nova.conf.CONF
  41. LOG = logging.getLogger(__name__)
  42. def generate_random_alphanumeric(length):
  43. """Creates a random alphanumeric string of specified length."""
  44. return ''.join(random.choice(string.ascii_uppercase + string.digits)
  45. for _x in range(length))
  46. def generate_random_numeric(length):
  47. """Creates a random numeric string of specified length."""
  48. return ''.join(random.choice(string.digits)
  49. for _x in range(length))
  50. def generate_new_element(items, prefix, numeric=False):
  51. """Creates a random string with prefix, that is not in 'items' list."""
  52. while True:
  53. if numeric:
  54. candidate = prefix + generate_random_numeric(8)
  55. else:
  56. candidate = prefix + generate_random_alphanumeric(8)
  57. if candidate not in items:
  58. return candidate
  59. LOG.debug("Random collision on %s", candidate)
  60. class _IntegratedTestBase(test.TestCase):
  61. REQUIRES_LOCKING = True
  62. ADMIN_API = False
  63. # Override this in subclasses which use the NeutronFixture. New tests
  64. # should rely on Neutron since nova-network is deprecated. The default
  65. # value of False here is only temporary while we update the existing
  66. # functional tests to use Neutron.
  67. USE_NEUTRON = False
  68. def setUp(self):
  69. super(_IntegratedTestBase, self).setUp()
  70. # TODO(mriedem): Fix the functional tests to work with Neutron.
  71. self.flags(use_neutron=self.USE_NEUTRON)
  72. # NOTE(mikal): this is used to stub away privsep helpers
  73. def fake_noop(*args, **kwargs):
  74. return None
  75. self.stub_out('nova.privsep.linux_net.bind_ip', fake_noop)
  76. self.fake_image_service =\
  77. nova.tests.unit.image.fake.stub_out_image_service(self)
  78. self.useFixture(cast_as_call.CastAsCall(self))
  79. placement = self.useFixture(func_fixtures.PlacementFixture())
  80. self.placement_api = placement.api
  81. self._setup_services()
  82. self.addCleanup(nova.tests.unit.image.fake.FakeImageService_reset)
  83. def _setup_compute_service(self):
  84. return self.start_service('compute')
  85. def _setup_scheduler_service(self):
  86. return self.start_service('scheduler')
  87. def _setup_services(self):
  88. # NOTE(danms): Set the global MQ connection to that of our first cell
  89. # for any cells-ignorant code. Normally this is defaulted in the tests
  90. # which will result in us not doing the right thing.
  91. if 'cell1' in self.cell_mappings:
  92. self.flags(transport_url=self.cell_mappings['cell1'].transport_url)
  93. self.conductor = self.start_service('conductor')
  94. self.consoleauth = self.start_service('consoleauth')
  95. if self.USE_NEUTRON:
  96. self.neutron = self.useFixture(nova_fixtures.NeutronFixture(self))
  97. else:
  98. self.network = self.start_service('network',
  99. manager=CONF.network_manager)
  100. self.scheduler = self._setup_scheduler_service()
  101. self.compute = self._setup_compute_service()
  102. self.api_fixture = self.useFixture(
  103. nova_fixtures.OSAPIFixture(self.api_major_version))
  104. # if the class needs to run as admin, make the api endpoint
  105. # the admin, otherwise it's safer to run as non admin user.
  106. if self.ADMIN_API:
  107. self.api = self.api_fixture.admin_api
  108. else:
  109. self.api = self.api_fixture.api
  110. if hasattr(self, 'microversion'):
  111. self.api.microversion = self.microversion
  112. def get_unused_server_name(self):
  113. servers = self.api.get_servers()
  114. server_names = [server['name'] for server in servers]
  115. return generate_new_element(server_names, 'server')
  116. def get_unused_flavor_name_id(self):
  117. flavors = self.api.get_flavors()
  118. flavor_names = list()
  119. flavor_ids = list()
  120. [(flavor_names.append(flavor['name']),
  121. flavor_ids.append(flavor['id']))
  122. for flavor in flavors]
  123. return (generate_new_element(flavor_names, 'flavor'),
  124. int(generate_new_element(flavor_ids, '', True)))
  125. def get_invalid_image(self):
  126. return uuids.fake
  127. def _build_minimal_create_server_request(self, image_uuid=None):
  128. server = {}
  129. # NOTE(takashin): In API version 2.36, image APIs were deprecated.
  130. # In API version 2.36 or greater, self.api.get_images() returns
  131. # a 404 error. In that case, 'image_uuid' should be specified.
  132. server[self._image_ref_parameter] = (image_uuid or
  133. self.api.get_images()[0]['id'])
  134. # Set a valid flavorId
  135. flavor = self.api.get_flavors()[0]
  136. LOG.debug("Using flavor: %s", flavor)
  137. server[self._flavor_ref_parameter] = ('http://fake.server/%s'
  138. % flavor['id'])
  139. # Set a valid server name
  140. server_name = self.get_unused_server_name()
  141. server['name'] = server_name
  142. return server
  143. def _create_flavor_body(self, name, ram, vcpus, disk, ephemeral, id, swap,
  144. rxtx_factor, is_public):
  145. return {
  146. "flavor": {
  147. "name": name,
  148. "ram": ram,
  149. "vcpus": vcpus,
  150. "disk": disk,
  151. "OS-FLV-EXT-DATA:ephemeral": ephemeral,
  152. "id": id,
  153. "swap": swap,
  154. "rxtx_factor": rxtx_factor,
  155. "os-flavor-access:is_public": is_public,
  156. }
  157. }
  158. def _create_flavor(self, memory_mb=2048, vcpu=2, disk=10, ephemeral=10,
  159. swap=0, rxtx_factor=1.0, is_public=True,
  160. extra_spec=None):
  161. flv_name, flv_id = self.get_unused_flavor_name_id()
  162. body = self._create_flavor_body(flv_name, memory_mb, vcpu, disk,
  163. ephemeral, flv_id, swap, rxtx_factor,
  164. is_public)
  165. self.api_fixture.admin_api.post_flavor(body)
  166. if extra_spec is not None:
  167. spec = {"extra_specs": extra_spec}
  168. self.api_fixture.admin_api.post_extra_spec(flv_id, spec)
  169. return flv_id
  170. def _build_server(self, flavor_id, image=None):
  171. server = {}
  172. if image is None:
  173. image = self.api.get_images()[0]
  174. LOG.debug("Image: %s", image)
  175. # We now have a valid imageId
  176. server[self._image_ref_parameter] = image['id']
  177. else:
  178. server[self._image_ref_parameter] = image
  179. # Set a valid flavorId
  180. flavor = self.api.get_flavor(flavor_id)
  181. LOG.debug("Using flavor: %s", flavor)
  182. server[self._flavor_ref_parameter] = ('http://fake.server/%s'
  183. % flavor['id'])
  184. # Set a valid server name
  185. server_name = self.get_unused_server_name()
  186. server['name'] = server_name
  187. return server
  188. def _check_api_endpoint(self, endpoint, expected_middleware):
  189. app = self.api_fixture.app().get((None, '/v2'))
  190. while getattr(app, 'application', False):
  191. for middleware in expected_middleware:
  192. if isinstance(app.application, middleware):
  193. expected_middleware.remove(middleware)
  194. break
  195. app = app.application
  196. self.assertEqual([],
  197. expected_middleware,
  198. ("The expected wsgi middlewares %s are not "
  199. "existed") % expected_middleware)
  200. class InstanceHelperMixin(object):
  201. def _wait_for_server_parameter(self, admin_api, server, expected_params,
  202. max_retries=10):
  203. retry_count = 0
  204. while True:
  205. server = admin_api.get_server(server['id'])
  206. if all([server[attr] == expected_params[attr]
  207. for attr in expected_params]):
  208. break
  209. retry_count += 1
  210. if retry_count == max_retries:
  211. self.fail('Wait for state change failed, '
  212. 'expected_params=%s, server=%s'
  213. % (expected_params, server))
  214. time.sleep(0.5)
  215. return server
  216. def _wait_for_state_change(self, admin_api, server, expected_status,
  217. max_retries=10):
  218. return self._wait_for_server_parameter(
  219. admin_api, server, {'status': expected_status}, max_retries)
  220. def _build_minimal_create_server_request(self, api, name, image_uuid=None,
  221. flavor_id=None, networks=None,
  222. az=None):
  223. server = {}
  224. # We now have a valid imageId
  225. server['imageRef'] = image_uuid or api.get_images()[0]['id']
  226. if not flavor_id:
  227. # Set a valid flavorId
  228. flavor_id = api.get_flavors()[1]['id']
  229. server['flavorRef'] = ('http://fake.server/%s' % flavor_id)
  230. server['name'] = name
  231. if networks is not None:
  232. server['networks'] = networks
  233. if az is not None:
  234. server['availability_zone'] = az
  235. return server
  236. def _wait_until_deleted(self, server):
  237. initially_in_error = (server['status'] == 'ERROR')
  238. try:
  239. for i in range(40):
  240. server = self.api.get_server(server['id'])
  241. if not initially_in_error and server['status'] == 'ERROR':
  242. self.fail('Server went to error state instead of'
  243. 'disappearing.')
  244. time.sleep(0.5)
  245. self.fail('Server failed to delete.')
  246. except api_client.OpenStackApiNotFoundException:
  247. return
  248. def _wait_for_action_fail_completion(
  249. self, server, expected_action, event_name, api=None):
  250. """Polls instance action events for the given instance, action and
  251. action event name until it finds the action event with an error
  252. result.
  253. """
  254. if api is None:
  255. api = self.api
  256. completion_event = None
  257. for attempt in range(10):
  258. actions = api.get_instance_actions(server['id'])
  259. # Look for the migrate action.
  260. for action in actions:
  261. if action['action'] == expected_action:
  262. events = (
  263. api.api_get(
  264. '/servers/%s/os-instance-actions/%s' %
  265. (server['id'], action['request_id'])
  266. ).body['instanceAction']['events'])
  267. # Look for the action event being in error state.
  268. for event in events:
  269. if (event['event'] == event_name and
  270. event['result'] is not None and
  271. event['result'].lower() == 'error'):
  272. completion_event = event
  273. # Break out of the events loop.
  274. break
  275. if completion_event:
  276. # Break out of the actions loop.
  277. break
  278. # We didn't find the completion event yet, so wait a bit.
  279. time.sleep(0.5)
  280. if completion_event is None:
  281. self.fail('Timed out waiting for %s failure event. Current '
  282. 'instance actions: %s' % (event_name, actions))
  283. def _wait_for_migration_status(self, server, expected_statuses):
  284. """Waits for a migration record with the given statuses to be found
  285. for the given server, else the test fails. The migration record, if
  286. found, is returned.
  287. """
  288. api = getattr(self, 'admin_api', None)
  289. if api is None:
  290. api = self.api
  291. statuses = [status.lower() for status in expected_statuses]
  292. for attempt in range(10):
  293. migrations = api.api_get('/os-migrations').body['migrations']
  294. for migration in migrations:
  295. if (migration['instance_uuid'] == server['id'] and
  296. migration['status'].lower() in statuses):
  297. return migration
  298. time.sleep(0.5)
  299. self.fail('Timed out waiting for migration with status "%s" for '
  300. 'instance: %s' % (expected_statuses, server['id']))
  301. def _wait_for_log(self, log_line):
  302. for i in range(10):
  303. if log_line in self.stdlog.logger.output:
  304. return
  305. time.sleep(0.5)
  306. self.fail('The line "%(log_line)s" did not appear in the log')
  307. class ProviderUsageBaseTestCase(test.TestCase, InstanceHelperMixin):
  308. """Base test class for functional tests that check provider usage
  309. and consumer allocations in Placement during various operations.
  310. Subclasses must define a **compute_driver** attribute for the virt driver
  311. to use.
  312. This class sets up standard fixtures and controller services but does not
  313. start any compute services, that is left to the subclass.
  314. """
  315. microversion = 'latest'
  316. # These must match the capabilities in
  317. # nova.virt.libvirt.driver.LibvirtDriver.capabilities
  318. expected_libvirt_driver_capability_traits = set([
  319. six.u(trait) for trait in [
  320. os_traits.COMPUTE_DEVICE_TAGGING,
  321. os_traits.COMPUTE_NET_ATTACH_INTERFACE,
  322. os_traits.COMPUTE_NET_ATTACH_INTERFACE_WITH_TAG,
  323. os_traits.COMPUTE_VOLUME_ATTACH_WITH_TAG,
  324. os_traits.COMPUTE_VOLUME_EXTEND,
  325. os_traits.COMPUTE_TRUSTED_CERTS,
  326. ]
  327. ])
  328. # These must match the capabilities in
  329. # nova.virt.fake.FakeDriver.capabilities
  330. expected_fake_driver_capability_traits = set([
  331. six.u(trait) for trait in [
  332. os_traits.COMPUTE_NET_ATTACH_INTERFACE,
  333. os_traits.COMPUTE_NET_ATTACH_INTERFACE_WITH_TAG,
  334. os_traits.COMPUTE_VOLUME_ATTACH_WITH_TAG,
  335. os_traits.COMPUTE_VOLUME_EXTEND,
  336. os_traits.COMPUTE_VOLUME_MULTI_ATTACH,
  337. os_traits.COMPUTE_TRUSTED_CERTS,
  338. ]
  339. ])
  340. def setUp(self):
  341. self.flags(compute_driver=self.compute_driver)
  342. super(ProviderUsageBaseTestCase, self).setUp()
  343. self.policy = self.useFixture(policy_fixture.RealPolicyFixture())
  344. self.neutron = self.useFixture(nova_fixtures.NeutronFixture(self))
  345. self.useFixture(nova_fixtures.AllServicesCurrent())
  346. fake_notifier.stub_notifier(self)
  347. self.addCleanup(fake_notifier.reset)
  348. placement = self.useFixture(func_fixtures.PlacementFixture())
  349. self.placement_api = placement.api
  350. self.api_fixture = self.useFixture(nova_fixtures.OSAPIFixture(
  351. api_version='v2.1'))
  352. self.admin_api = self.api_fixture.admin_api
  353. self.admin_api.microversion = self.microversion
  354. self.api = self.admin_api
  355. # the image fake backend needed for image discovery
  356. nova.tests.unit.image.fake.stub_out_image_service(self)
  357. self.start_service('conductor')
  358. self.scheduler_service = self.start_service('scheduler')
  359. self.addCleanup(nova.tests.unit.image.fake.FakeImageService_reset)
  360. self.computes = {}
  361. def _start_compute(self, host, cell_name=None):
  362. """Start a nova compute service on the given host
  363. :param host: the name of the host that will be associated to the
  364. compute service.
  365. :param cell_name: optional name of the cell in which to start the
  366. compute service (defaults to cell1)
  367. :return: the nova compute service object
  368. """
  369. compute = self.start_service('compute', host=host, cell=cell_name)
  370. self.computes[host] = compute
  371. return compute
  372. def _get_provider_uuid_by_host(self, host):
  373. # NOTE(gibi): the compute node id is the same as the compute node
  374. # provider uuid on that compute
  375. resp = self.admin_api.api_get(
  376. 'os-hypervisors?hypervisor_hostname_pattern=%s' % host).body
  377. return resp['hypervisors'][0]['id']
  378. def _get_provider_usages(self, provider_uuid):
  379. return self.placement_api.get(
  380. '/resource_providers/%s/usages' % provider_uuid).body['usages']
  381. def _get_allocations_by_server_uuid(self, server_uuid):
  382. return self.placement_api.get(
  383. '/allocations/%s' % server_uuid).body['allocations']
  384. def _get_allocations_by_provider_uuid(self, rp_uuid):
  385. return self.placement_api.get(
  386. '/resource_providers/%s/allocations' % rp_uuid).body['allocations']
  387. def _get_all_providers(self):
  388. return self.placement_api.get(
  389. '/resource_providers', version='1.14').body['resource_providers']
  390. def _create_trait(self, trait):
  391. return self.placement_api.put('/traits/%s' % trait, {}, version='1.6')
  392. def _delete_trait(self, trait):
  393. return self.placement_api.delete('/traits/%s' % trait, version='1.6')
  394. def _get_provider_traits(self, provider_uuid):
  395. return self.placement_api.get(
  396. '/resource_providers/%s/traits' % provider_uuid,
  397. version='1.6').body['traits']
  398. def _set_provider_traits(self, rp_uuid, traits):
  399. """This will overwrite any existing traits.
  400. :param rp_uuid: UUID of the resource provider to update
  401. :param traits: list of trait strings to set on the provider
  402. :returns: APIResponse object with the results
  403. """
  404. provider = self.placement_api.get(
  405. '/resource_providers/%s' % rp_uuid).body
  406. put_traits_req = {
  407. 'resource_provider_generation': provider['generation'],
  408. 'traits': traits
  409. }
  410. return self.placement_api.put(
  411. '/resource_providers/%s/traits' % rp_uuid,
  412. put_traits_req, version='1.6')
  413. def _get_all_resource_classes(self):
  414. dicts = self.placement_api.get(
  415. '/resource_classes', version='1.2').body['resource_classes']
  416. return [d['name'] for d in dicts]
  417. def _get_all_traits(self):
  418. return self.placement_api.get('/traits', version='1.6').body['traits']
  419. def _get_provider_inventory(self, rp_uuid):
  420. return self.placement_api.get(
  421. '/resource_providers/%s/inventories' % rp_uuid).body['inventories']
  422. def _get_provider_aggregates(self, rp_uuid):
  423. return self.placement_api.get(
  424. '/resource_providers/%s/aggregates' % rp_uuid,
  425. version='1.1').body['aggregates']
  426. def _post_resource_provider(self, rp_name):
  427. return self.placement_api.post(
  428. url='/resource_providers',
  429. version='1.20', body={'name': rp_name}).body
  430. def _set_inventory(self, rp_uuid, inv_body):
  431. """This will set the inventory for a given resource provider.
  432. :param rp_uuid: UUID of the resource provider to update
  433. :param inv_body: inventory to set on the provider
  434. :returns: APIResponse object with the results
  435. """
  436. return self.placement_api.post(
  437. url= ('/resource_providers/%s/inventories' % rp_uuid),
  438. version='1.15', body=inv_body).body
  439. def _update_inventory(self, rp_uuid, inv_body):
  440. """This will update the inventory for a given resource provider.
  441. :param rp_uuid: UUID of the resource provider to update
  442. :param inv_body: inventory to set on the provider
  443. :returns: APIResponse object with the results
  444. """
  445. return self.placement_api.put(
  446. url= ('/resource_providers/%s/inventories' % rp_uuid),
  447. body=inv_body).body
  448. def _get_resource_provider_by_uuid(self, rp_uuid):
  449. return self.placement_api.get(
  450. '/resource_providers/%s' % rp_uuid, version='1.15').body
  451. def _set_aggregate(self, rp_uuid, agg_id):
  452. provider = self.placement_api.get(
  453. '/resource_providers/%s' % rp_uuid).body
  454. post_agg_req = {"aggregates": [agg_id],
  455. "resource_provider_generation": provider['generation']}
  456. return self.placement_api.put(
  457. '/resource_providers/%s/aggregates' % rp_uuid, version='1.19',
  458. body=post_agg_req).body
  459. def _get_all_rp_uuids_in_a_tree(self, in_tree_rp_uuid):
  460. rps = self.placement_api.get(
  461. '/resource_providers?in_tree=%s' % in_tree_rp_uuid,
  462. version='1.20').body['resource_providers']
  463. return [rp['uuid'] for rp in rps]
  464. def assertRequestMatchesUsage(self, requested_resources, root_rp_uuid):
  465. # It matches the usages of the whole tree against the request
  466. rp_uuids = self._get_all_rp_uuids_in_a_tree(root_rp_uuid)
  467. # NOTE(gibi): flattening the placement usages means we cannot
  468. # verify the structure here. However I don't see any way to define this
  469. # function for nested and non-nested trees in a generic way.
  470. total_usage = collections.defaultdict(int)
  471. for rp in rp_uuids:
  472. usage = self._get_provider_usages(rp)
  473. for rc, amount in usage.items():
  474. total_usage[rc] += amount
  475. # Cannot simply do an assertEqual(expected, actual) as usages always
  476. # contain every RC even if the usage is 0 and the flavor could also
  477. # contain explicit 0 request for some resources.
  478. # So if the flavor contains an explicit 0 resource request (e.g. in
  479. # case of ironic resources:VCPU=0) then this code needs to assert that
  480. # such resource has 0 usage in the tree. In the other hand if the usage
  481. # contains 0 value for some resources that the flavor does not request
  482. # then that is totally fine.
  483. for rc, value in requested_resources.items():
  484. self.assertIn(
  485. rc, total_usage,
  486. 'The requested resource class not found in the total_usage of '
  487. 'the RP tree')
  488. self.assertEqual(
  489. value,
  490. total_usage[rc],
  491. 'The requested resource amount does not match with the total '
  492. 'resource usage of the RP tree')
  493. for rc, value in total_usage.items():
  494. if value != 0:
  495. self.assertEqual(
  496. requested_resources[rc],
  497. value,
  498. 'The requested resource amount does not match with the '
  499. 'total resource usage of the RP tree')
  500. def assertFlavorMatchesUsage(self, root_rp_uuid, *flavors):
  501. resources = collections.defaultdict(int)
  502. for flavor in flavors:
  503. res = self._resources_from_flavor(flavor)
  504. for rc, value in res.items():
  505. resources[rc] += value
  506. self.assertRequestMatchesUsage(resources, root_rp_uuid)
  507. def _resources_from_flavor(self, flavor):
  508. resources = collections.defaultdict(int)
  509. resources['VCPU'] = flavor['vcpus']
  510. resources['MEMORY_MB'] = flavor['ram']
  511. resources['DISK_GB'] = flavor['disk']
  512. for key, value in flavor['extra_specs'].items():
  513. if key.startswith('resources'):
  514. resources[key.split(':')[1]] += value
  515. return resources
  516. def assertFlavorMatchesAllocation(self, flavor, consumer_uuid,
  517. root_rp_uuid):
  518. # NOTE(gibi): This function does not handle sharing RPs today.
  519. expected_rps = self._get_all_rp_uuids_in_a_tree(root_rp_uuid)
  520. allocations = self._get_allocations_by_server_uuid(consumer_uuid)
  521. # NOTE(gibi): flattening the placement allocation means we cannot
  522. # verify the structure here. However I don't see any way to define this
  523. # function for nested and non-nested trees in a generic way.
  524. total_allocation = collections.defaultdict(int)
  525. for rp, alloc in allocations.items():
  526. self.assertIn(rp, expected_rps, 'Unexpected, out of tree RP in the'
  527. ' allocation')
  528. for rc, value in alloc['resources'].items():
  529. total_allocation[rc] += value
  530. self.assertEqual(
  531. self._resources_from_flavor(flavor),
  532. total_allocation,
  533. 'The resources requested in the flavor does not match with total '
  534. 'allocation in the RP tree')
  535. def get_migration_uuid_for_instance(self, instance_uuid):
  536. # NOTE(danms): This is too much introspection for a test like this, but
  537. # we can't see the migration uuid from the API, so we just encapsulate
  538. # the peek behind the curtains here to keep it out of the tests.
  539. # TODO(danms): Get the migration uuid from the API once it is exposed
  540. ctxt = context.get_admin_context()
  541. migrations = db.migration_get_all_by_filters(
  542. ctxt, {'instance_uuid': instance_uuid})
  543. self.assertEqual(1, len(migrations),
  544. 'Test expected a single migration, '
  545. 'but found %i' % len(migrations))
  546. return migrations[0].uuid
  547. def _boot_and_check_allocations(self, flavor, source_hostname):
  548. """Boot an instance and check that the resource allocation is correct
  549. After booting an instance on the given host with a given flavor it
  550. asserts that both the providers usages and resource allocations match
  551. with the resources requested in the flavor. It also asserts that
  552. running the periodic update_available_resource call does not change the
  553. resource state.
  554. :param flavor: the flavor the instance will be booted with
  555. :param source_hostname: the name of the host the instance will be
  556. booted on
  557. :return: the API representation of the booted instance
  558. """
  559. server_req = self._build_minimal_create_server_request(
  560. self.api, 'some-server', flavor_id=flavor['id'],
  561. image_uuid='155d900f-4e14-4e4c-a73d-069cbf4541e6',
  562. networks='none')
  563. server_req['availability_zone'] = 'nova:%s' % source_hostname
  564. LOG.info('booting on %s', source_hostname)
  565. created_server = self.api.post_server({'server': server_req})
  566. server = self._wait_for_state_change(
  567. self.admin_api, created_server, 'ACTIVE')
  568. # Verify that our source host is what the server ended up on
  569. self.assertEqual(source_hostname, server['OS-EXT-SRV-ATTR:host'])
  570. source_rp_uuid = self._get_provider_uuid_by_host(source_hostname)
  571. # Before we run periodics, make sure that we have allocations/usages
  572. # only on the source host
  573. self.assertFlavorMatchesUsage(source_rp_uuid, flavor)
  574. # Check that the other providers has no usage
  575. for rp_uuid in [self._get_provider_uuid_by_host(hostname)
  576. for hostname in self.computes.keys()
  577. if hostname != source_hostname]:
  578. self.assertRequestMatchesUsage({'VCPU': 0,
  579. 'MEMORY_MB': 0,
  580. 'DISK_GB': 0}, rp_uuid)
  581. # Check that the server only allocates resource from the host it is
  582. # booted on
  583. self.assertFlavorMatchesAllocation(flavor, server['id'],
  584. source_rp_uuid)
  585. self._run_periodics()
  586. # After running the periodics but before we start any other operation,
  587. # we should have exactly the same allocation/usage information as
  588. # before running the periodics
  589. # Check usages on the selected host after boot
  590. self.assertFlavorMatchesUsage(source_rp_uuid, flavor)
  591. # Check that the server only allocates resource from the host it is
  592. # booted on
  593. self.assertFlavorMatchesAllocation(flavor, server['id'],
  594. source_rp_uuid)
  595. # Check that the other providers has no usage
  596. for rp_uuid in [self._get_provider_uuid_by_host(hostname)
  597. for hostname in self.computes.keys()
  598. if hostname != source_hostname]:
  599. self.assertRequestMatchesUsage({'VCPU': 0,
  600. 'MEMORY_MB': 0,
  601. 'DISK_GB': 0}, rp_uuid)
  602. return server
  603. def _delete_and_check_allocations(self, server):
  604. """Delete the instance and asserts that the allocations are cleaned
  605. If the server was moved (resized or live migrated), also checks that
  606. migration-based allocations are also cleaned up.
  607. :param server: The API representation of the instance to be deleted
  608. """
  609. # First check to see if there is a related migration record so we can
  610. # assert its allocations (if any) are not leaked.
  611. with utils.temporary_mutation(self.admin_api, microversion='2.59'):
  612. migrations = self.admin_api.api_get(
  613. '/os-migrations?instance_uuid=%s' %
  614. server['id']).body['migrations']
  615. if migrations:
  616. # If there is more than one migration, they are sorted by
  617. # created_at in descending order so we'll get the last one
  618. # which is probably what we'd always want anyway.
  619. migration_uuid = migrations[0]['uuid']
  620. else:
  621. migration_uuid = None
  622. self.api.delete_server(server['id'])
  623. self._wait_until_deleted(server)
  624. # NOTE(gibi): The resource allocation is deleted after the instance is
  625. # destroyed in the db so wait_until_deleted might return before the
  626. # the resource are deleted in placement. So we need to wait for the
  627. # instance.delete.end notification as that is emitted after the
  628. # resources are freed.
  629. fake_notifier.wait_for_versioned_notifications('instance.delete.end')
  630. for rp_uuid in [self._get_provider_uuid_by_host(hostname)
  631. for hostname in self.computes.keys()]:
  632. self.assertRequestMatchesUsage({'VCPU': 0,
  633. 'MEMORY_MB': 0,
  634. 'DISK_GB': 0}, rp_uuid)
  635. # and no allocations for the deleted server
  636. allocations = self._get_allocations_by_server_uuid(server['id'])
  637. self.assertEqual(0, len(allocations))
  638. if migration_uuid:
  639. # and no allocations for the delete migration
  640. allocations = self._get_allocations_by_server_uuid(migration_uuid)
  641. self.assertEqual(0, len(allocations))
  642. def _run_periodics(self):
  643. """Run the update_available_resource task on every compute manager
  644. This runs periodics on the computes in an undefined order; some child
  645. class redefined this function to force a specific order.
  646. """
  647. ctx = context.get_admin_context()
  648. for compute in self.computes.values():
  649. LOG.info('Running periodic for compute (%s)',
  650. compute.manager.host)
  651. compute.manager.update_available_resource(ctx)
  652. LOG.info('Finished with periodics')
  653. def _move_and_check_allocations(self, server, request, old_flavor,
  654. new_flavor, source_rp_uuid, dest_rp_uuid):
  655. self.api.post_server_action(server['id'], request)
  656. self._wait_for_state_change(self.api, server, 'VERIFY_RESIZE')
  657. def _check_allocation():
  658. self.assertFlavorMatchesUsage(source_rp_uuid, old_flavor)
  659. self.assertFlavorMatchesUsage(dest_rp_uuid, new_flavor)
  660. # The instance should own the new_flavor allocation against the
  661. # destination host created by the scheduler
  662. self.assertFlavorMatchesAllocation(new_flavor, server['id'],
  663. dest_rp_uuid)
  664. # The migration should own the old_flavor allocation against the
  665. # source host created by conductor
  666. migration_uuid = self.get_migration_uuid_for_instance(server['id'])
  667. self.assertFlavorMatchesAllocation(old_flavor, migration_uuid,
  668. source_rp_uuid)
  669. # OK, so the move operation has run, but we have not yet confirmed or
  670. # reverted the move operation. Before we run periodics, make sure
  671. # that we have allocations/usages on BOTH the source and the
  672. # destination hosts.
  673. _check_allocation()
  674. self._run_periodics()
  675. _check_allocation()
  676. # Make sure the RequestSpec.flavor matches the new_flavor.
  677. ctxt = context.get_admin_context()
  678. reqspec = objects.RequestSpec.get_by_instance_uuid(ctxt, server['id'])
  679. self.assertEqual(new_flavor['id'], reqspec.flavor.flavorid)
  680. def _migrate_and_check_allocations(self, server, flavor, source_rp_uuid,
  681. dest_rp_uuid):
  682. request = {
  683. 'migrate': None
  684. }
  685. self._move_and_check_allocations(
  686. server, request=request, old_flavor=flavor, new_flavor=flavor,
  687. source_rp_uuid=source_rp_uuid, dest_rp_uuid=dest_rp_uuid)
  688. def _resize_and_check_allocations(self, server, old_flavor, new_flavor,
  689. source_rp_uuid, dest_rp_uuid):
  690. request = {
  691. 'resize': {
  692. 'flavorRef': new_flavor['id']
  693. }
  694. }
  695. self._move_and_check_allocations(
  696. server, request=request, old_flavor=old_flavor,
  697. new_flavor=new_flavor, source_rp_uuid=source_rp_uuid,
  698. dest_rp_uuid=dest_rp_uuid)
  699. def _resize_to_same_host_and_check_allocations(self, server, old_flavor,
  700. new_flavor, rp_uuid):
  701. # Resize the server to the same host and check usages in VERIFY_RESIZE
  702. # state
  703. self.flags(allow_resize_to_same_host=True)
  704. resize_req = {
  705. 'resize': {
  706. 'flavorRef': new_flavor['id']
  707. }
  708. }
  709. self.api.post_server_action(server['id'], resize_req)
  710. self._wait_for_state_change(self.api, server, 'VERIFY_RESIZE')
  711. self.assertFlavorMatchesUsage(rp_uuid, old_flavor, new_flavor)
  712. # The instance should hold a new_flavor allocation
  713. self.assertFlavorMatchesAllocation(new_flavor, server['id'],
  714. rp_uuid)
  715. # The migration should hold an old_flavor allocation
  716. migration_uuid = self.get_migration_uuid_for_instance(server['id'])
  717. self.assertFlavorMatchesAllocation(old_flavor, migration_uuid,
  718. rp_uuid)
  719. # We've resized to the same host and have doubled allocations for both
  720. # the old and new flavor on the same host. Run the periodic on the
  721. # compute to see if it tramples on what the scheduler did.
  722. self._run_periodics()
  723. # In terms of usage, it's still double on the host because the instance
  724. # and the migration each hold an allocation for the new and old
  725. # flavors respectively.
  726. self.assertFlavorMatchesUsage(rp_uuid, old_flavor, new_flavor)
  727. # The instance should hold a new_flavor allocation
  728. self.assertFlavorMatchesAllocation(new_flavor, server['id'],
  729. rp_uuid)
  730. # The migration should hold an old_flavor allocation
  731. self.assertFlavorMatchesAllocation(old_flavor, migration_uuid,
  732. rp_uuid)
  733. def _check_allocation_during_evacuate(
  734. self, flavor, server_uuid, source_root_rp_uuid, dest_root_rp_uuid):
  735. allocations = self._get_allocations_by_server_uuid(server_uuid)
  736. self.assertEqual(2, len(allocations))
  737. self.assertFlavorMatchesUsage(source_root_rp_uuid, flavor)
  738. self.assertFlavorMatchesUsage(dest_root_rp_uuid, flavor)
  739. def _revert_resize(self, server):
  740. self.api.post_server_action(server['id'], {'revertResize': None})
  741. server = self._wait_for_state_change(self.api, server, 'ACTIVE')
  742. self._wait_for_migration_status(server, ['reverted'])
  743. # Note that the migration status is changed to "reverted" in the
  744. # dest host revert_resize method but the allocations are cleaned up
  745. # in the source host finish_revert_resize method so we need to wait
  746. # for the finish_revert_resize method to complete.
  747. fake_notifier.wait_for_versioned_notifications(
  748. 'instance.resize_revert.end')
  749. return server