summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2012-08-17 18:24:21 +0000
committerGerrit Code Review <review@openstack.org>2012-08-17 18:24:21 +0000
commit9315004a3cb96c50d5a915ba67e9f9be2f02aa3e (patch)
tree9cbbcb67cb70c65662da16b56d582e1a1ced84af
parent460ba0416eaa66e751358cd0c4cdccc1c908f591 (diff)
parent10e25db4befe1173af516a053ac01f0f7b6dac56 (diff)
downloadnova-9315004a3cb96c50d5a915ba67e9f9be2f02aa3e.tar.gz
nova-9315004a3cb96c50d5a915ba67e9f9be2f02aa3e.tar.xz
nova-9315004a3cb96c50d5a915ba67e9f9be2f02aa3e.zip
Merge "Makes sure tests don't leave lockfiles around"
-rw-r--r--nova/tests/fake_flags.py1
-rw-r--r--nova/tests/test_misc.py80
-rw-r--r--nova/utils.py21
-rwxr-xr-xrun_tests.sh2
4 files changed, 57 insertions, 47 deletions
diff --git a/nova/tests/fake_flags.py b/nova/tests/fake_flags.py
index aada0d7e3..f6d9496b1 100644
--- a/nova/tests/fake_flags.py
+++ b/nova/tests/fake_flags.py
@@ -48,3 +48,4 @@ def set_defaults(conf):
conf.set_default('api_paste_config', '$state_path/etc/nova/api-paste.ini')
conf.set_default('rpc_response_timeout', 5)
conf.set_default('rpc_cast_timeout', 5)
+ conf.set_default('lock_path', None)
diff --git a/nova/tests/test_misc.py b/nova/tests/test_misc.py
index 58d52290b..10443ecde 100644
--- a/nova/tests/test_misc.py
+++ b/nova/tests/test_misc.py
@@ -108,47 +108,51 @@ class LockTestCase(test.TestCase):
def test_nested_external_works(self):
"""We can nest external syncs"""
- sentinel = object()
+ with utils.tempdir() as tempdir:
+ self.flags(lock_path=tempdir)
+ sentinel = object()
- @utils.synchronized('testlock1', external=True)
- def outer_lock():
+ @utils.synchronized('testlock1', external=True)
+ def outer_lock():
- @utils.synchronized('testlock2', external=True)
- def inner_lock():
- return sentinel
- return inner_lock()
+ @utils.synchronized('testlock2', external=True)
+ def inner_lock():
+ return sentinel
+ return inner_lock()
- self.assertEqual(sentinel, outer_lock())
+ self.assertEqual(sentinel, outer_lock())
def test_synchronized_externally(self):
"""We can lock across multiple processes"""
- rpipe1, wpipe1 = os.pipe()
- rpipe2, wpipe2 = os.pipe()
-
- @utils.synchronized('testlock1', external=True)
- def f(rpipe, wpipe):
- try:
- os.write(wpipe, "foo")
- except OSError, e:
- self.assertEquals(e.errno, errno.EPIPE)
- return
-
- rfds, _wfds, _efds = select.select([rpipe], [], [], 1)
- self.assertEquals(len(rfds), 0, "The other process, which was"
- " supposed to be locked, "
- "wrote on its end of the "
- "pipe")
- os.close(rpipe)
-
- pid = os.fork()
- if pid > 0:
- os.close(wpipe1)
- os.close(rpipe2)
-
- f(rpipe1, wpipe2)
- else:
- os.close(rpipe1)
- os.close(wpipe2)
-
- f(rpipe2, wpipe1)
- os._exit(0)
+ with utils.tempdir() as tempdir:
+ self.flags(lock_path=tempdir)
+ rpipe1, wpipe1 = os.pipe()
+ rpipe2, wpipe2 = os.pipe()
+
+ @utils.synchronized('testlock1', external=True)
+ def f(rpipe, wpipe):
+ try:
+ os.write(wpipe, "foo")
+ except OSError, e:
+ self.assertEquals(e.errno, errno.EPIPE)
+ return
+
+ rfds, _wfds, _efds = select.select([rpipe], [], [], 1)
+ self.assertEquals(len(rfds), 0, "The other process, which was"
+ " supposed to be locked, "
+ "wrote on its end of the "
+ "pipe")
+ os.close(rpipe)
+
+ pid = os.fork()
+ if pid > 0:
+ os.close(wpipe1)
+ os.close(rpipe2)
+
+ f(rpipe1, wpipe2)
+ else:
+ os.close(rpipe1)
+ os.close(wpipe2)
+
+ f(rpipe2, wpipe1)
+ os._exit(0)
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)
diff --git a/run_tests.sh b/run_tests.sh
index 2784bb374..0fd7f4169 100755
--- a/run_tests.sh
+++ b/run_tests.sh
@@ -95,8 +95,6 @@ function run_tests {
cat run_tests.log
fi
fi
- # cleanup locks - not really needed, but stops pollution of the source tree
- rm -f nova-ensure_bridge nova-ensure_vlan nova-iptables nova-testlock1 nova-testlock2
return $RESULT
}