Merge ~woutervb/snap-store-proxy-charm:SN-482 into snap-store-proxy-charm:master

Proposed by Wouter van Bommel
Status: Merged
Merged at revision: f871e40176f1a52e0738d3522464e8ef8414409b
Proposed branch: ~woutervb/snap-store-proxy-charm:SN-482
Merge into: snap-store-proxy-charm:master
Prerequisite: ~woutervb/snap-store-proxy-charm:start
Diff against target: 118 lines (+32/-6)
4 files modified
src/charm.py (+4/-1)
src/helpers.py (+13/-1)
tests/test_charm.py (+5/-4)
tests/test_helpers.py (+10/-0)
Reviewer Review Type Date Requested Status
Ubuntu One hackers Pending
Review via email: mp+415482@code.launchpad.net

Commit message

Expose the port so the service is externally reachable

Fixes lp:1960762

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/charm.py b/src/charm.py
2index 65eeb23..500e623 100755
3--- a/src/charm.py
4+++ b/src/charm.py
5@@ -18,7 +18,7 @@ from ops.main import main
6 from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, ModelError
7
8 from exceptions import ConfigException
9-from helpers import all_config_options, configure_proxy, default_values
10+from helpers import all_config_options, configure_proxy, default_values, open_port
11 from optionvalidation import OptionValidation
12 from resource_helpers import (
13 create_database,
14@@ -358,6 +358,9 @@ class SnapstoreProxyCharm(CharmBase):
15 )
16 return
17
18+ # We are in a proper state, let's open the port
19+ open_port(ssl=False)
20+
21 self.unit.status = ActiveStatus()
22
23 def _on_update_status(self, _):
24diff --git a/src/helpers.py b/src/helpers.py
25index 07cf19c..7b440e6 100644
26--- a/src/helpers.py
27+++ b/src/helpers.py
28@@ -3,7 +3,7 @@
29 #
30 import logging
31 from pathlib import Path
32-from subprocess import run
33+from subprocess import check_call, run
34
35 import yaml
36
37@@ -57,4 +57,16 @@ def config_options():
38 return type_dict, default_value
39
40
41+def open_port(ssl=False):
42+ # Based on charm-helpers open_port function, as this is not provided by
43+ # the charm framework
44+ if ssl:
45+ port = 443
46+ else:
47+ port = 80
48+
49+ # Don't catch exceptions, just lett them pass on
50+ check_call(["open-port", f"{port}/tcp"])
51+
52+
53 all_config_options, default_values = config_options()
54diff --git a/tests/test_charm.py b/tests/test_charm.py
55index 6dc62f1..69f0ba6 100644
56--- a/tests/test_charm.py
57+++ b/tests/test_charm.py
58@@ -390,7 +390,8 @@ class TestStatus(CharmTest):
59 self.harness.charm._stored.password = "minions password"
60 self.harness.charm._stored.registered = True
61
62- self.harness.charm.evaluate_status()
63+ with patch("charm.open_port"):
64+ self.harness.charm.evaluate_status()
65
66 assert self.harness.charm.unit.status.message == ""
67
68@@ -616,7 +617,7 @@ class TestSanity(CharmTest):
69 self.harness.charm._stored.db_uri = "postgresql://user@host/database"
70
71 # Since valid values will be pushed on to the proxy, we need to stub that here
72- with patch("charm.configure_proxy"):
73+ with patch("charm.configure_proxy"), patch("charm.open_port"):
74 # This should not result in a change, as none is not possible to be passed on
75 self.harness.update_config({"username": None})
76
77@@ -630,7 +631,7 @@ class TestSanity(CharmTest):
78 self.harness.charm._stored.db_uri = "postgresql://user@host/database"
79
80 # Since valid values will be pushed on to the proxy, we need to stub that here
81- with patch("charm.configure_proxy"):
82+ with patch("charm.configure_proxy"), patch("charm.open_port"):
83 self.harness.update_config({"password": None})
84
85 assert self.harness.charm._stored.password == "Some value"
86@@ -644,7 +645,7 @@ class TestSanity(CharmTest):
87 self.harness.charm._stored.db_uri = "postgresql://user@host/database"
88
89 # Since valid values will be pushed on to the proxy, we need to stub that here
90- with patch("charm.configure_proxy"):
91+ with patch("charm.configure_proxy"), patch("charm.open_port"):
92 self.harness.update_config({"domain": "http://my.super.domain"})
93
94 assert self.harness.charm._stored.domain == "Some value"
95diff --git a/tests/test_helpers.py b/tests/test_helpers.py
96index 1e61b15..a62308c 100644
97--- a/tests/test_helpers.py
98+++ b/tests/test_helpers.py
99@@ -1,5 +1,7 @@
100 from unittest import mock
101
102+import pytest
103+
104 import helpers
105
106
107@@ -54,3 +56,11 @@ def test_config_options():
108
109 for default_value_key in default_values.keys():
110 assert default_value_key in [key for key in all_options.keys()]
111+
112+
113+@pytest.mark.parametrize("ssl,port", [(True, 443), (False, 80)])
114+def test_open_port(ssl, port):
115+ with mock.patch("helpers.check_call") as mocked_call:
116+ helpers.open_port(ssl)
117+
118+ mocked_call.assert_called_once_with(["open-port", f"{port}/tcp"])

Subscribers

People subscribed via source and target branches

to all changes: