From cc803b10f65fa71336be3c4184726a97d2713d8c Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Mon, 12 Aug 2013 14:31:55 +0200 Subject: [PATCH] Use new olso fixture in CM tests Before this change mox and cfg.CONF wasn't cleaned up correctly if skipTest is called, because when skipTest is used teardown is not called. Now we use the oslo fixtures to setup oslo.config and mox to ensure the cleanup is done between each tests. Fixes bug #1211239 Change-Id: I5075c99106742859f0705b765f8ed4aa9be494d7 --- .../openstack/common/fixture/__init__.py | 0 ceilometer/openstack/common/fixture/config.py | 45 ++++++++++++++++ .../openstack/common/fixture/mockpatch.py | 51 +++++++++++++++++++ .../openstack/common/fixture/moxstubout.py | 37 ++++++++++++++ ceilometer/tests/base.py | 23 +++------ nova_tests/test_notifier.py | 5 ++ openstack-common.conf | 3 +- 7 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 ceilometer/openstack/common/fixture/__init__.py create mode 100644 ceilometer/openstack/common/fixture/config.py create mode 100644 ceilometer/openstack/common/fixture/mockpatch.py create mode 100644 ceilometer/openstack/common/fixture/moxstubout.py diff --git a/ceilometer/openstack/common/fixture/__init__.py b/ceilometer/openstack/common/fixture/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/ceilometer/openstack/common/fixture/config.py b/ceilometer/openstack/common/fixture/config.py new file mode 100644 index 000000000..cf52a6625 --- /dev/null +++ b/ceilometer/openstack/common/fixture/config.py @@ -0,0 +1,45 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 +# +# 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 + + +class Config(fixtures.Fixture): + """Override some 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. + + All overrides are automatically cleared at the end of the current + test by the reset() method, which is registred by addCleanup(). + """ + + def __init__(self, conf=cfg.CONF): + self.conf = conf + + def setUp(self): + super(Config, self).setUp() + self.addCleanup(self.conf.reset) + + def config(self, **kw): + group = kw.pop('group', None) + for k, v in kw.iteritems(): + self.conf.set_override(k, v, group) diff --git a/ceilometer/openstack/common/fixture/mockpatch.py b/ceilometer/openstack/common/fixture/mockpatch.py new file mode 100644 index 000000000..cd0d6ca6b --- /dev/null +++ b/ceilometer/openstack/common/fixture/mockpatch.py @@ -0,0 +1,51 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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. + +import fixtures +import mock + + +class PatchObject(fixtures.Fixture): + """Deal with code around mock.""" + + def __init__(self, obj, attr, **kwargs): + self.obj = obj + self.attr = attr + self.kwargs = kwargs + + def setUp(self): + super(PatchObject, self).setUp() + _p = mock.patch.object(self.obj, self.attr, **self.kwargs) + self.mock = _p.start() + self.addCleanup(_p.stop) + + +class Patch(fixtures.Fixture): + + """Deal with code around mock.patch.""" + + def __init__(self, obj, **kwargs): + self.obj = obj + self.kwargs = kwargs + + def setUp(self): + super(Patch, self).setUp() + _p = mock.patch(self.obj, **self.kwargs) + self.mock = _p.start() + self.addCleanup(_p.stop) diff --git a/ceilometer/openstack/common/fixture/moxstubout.py b/ceilometer/openstack/common/fixture/moxstubout.py new file mode 100644 index 000000000..f277fdd73 --- /dev/null +++ b/ceilometer/openstack/common/fixture/moxstubout.py @@ -0,0 +1,37 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# 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. + +import fixtures +import mox +import stubout + + +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) diff --git a/ceilometer/tests/base.py b/ceilometer/tests/base.py index 0e63be5de..2c0ae9ef5 100644 --- a/ceilometer/tests/base.py +++ b/ceilometer/tests/base.py @@ -20,24 +20,25 @@ """ import fixtures import functools -import mox from oslo.config import cfg import os.path -import stubout import testtools from testtools import testcase -cfg.CONF.import_opt('pipeline_cfg_file', 'ceilometer.pipeline') +from ceilometer.openstack.common.fixture import config +from ceilometer.openstack.common.fixture import moxstubout class TestCase(testtools.TestCase): - def setUp(self): super(TestCase, self).setUp() - self.mox = mox.Mox() - self.stubs = stubout.StubOutForTesting() self.tempdir = self.useFixture(fixtures.TempDir()) self.useFixture(fixtures.FakeLogger()) + self.useFixture(config.Config()) + moxfixture = self.useFixture(moxstubout.MoxStubout()) + self.mox = moxfixture.mox + self.stubs = moxfixture.stubs + cfg.CONF([], project='ceilometer') # Set a default location for the pipeline config file so the @@ -45,7 +46,7 @@ class TestCase(testtools.TestCase): # the system. cfg.CONF.set_override( 'pipeline_cfg_file', - self.path_get('etc/ceilometer/pipeline.yaml'), + self.path_get('etc/ceilometer/pipeline.yaml') ) def path_get(self, project_file=None): @@ -62,14 +63,6 @@ class TestCase(testtools.TestCase): def temp_config_file_path(self, name='ceilometer.conf'): return os.path.join(self.tempdir.path, name) - def tearDown(self): - self.mox.UnsetStubs() - self.stubs.UnsetAll() - self.stubs.SmartUnsetAll() - self.mox.VerifyAll() - cfg.CONF.reset() - super(TestCase, self).tearDown() - def _skip_decorator(func): @functools.wraps(func) diff --git a/nova_tests/test_notifier.py b/nova_tests/test_notifier.py index 87ab33578..3ebee9ee4 100644 --- a/nova_tests/test_notifier.py +++ b/nova_tests/test_notifier.py @@ -48,6 +48,11 @@ from nova.openstack.common import log as logging # sure it is defined. config.cfg.CONF.import_opt('compute_manager', 'nova.service') +# HACK(jd) Import this first because of the second HACK below, and because +# of Nova not having these module yet as of this writing +import ceilometer.openstack.common.fixture.config +import ceilometer.openstack.common.fixture.moxstubout + # HACK(dhellmann): Import this before any other ceilometer code # because the notifier module messes with the import path to force # nova's version of oslo to be used instead of ceilometer's. diff --git a/openstack-common.conf b/openstack-common.conf index e15ba6dc5..c8d2c8f27 100644 --- a/openstack-common.conf +++ b/openstack-common.conf @@ -1,9 +1,11 @@ [DEFAULT] +module=config module=context module=db module=db/sqlalchemy module=eventlet_backdoor module=excutils +module=fixture module=gettextutils module=importutils module=jsonutils @@ -17,5 +19,4 @@ module=rpc module=service module=threadgroup module=timeutils -module=config base=ceilometer