summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Nemec <bnemec@redhat.com>2014-09-26 17:24:05 +0000
committerBen Nemec <bnemec@redhat.com>2014-09-30 15:03:48 +0000
commit5d1461204d1cce680378d5b7c6c2b17f19b7f4b0 (patch)
treef3ca38076f04a3f0eb5c659c1de2212e45fb540c
parentc98d5edc44ad44da7196afa4b6bba0a2afea1f8e (diff)
downloadoslo-concurrency-5d1461204d1cce680378d5b7c6c2b17f19b7f4b0.tar.gz
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
-rw-r--r--oslo/concurrency/lockutils.py11
-rw-r--r--setup.cfg2
-rw-r--r--tests/unit/test_lockutils.py28
-rw-r--r--tox.ini6
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 <etc>
+ oslo-concurrency-lock-wrapper python setup.py testr <etc>
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