From 5d1461204d1cce680378d5b7c6c2b17f19b7f4b0 Mon Sep 17 00:00:00 2001 From: Ben Nemec Date: Fri, 26 Sep 2014 17:24:05 +0000 Subject: [PATCH] Make lockutils main() a console entry point This is more lib-ish than calling python -m on the module. As part of this change, I also improved the unit tests for this code. Before we weren't unsetting OSLO_LOCK_PATH before calling the main function, so we had no way of knowing if it was being set correctly. I also added a test case to verify return value propagation and removed a private method that was never called. Change-Id: I6c35b5409bf567767c5c71b9041dd7f7a012255d --- oslo/concurrency/lockutils.py | 11 +++++++---- setup.cfg | 2 ++ tests/unit/test_lockutils.py | 28 +++++++++++++--------------- tox.ini | 6 +++--- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/oslo/concurrency/lockutils.py b/oslo/concurrency/lockutils.py index 20f1a74..9ccc27c 100644 --- a/oslo/concurrency/lockutils.py +++ b/oslo/concurrency/lockutils.py @@ -332,11 +332,14 @@ def synchronized_with_prefix(lock_file_prefix): return functools.partial(synchronized, lock_file_prefix=lock_file_prefix) -def main(argv): +def lock_wrapper(argv): """Create a dir for locks and pass it to command from arguments + This is exposed as a console script entry point named + oslo-concurrency-lock-wrapper + If you run this: - python -m openstack.common.lockutils python setup.py testr + oslo-concurrency-lock-wrapper python setup.py testr a temporary directory will be created for all your locks and passed to all your tests in an environment variable. The temporary dir will be deleted @@ -352,5 +355,5 @@ def main(argv): return ret_val -if __name__ == '__main__': - sys.exit(main(sys.argv)) +def main(): + sys.exit(lock_wrapper(sys.argv)) diff --git a/setup.cfg b/setup.cfg index 1e1d347..a914c4f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -29,6 +29,8 @@ namespace_packages = [entry_points] oslo.config.opts = oslo.concurrency = oslo.concurrency.opts:list_opts +console_scripts = + lockutils-wrapper = oslo.concurrency.lockutils:main [build_sphinx] source-dir = doc/source diff --git a/tests/unit/test_lockutils.py b/tests/unit/test_lockutils.py index b1843fb..262b831 100644 --- a/tests/unit/test_lockutils.py +++ b/tests/unit/test_lockutils.py @@ -22,10 +22,8 @@ import tempfile import threading import time -from oslo.config import cfg from oslotest import base as test_base import six -from six import moves from oslo.concurrency.fixture import lockutils as fixtures from oslo.concurrency import lockutils @@ -454,23 +452,14 @@ class LockutilsModuleTestCase(test_base.BaseTestCase): def setUp(self): super(LockutilsModuleTestCase, self).setUp() self.old_env = os.environ.get('OSLO_LOCK_PATH') + if self.old_env is not None: + del os.environ['OSLO_LOCK_PATH'] def tearDown(self): - if self.old_env is None: - del os.environ['OSLO_LOCK_PATH'] - else: + if self.old_env is not None: os.environ['OSLO_LOCK_PATH'] = self.old_env super(LockutilsModuleTestCase, self).tearDown() - def _lock_path_conf_test(self, lock_dir): - cfg.CONF.unregister_opts(lockutils.util_opts) - lockutils_ = moves.reload_module(lockutils) - with lockutils_.lock('test-lock', external=True): - if not os.path.exists(lock_dir): - os._exit(2) - if not os.path.exists(os.path.join(lock_dir, 'test-lock')): - os._exit(3) - def test_main(self): script = '\n'.join([ 'import os', @@ -479,9 +468,18 @@ class LockutilsModuleTestCase(test_base.BaseTestCase): 'assert os.path.isdir(lock_path)', ]) argv = ['', sys.executable, '-c', script] - retval = lockutils.main(argv) + retval = lockutils.lock_wrapper(argv) self.assertEqual(retval, 0, "Bad OSLO_LOCK_PATH has been set") + def test_return_value_maintained(self): + script = '\n'.join([ + 'import sys', + 'sys.exit(1)', + ]) + argv = ['', sys.executable, '-c', script] + retval = lockutils.lock_wrapper(argv) + self.assertEqual(retval, 1) + class TestLockFixture(test_base.BaseTestCase): diff --git a/tox.ini b/tox.ini index cc5db6a..13cae6b 100644 --- a/tox.ini +++ b/tox.ini @@ -15,19 +15,19 @@ setenv = deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt commands = - python -m oslo.concurrency.lockutils python setup.py testr --slowest --testr-args='{posargs}' + lockutils-wrapper python setup.py testr --slowest --testr-args='{posargs}' [testenv:py33] deps = -r{toxinidir}/requirements-py3.txt -r{toxinidir}/test-requirements.txt commands = - python -m oslo.concurrency.lockutils python -m testtools.run tests.unit.test_lockutils + lockutils-wrapper python -m testtools.run tests.unit.test_lockutils [testenv:py34] deps = -r{toxinidir}/requirements-py3.txt -r{toxinidir}/test-requirements.txt commands = - python -m oslo.concurrency.lockutils python -m testtools.run tests.unit.test_lockutils + lockutils-wrapper python -m testtools.run tests.unit.test_lockutils [testenv:pep8] commands = flake8