Merge "Ensure update config function can handle new headers"

This commit is contained in:
Zuul 2021-07-22 17:14:06 +00:00 committed by Gerrit Code Review
commit 80534d77f2
2 changed files with 35 additions and 1 deletions

View File

@ -635,7 +635,12 @@ class MySQLRouterCharm(charms_openstack.charm.OpenStackCharm):
config.read(self.mysqlrouter_conf)
for heading in parameters.keys():
for param in parameters[heading].keys():
config[heading][param] = parameters[heading][param]
# BUG LP#1927981 - heading may not exist during a charm upgrade
# Handle missing heading via direct assignment in except.
try:
config[heading][param] = parameters[heading][param]
except KeyError:
config[heading] = {param: parameters[heading][param]}
ch_core.hookenv.log("Writing {}".format(
self.mysqlrouter_conf))
with open(self.mysqlrouter_conf, 'w') as configfile:

View File

@ -661,6 +661,35 @@ class TestMySQLRouterCharm(test_utils.PatchHelper):
_mock_config_parser.write.assert_called_once_with(
self.mock_open()().__enter__())
def test_update_config_parameters_missing_heading(self):
# test fix for Bug LP#1927981
class FakeConfigParser(dict):
def read(*args, **kwargs):
pass
def write(*args, **kwargs):
pass
current_config = {"DEFAULT": {"client_ssl_mode": "NONE"}}
fake_config = FakeConfigParser(current_config)
self.patch_object(mysql_router.configparser, "ConfigParser",
return_value=fake_config)
# metadata_cache:jujuCluster didn't exist in the previous config so the
# header needs to be created (c.f. BUG LP#1927981)
_params = {
"DEFAULT": {"client_ssl_mode": "PREFERRED"},
"metadata_cache:jujuCluster": {"thing": "a-thing"},
}
mrc = mysql_router.MySQLRouterCharm()
# should not throw a key error.
mrc.update_config_parameters(_params)
self.assertIn('metadata_cache:jujuCluster', fake_config)
self.assertEqual(fake_config['metadata_cache:jujuCluster'],
{"thing": "a-thing"})
def test_config_changed(self):
_config_data = {
"ttl": '5',