make enforce_type=True in CONF.set_override

Method CONF.set_override to change config option's
value with designated value in unit test, but never check if the
designated vaule is valid. Each config option has a type like StrOpt,
BoolOpt, etc. StrOpt with parameter choices only allows values in set
of choices. In short, each config option has limitation for type
and value. In production code, oslo.conf can ensure user's input is
valid, but in unit test, test methods can pass if we use method
CONF.set_override without parameter enforce_type=True even we pass wrong
type or wrong value to config option. This commit makes sure calling
method CONF.set_override with enforce_type=True and fixes violations.

Note: We can't set enforce_type=True by default in oslo.config now, it
may break all project's unit test. We can switch enforce_type=True by
default when all project fix violations like this commit.
Related-Bug: #1517839

Change-Id: Ia9f6b8a04290f93699fc1f613ce0e841f040d4ae
This commit is contained in:
LiuNanke 2016-01-12 20:45:28 +08:00
parent 4d78a7b34f
commit e0c1653fa8
5 changed files with 12 additions and 7 deletions

View File

@ -56,7 +56,7 @@ class TestCaseShell(testtools.TestCase):
CONF.clear()
def override_config(self, name, override, group=None):
CONF.set_override(name, override, group)
CONF.set_override(name, override, group, enforce_type=True)
self.addCleanup(CONF.clear_override, name, group)
def shell(self, cmd_args=None, exitcode=0):

View File

@ -30,7 +30,7 @@ class MuranoTestCase(testtools.TestCase):
self.useFixture(fixtures.FakeLogger('murano'))
def override_config(self, name, override, group=None):
CONF.set_override(name, override, group)
CONF.set_override(name, override, group, enforce_type=True)
self.addCleanup(CONF.clear_override, name, group)

View File

@ -55,14 +55,16 @@ class BaseWalkMigrationTestCase(object):
should use oslo.config and openstack.commom.db.sqlalchemy.session with
database functionality (reset default settings and session cleanup).
"""
CONF.set_override('connection', str(engine.url), group='database')
CONF.set_override('connection', str(engine.url), group='database',
enforce_type=True)
def _alembic_command(self, alembic_command, engine, *args, **kwargs):
"""Most of alembic command return data into output.
We should redefine this setting for getting info.
"""
self.ALEMBIC_CONFIG.stdout = buf = io.StringIO()
CONF.set_override('connection', str(engine.url), group='database')
CONF.set_override('connection', str(engine.url), group='database',
enforce_type=True)
getattr(command, alembic_command)(*args, **kwargs)
res = buf.getvalue().strip()
LOG.debug('Alembic command `{command}` returns: {result}'.format(

View File

@ -29,7 +29,8 @@ class TestCombinedPackageLoader(base.MuranoTestCase):
super(TestCombinedPackageLoader, cls).setUpClass()
location = os.path.dirname(__file__)
CONF.set_override('load_packages_from', [location], 'engine')
CONF.set_override('load_packages_from', [location], 'engine',
enforce_type=True)
cls.murano_client_factory = mock.MagicMock()
cls.loader = package_loader.CombinedPackageLoader(
cls.murano_client_factory, 'test_tenant_id')

View File

@ -45,7 +45,8 @@ class TestHeatStack(base.MuranoTestCase):
client_manager_mock.get_heat_client.return_value = \
self.heat_client_mock
self.environment_mock.clients = client_manager_mock
CONF.set_override('stack_tags', ['test-murano'], 'heat')
CONF.set_override('stack_tags', ['test-murano'], 'heat',
enforce_type=True)
self.mock_tag = ','.join(CONF.heat.stack_tags)
@mock.patch(MOD_NAME + '.HeatStack._wait_state')
@ -235,7 +236,8 @@ class TestHeatStack(base.MuranoTestCase):
status_get.return_value = 'NOT_FOUND'
wait_st.return_value = {}
CONF.set_override('stack_tags', ['test-murano', 'murano-tag'], 'heat')
CONF.set_override('stack_tags', ['test-murano', 'murano-tag'], 'heat',
enforce_type=True)
context = {constants.CTX_ENVIRONMENT: self.environment_mock}
with helpers.contextual(context):