From c334c3dd5a9fa6b4f5660f7915b0e90e07544947 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Tue, 19 Feb 2013 14:00:12 -0800 Subject: [PATCH] Replace direct tempfile usage with a fixture. 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 | 7 ++- quantum/tests/unit/test_agent_linux_utils.py | 4 +- quantum/tests/unit/test_policy.py | 58 ++++++++------------ 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/quantum/tests/unit/_test_rootwrap_exec.py b/quantum/tests/unit/_test_rootwrap_exec.py index f3c37d421..188717cca 100644 --- a/quantum/tests/unit/_test_rootwrap_exec.py +++ b/quantum/tests/unit/_test_rootwrap_exec.py @@ -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") diff --git a/quantum/tests/unit/test_agent_linux_utils.py b/quantum/tests/unit/test_agent_linux_utils.py index b1f5c0e7f..bd94dd6a9 100644 --- a/quantum/tests/unit/test_agent_linux_utils.py +++ b/quantum/tests/unit/test_agent_linux_utils.py @@ -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): diff --git a/quantum/tests/unit/test_policy.py b/quantum/tests/unit/test_policy.py index cc652a959..2539d84d4 100644 --- a/quantum/tests/unit/test_policy.py +++ b/quantum/tests/unit/test_policy.py @@ -16,12 +16,12 @@ """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') + 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 = 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) + 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):