Skip to content

Commit

Permalink
Add proxy juju env support (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrobinaubertin authored Dec 6, 2023
1 parent 07295f8 commit 316a818
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ def _create_discourse_environment_settings(self) -> typing.Dict[str, str]:
# self.config return an Any type
pod_config.update(THROTTLE_LEVELS.get(self.config["throttle_level"])) # type: ignore

# Update environment with proxy settings
pod_config["HTTP_PROXY"] = pod_config["http_proxy"] = (
os.environ.get("JUJU_CHARM_HTTP_PROXY") or ""
)
pod_config["HTTPS_PROXY"] = pod_config["https_proxy"] = (
os.environ.get("JUJU_CHARM_HTTPS_PROXY") or ""
)
pod_config["NO_PROXY"] = pod_config["no_proxy"] = (
os.environ.get("JUJU_CHARM_NO_PROXY") or ""
)

return pod_config

def _create_layer_config(self) -> ops.pebble.LayerDict:
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,32 @@ def test_relate_database_at_the_end():
harness.container_pebble_ready("discourse")
helpers.add_postgres_relation(harness)
assert harness.model.unit.status == ActiveStatus()


def test_http_proxy_env(monkeypatch):
"""
arrange: given a deployed discourse charm with all the required relations
act: when a juju http_proxy variable is changed
assert: the appropriate configuration values should be present in the created env
"""
harness = helpers.start_harness()

created_env = harness._charm._create_discourse_environment_settings()
assert created_env["HTTP_PROXY"] == ""
assert created_env["http_proxy"] == ""
assert created_env["HTTPS_PROXY"] == ""
assert created_env["https_proxy"] == ""
assert created_env["NO_PROXY"] == ""
assert created_env["no_proxy"] == ""

monkeypatch.setenv("JUJU_CHARM_HTTP_PROXY", "http://proxy.test")
monkeypatch.setenv("JUJU_CHARM_HTTPS_PROXY", "http://httpsproxy.test")
monkeypatch.setenv("JUJU_CHARM_NO_PROXY", "noproxy.test")
created_env = harness._charm._create_discourse_environment_settings()

assert created_env["HTTP_PROXY"] == "http://proxy.test"
assert created_env["http_proxy"] == "http://proxy.test"
assert created_env["HTTPS_PROXY"] == "http://httpsproxy.test"
assert created_env["https_proxy"] == "http://httpsproxy.test"
assert created_env["NO_PROXY"] == "noproxy.test"
assert created_env["no_proxy"] == "noproxy.test"

0 comments on commit 316a818

Please sign in to comment.