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
This commit is contained in:
Ben Nemec 2014-09-26 17:24:05 +00:00
parent c98d5edc44
commit 5d1461204d
4 changed files with 25 additions and 22 deletions

View File

@ -332,11 +332,14 @@ def synchronized_with_prefix(lock_file_prefix):
return functools.partial(synchronized, lock_file_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 """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: 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 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 your tests in an environment variable. The temporary dir will be deleted
@ -352,5 +355,5 @@ def main(argv):
return ret_val return ret_val
if __name__ == '__main__': def main():
sys.exit(main(sys.argv)) sys.exit(lock_wrapper(sys.argv))

View File

@ -29,6 +29,8 @@ namespace_packages =
[entry_points] [entry_points]
oslo.config.opts = oslo.config.opts =
oslo.concurrency = oslo.concurrency.opts:list_opts oslo.concurrency = oslo.concurrency.opts:list_opts
console_scripts =
lockutils-wrapper = oslo.concurrency.lockutils:main
[build_sphinx] [build_sphinx]
source-dir = doc/source source-dir = doc/source

View File

@ -22,10 +22,8 @@ import tempfile
import threading import threading
import time import time
from oslo.config import cfg
from oslotest import base as test_base from oslotest import base as test_base
import six import six
from six import moves
from oslo.concurrency.fixture import lockutils as fixtures from oslo.concurrency.fixture import lockutils as fixtures
from oslo.concurrency import lockutils from oslo.concurrency import lockutils
@ -454,23 +452,14 @@ class LockutilsModuleTestCase(test_base.BaseTestCase):
def setUp(self): def setUp(self):
super(LockutilsModuleTestCase, self).setUp() super(LockutilsModuleTestCase, self).setUp()
self.old_env = os.environ.get('OSLO_LOCK_PATH') 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): def tearDown(self):
if self.old_env is None: if self.old_env is not None:
del os.environ['OSLO_LOCK_PATH']
else:
os.environ['OSLO_LOCK_PATH'] = self.old_env os.environ['OSLO_LOCK_PATH'] = self.old_env
super(LockutilsModuleTestCase, self).tearDown() 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): def test_main(self):
script = '\n'.join([ script = '\n'.join([
'import os', 'import os',
@ -479,9 +468,18 @@ class LockutilsModuleTestCase(test_base.BaseTestCase):
'assert os.path.isdir(lock_path)', 'assert os.path.isdir(lock_path)',
]) ])
argv = ['', sys.executable, '-c', script] 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") 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): class TestLockFixture(test_base.BaseTestCase):

View File

@ -15,19 +15,19 @@ setenv =
deps = -r{toxinidir}/requirements.txt deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
commands = 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] [testenv:py33]
deps = -r{toxinidir}/requirements-py3.txt deps = -r{toxinidir}/requirements-py3.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
commands = 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] [testenv:py34]
deps = -r{toxinidir}/requirements-py3.txt deps = -r{toxinidir}/requirements-py3.txt
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
commands = 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] [testenv:pep8]
commands = flake8 commands = flake8