From 59e647f82c3b89f94517a1c3b15a40d677ccec79 Mon Sep 17 00:00:00 2001 From: ZhiQiang Fan Date: Sat, 14 Jun 2014 13:19:19 +0800 Subject: [PATCH] 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 --- ceilometer/tests/base.py | 6 ++++++ ceilometer/tests/compute/pollsters/base.py | 11 +++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ceilometer/tests/base.py b/ceilometer/tests/base.py index 3914788f3..bb7530170 100644 --- a/ceilometer/tests/base.py +++ b/ceilometer/tests/base.py @@ -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. diff --git a/ceilometer/tests/compute/pollsters/base.py b/ceilometer/tests/compute/pollsters/base.py index e7ff4b2d2..a9afc64f9 100644 --- a/ceilometer/tests/compute/pollsters/base.py +++ b/ceilometer/tests/compute/pollsters/base.py @@ -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)