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:
parent
c98d5edc44
commit
5d1461204d
@ -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))
|
||||
|
@ -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
|
||||
|
@ -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):
|
||||
|
||||
|
6
tox.ini
6
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user