]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
tests: don't spread fixtures.TempDir throughout test cases
authorIhar Hrachyshka <ihrachys@redhat.com>
Sat, 17 Jan 2015 12:57:21 +0000 (13:57 +0100)
committerIhar Hrachyshka <ihrachys@redhat.com>
Mon, 26 Jan 2015 12:27:01 +0000 (13:27 +0100)
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
neutron/tests/functional/agent/linux/test_async_process.py
neutron/tests/functional/agent/test_l3_agent.py
neutron/tests/unit/nec/test_nec_plugin.py
neutron/tests/unit/test_agent_linux_utils.py
neutron/tests/unit/test_policy.py

index eb1104c51f231903d2665df25817f22ea34bb3d3..6be0780502edbd05df495b1301b569e4438f9da9 100644 (file)
@@ -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(
index b2bcbd10537ed0df7918419a05a5b746d9430fb9..6a9c95d3bf45245ccf0a1b325b4d1a256650afd9 100644 (file)
@@ -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)
index 7ce844a2131b8cc20bf471844468d0228f2c7df6..138b213404eb2122b37a8db2e6158512196bb621 100755 (executable)
@@ -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()
index c1c48022d093d853349b14ac1ccb63fcb6c7747c..907af04853669952d6f50b9d7704e481ad407a07 100644 (file)
@@ -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)
 
index f23f7c37748d1b86970b00d40d7d3ca9042d858d..ec0db57c6c0e20d2adc569124c979f31f0694dd7 100644 (file)
@@ -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()
index 73cde0949811693766fede72844f4bcadcc879c2..35d983ee161cfaff63995b924d45dbb20b43cb86 100644 (file)
@@ -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": '!',