]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Replace direct tempfile usage with a fixture.
authorMonty Taylor <mordred@inaugust.com>
Tue, 19 Feb 2013 22:00:12 +0000 (14:00 -0800)
committerMonty Taylor <mordred@inaugust.com>
Thu, 28 Feb 2013 22:21:05 +0000 (17:21 -0500)
tempfiles should always be created in fixtures to ensure that they actually
get cleaned up when we're done.

Change-Id: If4eee03b2e9ec6bd5333b8d5022ec9809343584e

quantum/tests/unit/_test_rootwrap_exec.py
quantum/tests/unit/test_agent_linux_utils.py
quantum/tests/unit/test_policy.py

index f3c37d42117beb55b504249b7c47b29c3cc719f6..188717cca640d261b0b1dfb8a254cd64fbfe2945 100644 (file)
@@ -17,6 +17,7 @@
 
 import os
 
+import fixtures
 import testtools
 
 from quantum.agent.linux import utils
@@ -44,7 +45,8 @@ class RootwrapTestExec(testtools.TestCase):
         self.cwd = os.getcwd() + "/../../.."
         # stuff a stupid bash script into /tmp, so that the next
         # method can execute it.
-        self.test_file = '/tmp/rootwrap-test.sh'
+        self.test_file = self.useFixture(
+            fixtures.TempDir()).join("rootwrap-test.sh")
         with open(self.test_file, 'w') as f:
             f.write('#!/bin/bash\n')
             f.write('ID=`id | sed \'s/uid=//\' | sed \'s/(.*//\' `\n')
@@ -54,7 +56,8 @@ to the aid of their party.\"\n")
         # we need a temporary conf file, pointing into pwd for the filter
         # specs. there's probably a better way to do this, but I couldn't
         # figure it out.  08/15/12 -- jrd
-        self.conf_file = '/tmp/rootwrap.conf'
+        self.conf_file = self.useFixture(
+            fixtures.TempDir()).join("rootwrap.conf")
         with open(self.conf_file, 'w') as f:
             f.write("# temporary conf file for rootwrap-test, " +
                     "generated by test_rootwrap.py\n")
index b1f5c0e7fa14919b598e779b3e5a019a61e59627..bd94dd6a9dcceac27f7e05d94d94132cbaab51ac 100644 (file)
@@ -15,6 +15,7 @@
 #    under the License.
 # @author: Dan Wendlandt, Nicira, Inc.
 
+import fixtures
 import mock
 import testtools
 
@@ -25,7 +26,8 @@ class AgentUtilsExecuteTest(testtools.TestCase):
     def setUp(self):
         super(AgentUtilsExecuteTest, self).setUp()
         self.root_helper = "echo"
-        self.test_file = "/tmp/test_execute.tmp"
+        self.test_file = self.useFixture(
+            fixtures.TempDir()).join("test_execute.tmp")
         open(self.test_file, 'w').close()
 
     def test_without_helper(self):
index cc652a959c22fad3ead9b03510bd44d8cc39a897..2539d84d43180044d187b45b2b69b60033843b21 100644 (file)
 """Test of Policy Engine For Quantum"""
 
 import contextlib
-import os.path
+import os
 import shutil
 import StringIO
-import tempfile
 import urllib2
 
+import fixtures
 import mock
 import testtools
 
@@ -40,42 +40,30 @@ class PolicyFileTestCase(testtools.TestCase):
         self.addCleanup(policy.reset)
         self.context = context.Context('fake', 'fake')
         self.target = {}
-
-    @contextlib.contextmanager
-    def _tempdir(self, **kwargs):
-        tmpdir = tempfile.mkdtemp(**kwargs)
-        try:
-            yield tmpdir
-        finally:
-            try:
-                shutil.rmtree(tmpdir)
-            except OSError, e:
-                #TODO: fail test on raise
-                pass
+        self.tempdir = self.useFixture(fixtures.TempDir())
 
     def test_modified_policy_reloads(self):
-        with self._tempdir() as tmpdir:
-            def fake_find_config_file(_1, _2):
-                return os.path.join(tmpdir, 'policy')
-
-            with mock.patch.object(quantum.common.utils,
-                                   'find_config_file',
-                                   new=fake_find_config_file):
-                tmpfilename = os.path.join(tmpdir, 'policy')
-                action = "example:test"
-                with open(tmpfilename, "w") as policyfile:
-                    policyfile.write("""{"example:test": ""}""")
-                policy.enforce(self.context, action, self.target)
-                with open(tmpfilename, "w") as policyfile:
-                    policyfile.write("""{"example:test": "!"}""")
-                # NOTE(vish): reset stored policy cache so we don't have to
-                # sleep(1)
-                policy._POLICY_CACHE = {}
-                self.assertRaises(exceptions.PolicyNotAuthorized,
-                                  policy.enforce,
-                                  self.context,
-                                  action,
-                                  self.target)
+        def fake_find_config_file(_1, _2):
+            return self.tempdir.join('policy')
+
+        with mock.patch.object(quantum.common.utils,
+                               'find_config_file',
+                               new=fake_find_config_file):
+            tmpfilename = fake_find_config_file(None, None)
+            action = "example:test"
+            with open(tmpfilename, "w") as policyfile:
+                policyfile.write("""{"example:test": ""}""")
+            policy.enforce(self.context, action, self.target)
+            with open(tmpfilename, "w") as policyfile:
+                policyfile.write("""{"example:test": "!"}""")
+            # NOTE(vish): reset stored policy cache so we don't have to
+            # sleep(1)
+            policy._POLICY_CACHE = {}
+            self.assertRaises(exceptions.PolicyNotAuthorized,
+                              policy.enforce,
+                              self.context,
+                              action,
+                              self.target)
 
 
 class PolicyTestCase(testtools.TestCase):