update api_samples code to use better variables

_api_version and request_api_version are pretty confusing
semantically. Update these to more meaningful names to make the code
easier to understand: api_major_version, microversion.

Change-Id: Ief6b12fc755a6c48cbef7b82ba226e47850c0b9e
This commit is contained in:
Sean Dague 2015-12-11 13:27:50 -05:00 committed by He Jie Xu
parent df485344df
commit 415b221e33
16 changed files with 82 additions and 81 deletions

View File

@ -45,9 +45,9 @@ CONF = cfg.CONF
#
# Things we need to set:
#
# - _api_version - what version of the API we should be hitting
# - api_major_version - what version of the API we should be hitting
#
# - request_api_version - what API microversion should be used
# - microversion - what API microversion should be used
#
# - _additional_fixtures - any additional fixtures need
#
@ -58,12 +58,12 @@ CONF = cfg.CONF
# microversions, then replace the ``scenarios`` class variable in that
# test class with something like:
#
# [("v2_11", {'_api_version': 'v2.1', 'request_api_version', '2.11'})]
# [("v2_11", {'api_major_version': 'v2.1', 'microversion', '2.11'})]
class ApiSampleTestBaseV21(testscenarios.WithScenarios,
api_samples_test_base.ApiSampleTestBase):
_api_version = 'v2'
api_major_version = 'v2'
# any additional fixtures needed for this scenario
_additional_fixtures = []
sample_dir = None
@ -73,13 +73,13 @@ class ApiSampleTestBaseV21(testscenarios.WithScenarios,
scenarios = [
# test v2 with the v2.1 compatibility stack
('v2', {
'_api_version': 'v2'}),
'api_major_version': 'v2'}),
# test v2.1 base microversion
('v2_1', {
'_api_version': 'v2.1'}),
'api_major_version': 'v2.1'}),
# test v2 with the v2 legacy code
('v2legacy', {
'_api_version': 'v2',
'api_major_version': 'v2',
'_legacy_v2_code': True,
'_additional_fixtures': [
api_paste_fixture.ApiPasteLegacyV2Fixture]})

View File

@ -42,9 +42,9 @@ class ExtensionInfoAllSamplesJsonTest(api_sample_base.ApiSampleTestBaseV21):
# different between the API versions and the legacy vs. new
# stack. We default to the v2.1 case.
template = 'extensions-list-resp'
if self._api_version == 'v2':
if self.api_major_version == 'v2':
template = 'extensions-list-resp-v21-compatible'
if self._api_version == 'v2' and self._legacy_v2_code:
if self.api_major_version == 'v2' and self._legacy_v2_code:
template = 'extensions-list-resp-v2'
self._verify_response(template, subs, response, 200)

View File

@ -28,7 +28,7 @@ CONF.import_opt('osapi_compute_extension',
class FixedIpTest(test_servers.ServersSampleBase):
extension_name = "os-fixed-ips"
request_api_version = None
microversion = None
def _get_flags(self):
f = super(FixedIpTest, self)._get_flags()
@ -97,14 +97,14 @@ class FixedIpTest(test_servers.ServersSampleBase):
# Reserve a Fixed IP.
response = self._do_post('os-fixed-ips/192.168.1.1/action',
'fixedip-post-req', {},
api_version=self.request_api_version)
api_version=self.microversion)
self.assertEqual(202, response.status_code)
self.assertEqual("", response.content)
def _test_get_fixed_ip(self, **kwargs):
# Return data about the given fixed ip.
response = self._do_get('os-fixed-ips/192.168.1.1',
api_version=self.request_api_version)
api_version=self.microversion)
project = {'cidr': '192.168.1.0/24',
'hostname': 'openstack',
'host': 'host',
@ -117,11 +117,11 @@ class FixedIpTest(test_servers.ServersSampleBase):
class FixedIpV24Test(FixedIpTest):
request_api_version = '2.4'
microversion = '2.4'
# NOTE(gmann): microversion tests do not need to run for v2 API
# so defining scenarios only for v2.4 which will run the original tests
# by appending '(v2_4)' in test_id.
scenarios = [('v2_4', {'_api_version': 'v2.1'})]
scenarios = [('v2_4', {'api_major_version': 'v2.1'})]
def test_get_fixed_ip(self):
self._test_get_fixed_ip(reserved=False)

View File

@ -27,7 +27,7 @@ CONF.import_opt('osapi_compute_extension',
class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
request_api_version = None
microversion = None
sample_dir = "keypairs"
expected_delete_status_code = 202
expected_post_status_code = 200
@ -51,7 +51,7 @@ class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
key_name = 'keypair-' + str(uuid.uuid4())
subs = dict(keypair_name=key_name, **kwargs)
response = self._do_post('os-keypairs', 'keypairs-post-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['keypair_name'] = '(%s)' % key_name
@ -75,7 +75,7 @@ class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
}
subs.update(**kwargs)
response = self._do_post('os-keypairs', 'keypairs-import-post-req',
subs, api_version=self.request_api_version)
subs, api_version=self.microversion)
subs = self._get_regexes()
subs['keypair_name'] = '(%s)' % key_name
self._verify_response('keypairs-import-post-resp', subs, response,
@ -85,7 +85,7 @@ class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
# Get api sample of key pairs list request.
key_name = self.test_keypairs_post()
response = self._do_get('os-keypairs',
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['keypair_name'] = '(%s)' % key_name
self._verify_response('keypairs-list-resp', subs, response, 200)
@ -94,7 +94,7 @@ class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
# Get api sample of key pairs get request.
key_name = self.test_keypairs_post()
response = self._do_get('os-keypairs/%s' % key_name,
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['keypair_name'] = '(%s)' % key_name
self._verify_response('keypairs-get-resp', subs, response, 200)
@ -103,19 +103,19 @@ class KeyPairsSampleJsonTest(api_sample_base.ApiSampleTestBaseV21):
# Get api sample of key pairs delete request.
key_name = self.test_keypairs_post()
response = self._do_delete('os-keypairs/%s' % key_name,
api_version=self.request_api_version)
api_version=self.microversion)
self.assertEqual(self.expected_delete_status_code,
response.status_code)
class KeyPairsV22SampleJsonTest(KeyPairsSampleJsonTest):
request_api_version = '2.2'
microversion = '2.2'
expected_post_status_code = 201
expected_delete_status_code = 204
# NOTE(gmann): microversion tests do not need to run for v2 API
# so defining scenarios only for v2.2 which will run the original tests
# by appending '(v2_2)' in test_id.
scenarios = [('v2_2', {'_api_version': 'v2.1'})]
scenarios = [('v2_2', {'api_major_version': 'v2.1'})]
def test_keypairs_post(self):
# NOTE(claudiub): overrides the method with the same name in
@ -131,7 +131,7 @@ class KeyPairsV22SampleJsonTest(KeyPairsSampleJsonTest):
key_name = 'keypair-' + str(uuid.uuid4())
subs = dict(keypair_name=key_name, keypair_type='fakey_type')
response = self._do_post('os-keypairs', 'keypairs-post-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
self.assertEqual(400, response.status_code)
@ -156,7 +156,7 @@ class KeyPairsV22SampleJsonTest(KeyPairsSampleJsonTest):
'public_key': fake_crypto.get_ssh_public_key()
}
response = self._do_post('os-keypairs', 'keypairs-import-post-req',
subs, api_version=self.request_api_version)
subs, api_version=self.microversion)
self.assertEqual(400, response.status_code)
@ -171,10 +171,10 @@ class KeyPairsV22SampleJsonTest(KeyPairsSampleJsonTest):
class KeyPairsV210SampleJsonTest(KeyPairsSampleJsonTest):
ADMIN_API = True
request_api_version = '2.10'
microversion = '2.10'
expected_post_status_code = 201
expected_delete_status_code = 204
scenarios = [('v2_10', {'_api_version': 'v2.1'})]
scenarios = [('v2_10', {'api_major_version': 'v2.1'})]
def test_keypair_create_for_user(self):
subs = {
@ -206,7 +206,7 @@ class KeyPairsV210SampleJsonTest(KeyPairsSampleJsonTest):
}
key_name = self._check_keypairs_post(**subs)
response = self._do_delete('os-keypairs/%s?user_id=fake' % key_name,
api_version=self.request_api_version)
api_version=self.microversion)
self.assertEqual(self.expected_delete_status_code,
response.status_code)
@ -225,7 +225,7 @@ class KeyPairsV210SampleJsonTestNotAdmin(KeyPairsV210SampleJsonTest):
keypair_type=keypair_obj.KEYPAIR_TYPE_SSH,
user_id='fake1')
response = self._do_post('os-keypairs', 'keypairs-post-req', subs,
api_version=self.request_api_version,
api_version=self.microversion,
)
self.assertEqual(403, response.status_code)

View File

@ -84,12 +84,12 @@ class ConsolesSampleJsonTests(test_servers.ServersSampleBase):
class ConsolesV26SampleJsonTests(test_servers.ServersSampleBase):
request_api_version = '2.6'
microversion = '2.6'
extension_name = "os-remote-consoles"
# NOTE(gmann): microversion tests do not need to run for v2 API
# so defining scenarios only for v2.6 which will run the original tests
# by appending '(v2_6)' in test_id.
scenarios = [('v2_6', {'_api_version': 'v2.1'})]
scenarios = [('v2_6', {'api_major_version': 'v2.1'})]
def setUp(self):
super(ConsolesV26SampleJsonTests, self).setUp()
@ -109,9 +109,9 @@ class ConsolesV26SampleJsonTests(test_servers.ServersSampleBase):
class ConsolesV28SampleJsonTests(test_servers.ServersSampleBase):
extension_name = "os-remote-consoles"
request_api_version = '2.8'
scenarios = [('v2_8', {'_api_version': 'v2.1'})]
_api_version = 'v2'
microversion = '2.8'
scenarios = [('v2_8', {'api_major_version': 'v2.1'})]
api_major_version = 'v2'
def setUp(self):
super(ConsolesV28SampleJsonTests, self).setUp()

View File

@ -59,7 +59,7 @@ class ServersSampleBase(api_sample_base.ApiSampleTestBaseV21):
class ServersSampleJsonTest(ServersSampleBase):
sample_dir = 'servers'
request_api_version = None
microversion = None
def _get_flags(self):
f = super(ServersSampleBase, self)._get_flags()
@ -79,7 +79,7 @@ class ServersSampleJsonTest(ServersSampleBase):
def test_servers_get(self):
uuid = self.test_servers_post()
response = self._do_get('servers/%s' % uuid,
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['hostid'] = '[a-f0-9]+'
subs['id'] = uuid
@ -92,7 +92,7 @@ class ServersSampleJsonTest(ServersSampleBase):
def test_servers_list(self):
uuid = self._post_server()
response = self._do_get('servers',
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['id'] = uuid
self._verify_response('servers-list-resp', subs, response, 200)
@ -100,7 +100,7 @@ class ServersSampleJsonTest(ServersSampleBase):
def test_servers_details(self):
uuid = self._post_server()
response = self._do_get('servers/detail',
api_version=self.request_api_version)
api_version=self.microversion)
subs = self._get_regexes()
subs['hostid'] = '[a-f0-9]+'
subs['id'] = uuid
@ -112,11 +112,11 @@ class ServersSampleJsonTest(ServersSampleBase):
class ServersSampleJson29Test(ServersSampleJsonTest):
request_api_version = '2.9'
microversion = '2.9'
# NOTE(gmann): microversion tests do not need to run for v2 API
# so defining scenarios only for v2.9 which will run the original tests
# by appending '(v2_9)' in test_id.
scenarios = [('v2_9', {'_api_version': 'v2.1'})]
scenarios = [('v2_9', {'api_major_version': 'v2.1'})]
class ServerSortKeysJsonTests(ServersSampleBase):
@ -256,7 +256,7 @@ class ServerStartStopJsonTest(ServersSampleBase):
class ServersSampleMultiStatusJsonTest(ServersSampleBase):
sample_dir = 'servers'
extra_extensions_to_load = ["os-access-ips"]
_api_version = 'v2'
api_major_version = 'v2'
def _get_flags(self):
f = super(ServersSampleMultiStatusJsonTest, self)._get_flags()

View File

@ -28,7 +28,7 @@ CONF.import_opt('osapi_compute_extension',
class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
ADMIN_API = True
extension_name = "os-services"
request_api_version = None
microversion = None
def _get_flags(self):
f = super(ServicesJsonTest, self)._get_flags()
@ -61,7 +61,7 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
def test_services_list(self):
"""Return a list of all agent builds."""
response = self._do_get('os-services',
api_version=self.request_api_version)
api_version=self.microversion)
subs = {'binary': 'nova-compute',
'host': 'host1',
'zone': 'nova',
@ -76,7 +76,7 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
'binary': 'nova-compute'}
response = self._do_put('os-services/enable',
'service-enable-put-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
self._verify_response('service-enable-put-resp', subs, response, 200)
def test_service_disable(self):
@ -85,7 +85,7 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
'binary': 'nova-compute'}
response = self._do_put('os-services/disable',
'service-disable-put-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
self._verify_response('service-disable-put-resp', subs, response, 200)
def test_service_disable_log_reason(self):
@ -95,28 +95,28 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
'disabled_reason': 'test2'}
response = self._do_put('os-services/disable-log-reason',
'service-disable-log-put-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
self._verify_response('service-disable-log-put-resp',
subs, response, 200)
def test_service_delete(self):
"""Delete an existing service."""
response = self._do_delete('os-services/1',
api_version=self.request_api_version)
api_version=self.microversion)
self.assertEqual(204, response.status_code)
self.assertEqual("", response.content)
class ServicesV211JsonTest(ServicesJsonTest):
request_api_version = '2.11'
microversion = '2.11'
# NOTE(gryf): There is no need to run those tests on v2 API. Only
# scenarios for v2_11 will be run.
scenarios = [('v2_11', {'_api_version': 'v2.1'})]
scenarios = [('v2_11', {'api_major_version': 'v2.1'})]
def test_services_list(self):
"""Return a list of all agent builds."""
response = self._do_get('os-services',
api_version=self.request_api_version)
api_version=self.microversion)
subs = {'binary': 'nova-compute',
'host': 'host1',
'zone': 'nova',
@ -133,6 +133,6 @@ class ServicesV211JsonTest(ServicesJsonTest):
'forced_down': 'true'}
response = self._do_put('os-services/force-down',
'service-force-down-put-req', subs,
api_version=self.request_api_version)
api_version=self.microversion)
self._verify_response('service-force-down-put-resp', subs,
response, 200)

View File

@ -28,7 +28,7 @@ class VirtualInterfacesJsonTest(test_servers.ServersSampleBase):
def setUp(self):
super(VirtualInterfacesJsonTest, self).setUp()
self.template = 'vifs-list-resp'
if self._api_version == 'v2':
if self.api_major_version == 'v2':
self.template = 'vifs-list-resp-v2'
def _get_flags(self):
@ -53,8 +53,8 @@ class VirtualInterfacesJsonTest(test_servers.ServersSampleBase):
class VirtualInterfacesJsonV212Test(VirtualInterfacesJsonTest):
request_api_version = '2.12'
microversion = '2.12'
# NOTE(gmann): microversion tests do not need to run for v2 API
# so defining scenarios only for v2.12 which will run the original tests
# by appending '(v2_12)' in test_id.
scenarios = [('v2_12', {'_api_version': 'v2.1'})]
scenarios = [('v2_12', {'api_major_version': 'v2.1'})]

View File

@ -32,7 +32,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
all_extensions = False
extension_name = None
sample_dir = None
request_api_version = None
microversion = None
_use_common_server_api_samples = False
def _pretty_data(self, data):
@ -110,18 +110,18 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
api_version=api_version)
def _read_template(self, name):
template = self._get_template(name, self.request_api_version)
template = self._get_template(name, self.microversion)
with open(template) as inf:
return inf.read().strip()
def _write_template(self, name, data):
with open(self._get_template(name,
self.request_api_version), 'w') as outf:
self.microversion), 'w') as outf:
outf.write(data)
def _write_sample(self, name, data):
with open(self._get_sample(
name, self.request_api_version), 'w') as outf:
name, self.microversion), 'w') as outf:
outf.write(data)
def _compare_result(self, subs, expected, result, result_str):
@ -250,7 +250,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
def _update_links(self, sample_data):
"""Process sample data and update version specific links."""
url_re = self._get_host() + "/v(2|2\.1)"
new_url = self._get_host() + "/" + self._api_version
new_url = self._get_host() + "/" + self.api_major_version
updated_data = re.sub(url_re, new_url, sample_data)
return updated_data
@ -259,7 +259,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
response_data = response.content
response_data = self._pretty_data(response_data)
if not os.path.exists(self._get_template(name,
self.request_api_version)):
self.microversion)):
self._write_template(name, response_data)
template_data = response_data
else:
@ -267,12 +267,12 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
if (self.generate_samples and
not os.path.exists(self._get_sample(
name, self.request_api_version))):
name, self.microversion))):
self._write_sample(name, response_data)
sample_data = response_data
else:
with file(self._get_sample(name,
self.request_api_version)) as sample:
self.microversion)) as sample:
sample_data = sample.read()
sample_data = self._update_links(sample_data)
@ -343,7 +343,7 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
'text': text,
'int': '[0-9]+',
'user_id': text,
'api_vers': self._api_version,
'api_vers': self.api_major_version,
'compute_endpoint': self._get_compute_endpoint(),
'versioned_compute_endpoint': self._get_vers_compute_endpoint(),
}
@ -356,7 +356,8 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
def _get_vers_compute_endpoint(self):
# NOTE(sdague): "openstack" is stand in for project_id, it
# should be more generic in future.
return '%s/%s/%s' % (self._get_host(), self._api_version, 'openstack')
return '%s/%s/%s' % (self._get_host(), self.api_major_version,
'openstack')
def _get_response(self, url, method, body=None, strip_version=False,
api_version=None, headers=None):
@ -372,35 +373,35 @@ class ApiSampleTestBase(integrated_helpers._IntegratedTestBase):
headers=None):
return self._get_response(url, 'OPTIONS', strip_version=strip_version,
api_version=(api_version or
self.request_api_version),
self.microversion),
headers=headers)
def _do_get(self, url, strip_version=False, api_version=None,
headers=None):
return self._get_response(url, 'GET', strip_version=strip_version,
api_version=(api_version or
self.request_api_version),
self.microversion),
headers=headers)
def _do_post(self, url, name, subs, method='POST', api_version=None,
headers=None):
body = self._read_template(name) % subs
sample = self._get_sample(name, self.request_api_version)
sample = self._get_sample(name, self.microversion)
if self.generate_samples and not os.path.exists(sample):
self._write_sample(name, body)
return self._get_response(url, method, body,
api_version=(api_version or
self.request_api_version),
self.microversion),
headers=headers)
def _do_put(self, url, name, subs, api_version=None, headers=None):
return self._do_post(url, name, subs, method='PUT',
api_version=(api_version or
self.request_api_version),
self.microversion),
headers=headers)
def _do_delete(self, url, api_version=None, headers=None):
return self._get_response(url, 'DELETE',
api_version=(api_version or
self.request_api_version),
self.microversion),
headers=headers)

View File

@ -85,7 +85,7 @@ class _IntegratedTestBase(test.TestCase):
self._setup_services()
self.api_fixture = self.useFixture(
nova_fixtures.OSAPIFixture(self._api_version))
nova_fixtures.OSAPIFixture(self.api_major_version))
# if the class needs to run as admin, make the api endpoint
# the admin, otherwise it's safer to run as non admin user.
@ -125,7 +125,7 @@ class _IntegratedTestBase(test.TestCase):
self.osapi.start()
self.auth_url = 'http://%(host)s:%(port)s/%(api_version)s' % ({
'host': self.osapi.host, 'port': self.osapi.port,
'api_version': self._api_version})
'api_version': self.api_major_version})
def _get_flags(self):
"""Allow subclass to modify global config before we start services."""

View File

@ -25,7 +25,7 @@ LOG = logging.getLogger(__name__)
class ExtensionsTest(integrated_helpers._IntegratedTestBase):
_api_version = 'v2'
api_major_version = 'v2'
def setUp(self):
self.useFixture(api_paste_fixture.ApiPasteLegacyV2Fixture())

View File

@ -22,7 +22,7 @@ from nova.tests.unit import fake_network
class LegacyV2CompatibleTestBase(test_servers.ServersTestBase):
_api_version = 'v2'
api_major_version = 'v2'
def setUp(self):
super(LegacyV2CompatibleTestBase, self).setUp()

View File

@ -22,7 +22,7 @@ LOG = logging.getLogger(__name__)
class LoginTest(integrated_helpers._IntegratedTestBase):
_api_version = 'v2'
api_major_version = 'v2'
def test_login(self):
# Simple check - we list flavors - so we know we're logged in.
@ -32,4 +32,4 @@ class LoginTest(integrated_helpers._IntegratedTestBase):
class LoginTestV21(LoginTest):
_api_version = 'v2.1'
api_major_version = 'v2.1'

View File

@ -33,7 +33,7 @@ CONF = cfg.CONF
class ServerGroupTestBase(test.TestCase):
REQUIRES_LOCKING = True
_api_version = 'v2'
api_major_version = 'v2'
_image_ref_parameter = 'imageRef'
_flavor_ref_parameter = 'flavorRef'
@ -439,4 +439,4 @@ class ServerGroupAntiAffinityConfTest(ServerGroupTestBase):
class ServerGroupTestV21(ServerGroupTest):
_api_version = 'v2.1'
api_major_version = 'v2.1'

View File

@ -32,7 +32,7 @@ LOG = logging.getLogger(__name__)
class ServersTestBase(integrated_helpers._IntegratedTestBase):
_api_version = 'v2'
api_major_version = 'v2'
_force_delete_parameter = 'forceDelete'
_image_ref_parameter = 'imageRef'
_flavor_ref_parameter = 'flavorRef'
@ -510,4 +510,4 @@ class ServersTest(ServersTestBase):
class ServersTestV21(ServersTest):
_api_version = 'v2.1'
api_major_version = 'v2.1'

View File

@ -40,10 +40,10 @@ class SecgroupsFullstack(testscenarios.WithScenarios, test.TestCase):
# in the dictionary on ``self`` for each scenario.
scenarios = [
('v2', {
'_api_version': 'v2'}),
'api_major_version': 'v2'}),
# test v2.1 base microversion
('v2_1', {
'_api_version': 'v2.1'}),
'api_major_version': 'v2.1'}),
]
def setUp(self):