From 7a00b8f7e9d999f2c37d26221973d955534e7da0 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Mon, 16 Jun 2014 19:57:30 +1000 Subject: [PATCH] Favor addCleanup() over tearDown() If setUp() fails, meaning that tearDown() is not called, then any cleanup functions added will still be called. Note: any tempory files are now cleaned up by the NestedTempFile fixture in oslotest. https://github.com/openstack/oslotest/blob/master/oslotest/base.py#L37 Change-Id: I665de130b703e3aa111d53e7078722099ce91144 --- mistral/tests/api/base.py | 7 +++---- mistral/tests/api/test_auth.py | 6 ------ mistral/tests/base.py | 20 ++------------------ mistral/tests/unit/engine/test_transport.py | 9 ++------- 4 files changed, 7 insertions(+), 35 deletions(-) diff --git a/mistral/tests/api/base.py b/mistral/tests/api/base.py index 9ff19707..42d32858 100644 --- a/mistral/tests/api/base.py +++ b/mistral/tests/api/base.py @@ -48,6 +48,9 @@ class FunctionalTest(base.DbTestCase): 'auth_enable': False } }) + self.addCleanup(pecan.set_config, {}, overwrite=True) + self.addCleanup(cfg.CONF.set_default, 'auth_enable', False, + group='pecan') # make sure the api get the correct context. self.patch_ctx = mock.patch('mistral.context.context_from_headers') @@ -55,10 +58,6 @@ class FunctionalTest(base.DbTestCase): self.mock_ctx.return_value = self.ctx self.addCleanup(self.patch_ctx.stop) - def tearDown(self): - super(FunctionalTest, self).tearDown() - pecan.set_config({}, overwrite=True) - def assertNotFound(self, url): try: self.app.get(url, headers={'Accept': 'application/json'}) diff --git a/mistral/tests/api/test_auth.py b/mistral/tests/api/test_auth.py index 925f68c5..280d8deb 100644 --- a/mistral/tests/api/test_auth.py +++ b/mistral/tests/api/test_auth.py @@ -76,12 +76,6 @@ class TestKeystoneMiddleware(base.FunctionalTest): } }) - def tearDown(self): - # By default, unit tests in Mistral has auth disabled and will fail - # if this option is not reset to False after this test is completed. - cfg.CONF.set_default('auth_enable', False, group='pecan') - super(TestKeystoneMiddleware, self).tearDown() - @mock.patch.object( auth_token.AuthProtocol, '_get_user_token_from_header', mock.MagicMock(return_value='')) diff --git a/mistral/tests/base.py b/mistral/tests/base.py index da1d03a6..7528e15c 100644 --- a/mistral/tests/base.py +++ b/mistral/tests/base.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os import pkg_resources as pkg import sys import tempfile @@ -62,16 +61,6 @@ def get_fake_transport(): class BaseTest(base.BaseTestCase): - def setUp(self): - super(BaseTest, self).setUp() - - # TODO(everyone): add whatever is needed for all Mistral tests in here - - def tearDown(self): - super(BaseTest, self).tearDown() - - # TODO(everyone): add whatever is needed for all Mistral tests in here - def assertListEqual(self, l1, l2): if tuple(sys.version_info)[0:2] < (2, 7): # for python 2.6 compatibility @@ -116,10 +105,11 @@ class BaseTest(base.BaseTestCase): class DbTestCase(BaseTest): def setUp(self): super(DbTestCase, self).setUp() - self.db_fd, self.db_path = tempfile.mkstemp() + _db_fd, self.db_path = tempfile.mkstemp() cfg.CONF.set_default('connection', 'sqlite:///' + self.db_path, group='database') db_api.setup_db() + self.addCleanup(db_api.drop_db) self.ctx = auth_context.MistralContext(user_id='1-2-3-4', project_id='5-6-7-8', @@ -129,12 +119,6 @@ class DbTestCase(BaseTest): auth_context.set_ctx(self.ctx) self.addCleanup(auth_context.set_ctx, None) - def tearDown(self): - super(DbTestCase, self).tearDown() - db_api.drop_db() - os.close(self.db_fd) - os.unlink(self.db_path) - def is_db_session_open(self): return db_api._get_thread_local_session() is not None diff --git a/mistral/tests/unit/engine/test_transport.py b/mistral/tests/unit/engine/test_transport.py index e6bddf75..7684688f 100644 --- a/mistral/tests/unit/engine/test_transport.py +++ b/mistral/tests/unit/engine/test_transport.py @@ -48,14 +48,9 @@ class TestTransport(base.EngineTestCase): # Run the Engine and Executor in the background. self.en_thread = eventlet.spawn(launch.launch_engine, self.transport) + self.addCleanup(self.en_thread.kill) self.ex_thread = eventlet.spawn(launch.launch_executor, self.transport) - - def tearDown(self): - # Stop the Engine and the Executor. - self.en_thread.kill() - self.ex_thread.kill() - - super(TestTransport, self).tearDown() + self.addCleanup(self.ex_thread.kill) @mock.patch.object( db_api, 'workbook_get',