From 55a4c06d26c8751fefa6d64ddb4a2e0ecd9a189f Mon Sep 17 00:00:00 2001 From: Steve Martinelli Date: Fri, 30 Aug 2013 10:08:57 -0500 Subject: [PATCH] Move _generate_paste_config to tests.core Seeing as how tests for oauth1 and endpoint filtering each have their own private functions for creating a new paste-ini file, I think it's best to move these to tests.core. Future extensions will benefit from this too. fixes bug: #1218720 Change-Id: Ic31b78dda0a66ddc4c4214bca4ba3b683d554886 --- keystone/tests/core.py | 22 +++++++++++++ ...st_associate_project_endpoint_extension.py | 25 ++------------- keystone/tests/test_v3.py | 23 +++++++++++++ keystone/tests/test_v3_oauth1.py | 32 +++---------------- 4 files changed, 52 insertions(+), 50 deletions(-) diff --git a/keystone/tests/core.py b/keystone/tests/core.py index c318d369f1..576cd82537 100644 --- a/keystone/tests/core.py +++ b/keystone/tests/core.py @@ -131,6 +131,28 @@ def setup_test_database(): pass +def generate_paste_config(extension_name): + # Generate a file, based on keystone-paste.ini, that is named: + # extension_name.ini, and includes extension_name in the pipeline + with open(etcdir('keystone-paste.ini'), 'r') as f: + contents = f.read() + + new_contents = contents.replace(' service_v3', + ' %s service_v3' % (extension_name)) + + new_paste_file = tmpdir(extension_name + '.ini') + with open(new_paste_file, 'w') as f: + f.write(new_contents) + + return new_paste_file + + +def remove_generated_paste_config(extension_name): + # Remove the generated paste config file, named extension_name.ini + paste_file_to_remove = tmpdir(extension_name + '.ini') + os.remove(paste_file_to_remove) + + def teardown_test_database(): sql.core.set_global_engine(None) diff --git a/keystone/tests/test_associate_project_endpoint_extension.py b/keystone/tests/test_associate_project_endpoint_extension.py index a9f2ffbfea..111061dc6e 100644 --- a/keystone/tests/test_associate_project_endpoint_extension.py +++ b/keystone/tests/test_associate_project_endpoint_extension.py @@ -25,26 +25,10 @@ from keystone.tests import core as test import test_v3 -# TODO(gyee): we need to generalize this one and stash it into tests.core -def _generate_paste_config(filter_name, new_paste_file_name): - # Generate a file, based on keystone-paste.ini, that includes - # endpoint_filter_extension in the pipeline - - with open(test.etcdir('keystone-paste.ini'), 'r') as f: - contents = f.read() - - new_contents = contents.replace(' service_v3', - ' %s service_v3' % (filter_name)) - - with open(new_paste_file_name, 'w') as f: - f.write(new_contents) - - class TestExtensionCase(test_v3.RestfulTestCase): EXTENSION_NAME = 'endpoint_filter' - EXTENSION_FILTER_NAME = 'endpoint_filter_extension' - PASTE_INI = 'keystone-endpoint-filter-paste.ini' + EXTENSION_TO_ADD = 'endpoint_filter_extension' def setup_database(self): self.conf_files = super(TestExtensionCase, self).config_files() @@ -60,11 +44,7 @@ class TestExtensionCase(test_v3.RestfulTestCase): migration.db_sync(version=None, repo_path=self.repo_path) def setUp(self): - self._paste_file_name = test.tmpdir(self.PASTE_INI) - _generate_paste_config(self.EXTENSION_FILTER_NAME, - self._paste_file_name) - super(TestExtensionCase, self).setUp(app_conf='config:%s' % ( - self._paste_file_name)) + super(TestExtensionCase, self).setUp() self.default_request_url = ( '/OS-EP-FILTER/projects/%(project_id)s' '/endpoints/%(endpoint_id)s' % { @@ -73,7 +53,6 @@ class TestExtensionCase(test_v3.RestfulTestCase): def tearDown(self): super(TestExtensionCase, self).tearDown() - os.remove(self._paste_file_name) self.conf_files.pop() diff --git a/keystone/tests/test_v3.py b/keystone/tests/test_v3.py index 671887f473..8b0564ca12 100644 --- a/keystone/tests/test_v3.py +++ b/keystone/tests/test_v3.py @@ -53,6 +53,23 @@ class RestfulTestCase(test_content_types.RestfulTestCase): def teardown_database(self): test.teardown_test_database() + def generate_paste_config(self): + new_paste_file = None + try: + new_paste_file = test.generate_paste_config(self.EXTENSION_TO_ADD) + except AttributeError: + # no need to report this error here, as most tests will not have + # EXTENSION_TO_ADD defined. + pass + finally: + return new_paste_file + + def remove_generated_paste_config(self): + try: + test.remove_generated_paste_config(self.EXTENSION_TO_ADD) + except AttributeError: + pass + def setUp(self, load_sample_data=True, app_conf='keystone'): """Setup for v3 Restful Test Cases. @@ -65,6 +82,11 @@ class RestfulTestCase(test_content_types.RestfulTestCase): 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) @@ -147,6 +169,7 @@ class RestfulTestCase(test_content_types.RestfulTestCase): self.public_server = None self.admin_server = None self.teardown_database() + self.remove_generated_paste_config() # need to reset the plug-ins auth.controllers.AUTH_METHODS = {} #drop the policy rules diff --git a/keystone/tests/test_v3_oauth1.py b/keystone/tests/test_v3_oauth1.py index 70db01d8f8..6404f92e29 100644 --- a/keystone/tests/test_v3_oauth1.py +++ b/keystone/tests/test_v3_oauth1.py @@ -19,8 +19,6 @@ import os import urlparse import uuid -import webtest - from keystone.common import cms from keystone.common.sql import migration from keystone import config @@ -28,17 +26,17 @@ from keystone import contrib from keystone.contrib import oauth1 from keystone.contrib.oauth1 import controllers from keystone.openstack.common import importutils -from keystone.tests import core import test_v3 -OAUTH_PASTE_FILE = 'v3_oauth1-paste.ini' CONF = config.CONF class OAuth1Tests(test_v3.RestfulTestCase): + EXTENSION_NAME = 'oauth1' + EXTENSION_TO_ADD = 'oauth_extension' def setup_database(self): super(OAuth1Tests, self).setup_database() @@ -51,29 +49,10 @@ class OAuth1Tests(test_v3.RestfulTestCase): def setUp(self): super(OAuth1Tests, self).setUp() + + # Now that the app has been served, we can query CONF values + self.base_url = (CONF.public_endpoint % CONF) + "v3" self.controller = controllers.OAuthControllerV3() - self.base_url = CONF.public_endpoint % CONF + "v3" - self._generate_paste_config() - self.admin_app = webtest.TestApp( - self.loadapp('v3_oauth1', name='admin')) - self.public_app = webtest.TestApp( - self.loadapp('v3_oauth1', name='admin')) - - def tearDown(self): - os.remove(OAUTH_PASTE_FILE) - super(OAuth1Tests, self).tearDown() - - def _generate_paste_config(self): - # Generate a file, based on keystone-paste.ini, - # that includes oauth_extension in the pipeline - old_pipeline = " ec2_extension " - new_pipeline = " oauth_extension ec2_extension " - - with open(core.etcdir('keystone-paste.ini'), 'r') as f: - contents = f.read() - new_contents = contents.replace(old_pipeline, new_pipeline) - with open(OAUTH_PASTE_FILE, 'w') as f: - f.write(new_contents) def _create_single_consumer(self): ref = {'description': uuid.uuid4().hex} @@ -97,7 +76,6 @@ class OAuth1Tests(test_v3.RestfulTestCase): http_url=self.base_url + url, http_method='POST', parameters=params) - hmac = oauth1.SignatureMethod_HMAC_SHA1() oreq.sign_request(hmac, consumer, None) headers.update(oreq.to_header())