Add main() to lockutils that creates temp dir for locks
This change would allow us to create one directory to store all locks from all processes involved in testing in projects. Here are examples on how to use this: Heat - https://review.openstack.org/47360 Ceilometer - https://review.openstack.org/44809 (see tox.ini) Change-Id: I9e1260e202b7d8feb9c762a879b9804ed85ccbd6
This commit is contained in:
@@ -20,6 +20,10 @@ import contextlib
|
||||
import errno
|
||||
import functools
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import weakref
|
||||
@@ -277,3 +281,27 @@ def synchronized_with_prefix(lock_file_prefix):
|
||||
"""
|
||||
|
||||
return functools.partial(synchronized, lock_file_prefix=lock_file_prefix)
|
||||
|
||||
|
||||
def main(argv):
|
||||
"""Create a dir for locks and pass it to command from arguments
|
||||
|
||||
If you run this:
|
||||
python -m openstack.common.lockutils 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
|
||||
afterwards and the return value will be preserved.
|
||||
"""
|
||||
|
||||
lock_dir = tempfile.mkdtemp()
|
||||
os.environ["OSLO_LOCK_PATH"] = lock_dir
|
||||
try:
|
||||
ret_val = subprocess.call(argv[1:])
|
||||
finally:
|
||||
shutil.rmtree(lock_dir, ignore_errors=True)
|
||||
return ret_val
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
|
||||
@@ -18,6 +18,7 @@ import fcntl
|
||||
import multiprocessing
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
|
||||
@@ -359,6 +360,17 @@ class LockutilsModuleTestCase(test.BaseTestCase):
|
||||
if os.path.exists(lock_dir):
|
||||
shutil.rmtree(lock_dir, ignore_errors=True)
|
||||
|
||||
def test_main(self):
|
||||
script = '\n'.join([
|
||||
'import os',
|
||||
'lock_path = os.environ.get("OSLO_LOCK_PATH")',
|
||||
'assert lock_path is not None',
|
||||
'assert os.path.isdir(lock_path)',
|
||||
])
|
||||
argv = ['', sys.executable, '-c', script]
|
||||
retval = lockutils.main(argv)
|
||||
self.assertEqual(retval, 0, "Bad OSLO_LOCK_PATH has been set")
|
||||
|
||||
|
||||
class TestLockFixture(test.BaseTestCase):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user