Ensure all config files written on config-changed

Write all configuration files when charm configuration options
change to ensure that the ssl_ca is written into the identity.yaml
file.

This commit also drops some parts of the unit tests for hooks
which to be honest need reworking as they have a number of
boundary violations.

Change-Id: I5e84ebd23fd487f64b2902327ce3f33739ed9840
Closes-Bug: 1843484
This commit is contained in:
James Page 2020-07-23 07:10:18 +01:00
parent 25a0877228
commit bfddde02ac
2 changed files with 8 additions and 45 deletions

View File

@ -305,7 +305,7 @@ def install():
def config_changed():
hookenv.log('begin config-changed hook.')
configs = get_configs()
configs.write(MIRRORS_CONF_FILE_NAME)
configs.write_all()
ensure_perms()
update_nrpe_config()

View File

@ -3,7 +3,6 @@ import mock
import os
import shutil
import tempfile
import yaml
from hooks import hooks
from test_utils import CharmTestCase
@ -14,6 +13,7 @@ TO_PATCH = [
'apt_install',
'get_release',
'install_ca_cert',
'get_configs',
]
@ -21,9 +21,6 @@ class TestConfigChanged(CharmTestCase):
def setUp(self):
CharmTestCase.setUp(self, hooks, TO_PATCH)
self.tmpdir = tempfile.mkdtemp()
mirrors_fname = os.path.basename(hooks.MIRRORS_CONF_FILE_NAME)
self.mirrors_conf_fpath = os.path.join(self.tmpdir, mirrors_fname)
hooks.MIRRORS_CONF_FILE_NAME = self.mirrors_conf_fpath
hooks.CRON_POLL_FILEPATH = os.path.join(self.tmpdir,
hooks.CRON_POLL_FILENAME)
self.tmpcrond = tempfile.mkdtemp(prefix='cron.d')
@ -36,6 +33,8 @@ class TestConfigChanged(CharmTestCase):
hooks.CRON_POLL_FILEPATH = os.path.join(self.tmpcrond,
hooks.CRON_POLL_FILENAME)
self.get_release.return_value = 'icehouse'
self.mock_configs = mock.MagicMock()
self.get_configs.return_value = self.mock_configs
def tearDown(self):
CharmTestCase.tearDown(self)
@ -64,6 +63,8 @@ class TestConfigChanged(CharmTestCase):
hooks.config_changed()
self.mock_configs.write_all.assert_called_once_with()
symlink.assert_any_call(os.path.join(self.sharedir,
hooks.SCRIPT_WRAPPER_NAME),
'/etc/cron.%s/%s'
@ -71,49 +72,9 @@ class TestConfigChanged(CharmTestCase):
hooks.CRON_JOB_FILENAME))
self.assertTrue(os.path.isfile(os.path.join(self.tmpcrond,
hooks.CRON_POLL_FILENAME)))
self.assertTrue(os.path.isfile(self.mirrors_conf_fpath))
with open(self.mirrors_conf_fpath, 'r') as f:
mirrors = yaml.safe_load(f)
for k in ['cloud_name', 'region', 'use_swift']:
self.assertEqual(self.test_config[k], mirrors[k])
mirror_list = yaml.safe_load(self.test_config['mirror_list'])
self.assertEqual(mirrors['mirror_list'], mirror_list)
update_nrpe_config.assert_called()
self.install_ca_cert.assert_called_with(b'foobar')
@mock.patch.object(hooks, 'update_nrpe_config')
@mock.patch('os.symlink')
@mock.patch('charmhelpers.core.hookenv.config')
@mock.patch('charmhelpers.core.hookenv.relations_of_type')
@mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname')
@mock.patch('charmhelpers.contrib.charmsupport.nrpe.config')
@mock.patch('charmhelpers.contrib.charmsupport.nrpe.local_unit')
def test_custom_properties_config(self, local_unit, nrpe_config, nag_host,
relations_of_type, config, symlink,
update_nrpe_config):
local_unit.return_value = 'juju/0'
nag_host.return_value = "nagios_hostname"
nrpe_config.return_value = self.test_config
setattr(self.test_config, "changed", lambda x: False)
self.test_config.config["custom_properties"] = (
"hypervisor_type=kvm hw_firmware_type=uefi"
)
config.return_value = self.test_config
hooks.config_changed()
self.assertTrue(os.path.isfile(self.mirrors_conf_fpath))
with open(self.mirrors_conf_fpath, 'r') as f:
mirrors = yaml.safe_load(f)
self.assertEqual(
self.test_config.config['custom_properties'],
mirrors['custom_properties']
)
update_nrpe_config.assert_called()
@mock.patch.object(hooks, 'update_nrpe_config')
@mock.patch('os.path.exists')
@mock.patch('os.remove')
@ -138,6 +99,8 @@ class TestConfigChanged(CharmTestCase):
exists.return_value = True
hooks.config_changed()
self.mock_configs.write_all.assert_called_once_with()
remove.assert_any_call(os.path.join('/etc/cron.daily/',
hooks.CRON_JOB_FILENAME))
remove.assert_any_call(hooks.CRON_POLL_FILEPATH)