From eca9d8cd28f7ce6c71c8491e43281a0c0e3bf663 Mon Sep 17 00:00:00 2001 From: Chris MacNaughton Date: Wed, 30 Jan 2019 15:54:56 +0100 Subject: [PATCH] Update source/packages if config changes Also, this change removes the harden decorators as the required configuration for harden is _not_ present in the charm config, rendering it useless. Change-Id: I20c124d9588b8fd6c0e6611725a848eaf892f6af Closes-Bug: #1812219 --- hooks/ceph_hooks.py | 19 +++++++++++-------- unit_tests/test_ceph_hooks.py | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/hooks/ceph_hooks.py b/hooks/ceph_hooks.py index 202f654..6347e0e 100755 --- a/hooks/ceph_hooks.py +++ b/hooks/ceph_hooks.py @@ -53,7 +53,6 @@ from ceph_broker import ( ) from utils import get_unit_hostname -from charmhelpers.contrib.hardening.harden import harden hooks = Hooks() @@ -66,17 +65,19 @@ def install_upstart_scripts(): @hooks.hook('install.real') -@harden() def install(): execd_preinstall() - add_source(config('source'), config('key')) - apt_update(fatal=True) - apt_install(packages=ceph.PACKAGES, fatal=True) + package_install() install_upstart_scripts() -def emit_cephconf(): +def package_install(): + add_source(config('source'), config('key')) + apt_update(fatal=True) + apt_install(packages=ceph.PACKAGES, fatal=True) + +def emit_cephconf(): cephcontext = { 'auth_supported': config('auth-supported'), 'mon_hosts': config('monitor-hosts'), @@ -117,8 +118,11 @@ def emit_cephconf(): @hooks.hook('config-changed') -@harden() def config_changed(): + c = config() + if c.previous('source') != config('source') or \ + c.previous('key') != config('key'): + package_install() emit_cephconf() @@ -237,7 +241,6 @@ def assess_status(): @hooks.hook('update-status') -@harden() def update_status(): log('Updating status.') diff --git a/unit_tests/test_ceph_hooks.py b/unit_tests/test_ceph_hooks.py index 4f65596..0b394cf 100644 --- a/unit_tests/test_ceph_hooks.py +++ b/unit_tests/test_ceph_hooks.py @@ -51,8 +51,8 @@ class TestHooks(test_utils.CharmTestCase): self.log.side_effect = fake_log @mock.patch('subprocess.check_output') - def test_radosgw_realtion(self, mock_check_output): - + @mock.patch('ceph_hooks.apt_install') + def test_radosgw_relation(self, mock_apt_install, mock_check_output): settings = {'ceph-public-address': '127.0.0.1:1234 [::1]:4321', 'radosgw_key': CEPH_KEY, 'auth': 'cephx', @@ -66,6 +66,7 @@ class TestHooks(test_utils.CharmTestCase): hooks.radosgw_relation() self.relation_set.assert_called_with(relation_id=None, relation_settings=settings) + mock_apt_install.assert_called_with(packages=[]) @mock.patch('ceph.ceph_user') @mock.patch.object(hooks, 'radosgw_relation') @@ -136,3 +137,32 @@ class TestHooks(test_utils.CharmTestCase): self.relation_set.assert_called_with(relation_id='client:1', relation_settings=data) + + @mock.patch('ceph_hooks.emit_cephconf') + @mock.patch('ceph_hooks.package_install') + def test_config_get_skips_package_update(self, + mock_package_install, + mock_emit_cephconf): + previous_test_config = test_utils.TestConfig() + previous_test_config.set('source', 'distro') + previous_test_config.set('key', '') + previous = mock.MagicMock().return_value + previous.previous.side_effect = lambda x: previous_test_config.get(x) + self.config.side_effect = [previous, "distro", ""] + hooks.config_changed() + mock_package_install.assert_not_called() + mock_emit_cephconf.assert_any_call() + + @mock.patch('ceph_hooks.emit_cephconf') + @mock.patch('ceph_hooks.package_install') + def test_update_apt_source(self, mock_package_install, mock_emit_cephconf): + + previous_test_config = test_utils.TestConfig() + previous_test_config.set('source', 'distro') + previous_test_config.set('key', '') + previous = mock.MagicMock().return_value + previous.previous.side_effect = lambda x: previous_test_config.get(x) + self.config.side_effect = [previous, "cloud:cosmic-mimic", ""] + hooks.config_changed() + mock_package_install.assert_called_with() + mock_emit_cephconf.assert_called_with()