diff --git a/tests/test_cfg.py b/tests/test_cfg.py index 25a11e06..4fd9228b 100644 --- a/tests/test_cfg.py +++ b/tests/test_cfg.py @@ -19,14 +19,14 @@ import shutil import StringIO import sys import tempfile -import unittest -import stubout +import fixtures from oslo.config.cfg import * +from tests import utils -class ExceptionsTestCase(unittest.TestCase): +class ExceptionsTestCase(utils.BaseTestCase): def test_error(self): msg = str(Error('foobar')) @@ -73,7 +73,7 @@ class ExceptionsTestCase(unittest.TestCase): self.assertEquals(msg, 'Failed to parse foo: foobar') -class BaseTestCase(unittest.TestCase): +class BaseTestCase(utils.BaseTestCase): class TestConfigOpts(ConfigOpts): def __call__(self, args=None): @@ -85,35 +85,26 @@ class BaseTestCase(unittest.TestCase): default_config_files=[]) def setUp(self): + super(BaseTestCase, self).setUp() + self.useFixture(fixtures.NestedTempfile()) self.conf = self.TestConfigOpts() - self.tempfiles = [] self.tempdirs = [] - self.stubs = stubout.StubOutForTesting() - - def tearDown(self): - self.remove_tempfiles() - self.stubs.UnsetAll() def create_tempfiles(self, files, ext='.conf'): + tempfiles = [] for (basename, contents) in files: if not os.path.isabs(basename): (fd, path) = tempfile.mkstemp(prefix=basename, suffix=ext) else: path = basename + ext fd = os.open(path, os.O_CREAT | os.O_WRONLY) - self.tempfiles.append(path) + tempfiles.append(path) try: os.write(fd, contents) finally: os.close(fd) - return self.tempfiles[-len(files):] - - def remove_tempfiles(self): - for p in self.tempfiles: - os.remove(p) - for d in self.tempdirs: - shutil.rmtree(d, ignore_errors=True) + return tempfiles class UsageTestCase(BaseTestCase): @@ -1428,12 +1419,12 @@ class SadPathTestCase(BaseTestCase): self._do_test_bad_cli_value(FloatOpt) def test_conf_file_not_found(self): - paths = self.create_tempfiles([('test', '')]) - os.remove(paths[0]) - self.tempfiles.remove(paths[0]) + (fd, path) = tempfile.mkstemp() + + os.remove(path) self.assertRaises(ConfigFilesNotFoundError, - self.conf, ['--config-file', paths[0]]) + self.conf, ['--config-file', path]) def test_conf_file_broken(self): paths = self.create_tempfiles([('test', 'foo')]) @@ -1573,7 +1564,7 @@ class OptDumpingTestCase(BaseTestCase): ]) -class ConfigParserTestCase(unittest.TestCase): +class ConfigParserTestCase(utils.BaseTestCase): def test_no_section(self): with tempfile.NamedTemporaryFile() as tmpfile: tmpfile.write('foo = bar') diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..2d9d435f --- /dev/null +++ b/tests/utils.py @@ -0,0 +1,50 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010-2011 OpenStack, LLC +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# Copyright 2013 Hewlett-Packard Development Company, L.P. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Common utilities used in testing""" + +import fixtures +import mox +import stubout +import testtools + + +class MoxStubout(fixtures.Fixture): + """Deal with code around mox and stubout as a fixture.""" + + def setUp(self): + super(MoxStubout, self).setUp() + # emulate some of the mox stuff, we can't use the metaclass + # because it screws with our generators + self.mox = mox.Mox() + self.stubs = stubout.StubOutForTesting() + self.addCleanup(self.mox.UnsetStubs) + self.addCleanup(self.stubs.UnsetAll) + self.addCleanup(self.stubs.SmartUnsetAll) + self.addCleanup(self.mox.VerifyAll) + + +class BaseTestCase(testtools.TestCase): + + def setUp(self): + super(BaseTestCase, self).setUp() + self.stubs = self.useFixture(MoxStubout()).stubs + self.useFixture(fixtures.FakeLogger('oslo.config')) + self.useFixture(fixtures.Timeout(30, True)) diff --git a/tools/test-requires b/tools/test-requires index 264047e9..17899ff0 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -1,6 +1,8 @@ +fixtures>=0.3.12 mox nose nose-exclude +testtools>=0.9.22 # when we can require tox>= 1.4, this can go into tox.ini: # [testenv:cover]