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:
parent
b19c138295
commit
59e647f82c
@ -51,6 +51,12 @@ class BaseTestCase(test.BaseTestCase):
|
||||
'ceilometer.messaging.get_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):
|
||||
"""Checks that two timestamps are equals.
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
import mock
|
||||
|
||||
from ceilometer.openstack.common.fixture import mockpatch
|
||||
import ceilometer.tests.base as base
|
||||
|
||||
|
||||
@ -27,8 +28,6 @@ class TestPollsterBase(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TestPollsterBase, self).setUp()
|
||||
|
||||
self.addCleanup(mock.patch.stopall)
|
||||
|
||||
self.inspector = mock.Mock()
|
||||
self.instance = mock.MagicMock()
|
||||
self.instance.name = 'instance-00000001'
|
||||
@ -39,7 +38,7 @@ class TestPollsterBase(base.BaseTestCase):
|
||||
'ram': 512, 'disk': 20, 'ephemeral': 0}
|
||||
self.instance.status = 'active'
|
||||
|
||||
patch_virt = mock.patch('ceilometer.compute.virt.inspector'
|
||||
'.get_hypervisor_inspector',
|
||||
mock.Mock(return_value=self.inspector))
|
||||
patch_virt.start()
|
||||
patch_virt = mockpatch.Patch(
|
||||
'ceilometer.compute.virt.inspector.get_hypervisor_inspector',
|
||||
new=mock.Mock(return_value=self.inspector))
|
||||
self.useFixture(patch_virt)
|
||||
|
Loading…
Reference in New Issue
Block a user