Avoid reading real config files in unit test

When we call oslo.config.cfg.CONF(), it will do basic setup and
search configuration files under certain directories, by default,
they are [/etc, /etc/ceilometer, ~/, ~/.ceilometer]. However, we
should avoid such behaviour when we run unit test code, because that
if the system has run devstack or installed ceilometer services,
then /etc/ceilometer/ceilometer.conf will exist, then unit test result
can be different.

So we should mock the default searching directories of cfg in the
begining of all unit tests, which will call cfg.CONF() at somewhere
during the test. The best place to mock the default directories
can be ceilometer.test.base.BaseTestCase, which should be used as
parent class of all other test classes.

Note: since this patch sets default searching directory to
{repo}/etc/ceilometer, you can run the unit test with different settings
which are set in etc/ceilometer/ceilometer.conf (not tracked by git).

Note: mock.patch.stopall has compatibility problem with oslo.fixture,
so this patch slightly refactors the
tests.compute.pollsters.base.TestPollsterBase.setUp method.

Change-Id: I533ffb2ba2c9be0223cecbcf04176312e4a96369
Closes-Bug: #1328550
This commit is contained in:
ZhiQiang Fan 2014-06-14 13:19:19 +08:00 committed by ZhiQiang Fan
parent b19c138295
commit 59e647f82c
2 changed files with 11 additions and 6 deletions

View File

@ -51,6 +51,12 @@ class BaseTestCase(test.BaseTestCase):
'ceilometer.messaging.get_transport', 'ceilometer.messaging.get_transport',
return_value=self.transport)) return_value=self.transport))
def setUp(self):
super(BaseTestCase, self).setUp()
self.useFixture(mockpatch.Patch(
'oslo.config.cfg._get_config_dirs',
return_value=[self.path_get('etc/ceilometer')]))
def assertTimestampEqual(self, first, second, msg=None): def assertTimestampEqual(self, first, second, msg=None):
"""Checks that two timestamps are equals. """Checks that two timestamps are equals.

View File

@ -19,6 +19,7 @@
import mock import mock
from ceilometer.openstack.common.fixture import mockpatch
import ceilometer.tests.base as base import ceilometer.tests.base as base
@ -27,8 +28,6 @@ class TestPollsterBase(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestPollsterBase, self).setUp() super(TestPollsterBase, self).setUp()
self.addCleanup(mock.patch.stopall)
self.inspector = mock.Mock() self.inspector = mock.Mock()
self.instance = mock.MagicMock() self.instance = mock.MagicMock()
self.instance.name = 'instance-00000001' self.instance.name = 'instance-00000001'
@ -39,7 +38,7 @@ class TestPollsterBase(base.BaseTestCase):
'ram': 512, 'disk': 20, 'ephemeral': 0} 'ram': 512, 'disk': 20, 'ephemeral': 0}
self.instance.status = 'active' self.instance.status = 'active'
patch_virt = mock.patch('ceilometer.compute.virt.inspector' patch_virt = mockpatch.Patch(
'.get_hypervisor_inspector', 'ceilometer.compute.virt.inspector.get_hypervisor_inspector',
mock.Mock(return_value=self.inspector)) new=mock.Mock(return_value=self.inspector))
patch_virt.start() self.useFixture(patch_virt)