import os
+import fixtures
import testtools
from quantum.agent.linux import utils
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')
# 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")
"""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
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):