Test: make enforce_type=True in CONF.set_override

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.
Closes-bug: #1517839

Change-Id: If01841b30c10e994028dcdefbb389425cb0facb1
This commit is contained in:
LiuNanke 2016-01-06 22:42:00 +08:00
parent 4f56510f62
commit 9f525c098f
9 changed files with 37 additions and 21 deletions

View File

@ -43,7 +43,7 @@ class FunctionalTest(db_test_base.TestBase):
self.CONF.set_override('policy_file',
os.path.abspath('etc/aodh/policy.json'),
group='oslo_policy')
group='oslo_policy', enforce_type=True)
self.app = self._make_app()
def _make_app(self):

View File

@ -398,7 +398,8 @@ class TestAlarms(TestAlarmsBase):
def test_get_alarm_forbiden(self):
pf = os.path.abspath('aodh/tests/functional/api/v2/policy.json-test')
self.CONF.set_override('policy_file', pf, group='oslo_policy')
self.CONF.set_override('policy_file', pf, group='oslo_policy',
enforce_type=True)
self.app = self._make_app()
response = self.get_json('/alarms',

View File

@ -148,7 +148,8 @@ class TestBase(test_base.BaseTestCase):
conf = service.prepare_service(argv=[], config_files=[])
self.CONF = self.useFixture(fixture_config.Config(conf)).conf
self.CONF.set_override('connection', db_url, group="database")
self.CONF.set_override('connection', db_url, group="database",
enforce_type=True)
manager = self.DRIVER_MANAGERS.get(engine)
if not manager:
@ -159,7 +160,7 @@ class TestBase(test_base.BaseTestCase):
self.useFixture(self.db_manager)
self.CONF.set_override('connection', self.db_manager.url,
group="database")
group="database", enforce_type=True)
self.alarm_conn = storage.get_connection_from_config(self.CONF)
self.alarm_conn.upgrade()

View File

@ -70,12 +70,15 @@ class ConfigFixture(fixture.GabbiFixture):
conf.set_override('policy_file',
os.path.abspath(
'aodh/tests/open-policy.json'),
group='oslo_policy')
group='oslo_policy',
enforce_type=True)
database_name = '%s-%s' % (db_url, str(uuid.uuid4()))
conf.set_override('connection', database_name, group='database')
conf.set_override('connection', database_name, group='database',
enforce_type=True)
conf.set_override('pecan_debug', True, group='api')
conf.set_override('pecan_debug', True, group='api',
enforce_type=True)
def stop_fixture(self):
"""Reset the config and remove data."""

View File

@ -35,14 +35,15 @@ class EngineTest(base.BaseTestCase):
def test_get_connection(self):
self.CONF.set_override('connection', 'log://localhost',
group='database')
group='database', enforce_type=True)
engine = storage.get_connection_from_config(self.CONF)
self.assertIsInstance(engine, impl_log.Connection)
def test_get_connection_no_such_engine(self):
self.CONF.set_override('connection', 'no-such-engine://localhost',
group='database')
self.CONF.set_override('max_retries', 0, 'database')
group='database', enforce_type=True)
self.CONF.set_override('max_retries', 0, 'database',
enforce_type=True)
try:
storage.get_connection_from_config(self.CONF)
except RuntimeError as err:
@ -69,9 +70,12 @@ class ConnectionRetryTest(base.BaseTestCase):
raise ConnectionError
log_init.side_effect = x
self.CONF.set_override("connection", "log://", "database")
self.CONF.set_override("retry_interval", 0.00001, "database")
self.CONF.set_override("max_retries", max_retries, "database")
self.CONF.set_override("connection", "log://", "database",
enforce_type=True)
self.CONF.set_override("retry_interval", 0.00001, "database",
enforce_type=True)
self.CONF.set_override("max_retries", max_retries, "database",
enforce_type=True)
self.assertRaises(ConnectionError,
storage.get_connection_from_config,
self.CONF)
@ -85,6 +89,7 @@ class ConnectionConfigTest(base.BaseTestCase):
self.CONF = self.useFixture(fixture_config.Config(conf)).conf
def test_only_default_url(self):
self.CONF.set_override("connection", "log://", group="database")
self.CONF.set_override("connection", "log://", group="database",
enforce_type=True)
conn = storage.get_connection_from_config(self.CONF)
self.assertIsInstance(conn, impl_log.Connection)

View File

@ -51,11 +51,13 @@ class IndexTest(tests_db.TestBase):
# create a fake index and check it is deleted
coll = getattr(conn.db, coll_name)
index_name = '%s_ttl' % coll_name
self.CONF.set_override(ttl_opt, -1, group='database')
self.CONF.set_override(ttl_opt, -1, group='database',
enforce_type=True)
conn.upgrade()
self.assertNotIn(index_name, coll.index_information())
self.CONF.set_override(ttl_opt, 456789, group='database')
self.CONF.set_override(ttl_opt, 456789, group='database',
enforce_type=True)
conn.upgrade()
self.assertEqual(456789,
coll.index_information()
@ -67,14 +69,16 @@ class IndexTest(tests_db.TestBase):
def _test_ttl_index_present(self, conn, coll_name, ttl_opt):
coll = getattr(conn.db, coll_name)
self.CONF.set_override(ttl_opt, 456789, group='database')
self.CONF.set_override(ttl_opt, 456789, group='database',
enforce_type=True)
conn.upgrade()
index_name = '%s_ttl' % coll_name
self.assertEqual(456789,
coll.index_information()
[index_name]['expireAfterSeconds'])
self.CONF.set_override(ttl_opt, -1, group='database')
self.CONF.set_override(ttl_opt, -1, group='database',
enforce_type=True)
conn.upgrade()
self.assertNotIn(index_name, coll.index_information())

View File

@ -150,7 +150,7 @@ class TestPartitioning(base.BaseTestCase):
coordinator_cls=None):
coordinator_cls = coordinator_cls or MockToozCoordinator
self.CONF.set_override('backend_url', 'xxx://yyy',
group='coordination')
group='coordination', enforce_type=True)
with mock.patch('tooz.coordination.get_coordinator',
lambda _, member_id:
coordinator_cls(member_id, shared_storage)):

View File

@ -57,7 +57,8 @@ class TestAlarmEvaluationService(tests_base.BaseTestCase):
test_interval)
self.CONF.set_override('heartbeat',
coordination_heartbeat,
group='coordination')
group='coordination',
enforce_type=True)
with mock.patch('aodh.storage.get_connection_from_config',
return_value=self.storage_conn):
p_coord_mock = self.svc.partition_coordinator

View File

@ -24,7 +24,8 @@ def main(argv):
url = ("%s?table_prefix=%s" %
(os.getenv("AODH_TEST_STORAGE_URL"),
os.getenv("AODH_TEST_HBASE_TABLE_PREFIX", "test")))
cfg.CONF.set_override("connection", url, group="database")
cfg.CONF.set_override("connection", url, group="database",
enforce_type=True)
alarm_conn = storage.get_connection_from_config(cfg.CONF)
for arg in argv:
if arg == "--upgrade":