Remove graduated test and fixtures libraries

The config fixture is now part of oslo.config and the other ones are
part of oslotest. Let's remove them!

Change-Id: I90ab87772bda17a3f7872c41db869c4e278cf57c
This commit is contained in:
Julien Danjou 2014-09-24 10:16:17 +02:00
parent f563cf4a1a
commit 70189c940d
17 changed files with 9 additions and 441 deletions

View File

@ -1,5 +1,2 @@
# List of obsolete modules and files as glob patterns (since some
# files in a module may still belong in the incubator).
openstack/common/test.py oslotest
openstack/common/fixture/moxstubout.py oslotest
openstack/common/fixture/mockpatch.py oslotest

View File

@ -1,85 +0,0 @@
#
# Copyright 2013 Mirantis, Inc.
# Copyright 2013 OpenStack Foundation
# 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.
import fixtures
from oslo.config import cfg
import six
class Config(fixtures.Fixture):
"""Allows overriding configuration settings for the test.
`conf` will be reset on cleanup.
"""
def __init__(self, conf=cfg.CONF):
self.conf = conf
def setUp(self):
super(Config, self).setUp()
# NOTE(morganfainberg): unregister must be added to cleanup before
# reset is because cleanup works in reverse order of registered items,
# and a reset must occur before unregistering options can occur.
self.addCleanup(self._unregister_config_opts)
self.addCleanup(self.conf.reset)
self._registered_config_opts = {}
def config(self, **kw):
"""Override configuration values.
The keyword arguments are the names of configuration options to
override and their values.
If a `group` argument is supplied, the overrides are applied to
the specified configuration option group, otherwise the overrides
are applied to the ``default`` group.
"""
group = kw.pop('group', None)
for k, v in six.iteritems(kw):
self.conf.set_override(k, v, group)
def _unregister_config_opts(self):
for group in self._registered_config_opts:
self.conf.unregister_opts(self._registered_config_opts[group],
group=group)
def register_opt(self, opt, group=None):
"""Register a single option for the test run.
Options registered in this manner will automatically be unregistered
during cleanup.
If a `group` argument is supplied, it will register the new option
to that group, otherwise the option is registered to the ``default``
group.
"""
self.conf.register_opt(opt, group=group)
self._registered_config_opts.setdefault(group, set()).add(opt)
def register_opts(self, opts, group=None):
"""Register multiple options for the test run.
This works in the same manner as register_opt() but takes a list of
options as the first argument. All arguments will be registered to the
same group if the ``group`` argument is supplied, otherwise all options
will be registered to the ``default`` group.
"""
for opt in opts:
self.register_opt(opt, group=group)

View File

@ -1,62 +0,0 @@
# 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.
##############################################################################
##############################################################################
#
# DO NOT MODIFY THIS FILE
#
# This file is being graduated to the oslotest library. Please make all
# changes there, and only backport critical fixes here. - dhellmann
#
##############################################################################
##############################################################################
import fixtures
import mock
class PatchObject(fixtures.Fixture):
"""Deal with code around mock."""
def __init__(self, obj, attr, new=mock.DEFAULT, **kwargs):
self.obj = obj
self.attr = attr
self.kwargs = kwargs
self.new = new
def setUp(self):
super(PatchObject, self).setUp()
_p = mock.patch.object(self.obj, self.attr, self.new, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)
class Patch(fixtures.Fixture):
"""Deal with code around mock.patch."""
def __init__(self, obj, new=mock.DEFAULT, **kwargs):
self.obj = obj
self.kwargs = kwargs
self.new = new
def setUp(self):
super(Patch, self).setUp()
_p = mock.patch(self.obj, self.new, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)

View File

@ -1,43 +0,0 @@
# 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.
##############################################################################
##############################################################################
#
# DO NOT MODIFY THIS FILE
#
# This file is being graduated to the oslotest library. Please make all
# changes there, and only backport critical fixes here. - dhellmann
#
##############################################################################
##############################################################################
import fixtures
from six.moves import mox
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 = self.mox.stubs
self.addCleanup(self.mox.UnsetStubs)
self.addCleanup(self.mox.VerifyAll)

View File

@ -1,99 +0,0 @@
# Copyright (c) 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.
##############################################################################
##############################################################################
#
# DO NOT MODIFY THIS FILE
#
# This file is being graduated to the oslotest library. Please make all
# changes there, and only backport critical fixes here. - dhellmann
#
##############################################################################
##############################################################################
"""Common utilities used in testing"""
import logging
import os
import tempfile
import fixtures
import testtools
_TRUE_VALUES = ('True', 'true', '1', 'yes')
_LOG_FORMAT = "%(levelname)8s [%(name)s] %(message)s"
class BaseTestCase(testtools.TestCase):
def setUp(self):
super(BaseTestCase, self).setUp()
self._set_timeout()
self._fake_output()
self._fake_logs()
self.useFixture(fixtures.NestedTempfile())
self.useFixture(fixtures.TempHomeDir())
self.tempdirs = []
def _set_timeout(self):
test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
try:
test_timeout = int(test_timeout)
except ValueError:
# If timeout value is invalid do not set a timeout.
test_timeout = 0
if test_timeout > 0:
self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
def _fake_output(self):
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
stdout = self.useFixture(fixtures.StringStream('stdout')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
if os.environ.get('OS_STDERR_CAPTURE') in _TRUE_VALUES:
stderr = self.useFixture(fixtures.StringStream('stderr')).stream
self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
def _fake_logs(self):
if os.environ.get('OS_DEBUG') in _TRUE_VALUES:
level = logging.DEBUG
else:
level = logging.INFO
capture_logs = os.environ.get('OS_LOG_CAPTURE') in _TRUE_VALUES
if capture_logs:
self.useFixture(
fixtures.FakeLogger(
format=_LOG_FORMAT,
level=level,
nuke_handlers=capture_logs,
)
)
else:
logging.basicConfig(format=_LOG_FORMAT, level=level)
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)
tempfiles.append(path)
try:
os.write(fd, contents)
finally:
os.close(fd)
return tempfiles

View File

@ -1,68 +0,0 @@
#
# Copyright 2013 Mirantis, Inc.
# Copyright 2013 OpenStack Foundation
# 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.
from oslo.config import cfg
from openstack.common.fixture import config
from tests import utils
conf = cfg.CONF
class ConfigTestCase(utils.BaseTestCase):
def setUp(self):
super(ConfigTestCase, self).setUp()
self.config_fixture = self.useFixture(config.Config(conf))
self.config = self.config_fixture.config
self.register_opt = self.config_fixture.register_opt
self.register_opts = self.config_fixture.register_opts
self.config_fixture.register_opt(cfg.StrOpt(
'testing_option', default='initial_value'))
def test_overriden_value(self):
self.assertEqual(conf.get('testing_option'), 'initial_value')
self.config(testing_option='changed_value')
self.assertEqual(conf.get('testing_option'),
self.config_fixture.conf.get('testing_option'))
def test_cleanup(self):
self.config(testing_option='changed_value')
self.assertEqual(self.config_fixture.conf.get('testing_option'),
'changed_value')
self.config_fixture.conf.reset()
self.assertEqual(conf.get('testing_option'), 'initial_value')
def test_register_option(self):
opt = cfg.StrOpt('new_test_opt', default='initial_value')
self.config_fixture.register_opt(opt)
self.assertEqual(conf.get('new_test_opt'),
opt.default)
def test_register_options(self):
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
self.register_opts([opt1, opt2])
self.assertEqual(conf.get('first_test_opt'), opt1.default)
self.assertEqual(conf.get('second_test_opt'), opt2.default)
def test_cleanup_unregister_option(self):
opt = cfg.StrOpt('new_test_opt', default='initial_value')
self.config_fixture.register_opt(opt)
self.assertEqual(conf.get('new_test_opt'),
opt.default)
self.config_fixture.cleanUp()
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')

View File

@ -1,65 +0,0 @@
# Copyright 2014 IBM Corp.
#
# 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.
##############################################################################
##############################################################################
#
# DO NOT MODIFY THIS FILE
#
# This file is being graduated to the oslotest library. Please make all
# changes there, and only backport critical fixes here. - dhellmann
#
##############################################################################
##############################################################################
import mock
from openstack.common.fixture import mockpatch
from tests import utils
class Foo(object):
def bar(self):
pass
def mocking_bar(self):
return 'mocked!'
class TestMockPatch(utils.BaseTestCase):
def test_mock_patch_with_replacement(self):
self.useFixture(mockpatch.Patch('%s.Foo.bar' % (__name__),
mocking_bar))
instance = Foo()
self.assertEqual(instance.bar(), 'mocked!')
def test_mock_patch_without_replacement(self):
self.useFixture(mockpatch.Patch('%s.Foo.bar' % (__name__)))
instance = Foo()
self.assertIsInstance(instance.bar(), mock.MagicMock)
class TestMockPatchObject(utils.BaseTestCase):
def test_mock_patch_object_with_replacement(self):
self.useFixture(mockpatch.PatchObject(Foo, 'bar', mocking_bar))
instance = Foo()
self.assertEqual(instance.bar(), 'mocked!')
def test_mock_patch_object_without_replacement(self):
self.useFixture(mockpatch.PatchObject(Foo, 'bar'))
instance = Foo()
self.assertIsInstance(instance.bar(), mock.MagicMock)

View File

@ -11,12 +11,11 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from oslo.config import fixture as config
from oslotest import base as test_base
import six
import webob
from openstack.common.fixture import config
from openstack.common.middleware import sizelimit

View File

@ -14,10 +14,10 @@
# under the License.
import mock
from oslo.config import fixture as config
from oslotest import base as test_base
from oslotest import mockpatch
from openstack.common.fixture import config
from openstack.common import log as logging
LOG = logging.getLogger(__name__)

View File

@ -21,11 +21,11 @@ import errno
import socket
import eventlet
from oslo.config import fixture as config
from oslotest import base as test_base
from oslotest import moxstubout
from openstack.common import eventlet_backdoor
from openstack.common.fixture import config
class BackdoorPortTest(test_base.BaseTestCase):

View File

@ -23,11 +23,11 @@ import threading
import time
from oslo.config import cfg
from oslo.config import fixture as config
from oslotest import base as test_base
import six
from six import moves
from openstack.common.fixture import config
from openstack.common.fixture import lockutils as fixtures
from openstack.common import lockutils

View File

@ -21,12 +21,12 @@ import tempfile
import mock
from oslo.config import cfg
from oslo.config import fixture as config
from oslotest import base as test_base
import six
from openstack.common import context
from openstack.common import fileutils
from openstack.common.fixture import config
from openstack.common import gettextutils
from openstack.common import jsonutils
from openstack.common import local

View File

@ -19,10 +19,10 @@ Unit Tests for periodic_task decorator and PeriodicTasks class.
"""
import mock
from oslo.config import fixture as config
from oslotest import base as test_base
from testtools import matchers
from openstack.common.fixture import config
from openstack.common import periodic_task

View File

@ -19,13 +19,13 @@ import os
import mock
from oslo.config import cfg
from oslo.config import fixture as config
from oslotest import base as test_base
import six
import six.moves.urllib.parse as urlparse
import six.moves.urllib.request as urlrequest
from openstack.common import fileutils
from openstack.common.fixture import config
from openstack.common.fixture import lockutils
from openstack.common import jsonutils
from openstack.common import policy

View File

@ -14,10 +14,10 @@
import datetime
import mock
from oslo.config import fixture as config
from oslotest import base as test_base
from oslotest import moxstubout
from openstack.common.fixture import config
from openstack.common import quota

View File

@ -31,12 +31,12 @@ import traceback
import eventlet
from eventlet import event
import mock
from oslo.config import fixture as config
from oslotest import base as test_base
from oslotest import moxstubout
from six.moves import mox
from openstack.common import eventlet_backdoor
from openstack.common.fixture import config
from openstack.common import log as logging
from openstack.common import service

View File

@ -56,9 +56,6 @@ commands =
tests.unit.cache.test_memory \
tests.unit.config.test_generator \
tests.unit.crypto.test_utils \
tests.unit.fixture.test_config \
tests.unit.fixture.test_logging \
tests.unit.fixture.test_mockpatch \
tests.unit.middleware.test_catch_errors \
tests.unit.middleware.test_correlation_id \
tests.unit.middleware.test_request_id \
@ -112,9 +109,6 @@ commands =
tests.unit.cache.test_memory \
tests.unit.config.test_generator \
tests.unit.crypto.test_utils \
tests.unit.fixture.test_config \
tests.unit.fixture.test_logging \
tests.unit.fixture.test_mockpatch \
tests.unit.middleware.test_catch_errors \
tests.unit.middleware.test_correlation_id \
tests.unit.middleware.test_notifier \