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
This commit is contained in:
Mehdi Abaakouk 2013-08-12 14:31:55 +02:00 committed by Julien Danjou
parent b26112ba9e
commit cc803b10f6
7 changed files with 148 additions and 16 deletions

View File

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

View File

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

View File

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

View File

@ -20,24 +20,25 @@
""" """
import fixtures import fixtures
import functools import functools
import mox
from oslo.config import cfg from oslo.config import cfg
import os.path import os.path
import stubout
import testtools import testtools
from testtools import testcase 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): class TestCase(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestCase, self).setUp() super(TestCase, self).setUp()
self.mox = mox.Mox()
self.stubs = stubout.StubOutForTesting()
self.tempdir = self.useFixture(fixtures.TempDir()) self.tempdir = self.useFixture(fixtures.TempDir())
self.useFixture(fixtures.FakeLogger()) 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') cfg.CONF([], project='ceilometer')
# Set a default location for the pipeline config file so the # Set a default location for the pipeline config file so the
@ -45,7 +46,7 @@ class TestCase(testtools.TestCase):
# the system. # the system.
cfg.CONF.set_override( cfg.CONF.set_override(
'pipeline_cfg_file', 'pipeline_cfg_file',
self.path_get('etc/ceilometer/pipeline.yaml'), self.path_get('etc/ceilometer/pipeline.yaml')
) )
def path_get(self, project_file=None): def path_get(self, project_file=None):
@ -62,14 +63,6 @@ class TestCase(testtools.TestCase):
def temp_config_file_path(self, name='ceilometer.conf'): def temp_config_file_path(self, name='ceilometer.conf'):
return os.path.join(self.tempdir.path, name) 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): def _skip_decorator(func):
@functools.wraps(func) @functools.wraps(func)

View File

@ -48,6 +48,11 @@ from nova.openstack.common import log as logging
# sure it is defined. # sure it is defined.
config.cfg.CONF.import_opt('compute_manager', 'nova.service') 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 # HACK(dhellmann): Import this before any other ceilometer code
# because the notifier module messes with the import path to force # because the notifier module messes with the import path to force
# nova's version of oslo to be used instead of ceilometer's. # nova's version of oslo to be used instead of ceilometer's.

View File

@ -1,9 +1,11 @@
[DEFAULT] [DEFAULT]
module=config
module=context module=context
module=db module=db
module=db/sqlalchemy module=db/sqlalchemy
module=eventlet_backdoor module=eventlet_backdoor
module=excutils module=excutils
module=fixture
module=gettextutils module=gettextutils
module=importutils module=importutils
module=jsonutils module=jsonutils
@ -17,5 +19,4 @@ module=rpc
module=service module=service
module=threadgroup module=threadgroup
module=timeutils module=timeutils
module=config
base=ceilometer base=ceilometer