Skip to content

Commit

Permalink
Merge "functional: Remove 'api' parameter"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed Dec 12, 2019
2 parents 66ffed4 + 7ae1a10 commit 2e7a008
Show file tree
Hide file tree
Showing 65 changed files with 449 additions and 519 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def setUp(self):
response_data = api_samples_test_base.pretty_data(response.content)
actions = api_samples_test_base.objectify(response_data)
self.action_stop = actions['instanceActions'][0]
self._wait_for_state_change(self.api, {'id': self.uuid}, 'SHUTOFF')
self._wait_for_state_change({'id': self.uuid}, 'SHUTOFF')

def _get_subs(self):
return {
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/functional/api_sample_tests/test_multinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def setUp(self):

def _boot_a_server(self, expected_status='ACTIVE', extra_params=None):
server = self._build_minimal_create_server_request(
self.api, 'MultinicSampleJsonTestServer')
'MultinicSampleJsonTestServer')
if extra_params:
server.update(extra_params)

created_server = self.api.post_server({'server': server})

# Wait for it to finish being created
found_server = self._wait_for_state_change(self.api, created_server,
found_server = self._wait_for_state_change(created_server,
expected_status)
return found_server

Expand Down
13 changes: 6 additions & 7 deletions nova/tests/functional/compute/test_init_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def test_migrate_disk_and_power_off_crash_finish_revert_migration(self):
# Create a server, it does not matter on which host it lands.
name = 'test_migrate_disk_and_power_off_crash_finish_revert_migration'
server = self._build_minimal_create_server_request(
self.api, name, image_uuid=fake_image.get_valid_image_id(),
name, image_uuid=fake_image.get_valid_image_id(),
networks='auto')
server = self.api.post_server({'server': server})
server = self._wait_for_state_change(self.admin_api, server, 'ACTIVE')
server = self._wait_for_state_change(server, 'ACTIVE')
# Save the source hostname for assertions later.
source_host = server['OS-EXT-SRV-ATTR:host']

Expand All @@ -66,8 +66,7 @@ def fake_migrate_disk_and_power_off(*args, **kwargs):
self.admin_api.post_server_action(server['id'], {'migrate': None})
# Now wait for the task_state to be reset to None during
# _init_instance.
server = self._wait_for_server_parameter(
self.admin_api, server, {
server = self._wait_for_server_parameter(server, {
'status': 'ACTIVE',
'OS-EXT-STS:task_state': None,
'OS-EXT-SRV-ATTR:host': source_host
Expand Down Expand Up @@ -158,7 +157,7 @@ def test_restart_compute_while_instance_waiting_for_resource_claim(self):
# instance_claim() to stop it. This is less realistic but it works in
# the test env.
server_req = self._build_minimal_create_server_request(
self.api, 'interrupted-server', flavor_id=self.flavor1['id'],
'interrupted-server', flavor_id=self.flavor1['id'],
image_uuid='155d900f-4e14-4e4c-a73d-069cbf4541e6',
networks='none')

Expand All @@ -170,7 +169,7 @@ def sleep_forever(*args, **kwargs):
mock_instance_claim.side_effect = sleep_forever

server = self.api.post_server({'server': server_req})
self._wait_for_state_change(self.admin_api, server, 'BUILD')
self._wait_for_state_change(server, 'BUILD')

# the instance.create.start is the closest thing to the
# instance_claim call we can wait for in the test
Expand All @@ -182,7 +181,7 @@ def sleep_forever(*args, **kwargs):

# We expect that the instance is pushed to ERROR state during the
# compute restart.
self._wait_for_state_change(self.admin_api, server, 'ERROR')
self._wait_for_state_change(server, 'ERROR')
mock_log.assert_called_with(
'Instance spawn was interrupted before instance_claim, setting '
'instance to ERROR state',
Expand Down
4 changes: 2 additions & 2 deletions nova/tests/functional/compute/test_live_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_live_migrate_attachment_delete_fails(self):
'uuid': uuids.working_volume,
'source_type': 'volume',
'destination_type': 'volume'}]}})
server = self._wait_for_state_change(self.api, server, 'ACTIVE')
server = self._wait_for_state_change(server, 'ACTIVE')

source = server['OS-EXT-SRV-ATTR:host']
if source == self.compute.host:
Expand All @@ -87,7 +87,7 @@ def test_live_migrate_attachment_delete_fails(self):
self.stub_out('nova.volume.cinder.API.attachment_delete',
stub_attachment_delete)
self.api.post_server_action(server['id'], post)
self._wait_for_server_parameter(self.api, server,
self._wait_for_server_parameter(server,
{'OS-EXT-SRV-ATTR:host': dest,
'status': 'ACTIVE'})
self.assertEqual(2, stub_attachment_delete.call_count)
Expand Down
91 changes: 48 additions & 43 deletions nova/tests/functional/integrated_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ def generate_new_element(items, prefix, numeric=False):


class InstanceHelperMixin(object):
def _wait_for_server_parameter(self, admin_api, server, expected_params,
max_retries=10):

def _wait_for_server_parameter(
self, server, expected_params, max_retries=10, api=None):
api = api or getattr(self, 'admin_api', self.api)

retry_count = 0
while True:
server = admin_api.get_server(server['id'])
server = api.get_server(server['id'])
if all([server[attr] == expected_params[attr]
for attr in expected_params]):
break
Expand All @@ -90,22 +93,21 @@ def _wait_for_server_parameter(self, admin_api, server, expected_params,

return server

def _wait_for_state_change(self, admin_api, server, expected_status,
max_retries=10):
def _wait_for_state_change(self, server, expected_status, max_retries=10):
return self._wait_for_server_parameter(
admin_api, server, {'status': expected_status}, max_retries)
server, {'status': expected_status}, max_retries)

def _build_minimal_create_server_request(
self, name=None, image_uuid=None, flavor_id=None, networks=None,
az=None, host=None):

def _build_minimal_create_server_request(self, api, name=None,
image_uuid=None, flavor_id=None,
networks=None, az=None,
host=None):
server = {}

if not image_uuid:
# NOTE(takashin): In API version 2.36, image APIs were deprecated.
# In API version 2.36 or greater, self.api.get_images() returns
# a 404 error. In that case, 'image_uuid' should be specified.
image_uuid = api.get_images()[0]['id']
image_uuid = self.api.get_images()[0]['id']
server['imageRef'] = image_uuid

if not name:
Expand All @@ -115,7 +117,7 @@ def _build_minimal_create_server_request(self, api, name=None,

if not flavor_id:
# Set a valid flavorId
flavor_id = api.get_flavors()[0]['id']
flavor_id = self.api.get_flavors()[0]['id']
server['flavorRef'] = 'http://fake.server/%s' % flavor_id

if networks is not None:
Expand All @@ -142,40 +144,43 @@ def _wait_until_deleted(self, server):
return

def _wait_for_action_fail_completion(
self, server, expected_action, event_name, api=None):
self, server, expected_action, event_name):
"""Polls instance action events for the given instance, action and
action event name until it finds the action event with an error
result.
"""
if api is None:
api = self.api
return self._wait_for_instance_action_event(
api, server, expected_action, event_name, event_result='error')
server, expected_action, event_name, event_result='error')

def _wait_for_instance_action_event(
self, api, server, action_name, event_name, event_result):
self, server, action_name, event_name, event_result):
"""Polls the instance action events for the given instance, action,
event, and event result until it finds the event.
"""
api = getattr(self, 'admin_api', self.api)

actions = []
events = []
for attempt in range(10):
actions = api.get_instance_actions(server['id'])
# The API returns the newest event first
for action in actions:
if action['action'] == action_name:
events = (
api.api_get(
'/servers/%s/os-instance-actions/%s' %
(server['id'], action['request_id'])
).body['instanceAction']['events'])
# Look for the action event being in error state.
for event in events:
result = event['result']
if (event['event'] == event_name and
result is not None and
result.lower() == event_result.lower()):
return event
if action['action'] != action_name:
continue

events = api.api_get(
'/servers/%s/os-instance-actions/%s' % (
server['id'], action['request_id'])
).body['instanceAction']['events']

# Look for the action event being in error state.
for event in events:
result = event['result']
if (event['event'] == event_name and
result is not None and
result.lower() == event_result.lower()):
return event

# We didn't find the completion event yet, so wait a bit.
time.sleep(0.5)

Expand All @@ -192,19 +197,16 @@ def _assert_resize_migrate_action_fail(self, server, action, error_in_tb):
:param action: Either "resize" or "migrate" instance action.
:param error_in_tb: Some expected part of the error event traceback.
"""
api = self.admin_api if hasattr(self, 'admin_api') else self.api
event = self._wait_for_action_fail_completion(
server, action, 'conductor_migrate_server', api=api)
server, action, 'conductor_migrate_server')
self.assertIn(error_in_tb, event['traceback'])

def _wait_for_migration_status(self, server, expected_statuses):
"""Waits for a migration record with the given statuses to be found
for the given server, else the test fails. The migration record, if
found, is returned.
"""
api = getattr(self, 'admin_api', None)
if api is None:
api = self.api
api = getattr(self, 'admin_api', self.api)

statuses = [status.lower() for status in expected_statuses]
for attempt in range(10):
Expand Down Expand Up @@ -297,10 +299,14 @@ def _setup_services(self):
self.api = self.api_fixture.admin_api
else:
self.api = self.api_fixture.api
self.admin_api = self.api_fixture.admin_api

if hasattr(self, 'microversion'):
self.api.microversion = self.microversion

if not self.ADMIN_API:
self.admin_api.microversion = self.microversion

def get_unused_server_name(self):
servers = self.api.get_servers()
server_names = [server['name'] for server in servers]
Expand Down Expand Up @@ -728,14 +734,13 @@ def _boot_and_check_allocations(
:return: the API representation of the booted instance
"""
server_req = self._build_minimal_create_server_request(
self.api, 'some-server', flavor_id=flavor['id'],
'some-server', flavor_id=flavor['id'],
image_uuid='155d900f-4e14-4e4c-a73d-069cbf4541e6',
networks=networks)
server_req['availability_zone'] = 'nova:%s' % source_hostname
LOG.info('booting on %s', source_hostname)
created_server = self.api.post_server({'server': server_req})
server = self._wait_for_state_change(
self.admin_api, created_server, 'ACTIVE')
server = self._wait_for_state_change(created_server, 'ACTIVE')

# Verify that our source host is what the server ended up on
self.assertEqual(source_hostname, server['OS-EXT-SRV-ATTR:host'])
Expand Down Expand Up @@ -849,7 +854,7 @@ class redefined this function to force a specific order.
def _move_and_check_allocations(self, server, request, old_flavor,
new_flavor, source_rp_uuid, dest_rp_uuid):
self.api.post_server_action(server['id'], request)
self._wait_for_state_change(self.api, server, 'VERIFY_RESIZE')
self._wait_for_state_change(server, 'VERIFY_RESIZE')

def _check_allocation():
self.assertFlavorMatchesUsage(source_rp_uuid, old_flavor)
Expand Down Expand Up @@ -911,7 +916,7 @@ def _resize_to_same_host_and_check_allocations(self, server, old_flavor,
}
}
self.api.post_server_action(server['id'], resize_req)
self._wait_for_state_change(self.api, server, 'VERIFY_RESIZE')
self._wait_for_state_change(server, 'VERIFY_RESIZE')

self.assertFlavorMatchesUsage(rp_uuid, old_flavor, new_flavor)

Expand Down Expand Up @@ -981,15 +986,15 @@ def assert_hypervisor_usage(self, compute_node_uuid, flavor,

def _confirm_resize(self, server):
self.api.post_server_action(server['id'], {'confirmResize': None})
server = self._wait_for_state_change(self.api, server, 'ACTIVE')
server = self._wait_for_state_change(server, 'ACTIVE')
self._wait_for_instance_action_event(
self.api, server, instance_actions.CONFIRM_RESIZE,
server, instance_actions.CONFIRM_RESIZE,
'compute_confirm_resize', 'success')
return server

def _revert_resize(self, server):
self.api.post_server_action(server['id'], {'revertResize': None})
server = self._wait_for_state_change(self.api, server, 'ACTIVE')
server = self._wait_for_state_change(server, 'ACTIVE')
self._wait_for_migration_status(server, ['reverted'])
# Note that the migration status is changed to "reverted" in the
# dest host revert_resize method but the allocations are cleaned up
Expand Down
10 changes: 5 additions & 5 deletions nova/tests/functional/libvirt/test_shared_resource_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_shared_storage_rp_configuration_with_cn_rp(self):
}
# create server
server = self.api.post_server(server_req_body)
self._wait_for_state_change(self.api, server, 'ACTIVE')
self._wait_for_state_change(server, 'ACTIVE')

# get shared_rp and cn_rp usages
shared_rp_usages = self._get_provider_usages(shared_RP['uuid'])
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_rebuild_instance_with_image_traits_on_shared_rp(self):
}
# create server
server = self.api.post_server(server_req_body)
self._wait_for_state_change(self.api, server, 'ACTIVE')
self._wait_for_state_change(server, 'ACTIVE')

rebuild_image_ref = (
nova.tests.unit.image.fake.AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID)
Expand All @@ -152,7 +152,7 @@ def test_rebuild_instance_with_image_traits_on_shared_rp(self):
self.api.api_post('/servers/%s/action' % server['id'],
rebuild_req_body)
self._wait_for_server_parameter(
self.api, server, {'OS-EXT-STS:task_state': None})
server, {'OS-EXT-STS:task_state': None})

# get shared_rp and cn_rp usages
shared_rp_usages = self._get_provider_usages(shared_rp_uuid)
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_rebuild_instance_with_image_traits_on_shared_rp_no_valid_host(
}
# create server
server = self.api.post_server(server_req_body)
self._wait_for_state_change(self.api, server, 'ACTIVE')
self._wait_for_state_change(server, 'ACTIVE')

rebuild_image_ref = (
nova.tests.unit.image.fake.AUTO_DISK_CONFIG_ENABLED_IMAGE_UUID)
Expand All @@ -216,7 +216,7 @@ def test_rebuild_instance_with_image_traits_on_shared_rp_no_valid_host(
rebuild_req_body)
# Look for the failed rebuild action.
self._wait_for_action_fail_completion(
server, instance_actions.REBUILD, 'rebuild_server', self.admin_api)
server, instance_actions.REBUILD, 'rebuild_server')
# Assert the server image_ref was rolled back on failure.
server = self.api.get_server(server['id'])
self.assertEqual(org_image_id, server['image']['id'])
Expand Down
Loading

0 comments on commit 2e7a008

Please sign in to comment.