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

View File

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

View File

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

View File

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

View File

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

View File

@ -281,31 +281,30 @@ class TestUtils(unittest.TestCase):
self.assertEquals([], list(utils.backward(f))) self.assertEquals([], list(utils.backward(f)))
def test_mkdirs(self): def test_mkdirs(self):
testroot = os.path.join(os.path.dirname(__file__), 'mkdirs') testdir_base = mkdtemp()
testroot = os.path.join(testdir_base, 'mkdirs')
try: try:
self.assert_(not os.path.exists(testroot))
utils.mkdirs(testroot)
self.assert_(os.path.exists(testroot))
utils.mkdirs(testroot)
self.assert_(os.path.exists(testroot))
rmtree(testroot, ignore_errors=1)
testdir = os.path.join(testroot, 'one/two/three')
self.assert_(not os.path.exists(testdir))
utils.mkdirs(testdir)
self.assert_(os.path.exists(testdir))
utils.mkdirs(testdir)
self.assert_(os.path.exists(testdir))
rmtree(testroot, ignore_errors=1)
open(testroot, 'wb').close()
self.assert_(not os.path.exists(testdir))
self.assertRaises(OSError, utils.mkdirs, testdir)
os.unlink(testroot) os.unlink(testroot)
except Exception: finally:
pass rmtree(testdir_base)
rmtree(testroot, ignore_errors=1)
self.assert_(not os.path.exists(testroot))
utils.mkdirs(testroot)
self.assert_(os.path.exists(testroot))
utils.mkdirs(testroot)
self.assert_(os.path.exists(testroot))
rmtree(testroot, ignore_errors=1)
testdir = os.path.join(testroot, 'one/two/three')
self.assert_(not os.path.exists(testdir))
utils.mkdirs(testdir)
self.assert_(os.path.exists(testdir))
utils.mkdirs(testdir)
self.assert_(os.path.exists(testdir))
rmtree(testroot, ignore_errors=1)
open(testroot, 'wb').close()
self.assert_(not os.path.exists(testdir))
self.assertRaises(OSError, utils.mkdirs, testdir)
os.unlink(testroot)
def test_split_path(self): def test_split_path(self):
# Test swift.common.utils.split_account_path # Test swift.common.utils.split_account_path

View File

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