Fixes unit tests to clean up temporary directories

This patch fixes the unit tests to remove the temporary directories
created during run of unit tests. Some of unit tests did not tear down
correctly, whatever it had set it up for running. This would over period
of time bloat up the tmp directory. As on date, there were around 49 tmp
directories left uncleared per round of unit tests. This patch fixes it.

Change-Id: If591375ca9cc87d52c7c9c6dc16c9fb4b49e99fc
This commit is contained in:
Keshava Bharadwaj 2014-09-25 00:48:49 +05:30
parent 0d502de2f5
commit 0f93fff46a
7 changed files with 43 additions and 15 deletions

View File

@ -208,6 +208,9 @@ class TestRecon(unittest.TestCase):
self.fail('Did not find expected substring %r ' self.fail('Did not find expected substring %r '
'in output:\n%s' % (expected, output)) 'in output:\n%s' % (expected, output))
for ring in ('account', 'container', 'object', 'object-1'):
os.remove(os.path.join(self.swift_dir, "%s.ring.gz" % ring))
class TestReconCommands(unittest.TestCase): class TestReconCommands(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -18,13 +18,16 @@ import contextlib
import hashlib import hashlib
import json import json
import mock import mock
import shutil
import tempfile import tempfile
from textwrap import dedent
import time import time
import unittest import unittest
from swift.common import exceptions, swob from swift.common import exceptions, swob
from swift.common.middleware import dlo from swift.common.middleware import dlo
from test.unit.common.middleware.helpers import FakeSwift from test.unit.common.middleware.helpers import FakeSwift
from textwrap import dedent
LIMIT = 'swift.common.constraints.CONTAINER_LISTING_LIMIT' LIMIT = 'swift.common.constraints.CONTAINER_LISTING_LIMIT'
@ -898,6 +901,12 @@ class TestDloConfiguration(unittest.TestCase):
proxy's config section if we don't have any config values. proxy's config section if we don't have any config values.
""" """
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_skip_defaults_if_configured(self): def test_skip_defaults_if_configured(self):
# The presence of even one config value in our config section means we # The presence of even one config value in our config section means we
# won't go looking for the proxy config at all. # won't go looking for the proxy config at all.
@ -984,7 +993,7 @@ class TestDloConfiguration(unittest.TestCase):
max_get_time = 2900 max_get_time = 2900
""") """)
conf_dir = tempfile.mkdtemp() conf_dir = self.tmpdir
conffile1 = tempfile.NamedTemporaryFile(dir=conf_dir, suffix='.conf') conffile1 = tempfile.NamedTemporaryFile(dir=conf_dir, suffix='.conf')
conffile1.write(proxy_conf1) conffile1.write(proxy_conf1)

View File

@ -195,8 +195,9 @@ class Test_profile_log(unittest.TestCase):
def setUp(self): def setUp(self):
if xprofile is None: if xprofile is None:
raise SkipTest raise SkipTest
self.tempdirs = [tempfile.mkdtemp(), tempfile.mkdtemp()]
self.log_filename_prefix1 = self.tempdirs[0] + '/unittest.profile' self.dir1 = tempfile.mkdtemp()
self.log_filename_prefix1 = self.dir1 + '/unittest.profile'
self.profile_log1 = ProfileLog(self.log_filename_prefix1, False) self.profile_log1 = ProfileLog(self.log_filename_prefix1, False)
self.pids1 = ['123', '456', str(os.getpid())] self.pids1 = ['123', '456', str(os.getpid())]
profiler1 = xprofile.get_profiler('eventlet.green.profile') profiler1 = xprofile.get_profiler('eventlet.green.profile')
@ -204,7 +205,8 @@ class Test_profile_log(unittest.TestCase):
profiler1.runctx('import os;os.getcwd();', globals(), locals()) profiler1.runctx('import os;os.getcwd();', globals(), locals())
self.profile_log1.dump_profile(profiler1, pid) self.profile_log1.dump_profile(profiler1, pid)
self.log_filename_prefix2 = self.tempdirs[1] + '/unittest.profile' self.dir2 = tempfile.mkdtemp()
self.log_filename_prefix2 = self.dir2 + '/unittest.profile'
self.profile_log2 = ProfileLog(self.log_filename_prefix2, True) self.profile_log2 = ProfileLog(self.log_filename_prefix2, True)
self.pids2 = ['321', '654', str(os.getpid())] self.pids2 = ['321', '654', str(os.getpid())]
profiler2 = xprofile.get_profiler('eventlet.green.profile') profiler2 = xprofile.get_profiler('eventlet.green.profile')
@ -215,8 +217,8 @@ class Test_profile_log(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.profile_log1.clear('all') self.profile_log1.clear('all')
self.profile_log2.clear('all') self.profile_log2.clear('all')
for tempdir in self.tempdirs: shutil.rmtree(self.dir1, ignore_errors=True)
shutil.rmtree(tempdir, ignore_errors=True) shutil.rmtree(self.dir2, ignore_errors=True)
def test_get_all_pids(self): def test_get_all_pids(self):
self.assertEquals(self.profile_log1.get_all_pids(), self.assertEquals(self.profile_log1.get_all_pids(),

View File

@ -3862,6 +3862,7 @@ class TestAuditLocationGenerator(unittest.TestCase):
audit = lambda: list(utils.audit_location_generator( audit = lambda: list(utils.audit_location_generator(
tmpdir, "data", mount_check=False)) tmpdir, "data", mount_check=False))
self.assertRaises(OSError, audit) self.assertRaises(OSError, audit)
rmtree(tmpdir)
#Check Raise on Bad Suffix #Check Raise on Bad Suffix
tmpdir = mkdtemp() tmpdir = mkdtemp()
@ -3880,6 +3881,7 @@ class TestAuditLocationGenerator(unittest.TestCase):
audit = lambda: list(utils.audit_location_generator( audit = lambda: list(utils.audit_location_generator(
tmpdir, "data", mount_check=False)) tmpdir, "data", mount_check=False))
self.assertRaises(OSError, audit) self.assertRaises(OSError, audit)
rmtree(tmpdir)
#Check Raise on Bad Hash #Check Raise on Bad Hash
tmpdir = mkdtemp() tmpdir = mkdtemp()
@ -3898,6 +3900,7 @@ class TestAuditLocationGenerator(unittest.TestCase):
audit = lambda: list(utils.audit_location_generator( audit = lambda: list(utils.audit_location_generator(
tmpdir, "data", mount_check=False)) tmpdir, "data", mount_check=False))
self.assertRaises(OSError, audit) self.assertRaises(OSError, audit)
rmtree(tmpdir)
def test_non_dir_drive(self): def test_non_dir_drive(self):
with temptree([]) as tmpdir: with temptree([]) as tmpdir:

View File

@ -41,6 +41,7 @@ def not_sleep(seconds):
class TestObjectExpirer(TestCase): class TestObjectExpirer(TestCase):
maxDiff = None maxDiff = None
internal_client = None
def setUp(self): def setUp(self):
global not_sleep global not_sleep
@ -54,10 +55,10 @@ class TestObjectExpirer(TestCase):
self.rcache = mkdtemp() self.rcache = mkdtemp()
self.logger = FakeLogger() self.logger = FakeLogger()
def teardown(self): def tearDown(self):
rmtree(self.rcache) rmtree(self.rcache)
internal_client.sleep = self.old_sleep internal_client.sleep = self.old_sleep
internal_client.loadapp = self.loadapp internal_client.loadapp = self.old_loadapp
def test_get_process_values_from_kwargs(self): def test_get_process_values_from_kwargs(self):
x = expirer.ObjectExpirer({}) x = expirer.ObjectExpirer({})

View File

@ -62,8 +62,9 @@ class TestObjectController(unittest.TestCase):
"""Set up for testing swift.object.server.ObjectController""" """Set up for testing swift.object.server.ObjectController"""
utils.HASH_PATH_SUFFIX = 'endcap' utils.HASH_PATH_SUFFIX = 'endcap'
utils.HASH_PATH_PREFIX = 'startcap' utils.HASH_PATH_PREFIX = 'startcap'
self.testdir = \ self.tmpdir = mkdtemp()
os.path.join(mkdtemp(), 'tmp_test_object_server_ObjectController') self.testdir = os.path.join(self.tmpdir,
'tmp_test_object_server_ObjectController')
conf = {'devices': self.testdir, 'mount_check': 'false'} conf = {'devices': self.testdir, 'mount_check': 'false'}
self.object_controller = object_server.ObjectController( self.object_controller = object_server.ObjectController(
conf, logger=debug_logger()) conf, logger=debug_logger())
@ -75,7 +76,7 @@ class TestObjectController(unittest.TestCase):
def tearDown(self): def tearDown(self):
"""Tear down for testing swift.object.server.ObjectController""" """Tear down for testing swift.object.server.ObjectController"""
rmtree(os.path.dirname(self.testdir)) rmtree(self.tmpdir)
tpool.execute = self._orig_tpool_exc tpool.execute = self._orig_tpool_exc
def _stage_tmp_dir(self, policy): def _stage_tmp_dir(self, policy):
@ -4303,7 +4304,8 @@ class TestObjectServer(unittest.TestCase):
def setUp(self): def setUp(self):
# dirs # dirs
self.tempdir = os.path.join(tempfile.mkdtemp(), 'tmp_test_obj_server') self.tmpdir = tempfile.mkdtemp()
self.tempdir = os.path.join(self.tmpdir, 'tmp_test_obj_server')
self.devices = os.path.join(self.tempdir, 'srv/node') self.devices = os.path.join(self.tempdir, 'srv/node')
for device in ('sda1', 'sdb1'): for device in ('sda1', 'sdb1'):
@ -4320,6 +4322,9 @@ class TestObjectServer(unittest.TestCase):
self.server = spawn(wsgi.server, sock, app, utils.NullLogger()) self.server = spawn(wsgi.server, sock, app, utils.NullLogger())
self.port = sock.getsockname()[1] self.port = sock.getsockname()[1]
def tearDown(self):
rmtree(self.tmpdir)
def test_not_found(self): def test_not_found(self):
conn = bufferedhttp.http_connect('127.0.0.1', self.port, 'sda1', '0', conn = bufferedhttp.http_connect('127.0.0.1', self.port, 'sda1', '0',
'GET', '/a/c/o') 'GET', '/a/c/o')

View File

@ -16,6 +16,7 @@ import unittest
import os import os
from tempfile import mkdtemp from tempfile import mkdtemp
from urllib import quote from urllib import quote
import shutil
from swift.common.storage_policy import StoragePolicy from swift.common.storage_policy import StoragePolicy
from swift.common.swob import Request from swift.common.swob import Request
from swift.common.utils import mkdirs, split_path from swift.common.utils import mkdirs, split_path
@ -128,8 +129,9 @@ class TestObjectSysmeta(unittest.TestCase):
account_ring=FakeRing(replicas=1), account_ring=FakeRing(replicas=1),
container_ring=FakeRing(replicas=1)) container_ring=FakeRing(replicas=1))
monkey_patch_mimetools() monkey_patch_mimetools()
self.testdir = \ self.tmpdir = mkdtemp()
os.path.join(mkdtemp(), 'tmp_test_object_server_ObjectController') self.testdir = os.path.join(self.tmpdir,
'tmp_test_object_server_ObjectController')
mkdirs(os.path.join(self.testdir, 'sda1', 'tmp')) mkdirs(os.path.join(self.testdir, 'sda1', 'tmp'))
conf = {'devices': self.testdir, 'mount_check': 'false'} conf = {'devices': self.testdir, 'mount_check': 'false'}
self.obj_ctlr = object_server.ObjectController( self.obj_ctlr = object_server.ObjectController(
@ -142,6 +144,9 @@ class TestObjectSysmeta(unittest.TestCase):
swift.proxy.controllers.base.http_connect = http_connect swift.proxy.controllers.base.http_connect = http_connect
swift.proxy.controllers.obj.http_connect = http_connect swift.proxy.controllers.obj.http_connect = http_connect
def tearDown(self):
shutil.rmtree(self.tmpdir)
original_sysmeta_headers_1 = {'x-object-sysmeta-test0': 'val0', original_sysmeta_headers_1 = {'x-object-sysmeta-test0': 'val0',
'x-object-sysmeta-test1': 'val1'} 'x-object-sysmeta-test1': 'val1'}
original_sysmeta_headers_2 = {'x-object-sysmeta-test2': 'val2'} original_sysmeta_headers_2 = {'x-object-sysmeta-test2': 'val2'}