Browse Source

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
changes/69/633969/5
Chris MacNaughton 3 months ago
parent
commit
eca9d8cd28
2 changed files with 41 additions and 8 deletions
  1. 9
    6
      hooks/ceph_hooks.py
  2. 32
    2
      unit_tests/test_ceph_hooks.py

+ 9
- 6
hooks/ceph_hooks.py View File

@@ -53,7 +53,6 @@ from ceph_broker import (
53 53
 )
54 54
 
55 55
 from utils import get_unit_hostname
56
-from charmhelpers.contrib.hardening.harden import harden
57 56
 
58 57
 hooks = Hooks()
59 58
 
@@ -66,17 +65,19 @@ def install_upstart_scripts():
66 65
 
67 66
 
68 67
 @hooks.hook('install.real')
69
-@harden()
70 68
 def install():
71 69
     execd_preinstall()
70
+    package_install()
71
+    install_upstart_scripts()
72
+
73
+
74
+def package_install():
72 75
     add_source(config('source'), config('key'))
73 76
     apt_update(fatal=True)
74 77
     apt_install(packages=ceph.PACKAGES, fatal=True)
75
-    install_upstart_scripts()
76 78
 
77 79
 
78 80
 def emit_cephconf():
79
-
80 81
     cephcontext = {
81 82
         'auth_supported': config('auth-supported'),
82 83
         'mon_hosts': config('monitor-hosts'),
@@ -117,8 +118,11 @@ def emit_cephconf():
117 118
 
118 119
 
119 120
 @hooks.hook('config-changed')
120
-@harden()
121 121
 def config_changed():
122
+    c = config()
123
+    if c.previous('source') != config('source') or \
124
+       c.previous('key') != config('key'):
125
+        package_install()
122 126
     emit_cephconf()
123 127
 
124 128
 
@@ -237,7 +241,6 @@ def assess_status():
237 241
 
238 242
 
239 243
 @hooks.hook('update-status')
240
-@harden()
241 244
 def update_status():
242 245
     log('Updating status.')
243 246
 

+ 32
- 2
unit_tests/test_ceph_hooks.py View File

@@ -51,8 +51,8 @@ class TestHooks(test_utils.CharmTestCase):
51 51
         self.log.side_effect = fake_log
52 52
 
53 53
     @mock.patch('subprocess.check_output')
54
-    def test_radosgw_realtion(self, mock_check_output):
55
-
54
+    @mock.patch('ceph_hooks.apt_install')
55
+    def test_radosgw_relation(self, mock_apt_install, mock_check_output):
56 56
         settings = {'ceph-public-address': '127.0.0.1:1234 [::1]:4321',
57 57
                     'radosgw_key': CEPH_KEY,
58 58
                     'auth': 'cephx',
@@ -66,6 +66,7 @@ class TestHooks(test_utils.CharmTestCase):
66 66
         hooks.radosgw_relation()
67 67
         self.relation_set.assert_called_with(relation_id=None,
68 68
                                              relation_settings=settings)
69
+        mock_apt_install.assert_called_with(packages=[])
69 70
 
70 71
     @mock.patch('ceph.ceph_user')
71 72
     @mock.patch.object(hooks, 'radosgw_relation')
@@ -136,3 +137,32 @@ class TestHooks(test_utils.CharmTestCase):
136 137
 
137 138
         self.relation_set.assert_called_with(relation_id='client:1',
138 139
                                              relation_settings=data)
140
+
141
+    @mock.patch('ceph_hooks.emit_cephconf')
142
+    @mock.patch('ceph_hooks.package_install')
143
+    def test_config_get_skips_package_update(self,
144
+                                             mock_package_install,
145
+                                             mock_emit_cephconf):
146
+        previous_test_config = test_utils.TestConfig()
147
+        previous_test_config.set('source', 'distro')
148
+        previous_test_config.set('key', '')
149
+        previous = mock.MagicMock().return_value
150
+        previous.previous.side_effect = lambda x: previous_test_config.get(x)
151
+        self.config.side_effect = [previous, "distro", ""]
152
+        hooks.config_changed()
153
+        mock_package_install.assert_not_called()
154
+        mock_emit_cephconf.assert_any_call()
155
+
156
+    @mock.patch('ceph_hooks.emit_cephconf')
157
+    @mock.patch('ceph_hooks.package_install')
158
+    def test_update_apt_source(self, mock_package_install, mock_emit_cephconf):
159
+
160
+        previous_test_config = test_utils.TestConfig()
161
+        previous_test_config.set('source', 'distro')
162
+        previous_test_config.set('key', '')
163
+        previous = mock.MagicMock().return_value
164
+        previous.previous.side_effect = lambda x: previous_test_config.get(x)
165
+        self.config.side_effect = [previous, "cloud:cosmic-mimic", ""]
166
+        hooks.config_changed()
167
+        mock_package_install.assert_called_with()
168
+        mock_emit_cephconf.assert_called_with()

Loading…
Cancel
Save