From 0f93fff46ad098a360f719a762dde4d17e852fbc Mon Sep 17 00:00:00 2001 From: Keshava Bharadwaj Date: Thu, 25 Sep 2014 00:48:49 +0530 Subject: [PATCH] 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 --- test/unit/cli/test_recon.py | 3 +++ test/unit/common/middleware/test_dlo.py | 13 +++++++++++-- test/unit/common/middleware/test_xprofile.py | 12 +++++++----- test/unit/common/test_utils.py | 3 +++ test/unit/obj/test_expirer.py | 5 +++-- test/unit/obj/test_server.py | 13 +++++++++---- test/unit/proxy/test_sysmeta.py | 9 +++++++-- 7 files changed, 43 insertions(+), 15 deletions(-) diff --git a/test/unit/cli/test_recon.py b/test/unit/cli/test_recon.py index 81866e0d0b..ee82d6636a 100644 --- a/test/unit/cli/test_recon.py +++ b/test/unit/cli/test_recon.py @@ -208,6 +208,9 @@ class TestRecon(unittest.TestCase): self.fail('Did not find expected substring %r ' '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): def setUp(self): diff --git a/test/unit/common/middleware/test_dlo.py b/test/unit/common/middleware/test_dlo.py index ad7a555017..492a8092fc 100644 --- a/test/unit/common/middleware/test_dlo.py +++ b/test/unit/common/middleware/test_dlo.py @@ -18,13 +18,16 @@ import contextlib import hashlib import json import mock +import shutil import tempfile +from textwrap import dedent import time import unittest + from swift.common import exceptions, swob from swift.common.middleware import dlo from test.unit.common.middleware.helpers import FakeSwift -from textwrap import dedent + 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. """ + def setUp(self): + self.tmpdir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.tmpdir) + def test_skip_defaults_if_configured(self): # The presence of even one config value in our config section means we # won't go looking for the proxy config at all. @@ -984,7 +993,7 @@ class TestDloConfiguration(unittest.TestCase): max_get_time = 2900 """) - conf_dir = tempfile.mkdtemp() + conf_dir = self.tmpdir conffile1 = tempfile.NamedTemporaryFile(dir=conf_dir, suffix='.conf') conffile1.write(proxy_conf1) diff --git a/test/unit/common/middleware/test_xprofile.py b/test/unit/common/middleware/test_xprofile.py index 545a9fee6d..66baad9b82 100644 --- a/test/unit/common/middleware/test_xprofile.py +++ b/test/unit/common/middleware/test_xprofile.py @@ -195,8 +195,9 @@ class Test_profile_log(unittest.TestCase): def setUp(self): if xprofile is None: 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.pids1 = ['123', '456', str(os.getpid())] 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()) 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.pids2 = ['321', '654', str(os.getpid())] profiler2 = xprofile.get_profiler('eventlet.green.profile') @@ -215,8 +217,8 @@ class Test_profile_log(unittest.TestCase): def tearDown(self): self.profile_log1.clear('all') self.profile_log2.clear('all') - for tempdir in self.tempdirs: - shutil.rmtree(tempdir, ignore_errors=True) + shutil.rmtree(self.dir1, ignore_errors=True) + shutil.rmtree(self.dir2, ignore_errors=True) def test_get_all_pids(self): self.assertEquals(self.profile_log1.get_all_pids(), diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 77ab95cec8..e62abc45d0 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -3862,6 +3862,7 @@ class TestAuditLocationGenerator(unittest.TestCase): audit = lambda: list(utils.audit_location_generator( tmpdir, "data", mount_check=False)) self.assertRaises(OSError, audit) + rmtree(tmpdir) #Check Raise on Bad Suffix tmpdir = mkdtemp() @@ -3880,6 +3881,7 @@ class TestAuditLocationGenerator(unittest.TestCase): audit = lambda: list(utils.audit_location_generator( tmpdir, "data", mount_check=False)) self.assertRaises(OSError, audit) + rmtree(tmpdir) #Check Raise on Bad Hash tmpdir = mkdtemp() @@ -3898,6 +3900,7 @@ class TestAuditLocationGenerator(unittest.TestCase): audit = lambda: list(utils.audit_location_generator( tmpdir, "data", mount_check=False)) self.assertRaises(OSError, audit) + rmtree(tmpdir) def test_non_dir_drive(self): with temptree([]) as tmpdir: diff --git a/test/unit/obj/test_expirer.py b/test/unit/obj/test_expirer.py index 8046c719ba..19a84f29b9 100644 --- a/test/unit/obj/test_expirer.py +++ b/test/unit/obj/test_expirer.py @@ -41,6 +41,7 @@ def not_sleep(seconds): class TestObjectExpirer(TestCase): maxDiff = None + internal_client = None def setUp(self): global not_sleep @@ -54,10 +55,10 @@ class TestObjectExpirer(TestCase): self.rcache = mkdtemp() self.logger = FakeLogger() - def teardown(self): + def tearDown(self): rmtree(self.rcache) 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): x = expirer.ObjectExpirer({}) diff --git a/test/unit/obj/test_server.py b/test/unit/obj/test_server.py index d3c2d90706..60b9bf385b 100755 --- a/test/unit/obj/test_server.py +++ b/test/unit/obj/test_server.py @@ -62,8 +62,9 @@ class TestObjectController(unittest.TestCase): """Set up for testing swift.object.server.ObjectController""" utils.HASH_PATH_SUFFIX = 'endcap' utils.HASH_PATH_PREFIX = 'startcap' - self.testdir = \ - os.path.join(mkdtemp(), 'tmp_test_object_server_ObjectController') + self.tmpdir = mkdtemp() + self.testdir = os.path.join(self.tmpdir, + 'tmp_test_object_server_ObjectController') conf = {'devices': self.testdir, 'mount_check': 'false'} self.object_controller = object_server.ObjectController( conf, logger=debug_logger()) @@ -75,7 +76,7 @@ class TestObjectController(unittest.TestCase): def tearDown(self): """Tear down for testing swift.object.server.ObjectController""" - rmtree(os.path.dirname(self.testdir)) + rmtree(self.tmpdir) tpool.execute = self._orig_tpool_exc def _stage_tmp_dir(self, policy): @@ -4303,7 +4304,8 @@ class TestObjectServer(unittest.TestCase): def setUp(self): # 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') for device in ('sda1', 'sdb1'): @@ -4320,6 +4322,9 @@ class TestObjectServer(unittest.TestCase): self.server = spawn(wsgi.server, sock, app, utils.NullLogger()) self.port = sock.getsockname()[1] + def tearDown(self): + rmtree(self.tmpdir) + def test_not_found(self): conn = bufferedhttp.http_connect('127.0.0.1', self.port, 'sda1', '0', 'GET', '/a/c/o') diff --git a/test/unit/proxy/test_sysmeta.py b/test/unit/proxy/test_sysmeta.py index 67cb572ea3..c3b6731082 100644 --- a/test/unit/proxy/test_sysmeta.py +++ b/test/unit/proxy/test_sysmeta.py @@ -16,6 +16,7 @@ import unittest import os from tempfile import mkdtemp from urllib import quote +import shutil from swift.common.storage_policy import StoragePolicy from swift.common.swob import Request from swift.common.utils import mkdirs, split_path @@ -128,8 +129,9 @@ class TestObjectSysmeta(unittest.TestCase): account_ring=FakeRing(replicas=1), container_ring=FakeRing(replicas=1)) monkey_patch_mimetools() - self.testdir = \ - os.path.join(mkdtemp(), 'tmp_test_object_server_ObjectController') + self.tmpdir = mkdtemp() + self.testdir = os.path.join(self.tmpdir, + 'tmp_test_object_server_ObjectController') mkdirs(os.path.join(self.testdir, 'sda1', 'tmp')) conf = {'devices': self.testdir, 'mount_check': 'false'} 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.obj.http_connect = http_connect + def tearDown(self): + shutil.rmtree(self.tmpdir) + original_sysmeta_headers_1 = {'x-object-sysmeta-test0': 'val0', 'x-object-sysmeta-test1': 'val1'} original_sysmeta_headers_2 = {'x-object-sysmeta-test2': 'val2'}