From ca87827db902ebe98126e88d43098e3097d42044 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Thu, 16 Jan 2014 00:49:28 -0500 Subject: [PATCH] Use a tempfile.mkdtemp() based temporary directory Change-Id: Ie0e8137615348b130b67323d0d2913dc5ebfd5fb --- test/unit/account/test_server.py | 7 +-- .../common/middleware/test_list_endpoints.py | 5 +-- test/unit/common/ring/test_builder.py | 6 +-- test/unit/common/ring/test_ring.py | 5 +-- test/unit/common/test_db.py | 29 ++++++------ test/unit/common/test_utils.py | 45 +++++++++---------- test/unit/obj/test_updater.py | 6 +-- 7 files changed, 47 insertions(+), 56 deletions(-) diff --git a/test/unit/account/test_server.py b/test/unit/account/test_server.py index c91ff0d416..40ec7efdb3 100644 --- a/test/unit/account/test_server.py +++ b/test/unit/account/test_server.py @@ -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 diff --git a/test/unit/common/middleware/test_list_endpoints.py b/test/unit/common/middleware/test_list_endpoints.py index 1f98cc72c9..218394ad00 100644 --- a/test/unit/common/middleware/test_list_endpoints.py +++ b/test/unit/common/middleware/test_list_endpoints.py @@ -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') diff --git a/test/unit/common/ring/test_builder.py b/test/unit/common/ring/test_builder.py index d6b78a103a..1cdb939100 100644 --- a/test/unit/common/ring/test_builder.py +++ b/test/unit/common/ring/test_builder.py @@ -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) diff --git a/test/unit/common/ring/test_ring.py b/test/unit/common/ring/test_ring.py index 82cc8a134f..04eb1b7cb5 100644 --- a/test/unit/common/ring/test_ring.py +++ b/test/unit/common/ring/test_ring.py @@ -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]), diff --git a/test/unit/common/test_db.py b/test/unit/common/test_db.py index 8952ec4253..e3bcd2f2bf 100644 --- a/test/unit/common/test_db.py +++ b/test/unit/common/test_db.py @@ -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) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index db2c9ba3bb..63c6909327 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -281,31 +281,30 @@ 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: + 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) - except Exception: - pass - 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) + finally: + rmtree(testdir_base) def test_split_path(self): # Test swift.common.utils.split_account_path diff --git a/test/unit/obj/test_updater.py b/test/unit/obj/test_updater.py index 41bec125f2..56a6b5a271 100644 --- a/test/unit/obj/test_updater.py +++ b/test/unit/obj/test_updater.py @@ -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(