From 10e25db4befe1173af516a053ac01f0f7b6dac56 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 16 Aug 2012 16:51:04 -0700 Subject: Makes sure tests don't leave lockfiles around Our new lockfile code works fine if the locks already exist, but it is annoying to pollute the source tree with lockfiles in the tests. This modifies the lockfile code slightly to use a tempdir if lock_path is none. Note that a tempdir will not work in production but it is fine for testing. We set the lock_path to None in the testing flags. Also removes the explicit cleanup in run_tests.sh. The new solution works with tox and will work even if we add new locks. Fixes bug 1035426 Change-Id: I613926e4288442e79df50ea5f5bd8f9eafdfe7e7 --- nova/utils.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 180210dc8..6360f9133 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -715,14 +715,21 @@ def synchronized(name, external=False): LOG.debug(_('Attempting to grab file lock "%(lock)s" for ' 'method "%(method)s"...'), {'lock': name, 'method': f.__name__}) - lock_file_path = os.path.join(FLAGS.lock_path, - 'nova-%s' % name) + lock_path = FLAGS.lock_path or tempfile.mkdtemp() + lock_file_path = os.path.join(lock_path, 'nova-%s' % name) lock = InterProcessLock(lock_file_path) - with lock: - LOG.debug(_('Got file lock "%(lock)s" for ' - 'method "%(method)s"...'), - {'lock': name, 'method': f.__name__}) - retval = f(*args, **kwargs) + try: + with lock: + LOG.debug(_('Got file lock "%(lock)s" for ' + 'method "%(method)s"...'), + {'lock': name, 'method': f.__name__}) + retval = f(*args, **kwargs) + finally: + # NOTE(vish): This removes the tempdir if we needed + # to create one. This is used to cleanup + # the locks left behind by unit tests. + if not FLAGS.lock_path: + shutil.rmtree(lock_path) else: retval = f(*args, **kwargs) -- cgit