Some test cleanup

- Tests are based on oslotest.base.BaseTestCase, which uses the
  NestedTempFile fixture, which uses the TempDir fixture, which adds a
  cleanup routine to remove the base temporary directory it creates.
  There's therefore no need for tests to clean up this directory, so all
  the code that does that is removed.
- The eventlet incarnation of tests was trying to make use of the `env`
  external without whitelisting it, resulting in an ugly red deprecation
  warning. This commit adds `env` to whitelist_externals in [testenv].
- A handful of typos in the Beowulf quote are corrected.

Change-Id: I91cc52e00e0a918dadd2a3a771bd322b0f165ed2
This commit is contained in:
Eric Fried 2019-09-17 15:54:42 -05:00
parent 610df387b6
commit fe86f5e4ae
4 changed files with 84 additions and 138 deletions

View File

@ -15,7 +15,6 @@
import collections import collections
import multiprocessing import multiprocessing
import os import os
import shutil
import signal import signal
import subprocess import subprocess
import sys import sys
@ -188,9 +187,7 @@ class LockTestCase(test_base.BaseTestCase):
def test_nested_synchronized_external_works(self): def test_nested_synchronized_external_works(self):
"""We can nest external syncs.""" """We can nest external syncs."""
tempdir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
try:
self.config(lock_path=tempdir, group='oslo_concurrency')
sentinel = object() sentinel = object()
@lockutils.synchronized('testlock1', 'test-', external=True) @lockutils.synchronized('testlock1', 'test-', external=True)
@ -203,50 +200,32 @@ class LockTestCase(test_base.BaseTestCase):
self.assertEqual(sentinel, outer_lock()) self.assertEqual(sentinel, outer_lock())
finally:
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
def _do_test_lock_externally(self): def _do_test_lock_externally(self):
"""We can lock across multiple processes.""" """We can lock across multiple processes."""
handles_dir = tempfile.mkdtemp()
try:
children = [] children = []
for n in range(50): for n in range(50):
queue = multiprocessing.Queue() queue = multiprocessing.Queue()
proc = multiprocessing.Process( proc = multiprocessing.Process(
target=lock_files, target=lock_files,
args=(handles_dir, queue)) args=(tempfile.mkdtemp(), queue))
proc.start() proc.start()
children.append((proc, queue)) children.append((proc, queue))
for child, queue in children: for child, queue in children:
child.join() child.join()
count = queue.get(block=False) count = queue.get(block=False)
self.assertEqual(50, count) self.assertEqual(50, count)
finally:
if os.path.exists(handles_dir):
shutil.rmtree(handles_dir, ignore_errors=True)
def test_lock_externally(self): def test_lock_externally(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
try:
self._do_test_lock_externally() self._do_test_lock_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_lock_externally_lock_dir_not_exist(self): def test_lock_externally_lock_dir_not_exist(self):
lock_dir = tempfile.mkdtemp() lock_dir = tempfile.mkdtemp()
os.rmdir(lock_dir) os.rmdir(lock_dir)
self.config(lock_path=lock_dir, group='oslo_concurrency') self.config(lock_path=lock_dir, group='oslo_concurrency')
try:
self._do_test_lock_externally() self._do_test_lock_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_synchronized_with_prefix(self): def test_synchronized_with_prefix(self):
lock_name = 'mylock' lock_name = 'mylock'
@ -264,40 +243,28 @@ class LockTestCase(test_base.BaseTestCase):
self.assertTrue(bar(lock_dir, lock_pfix, lock_name)) self.assertTrue(bar(lock_dir, lock_pfix, lock_name))
def test_synchronized_without_prefix(self): def test_synchronized_without_prefix(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
@lockutils.synchronized('lock', external=True) @lockutils.synchronized('lock', external=True)
def test_without_prefix(): def test_without_prefix():
# We can't check much # We can't check much
pass pass
try:
test_without_prefix() test_without_prefix()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_synchronized_prefix_without_hypen(self): def test_synchronized_prefix_without_hypen(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
@lockutils.synchronized('lock', 'hypen', True) @lockutils.synchronized('lock', 'hypen', True)
def test_without_hypen(): def test_without_hypen():
# We can't check much # We can't check much
pass pass
try:
test_without_hypen() test_without_hypen()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock(self): def test_contextlock(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
try:
# Note(flaper87): Lock is not external, which means # Note(flaper87): Lock is not external, which means
# a semaphore will be yielded # a semaphore will be yielded
with lockutils.lock("test") as sem: with lockutils.lock("test") as sem:
@ -311,21 +278,12 @@ class LockTestCase(test_base.BaseTestCase):
with lockutils.lock("test2", external=True) as lock: with lockutils.lock("test2", external=True) as lock:
self.assertTrue(lock.exists()) self.assertTrue(lock.exists())
with lockutils.lock("test1", with lockutils.lock("test1", external=True) as lock1:
external=True) as lock1: self.assertIsInstance(lock1, lockutils.InterProcessLock)
self.assertIsInstance(lock1,
lockutils.InterProcessLock)
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock_unlocks(self): def test_contextlock_unlocks(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
sem = None
try:
with lockutils.lock("test") as sem: with lockutils.lock("test") as sem:
if six.PY2: if six.PY2:
self.assertIsInstance(sem, threading._Semaphore) self.assertIsInstance(sem, threading._Semaphore)
@ -343,9 +301,6 @@ class LockTestCase(test_base.BaseTestCase):
# but semaphore should already exist. # but semaphore should already exist.
with lockutils.lock("test") as sem2: with lockutils.lock("test") as sem2:
self.assertEqual(sem, sem2) self.assertEqual(sem, sem2)
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def _test_remove_lock_external_file(self, lock_dir, use_external=False): def _test_remove_lock_external_file(self, lock_dir, use_external=False):
lock_name = 'mylock' lock_name = 'mylock'
@ -361,17 +316,13 @@ class LockTestCase(test_base.BaseTestCase):
for ent in os.listdir(lock_dir): for ent in os.listdir(lock_dir):
self.assertRaises(OSError, ent.startswith, lock_pfix) self.assertRaises(OSError, ent.startswith, lock_pfix)
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_remove_lock_external_file(self): def test_remove_lock_external_file(self):
lock_dir = tempfile.mkdtemp() lock_dir = tempfile.mkdtemp()
self.config(lock_path=lock_dir, group='oslo_concurrency') self.config(lock_path=lock_dir, group='oslo_concurrency')
self._test_remove_lock_external_file(lock_dir) self._test_remove_lock_external_file(lock_dir)
def test_remove_lock_external_file_lock_path(self): def test_remove_lock_external_file_lock_path(self):
lock_dir = tempfile.mkdtemp() self._test_remove_lock_external_file(tempfile.mkdtemp(),
self._test_remove_lock_external_file(lock_dir,
use_external=True) use_external=True)
def test_no_slash_in_b64(self): def test_no_slash_in_b64(self):

View File

@ -13,7 +13,6 @@
# under the License. # under the License.
import os import os
import shutil
import tempfile import tempfile
import eventlet import eventlet
@ -28,7 +27,6 @@ class TestFileLocks(test_base.BaseTestCase):
def test_concurrent_green_lock_succeeds(self): def test_concurrent_green_lock_succeeds(self):
"""Verify spawn_n greenthreads with two locks run concurrently.""" """Verify spawn_n greenthreads with two locks run concurrently."""
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
try:
self.completed = False self.completed = False
def locka(wait): def locka(wait):
@ -53,7 +51,3 @@ class TestFileLocks(test_base.BaseTestCase):
pool.waitall() pool.waitall()
self.assertTrue(self.completed) self.assertTrue(self.completed)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)

View File

@ -190,18 +190,18 @@ class ProcessExecutionErrorTest(test_base.BaseTestCase):
stdout = """ stdout = """
Lo, praise of the prowess of people-kings Lo, praise of the prowess of people-kings
of spear-armed Danes, in days long sped, of spear-armed Danes, in days long sped,
we have heard, and what honot the athelings won! we have heard, and what honor the athelings won!
Oft Scyld the Scefing from squadroned foes, Oft Scyld the Scefing from squadroned foes,
from many a tribe, the mead-bench tore, from many a tribe, the mead-bench tore,
awing the earls. Since erse he lay awing the earls. Since erst he lay
friendless, a foundling, fate repaid him: friendless, a foundling, fate repaid him:
for he waxed under welkin, in wealth he trove, for he waxed under welkin, in wealth he throve,
till before him the folk, both far and near, till before him the folk, both far and near,
who house by the whale-path, heard his mandate, who house by the whale-path, heard his mandate,
gabe him gits: a good king he! gave him gifts: a good king he!
To him an heir was afterward born, To him an heir was afterward born,
a son in his halls, whom heaven sent a son in his halls, whom heaven sent
to favor the fol, feeling their woe to favor the folk, feeling their woe
that erst they had lacked an earl for leader that erst they had lacked an earl for leader
so long a while; the Lord endowed him, so long a while; the Lord endowed him,
the Wielder of Wonder, with world's renown. the Wielder of Wonder, with world's renown.

View File

@ -9,6 +9,7 @@ deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
-r{toxinidir}/requirements.txt -r{toxinidir}/requirements.txt
# We want to support both vanilla stdlib and eventlet monkey patched # We want to support both vanilla stdlib and eventlet monkey patched
whitelist_externals = env
commands = commands =
lockutils-wrapper stestr run --slowest {posargs} lockutils-wrapper stestr run --slowest {posargs}
env TEST_EVENTLET=1 lockutils-wrapper stestr run --slowest {posargs} env TEST_EVENTLET=1 lockutils-wrapper stestr run --slowest {posargs}