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
This commit is contained in:
Monty Taylor 2013-02-19 14:00:12 -08:00
parent 06e98c9933
commit c334c3dd5a
3 changed files with 31 additions and 38 deletions

View File

@ -17,6 +17,7 @@
import os import os
import fixtures
import testtools import testtools
from quantum.agent.linux import utils from quantum.agent.linux import utils
@ -44,7 +45,8 @@ class RootwrapTestExec(testtools.TestCase):
self.cwd = os.getcwd() + "/../../.." self.cwd = os.getcwd() + "/../../.."
# stuff a stupid bash script into /tmp, so that the next # stuff a stupid bash script into /tmp, so that the next
# method can execute it. # 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: with open(self.test_file, 'w') as f:
f.write('#!/bin/bash\n') f.write('#!/bin/bash\n')
f.write('ID=`id | sed \'s/uid=//\' | sed \'s/(.*//\' `\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 # 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 # specs. there's probably a better way to do this, but I couldn't
# figure it out. 08/15/12 -- jrd # 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: with open(self.conf_file, 'w') as f:
f.write("# temporary conf file for rootwrap-test, " + f.write("# temporary conf file for rootwrap-test, " +
"generated by test_rootwrap.py\n") "generated by test_rootwrap.py\n")

View File

@ -15,6 +15,7 @@
# under the License. # under the License.
# @author: Dan Wendlandt, Nicira, Inc. # @author: Dan Wendlandt, Nicira, Inc.
import fixtures
import mock import mock
import testtools import testtools
@ -25,7 +26,8 @@ class AgentUtilsExecuteTest(testtools.TestCase):
def setUp(self): def setUp(self):
super(AgentUtilsExecuteTest, self).setUp() super(AgentUtilsExecuteTest, self).setUp()
self.root_helper = "echo" 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() open(self.test_file, 'w').close()
def test_without_helper(self): def test_without_helper(self):

View File

@ -16,12 +16,12 @@
"""Test of Policy Engine For Quantum""" """Test of Policy Engine For Quantum"""
import contextlib import contextlib
import os.path import os
import shutil import shutil
import StringIO import StringIO
import tempfile
import urllib2 import urllib2
import fixtures
import mock import mock
import testtools import testtools
@ -40,42 +40,30 @@ class PolicyFileTestCase(testtools.TestCase):
self.addCleanup(policy.reset) self.addCleanup(policy.reset)
self.context = context.Context('fake', 'fake') self.context = context.Context('fake', 'fake')
self.target = {} self.target = {}
self.tempdir = self.useFixture(fixtures.TempDir())
@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
def test_modified_policy_reloads(self): def test_modified_policy_reloads(self):
with self._tempdir() as tmpdir: def fake_find_config_file(_1, _2):
def fake_find_config_file(_1, _2): return self.tempdir.join('policy')
return os.path.join(tmpdir, 'policy')
with mock.patch.object(quantum.common.utils, with mock.patch.object(quantum.common.utils,
'find_config_file', 'find_config_file',
new=fake_find_config_file): new=fake_find_config_file):
tmpfilename = os.path.join(tmpdir, 'policy') tmpfilename = fake_find_config_file(None, None)
action = "example:test" action = "example:test"
with open(tmpfilename, "w") as policyfile: with open(tmpfilename, "w") as policyfile:
policyfile.write("""{"example:test": ""}""") policyfile.write("""{"example:test": ""}""")
policy.enforce(self.context, action, self.target) policy.enforce(self.context, action, self.target)
with open(tmpfilename, "w") as policyfile: with open(tmpfilename, "w") as policyfile:
policyfile.write("""{"example:test": "!"}""") policyfile.write("""{"example:test": "!"}""")
# NOTE(vish): reset stored policy cache so we don't have to # NOTE(vish): reset stored policy cache so we don't have to
# sleep(1) # sleep(1)
policy._POLICY_CACHE = {} policy._POLICY_CACHE = {}
self.assertRaises(exceptions.PolicyNotAuthorized, self.assertRaises(exceptions.PolicyNotAuthorized,
policy.enforce, policy.enforce,
self.context, self.context,
action, action,
self.target) self.target)
class PolicyTestCase(testtools.TestCase): class PolicyTestCase(testtools.TestCase):