Skip to content

Commit

Permalink
Fixes unit tests to clean up temporary directories
Browse files Browse the repository at this point in the history
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
  • Loading branch information
keshavab committed Sep 26, 2014
1 parent 0d502de commit 0f93fff
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
3 changes: 3 additions & 0 deletions test/unit/cli/test_recon.py
Expand Up @@ -208,6 +208,9 @@ def test_get_ringmd5(self):
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):
Expand Down
13 changes: 11 additions & 2 deletions test/unit/common/middleware/test_dlo.py
Expand Up @@ -18,13 +18,16 @@
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'

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -984,7 +993,7 @@ def test_finding_defaults_from_dir(self):
max_get_time = 2900
""")

conf_dir = tempfile.mkdtemp()
conf_dir = self.tmpdir

conffile1 = tempfile.NamedTemporaryFile(dir=conf_dir, suffix='.conf')
conffile1.write(proxy_conf1)
Expand Down
12 changes: 7 additions & 5 deletions test/unit/common/middleware/test_xprofile.py
Expand Up @@ -195,16 +195,18 @@ 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')
for pid in self.pids1:
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')
Expand All @@ -215,8 +217,8 @@ def setUp(self):
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(),
Expand Down
3 changes: 3 additions & 0 deletions test/unit/common/test_utils.py
Expand Up @@ -3862,6 +3862,7 @@ def _mock_utils_listdir(path):
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()
Expand All @@ -3880,6 +3881,7 @@ def _mock_utils_listdir(path):
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()
Expand All @@ -3898,6 +3900,7 @@ def _mock_utils_listdir(path):
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:
Expand Down
5 changes: 3 additions & 2 deletions test/unit/obj/test_expirer.py
Expand Up @@ -41,6 +41,7 @@ def not_sleep(seconds):

class TestObjectExpirer(TestCase):
maxDiff = None
internal_client = None

def setUp(self):
global not_sleep
Expand All @@ -54,10 +55,10 @@ def setUp(self):
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({})
Expand Down
13 changes: 9 additions & 4 deletions test/unit/obj/test_server.py
Expand Up @@ -62,8 +62,9 @@ def setUp(self):
"""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())
Expand All @@ -75,7 +76,7 @@ def setUp(self):

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):
Expand Down Expand Up @@ -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'):
Expand All @@ -4320,6 +4322,9 @@ def setUp(self):
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')
Expand Down
9 changes: 7 additions & 2 deletions test/unit/proxy/test_sysmeta.py
Expand Up @@ -16,6 +16,7 @@
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
Expand Down Expand Up @@ -128,8 +129,9 @@ def setUp(self):
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(
Expand All @@ -142,6 +144,9 @@ def setUp(self):
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'}
Expand Down

0 comments on commit 0f93fff

Please sign in to comment.