Merge ~nick-moffitt/content-cache-charm:open-ports into content-cache-charm:master

Proposed by Nick Moffitt
Status: Merged
Approved by: Nick Moffitt
Approved revision: 6f0e6d7a9876dfd7dd657344bd50c966a2d03412
Merged at revision: 9a84bd3cf23a415ad640a5564f9cb570f1fcc2d6
Proposed branch: ~nick-moffitt/content-cache-charm:open-ports
Merge into: content-cache-charm:master
Diff against target: 62 lines (+19/-3)
2 files modified
reactive/content_cache.py (+11/-0)
tests/unit/test_content_cache.py (+8/-3)
Reviewer Review Type Date Requested Status
Joel Sing (community) +1 Approve
Review via email: mp+366716@code.launchpad.net

Commit message

open_port to fix LP:1825320

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
Joel Sing (jsing) wrote :

This only opens ports - don't we also need to close them?

review: Needs Information
Revision history for this message
Nick Moffitt (nick-moffitt) wrote :

When would we close them? And when would we have the information about which ports to close?

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

LGTM with one nit comment inline.

It would be good to have test coverage of the open_port/close_port calls though (follow up is fine).

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

Change successfully merged at revision 9a84bd3cf23a415ad640a5564f9cb570f1fcc2d6

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/reactive/content_cache.py b/reactive/content_cache.py
2index 88509a5..305ea0a 100644
3--- a/reactive/content_cache.py
4+++ b/reactive/content_cache.py
5@@ -154,6 +154,8 @@ def configure_haproxy():
6 status.blocked('list of sites provided is invalid')
7 return
8
9+ old_ports = set(hookenv.opened_ports())
10+ opened_ports = set()
11 # We need to slot in the caching layer here.
12 new_conf = {}
13 for site, site_conf in sites.items():
14@@ -171,6 +173,12 @@ def configure_haproxy():
15 new_conf[cached_site]['tls-cert-bundle-path'] = tls_cert_bundle_path
16
17 new_conf[cached_site]['port'] = site_conf.get('port') or default_port
18+ try:
19+ new_port = int(new_conf[cached_site]['port'])
20+ except ValueError as e:
21+ hookenv.log('Only integer ports are supported: {}'.format(e))
22+ hookenv.open_port(new_port)
23+ opened_ports.add(new_port)
24
25 # XXX: Reduce complexity here
26
27@@ -225,6 +233,9 @@ def configure_haproxy():
28 if not new_conf[cached_site]['locations']:
29 new_conf[cached_site]['locations'][location] = new_cached_loc_conf
30
31+ for obsolete_port in old_ports.difference(opened_ports):
32+ hookenv.close_port(obsolete_port)
33+
34 if haproxy.monitoring_password:
35 rendered_config = haproxy.render(new_conf, monitoring_password=haproxy.monitoring_password)
36 else:
37diff --git a/tests/unit/test_content_cache.py b/tests/unit/test_content_cache.py
38index e1390c0..4752787 100644
39--- a/tests/unit/test_content_cache.py
40+++ b/tests/unit/test_content_cache.py
41@@ -221,13 +221,18 @@ site1.local:
42
43 with mock.patch('lib.haproxy.HAProxyConf.conf_file', new_callable=mock.PropertyMock) as mock_conf_file:
44 mock_conf_file.return_value = os.path.join(self.tmpdir, 'haproxy.cfg')
45- with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"):
46- content_cache.configure_haproxy()
47+ with mock.patch('charmhelpers.core.host.pwgen', return_value="biometricsarenotsecret"), \
48+ mock.patch('charmhelpers.core.hookenv.opened_ports', return_value=[443]), \
49+ mock.patch('charmhelpers.core.hookenv.open_port'), mock.patch('charmhelpers.core.hookenv.close_port'):
50+ content_cache.configure_haproxy()
51 self.assertFalse(service_start_or_restart.assert_called_with('haproxy'))
52
53 # Again, this time should be no change so no need to restart HAProxy
54 service_start_or_restart.reset_mock()
55- content_cache.configure_haproxy()
56+ with mock.patch('charmhelpers.core.hookenv.opened_ports', return_value=[443]),\
57+ mock.patch('charmhelpers.core.hookenv.open_port'), \
58+ mock.patch('charmhelpers.core.hookenv.close_port'):
59+ content_cache.configure_haproxy()
60 self.assertFalse(service_start_or_restart.assert_not_called())
61
62 with open('tests/unit/files/content_cache_rendered_haproxy_test_output.txt', 'r', encoding='utf-8') as f:

Subscribers

People subscribed via source and target branches