From c4f10dce1391fd5183c36669dd19dbf1a95efcd9 Mon Sep 17 00:00:00 2001 From: Vasyl Saienko Date: Thu, 9 Nov 2017 16:58:50 +0200 Subject: [PATCH] Use oslo_config.fixture in unit tests It provides cleaner per-tests config objects, with more options to configure the fixture. Also, allow overriding config fixture in particular test cases if needed. Change-Id: I2e9945b6168d97d7ee861924ac2f4ca5cd03dde2 --- ironic_python_agent/tests/unit/base.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ironic_python_agent/tests/unit/base.py b/ironic_python_agent/tests/unit/base.py index 97d1bfe22..c8a399572 100644 --- a/ironic_python_agent/tests/unit/base.py +++ b/ironic_python_agent/tests/unit/base.py @@ -20,10 +20,14 @@ import subprocess import ironic_lib import mock from oslo_concurrency import processutils +from oslo_config import cfg +from oslo_config import fixture as config_fixture from oslotest import base as test_base from ironic_python_agent import utils +CONF = cfg.CONF + class IronicAgentTest(test_base.BaseTestCase): """Extends the base test to provide common features across agent tests.""" @@ -40,6 +44,7 @@ class IronicAgentTest(test_base.BaseTestCase): If the mock is called, an exception is raised to warn the tester. """ + self._set_config() # NOTE(bigjools): Not using a decorator on tests because I don't # want to force every test method to accept a new arg. Instead, they # can override or examine this self._exec_patch Mock as needed. @@ -58,3 +63,16 @@ class IronicAgentTest(test_base.BaseTestCase): self.patch(subprocess, 'check_call', self._exec_patch) self.patch(subprocess, 'check_output', self._exec_patch) self.patch(utils, 'execute', self._exec_patch) + + def _set_config(self): + self.cfg_fixture = self.useFixture(config_fixture.Config(CONF)) + + def config(self, **kw): + """Override config options for a test.""" + self.cfg_fixture.config(**kw) + + def set_defaults(self, **kw): + """Set default values of config options.""" + group = kw.pop('group', None) + for o, v in kw.items(): + self.cfg_fixture.set_default(o, v, group=group)