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 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")

View 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):

View File

@ -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):