Use a tempfile.mkdtemp() based temporary directory

Change-Id: Ie0e8137615348b130b67323d0d2913dc5ebfd5fb
This commit is contained in:
Peter Portante 2014-01-16 00:49:28 -05:00
parent 8eabb13b8f
commit ca87827db9
7 changed files with 47 additions and 56 deletions

View File

@ -17,6 +17,7 @@ import errno
import os
import mock
import unittest
from tempfile import mkdtemp
from shutil import rmtree
from StringIO import StringIO
@ -33,15 +34,15 @@ class TestAccountController(unittest.TestCase):
"""Test swift.account.server.AccountController"""
def setUp(self):
"""Set up for testing swift.account.server.AccountController"""
self.testdir = os.path.join(os.path.dirname(__file__),
'account_server')
self.testdir_base = mkdtemp()
self.testdir = os.path.join(self.testdir_base, 'account_server')
self.controller = AccountController(
{'devices': self.testdir, 'mount_check': 'false'})
def tearDown(self):
"""Tear down for testing swift.account.server.AccountController"""
try:
rmtree(self.testdir)
rmtree(self.testdir_base)
except OSError as err:
if err.errno != errno.ENOENT:
raise

View File

@ -15,6 +15,7 @@
import array
import unittest
from tempfile import mkdtemp
from shutil import rmtree
import os
@ -37,9 +38,7 @@ class TestListEndpoints(unittest.TestCase):
def setUp(self):
utils.HASH_PATH_SUFFIX = 'endcap'
utils.HASH_PATH_PREFIX = ''
self.testdir = os.path.join(os.path.dirname(__file__), 'ring')
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)
self.testdir = mkdtemp()
accountgz = os.path.join(self.testdir, 'account.ring.gz')
containergz = os.path.join(self.testdir, 'container.ring.gz')

View File

@ -19,6 +19,7 @@ import os
import unittest
import cPickle as pickle
from collections import defaultdict
from tempfile import mkdtemp
from shutil import rmtree
from swift.common import exceptions
@ -29,10 +30,7 @@ from swift.common.ring.builder import MAX_BALANCE
class TestRingBuilder(unittest.TestCase):
def setUp(self):
self.testdir = os.path.join(os.path.dirname(__file__),
'ring_builder')
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)
self.testdir = mkdtemp()
def tearDown(self):
rmtree(self.testdir, ignore_errors=1)

View File

@ -20,6 +20,7 @@ import sys
import unittest
from contextlib import closing
from gzip import GzipFile
from tempfile import mkdtemp
from shutil import rmtree
from time import sleep, time
@ -103,9 +104,7 @@ class TestRing(unittest.TestCase):
def setUp(self):
utils.HASH_PATH_SUFFIX = 'endcap'
utils.HASH_PATH_PREFIX = ''
self.testdir = os.path.join(os.path.dirname(__file__), 'ring')
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)
self.testdir = mkdtemp()
self.testgz = os.path.join(self.testdir, 'whatever.ring.gz')
self.intended_replica2part2dev_id = [
array.array('H', [0, 1, 0, 1]),

View File

@ -17,6 +17,7 @@
import os
import unittest
from tempfile import mkdtemp
from shutil import rmtree, copy
from uuid import uuid4
@ -30,7 +31,7 @@ import swift.common.db
from swift.common.db import chexor, dict_factory, get_db_connection, \
DatabaseBroker, DatabaseConnectionError, DatabaseAlreadyExists, \
GreenDBConnection
from swift.common.utils import normalize_timestamp
from swift.common.utils import normalize_timestamp, mkdirs
from swift.common.exceptions import LockTimeout
@ -149,9 +150,7 @@ class TestGetDBConnection(unittest.TestCase):
class TestDatabaseBroker(unittest.TestCase):
def setUp(self):
self.testdir = os.path.join(os.path.dirname(__file__), 'db')
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)
self.testdir = mkdtemp()
def tearDown(self):
rmtree(self.testdir, ignore_errors=1)
@ -303,18 +302,16 @@ class TestDatabaseBroker(unittest.TestCase):
with broker.get() as conn:
self.assertEquals(
[r[0] for r in conn.execute('SELECT * FROM test')], ['1'])
dbpath = os.path.join(self.testdir, 'dev', 'dbs', 'par', 'pre', 'db')
mkdirs(dbpath)
qpath = os.path.join(self.testdir, 'dev', 'quarantined', 'tests', 'db')
with patch('swift.common.db.renamer', lambda a, b: b):
qpath = os.path.dirname(os.path.dirname(os.path.dirname(
os.path.dirname(self.testdir))))
if qpath:
qpath += '/quarantined/tests/db'
else:
qpath = 'quarantined/tests/db'
# Test malformed database
copy(os.path.join(os.path.dirname(__file__),
'malformed_example.db'),
os.path.join(self.testdir, '1.db'))
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'))
os.path.join(dbpath, '1.db'))
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
broker.db_type = 'test'
exc = None
try:
@ -325,12 +322,12 @@ class TestDatabaseBroker(unittest.TestCase):
self.assertEquals(
str(exc),
'Quarantined %s to %s due to malformed database' %
(self.testdir, qpath))
(dbpath, qpath))
# Test corrupted database
copy(os.path.join(os.path.dirname(__file__),
'corrupted_example.db'),
os.path.join(self.testdir, '1.db'))
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'))
os.path.join(dbpath, '1.db'))
broker = DatabaseBroker(os.path.join(dbpath, '1.db'))
broker.db_type = 'test'
exc = None
try:
@ -341,7 +338,7 @@ class TestDatabaseBroker(unittest.TestCase):
self.assertEquals(
str(exc),
'Quarantined %s to %s due to corrupted database' %
(self.testdir, qpath))
(dbpath, qpath))
def test_lock(self):
broker = DatabaseBroker(os.path.join(self.testdir, '1.db'), timeout=.1)

View File

@ -281,12 +281,9 @@ class TestUtils(unittest.TestCase):
self.assertEquals([], list(utils.backward(f)))
def test_mkdirs(self):
testroot = os.path.join(os.path.dirname(__file__), 'mkdirs')
testdir_base = mkdtemp()
testroot = os.path.join(testdir_base, 'mkdirs')
try:
os.unlink(testroot)
except Exception:
pass
rmtree(testroot, ignore_errors=1)
self.assert_(not os.path.exists(testroot))
utils.mkdirs(testroot)
self.assert_(os.path.exists(testroot))
@ -306,6 +303,8 @@ class TestUtils(unittest.TestCase):
self.assert_(not os.path.exists(testdir))
self.assertRaises(OSError, utils.mkdirs, testdir)
os.unlink(testroot)
finally:
rmtree(testdir_base)
def test_split_path(self):
# Test swift.common.utils.split_account_path

View File

@ -19,6 +19,7 @@ import os
import unittest
from contextlib import closing
from gzip import GzipFile
from tempfile import mkdtemp
from shutil import rmtree
from time import time
from distutils.dir_util import mkpath
@ -39,10 +40,7 @@ class TestObjectUpdater(unittest.TestCase):
def setUp(self):
utils.HASH_PATH_SUFFIX = 'endcap'
utils.HASH_PATH_PREFIX = ''
self.testdir = os.path.join(os.path.dirname(__file__),
'object_updater')
rmtree(self.testdir, ignore_errors=1)
os.mkdir(self.testdir)
self.testdir = mkdtemp()
ring_file = os.path.join(self.testdir, 'container.ring.gz')
with closing(GzipFile(ring_file, 'wb')) as f:
pickle.dump(