Test: 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.

Change-Id: I4ec78078e8e6effc0123866c0ce941353c96fb3d
Related-Bug: #1517839
This commit is contained in:
ChangBo Guo(gcb)
2015-11-19 19:17:32 +08:00
parent ff23237343
commit a687c6f02a
6 changed files with 53 additions and 27 deletions
@@ -282,7 +282,8 @@ class TestClusterControllerWithStrategy(TestCase):
mock_cluster_create,
mock_get_datastore_version):
cfg.CONF.set_override('cluster_support', False, group='mongodb')
cfg.CONF.set_override('cluster_support', False, group='mongodb',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -307,7 +308,8 @@ class TestClusterControllerWithStrategy(TestCase):
mock_get_datastore_version,
mock_cluster_view_data):
cfg.CONF.set_override('cluster_support', True, group='mongodb')
cfg.CONF.set_override('cluster_support', True, group='mongodb',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -254,7 +254,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_cluster_create,
mock_get_datastore_version):
cfg.CONF.set_override('cluster_support', False, group='pxc')
cfg.CONF.set_override('cluster_support', False, group='pxc',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -279,7 +280,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_get_datastore_version,
mock_cluster_view_data):
cfg.CONF.set_override('cluster_support', True, group='pxc')
cfg.CONF.set_override('cluster_support', True, group='pxc',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -287,7 +287,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_cluster_create,
mock_get_datastore_version):
cfg.CONF.set_override('cluster_support', False, group='redis')
cfg.CONF.set_override('cluster_support', False, group='redis',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -316,7 +317,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_get_datastore_version,
mock_cluster_view_data):
cfg.CONF.set_override('cluster_support', True, group='redis')
cfg.CONF.set_override('cluster_support', True, group='redis',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -254,7 +254,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_cluster_create,
mock_get_datastore_version):
cfg.CONF.set_override('cluster_support', False, group='vertica')
cfg.CONF.set_override('cluster_support', False, group='vertica',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
@@ -279,7 +280,8 @@ class TestClusterControllerWithStrategy(trove_testtools.TestCase):
mock_get_datastore_version,
mock_cluster_view_data):
cfg.CONF.set_override('cluster_support', True, group='vertica')
cfg.CONF.set_override('cluster_support', True, group='vertica',
enforce_type=True)
body = self.cluster
tenant_id = Mock()
+32 -16
View File
@@ -296,7 +296,8 @@ class TestCreateCinderClient(trove_testtools.TestCase):
def test_create_with_conf_override(self):
cinder_url_from_conf = 'http://example.com'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('cinder_url', cinder_url_from_conf)
cfg.CONF.set_override('cinder_url', cinder_url_from_conf,
enforce_type=True)
client = remote.create_cinder_client(
TroveContext(tenant=tenant_from_ctx))
@@ -306,7 +307,8 @@ class TestCreateCinderClient(trove_testtools.TestCase):
def test_create_with_conf_override_trailing_slash(self):
cinder_url_from_conf = 'http://example.com/'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('cinder_url', cinder_url_from_conf)
cfg.CONF.set_override('cinder_url', cinder_url_from_conf,
enforce_type=True)
client = remote.create_cinder_client(
TroveContext(tenant=tenant_from_ctx))
self.assertEqual('%s%s' % (cinder_url_from_conf, tenant_from_ctx),
@@ -319,8 +321,10 @@ class TestCreateCinderClient(trove_testtools.TestCase):
client.client.management_url)
def test_create_with_catalog_all_opts(self):
cfg.CONF.set_override('cinder_service_type', 'volume')
cfg.CONF.set_override('os_region_name', 'RegionTwo')
cfg.CONF.set_override('cinder_service_type', 'volume',
enforce_type=True)
cfg.CONF.set_override('os_region_name', 'RegionTwo',
enforce_type=True)
client = remote.create_cinder_client(
TroveContext(service_catalog=self.service_catalog))
self.assertEqual(self.volume_public_url_region_two,
@@ -371,7 +375,8 @@ class TestCreateNovaClient(trove_testtools.TestCase):
def test_create_with_conf_override(self):
nova_url_from_conf = 'http://example.com'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf)
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
enforce_type=True)
client = remote.create_nova_client(
TroveContext(tenant=tenant_from_ctx))
@@ -381,7 +386,8 @@ class TestCreateNovaClient(trove_testtools.TestCase):
def test_create_with_conf_override_trailing_slash(self):
nova_url_from_conf = 'http://example.com/'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf)
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
enforce_type=True)
client = remote.create_nova_client(
TroveContext(tenant=tenant_from_ctx))
self.assertEqual('%s%s' % (nova_url_from_conf, tenant_from_ctx),
@@ -394,8 +400,10 @@ class TestCreateNovaClient(trove_testtools.TestCase):
client.client.management_url)
def test_create_with_catalog_all_opts(self):
cfg.CONF.set_override('nova_compute_service_type', 'computev3')
cfg.CONF.set_override('os_region_name', 'RegionTwo')
cfg.CONF.set_override('nova_compute_service_type', 'computev3',
enforce_type=True)
cfg.CONF.set_override('os_region_name', 'RegionTwo',
enforce_type=True)
client = remote.create_nova_client(
TroveContext(service_catalog=self.service_catalog))
self.assertEqual(self.computev3_public_url_region_two,
@@ -403,7 +411,8 @@ class TestCreateNovaClient(trove_testtools.TestCase):
def test_create_admin_client(self):
nova_url_from_conf = 'http://adminexample.com/'
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf)
cfg.CONF.set_override('nova_compute_url', nova_url_from_conf,
enforce_type=True)
admin_user = 'admin1'
admin_pass = 'adminpwd'
admin_tenant_id = uuid.uuid4().hex
@@ -461,7 +470,8 @@ class TestCreateHeatClient(trove_testtools.TestCase):
def test_create_with_conf_override(self):
heat_url_from_conf = 'http://example.com'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('heat_url', heat_url_from_conf)
cfg.CONF.set_override('heat_url', heat_url_from_conf,
enforce_type=True)
client = remote.create_heat_client(
TroveContext(tenant=tenant_from_ctx))
@@ -471,7 +481,8 @@ class TestCreateHeatClient(trove_testtools.TestCase):
def test_create_with_conf_override_trailing_slash(self):
heat_url_from_conf = 'http://example.com/'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('heat_url', heat_url_from_conf)
cfg.CONF.set_override('heat_url', heat_url_from_conf,
enforce_type=True)
client = remote.create_heat_client(
TroveContext(tenant=tenant_from_ctx))
self.assertEqual('%s%s' % (heat_url_from_conf, tenant_from_ctx),
@@ -484,8 +495,10 @@ class TestCreateHeatClient(trove_testtools.TestCase):
client.http_client.endpoint)
def test_create_with_catalog_all_opts(self):
cfg.CONF.set_override('heat_service_type', 'orchestrationv3')
cfg.CONF.set_override('os_region_name', 'RegionTwo')
cfg.CONF.set_override('heat_service_type', 'orchestrationv3',
enforce_type=True)
cfg.CONF.set_override('os_region_name', 'RegionTwo',
enforce_type=True)
client = remote.create_heat_client(
TroveContext(service_catalog=self.service_catalog))
self.assertEqual(self.heatv3_public_url_region_two,
@@ -536,7 +549,8 @@ class TestCreateSwiftClient(trove_testtools.TestCase):
def test_create_with_conf_override(self):
swift_url_from_conf = 'http://example.com/AUTH_'
tenant_from_ctx = uuid.uuid4().hex
cfg.CONF.set_override('swift_url', swift_url_from_conf)
cfg.CONF.set_override('swift_url', swift_url_from_conf,
enforce_type=True)
client = remote.create_swift_client(
TroveContext(tenant=tenant_from_ctx))
@@ -550,8 +564,10 @@ class TestCreateSwiftClient(trove_testtools.TestCase):
client.url)
def test_create_with_catalog_all_opts(self):
cfg.CONF.set_override('swift_service_type', 'object-storev3')
cfg.CONF.set_override('os_region_name', 'RegionTwo')
cfg.CONF.set_override('swift_service_type', 'object-storev3',
enforce_type=True)
cfg.CONF.set_override('os_region_name', 'RegionTwo',
enforce_type=True)
client = remote.create_swift_client(
TroveContext(service_catalog=self.service_catalog))
self.assertEqual(self.swiftv3_public_url_region_two,
+5 -3
View File
@@ -80,9 +80,11 @@ class MockMgmtInstanceTest(trove_testtools.TestCase):
remote, 'create_admin_nova_client', return_value=self.client)
self.addCleanup(self.admin_client_patch.stop)
self.admin_client_patch.start()
CONF.set_override('host', 'test_host')
CONF.set_override('exists_notification_interval', 1)
CONF.set_override('notification_service_id', {'mysql': '123'})
CONF.set_override('host', '127.0.0.1', enforce_type=True)
CONF.set_override('exists_notification_interval', 1,
enforce_type=True)
CONF.set_override('notification_service_id', {'mysql': '123'},
enforce_type=True)
super(MockMgmtInstanceTest, self).setUp()