Detangle v3 RestfulTestCase setup

The v3 RestfulTestCase and base RestfulTestCase setup were intertwined
in such a way that there was lots of duplicated work executed for each
test.

Change-Id: I345ae83f60aba0a197b08103bd457e546942eeca
This commit is contained in:
David Stanek 2013-10-25 18:22:53 +00:00
parent a0f8236a39
commit 50664d0680
7 changed files with 130 additions and 121 deletions

View File

@ -55,23 +55,16 @@ class RestfulTestCase(tests.TestCase):
# default content type to test
content_type = 'json'
def setUp(self):
def setUp(self, app_conf='keystone'):
super(RestfulTestCase, self).setUp()
self.load_backends()
self.load_fixtures(default_fixtures)
self.public_app = webtest.TestApp(
self.loadapp('keystone', name='main'))
self.loadapp(app_conf, name='main'))
self.admin_app = webtest.TestApp(
self.loadapp('keystone', name='admin'))
# TODO(termie): add an admin user to the fixtures and use that user
# override the fixtures, for now
self.md_foobar = self.assignment_api.add_role_to_user_and_project(
self.user_foo['id'],
self.tenant_bar['id'],
self.role_admin['id'])
self.loadapp(app_conf, name='admin'))
def tearDown(self):
self.public_app = None

View File

@ -1,2 +1,2 @@
[catalog]
driver = keystone.contrib.endpoint_filter.backends.catalog_sql.Endpoint_Filter_Catalog
driver = keystone.contrib.endpoint_filter.backends.catalog_sql.EndpointFilterCatalog

View File

@ -32,6 +32,13 @@ class V2CatalogTestCase(rest.RestfulTestCase):
self.service_id,
self.service.copy())
# TODO(termie): add an admin user to the fixtures and use that user
# override the fixtures, for now
self.assignment_api.add_role_to_user_and_project(
self.user_foo['id'],
self.tenant_bar['id'],
self.role_admin['id'])
def new_ref(self):
"""Populates a ref with attributes common to all API entities."""
return {

View File

@ -744,7 +744,20 @@ class LegacyV2UsernameTests(object):
self.assertEqual(user.get('username'), 'new_username')
class JsonTestCase(rest.RestfulTestCase, CoreApiTests, LegacyV2UsernameTests):
class RestfulTestCase(rest.RestfulTestCase):
def setUp(self):
super(RestfulTestCase, self).setUp()
# TODO(termie): add an admin user to the fixtures and use that user
# override the fixtures, for now
self.assignment_api.add_role_to_user_and_project(
self.user_foo['id'],
self.tenant_bar['id'],
self.role_admin['id'])
class JsonTestCase(RestfulTestCase, CoreApiTests, LegacyV2UsernameTests):
content_type = 'json'
def _get_user_id(self, r):
@ -1083,7 +1096,7 @@ class JsonTestCase(rest.RestfulTestCase, CoreApiTests, LegacyV2UsernameTests):
expected_status=200)
class XmlTestCase(rest.RestfulTestCase, CoreApiTests, LegacyV2UsernameTests):
class XmlTestCase(RestfulTestCase, CoreApiTests, LegacyV2UsernameTests):
xmlns = 'http://docs.openstack.org/identity/api/v2.0'
content_type = 'xml'

View File

@ -18,7 +18,6 @@ import datetime
import uuid
from lxml import etree
import webtest
from keystone import auth
from keystone.common import cache
@ -69,92 +68,15 @@ class RestfulTestCase(rest.RestfulTestCase):
except AttributeError:
pass
def setUp(self, load_sample_data=True, app_conf='keystone'):
def setUp(self, app_conf='keystone'):
"""Setup for v3 Restful Test Cases.
If a child class wants to create their own sample data
and provide their own auth data to obtain tokens, then
load_sample_data should be set to false.
"""
super(RestfulTestCase, self).setUp()
self.config(self.config_files())
self.setup_database()
new_paste_file = self.generate_paste_config()
if new_paste_file:
app_conf = 'config:%s' % (new_paste_file)
# ensure the cache region instance is setup
cache.configure_cache_region(cache.REGION)
self.load_backends()
self.public_app = webtest.TestApp(
self.loadapp(app_conf, name='main'))
self.admin_app = webtest.TestApp(
self.loadapp(app_conf, name='admin'))
if load_sample_data:
self.domain_id = uuid.uuid4().hex
self.domain = self.new_domain_ref()
self.domain['id'] = self.domain_id
self.assignment_api.create_domain(self.domain_id, self.domain)
self.project_id = uuid.uuid4().hex
self.project = self.new_project_ref(
domain_id=self.domain_id)
self.project['id'] = self.project_id
self.assignment_api.create_project(self.project_id, self.project)
self.user_id = uuid.uuid4().hex
self.user = self.new_user_ref(domain_id=self.domain_id)
self.user['id'] = self.user_id
self.identity_api.create_user(self.user_id, self.user)
self.default_domain_project_id = uuid.uuid4().hex
self.default_domain_project = self.new_project_ref(
domain_id=DEFAULT_DOMAIN_ID)
self.default_domain_project['id'] = self.default_domain_project_id
self.assignment_api.create_project(self.default_domain_project_id,
self.default_domain_project)
self.default_domain_user_id = uuid.uuid4().hex
self.default_domain_user = self.new_user_ref(
domain_id=DEFAULT_DOMAIN_ID)
self.default_domain_user['id'] = self.default_domain_user_id
self.identity_api.create_user(self.default_domain_user_id,
self.default_domain_user)
# create & grant policy.json's default role for admin_required
self.role_id = uuid.uuid4().hex
self.role = self.new_role_ref()
self.role['id'] = self.role_id
self.role['name'] = 'admin'
self.assignment_api.create_role(self.role_id, self.role)
self.assignment_api.add_role_to_user_and_project(
self.user_id, self.project_id, self.role_id)
self.assignment_api.add_role_to_user_and_project(
self.default_domain_user_id, self.default_domain_project_id,
self.role_id)
self.assignment_api.add_role_to_user_and_project(
self.default_domain_user_id, self.project_id,
self.role_id)
self.service_id = uuid.uuid4().hex
self.service = self.new_service_ref()
self.service['id'] = self.service_id
self.catalog_api.create_service(
self.service_id,
self.service.copy())
self.endpoint_id = uuid.uuid4().hex
self.endpoint = self.new_endpoint_ref(service_id=self.service_id)
self.endpoint['id'] = self.endpoint_id
self.catalog_api.create_endpoint(
self.endpoint_id,
self.endpoint.copy())
super(RestfulTestCase, self).setUp(app_conf=app_conf)
self.empty_context = {'environment': {}}
@ -168,6 +90,79 @@ class RestfulTestCase(rest.RestfulTestCase):
rules.reset()
super(RestfulTestCase, self).tearDown()
def load_backends(self):
self.config(self.config_files())
self.setup_database()
# ensure the cache region instance is setup
cache.configure_cache_region(cache.REGION)
super(RestfulTestCase, self).load_backends()
def load_fixtures(self, fixtures):
self.load_sample_data()
def load_sample_data(self):
self.domain_id = uuid.uuid4().hex
self.domain = self.new_domain_ref()
self.domain['id'] = self.domain_id
self.assignment_api.create_domain(self.domain_id, self.domain)
self.project_id = uuid.uuid4().hex
self.project = self.new_project_ref(
domain_id=self.domain_id)
self.project['id'] = self.project_id
self.assignment_api.create_project(self.project_id, self.project)
self.user_id = uuid.uuid4().hex
self.user = self.new_user_ref(domain_id=self.domain_id)
self.user['id'] = self.user_id
self.identity_api.create_user(self.user_id, self.user)
self.default_domain_project_id = uuid.uuid4().hex
self.default_domain_project = self.new_project_ref(
domain_id=DEFAULT_DOMAIN_ID)
self.default_domain_project['id'] = self.default_domain_project_id
self.assignment_api.create_project(self.default_domain_project_id,
self.default_domain_project)
self.default_domain_user_id = uuid.uuid4().hex
self.default_domain_user = self.new_user_ref(
domain_id=DEFAULT_DOMAIN_ID)
self.default_domain_user['id'] = self.default_domain_user_id
self.identity_api.create_user(self.default_domain_user_id,
self.default_domain_user)
# create & grant policy.json's default role for admin_required
self.role_id = uuid.uuid4().hex
self.role = self.new_role_ref()
self.role['id'] = self.role_id
self.role['name'] = 'admin'
self.assignment_api.create_role(self.role_id, self.role)
self.assignment_api.add_role_to_user_and_project(
self.user_id, self.project_id, self.role_id)
self.assignment_api.add_role_to_user_and_project(
self.default_domain_user_id, self.default_domain_project_id,
self.role_id)
self.assignment_api.add_role_to_user_and_project(
self.default_domain_user_id, self.project_id,
self.role_id)
self.service_id = uuid.uuid4().hex
self.service = self.new_service_ref()
self.service['id'] = self.service_id
self.catalog_api.create_service(
self.service_id,
self.service.copy())
self.endpoint_id = uuid.uuid4().hex
self.endpoint = self.new_endpoint_ref(service_id=self.service_id)
self.endpoint['id'] = self.endpoint_id
self.catalog_api.create_endpoint(
self.endpoint_id,
self.endpoint.copy())
def new_ref(self):
"""Populates a ref with attributes common to all API entities."""
return {

View File

@ -32,8 +32,6 @@ class TestAuthInfo(test_v3.RestfulTestCase):
# building helper functions, they cause backend databases and fixtures
# to be loaded unnecessarily. Separating out the helper functions from
# this base class would improve efficiency (Bug #1134836)
def setUp(self, load_sample_data=False):
super(TestAuthInfo, self).setUp(load_sample_data=load_sample_data)
def test_missing_auth_methods(self):
auth_data = {'identity': {}}
@ -1803,7 +1801,7 @@ class TestTrustOptional(test_v3.RestfulTestCase):
class TestTrustAuth(TestAuthInfo):
def setUp(self):
self.opt_in_group('trust', enabled=True)
super(TestTrustAuth, self).setUp(load_sample_data=True)
super(TestTrustAuth, self).setUp()
# create a trustee to delegate stuff to
self.trustee_user_id = uuid.uuid4().hex

View File

@ -52,7 +52,26 @@ class IdentityTestProtectedCase(test_v3.RestfulTestCase):
# Ensure that test_v3.RestfulTestCase doesn't load its own
# sample data, which would make checking the results of our
# tests harder
super(IdentityTestProtectedCase, self).setUp(load_sample_data=False)
super(IdentityTestProtectedCase, self).setUp()
# Initialize the policy engine and allow us to write to a temp
# file in each test to create the policies
self.orig_policy_file = CONF.policy_file
rules.reset()
_unused, self.tmpfilename = tempfile.mkstemp()
self.opt(policy_file=self.tmpfilename)
# A default auth request we can use - un-scoped user token
self.auth = self.build_authentication_request(
user_id=self.user1['id'],
password=self.user1['password'])
def tearDown(self):
super(IdentityTestProtectedCase, self).tearDown()
rules.reset()
self.opt(policy_file=self.orig_policy_file)
def load_sample_data(self):
# Start by creating a couple of domains
self.domainA = self.new_domain_ref()
self.assignment_api.create_domain(self.domainA['id'], self.domainA)
@ -98,23 +117,6 @@ class IdentityTestProtectedCase(test_v3.RestfulTestCase):
user_id=self.user1['id'],
domain_id=self.domainA['id'])
# Initialize the policy engine and allow us to write to a temp
# file in each test to create the policies
self.orig_policy_file = CONF.policy_file
rules.reset()
_unused, self.tmpfilename = tempfile.mkstemp()
self.opt(policy_file=self.tmpfilename)
# A default auth request we can use - un-scoped user token
self.auth = self.build_authentication_request(
user_id=self.user1['id'],
password=self.user1['password'])
def tearDown(self):
super(IdentityTestProtectedCase, self).tearDown()
rules.reset()
self.opt(policy_file=self.orig_policy_file)
def _get_id_list_from_ref_list(self, ref_list):
result_list = []
for x in ref_list:
@ -407,8 +409,14 @@ class IdentityTestv3CloudPolicySample(test_v3.RestfulTestCase):
# Ensure that test_v3.RestfulTestCase doesn't load its own
# sample data, which would make checking the results of our
# tests harder
super(IdentityTestv3CloudPolicySample, self).setUp(
load_sample_data=False)
super(IdentityTestv3CloudPolicySample, self).setUp()
# Finally, switch to the v3 sample policy file
self.orig_policy_file = CONF.policy_file
rules.reset()
self.opt(policy_file=tests.etcdir('policy.v3cloudsample.json'))
def load_sample_data(self):
# Start by creating a couple of domains
self.domainA = self.new_domain_ref()
self.assignment_api.create_domain(self.domainA['id'], self.domainA)
@ -467,11 +475,6 @@ class IdentityTestv3CloudPolicySample(test_v3.RestfulTestCase):
user_id=self.just_a_user['id'],
project_id=self.project['id'])
# Finally, switch to the v3 sample policy file
self.orig_policy_file = CONF.policy_file
rules.reset()
self.opt(policy_file=tests.etcdir('policy.v3cloudsample.json'))
def tearDown(self):
super(IdentityTestv3CloudPolicySample, self).tearDown()
rules.reset()