From 6df0f426735da24eb4a0c1400e26f7626f022778 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Sat, 17 Jan 2015 13:57:21 +0100 Subject: [PATCH] tests: don't spread fixtures.TempDir throughout test cases Instead, provide self.get_temp_file_path() utility method for tests interested in creating temporary files. There also cases when tests are interested in multiple separate temporary directories. With this in mind, self.get_temp_file_path() supports root= argument that allows to pass a different temporary directory fixture than default. While at it, consolidated cleanup setup for NEC temporary file in single place. Change-Id: Ie041edcfde1b16183244a3e6068658308d2a67f5 --- neutron/tests/base.py | 39 ++++++++++++++++++- .../agent/linux/test_async_process.py | 4 +- .../tests/functional/agent/test_l3_agent.py | 13 ++++--- neutron/tests/unit/nec/test_nec_plugin.py | 9 +---- neutron/tests/unit/test_agent_linux_utils.py | 5 +-- neutron/tests/unit/test_policy.py | 7 +--- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index eb1104c51..6be078050 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -88,8 +88,7 @@ class BaseTestCase(sub_base.SubBaseTestCase): self.useFixture(lockutils.ExternalLockFixture()) - self.temp_dir = self.useFixture(fixtures.TempDir()).path - cfg.CONF.set_override('state_path', self.temp_dir) + cfg.CONF.set_override('state_path', self.get_default_temp_dir().path) self.addCleanup(CONF.reset) @@ -100,6 +99,42 @@ class BaseTestCase(sub_base.SubBaseTestCase): self.setup_rpc_mocks() self.setup_config() + def get_new_temp_dir(self): + """Create a new temporary directory. + + :returns fixtures.TempDir + """ + return self.useFixture(fixtures.TempDir()) + + def get_default_temp_dir(self): + """Create a default temporary directory. + + Returns the same directory during the whole test case. + + :returns fixtures.TempDir + """ + if not hasattr(self, '_temp_dir'): + self._temp_dir = self.get_new_temp_dir() + return self._temp_dir + + def get_temp_file_path(self, filename, root=None): + """Returns an absolute path for a temporary file. + + If root is None, the file is created in default temporary directory. It + also creates the directory if it's not initialized yet. + + If root is not None, the file is created inside the directory passed as + root= argument. + + :param filename: filename + :type filename: string + :param root: temporary directory to create a new file in + :type root: fixtures.TempDir + :returns absolute file path string + """ + root = root or self.get_default_temp_dir() + return root.join(filename) + def setup_rpc_mocks(self): # don't actually start RPC listeners when testing self.useFixture(fixtures.MonkeyPatch( diff --git a/neutron/tests/functional/agent/linux/test_async_process.py b/neutron/tests/functional/agent/linux/test_async_process.py index b2bcbd105..6a9c95d3b 100644 --- a/neutron/tests/functional/agent/linux/test_async_process.py +++ b/neutron/tests/functional/agent/linux/test_async_process.py @@ -13,7 +13,6 @@ # under the License. import eventlet -import fixtures from six import moves @@ -26,8 +25,7 @@ class TestAsyncProcess(base.BaseTestCase): def setUp(self): super(TestAsyncProcess, self).setUp() - self.test_file_path = self.useFixture( - fixtures.TempDir()).join("test_async_process.tmp") + self.test_file_path = self.get_temp_file_path('test_async_process.tmp') self.data = [str(x) for x in moves.xrange(4)] with file(self.test_file_path, 'w') as f: f.writelines('%s\n' % item for item in self.data) diff --git a/neutron/tests/functional/agent/test_l3_agent.py b/neutron/tests/functional/agent/test_l3_agent.py index 7ce844a21..138b21340 100755 --- a/neutron/tests/functional/agent/test_l3_agent.py +++ b/neutron/tests/functional/agent/test_l3_agent.py @@ -16,7 +16,6 @@ import copy import functools -import fixtures import mock import netaddr from oslo.config import cfg @@ -81,14 +80,16 @@ class L3AgentTestFramework(base.BaseOVSLinuxTestCase): conf.set_override('ovs_integration_bridge', br_int.br_name) conf.set_override('external_network_bridge', br_ex.br_name) - temp_dir = self.useFixture(fixtures.TempDir()).path - conf.set_override('state_path', temp_dir) + temp_dir = self.get_new_temp_dir() + get_temp_file_path = functools.partial(self.get_temp_file_path, + root=temp_dir) + conf.set_override('state_path', temp_dir.path) conf.set_override('metadata_proxy_socket', - '%s/metadata_proxy' % temp_dir) + get_temp_file_path('metadata_proxy')) conf.set_override('ha_confs_path', - '%s/ha_confs' % temp_dir) + get_temp_file_path('ha_confs')) conf.set_override('external_pids', - '%s/external/pids' % temp_dir) + get_temp_file_path('external/pids')) conf.set_override('host', host) agent = l3_test_agent.TestL3NATAgent(host, conf) mock.patch.object(ip_lib, 'send_gratuitous_arp').start() diff --git a/neutron/tests/unit/nec/test_nec_plugin.py b/neutron/tests/unit/nec/test_nec_plugin.py index c1c48022d..907af0485 100644 --- a/neutron/tests/unit/nec/test_nec_plugin.py +++ b/neutron/tests/unit/nec/test_nec_plugin.py @@ -13,9 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - -import fixtures import mock import webob.exc @@ -49,7 +46,7 @@ class NecPluginV2TestCaseBase(object): _nec_ini = NEC_PLUGIN_INI def _set_nec_ini(self): - self.nec_ini_file = self.useFixture(fixtures.TempDir()).join("nec.ini") + self.nec_ini_file = self.get_temp_file_path('nec.ini') with open(self.nec_ini_file, 'w') as f: f.write(self._nec_ini) if 'config_files' in test_lib.test_config.keys(): @@ -59,10 +56,10 @@ class NecPluginV2TestCaseBase(object): test_lib.test_config['config_files'].append(self.nec_ini_file) else: test_lib.test_config['config_files'] = [self.nec_ini_file] + self.addCleanup(self._clean_nec_ini) def _clean_nec_ini(self): test_lib.test_config['config_files'].remove(self.nec_ini_file) - os.remove(self.nec_ini_file) self.nec_ini_file = None def patch_remote_calls(self): @@ -73,7 +70,6 @@ class NecPluginV2TestCaseBase(object): def setup_nec_plugin_base(self): self._set_nec_ini() - self.addCleanup(self._clean_nec_ini) self.patch_remote_calls() @@ -93,7 +89,6 @@ class NecPluginV2TestCase(NecPluginV2TestCaseBase, def setUp(self, plugin=None, ext_mgr=None): self._set_nec_ini() - self.addCleanup(self._clean_nec_ini) plugin = plugin or self._plugin_name super(NecPluginV2TestCase, self).setUp(plugin, ext_mgr=ext_mgr) diff --git a/neutron/tests/unit/test_agent_linux_utils.py b/neutron/tests/unit/test_agent_linux_utils.py index f23f7c377..ec0db57c6 100644 --- a/neutron/tests/unit/test_agent_linux_utils.py +++ b/neutron/tests/unit/test_agent_linux_utils.py @@ -11,8 +11,6 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. - -import fixtures import mock import testtools @@ -40,8 +38,7 @@ class AgentUtilsExecuteTest(base.BaseTestCase): def setUp(self): super(AgentUtilsExecuteTest, self).setUp() self.root_helper = "echo" - self.test_file = self.useFixture( - fixtures.TempDir()).join("test_execute.tmp") + self.test_file = self.get_temp_file_path('test_execute.tmp') open(self.test_file, 'w').close() self.mock_popen_p = mock.patch("subprocess.Popen.communicate") self.mock_popen = self.mock_popen_p.start() diff --git a/neutron/tests/unit/test_policy.py b/neutron/tests/unit/test_policy.py index 73cde0949..35d983ee1 100644 --- a/neutron/tests/unit/test_policy.py +++ b/neutron/tests/unit/test_policy.py @@ -19,7 +19,6 @@ import contextlib import StringIO import urllib2 -import fixtures import mock from oslo.config import cfg from oslo.serialization import jsonutils @@ -44,10 +43,9 @@ class PolicyFileTestCase(base.BaseTestCase): self.addCleanup(policy.reset) self.context = context.Context('fake', 'fake', is_admin=False) self.target = {'tenant_id': 'fake'} - self.tempdir = self.useFixture(fixtures.TempDir()) def test_modified_policy_reloads(self): - tmpfilename = self.tempdir.join('policy') + tmpfilename = self.get_temp_file_path('policy') action = "example:test" with open(tmpfilename, "w") as policyfile: policyfile.write("""{"example:test": ""}""") @@ -167,8 +165,7 @@ class DefaultPolicyTestCase(base.BaseTestCase): def setUp(self): super(DefaultPolicyTestCase, self).setUp() - self.tempdir = self.useFixture(fixtures.TempDir()) - tmpfilename = self.tempdir.join('policy.json') + tmpfilename = self.get_temp_file_path('policy.json') self.rules = { "default": '', "example:exist": '!', -- 2.45.2