Merge lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed-3 into lp:ubuntu-repository-cache

Proposed by Haw Loeung
Status: Merged
Approved by: Haw Loeung
Approved revision: 321
Merged at revision: 311
Proposed branch: lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed-3
Merge into: lp:ubuntu-repository-cache
Diff against target: 107 lines (+38/-28)
2 files modified
lib/ubuntu_repository_cache/apache.py (+23/-14)
tests/unit/test_apache.py (+15/-14)
To merge this branch: bzr merge lp:~hloeung/ubuntu-repository-cache/only-reload-apache2-when-needed-3
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+398328@code.launchpad.net

Commit message

Only reload/graceful apache2 when required - LP:1916084

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.

Revision history for this message
Joel Sing (jsing) wrote :

LGTM, see minor comment inline.

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Merge proposal is approved, but source revision has changed, setting status to needs review.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change has no commit message, setting status to needs review.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 311

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/ubuntu_repository_cache/apache.py'
2--- lib/ubuntu_repository_cache/apache.py 2021-02-19 05:25:57 +0000
3+++ lib/ubuntu_repository_cache/apache.py 2021-02-19 09:01:14 +0000
4@@ -208,18 +208,27 @@
5 start()
6
7
8-def configure_health_check():
9- config = hookenv.config()
10-
11- LOG('Apache enable_healthcheck changed')
12- if not config['enable_healthcheck']:
13- subprocess.check_call(['a2dismod', 'cgi'])
14- LOG('Disabled health check endpoint')
15- return
16-
17- subprocess.check_call(['touch', HEALTH_CHECK_LOG])
18- subprocess.check_call(['chown', HEALTH_CHECK_LOG_OWNER, HEALTH_CHECK_LOG])
19- subprocess.check_call(['a2enmod', 'cgi'])
20+def configure_health_check(config=None):
21+ if not config:
22+ config = hookenv.config()
23+
24+ restart_required = False
25+
26+ if config.changed('enable_healthcheck'):
27+ LOG('Apache enable_healthcheck changed')
28+ if config['enable_healthcheck']:
29+ cmd = 'a2enmod'
30+ action = 'Enabled'
31+ else:
32+ cmd = 'a2dismod'
33+ action = 'Disabled'
34+
35+ subprocess.check_call([cmd, 'cgi'])
36+ LOG('{} health check endpoint'.format(action))
37+ restart_required = True
38+
39+ subprocess.call(['touch', HEALTH_CHECK_LOG])
40+ subprocess.call(['chown', HEALTH_CHECK_LOG_OWNER, HEALTH_CHECK_LOG])
41
42 apache_path = unitdata.kv().get('apache-root')
43 healthcheck_path = os.path.join(apache_path, 'health-check')
44@@ -228,8 +237,8 @@
45 with open('files/health_check.py', 'r') as f:
46 content = f.read()
47 host.write_file(path=healthcheck_script, content=content, perms=0o755)
48- LOG('Enabled health check endpoint')
49- start()
50+
51+ return restart_required
52
53
54 @util.run_as_user('root')
55
56=== modified file 'tests/unit/test_apache.py'
57--- tests/unit/test_apache.py 2021-02-19 05:23:14 +0000
58+++ tests/unit/test_apache.py 2021-02-19 09:01:14 +0000
59@@ -10,7 +10,7 @@
60 sys.modules['charms.layer'] = mock.MagicMock()
61
62 from charms.layer import status # NOQA: E402
63-from charmhelpers.core import unitdata # NOQA: E402
64+from charmhelpers.core import hookenv, unitdata # NOQA: E402
65
66 # Add path to where our reactive layer lives and import.
67 sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
68@@ -58,25 +58,26 @@
69 status.maintenance.reset_mock()
70
71 @mock.patch('charmhelpers.core.host.write_file')
72- @mock.patch('lib.ubuntu_repository_cache.apache.start')
73 @mock.patch('os.chown')
74 @mock.patch('os.setegid')
75 @mock.patch('os.seteuid')
76+ @mock.patch('subprocess.call')
77 @mock.patch('subprocess.check_call')
78- def test_configure_health_check(self, check_call, seteuid, setegid, chown, start, write_file):
79- self.mock_config.return_value = {
80- 'enable_healthcheck': False,
81- }
82- apache.configure_health_check()
83+ def test_configure_health_check(self, check_call, call, seteuid, setegid, chown, write_file):
84+ config = hookenv.Config({'enable_healthcheck': True})
85+ self.assertEqual(apache.configure_health_check(config), True)
86+ check_call.assert_called_with(['a2enmod', 'cgi'])
87+
88+ check_call.reset_mock()
89+ config = hookenv.Config({'enable_healthcheck': False})
90+ self.assertEqual(apache.configure_health_check(config), True)
91 check_call.assert_called_with(['a2dismod', 'cgi'])
92
93- os.mkdir(os.path.join(self.tmpdir, 'apache'))
94-
95- self.mock_config.return_value = {
96- 'enable_healthcheck': True,
97- }
98- apache.configure_health_check()
99- check_call.assert_called_with(['a2enmod', 'cgi'])
100+ check_call.reset_mock()
101+ config = hookenv.Config({'enable_healthcheck': True})
102+ config.save()
103+ config = hookenv.Config({'enable_healthcheck': True})
104+ self.assertEqual(apache.configure_health_check(config), False)
105
106 @mock.patch('charmhelpers.core.templating.render')
107 @mock.patch('lib.ubuntu_repository_cache.apache.start')

Subscribers

People subscribed via source and target branches