From e4346e7af2f1067f280b860e214fb950eb61dad6 Mon Sep 17 00:00:00 2001 From: "Amanda H. L. de Andrade Katz" Date: Mon, 7 Oct 2024 10:18:18 -0300 Subject: [PATCH] chore(refactor): move NGINX to Synapse rock (#541) * chore(refactor): move NGINX to Synapse rock * fix: remove synapse-nginx-image param * fix: change rock version just in case --- docs/explanation/charm-architecture.md | 22 ++++---- docs/how-to/contribute.md | 16 +++--- metadata.yaml | 5 -- nginx_rock/rockcraft.yaml | 41 --------------- src-docs/charm.py.md | 22 ++++---- src/charm.py | 28 +--------- src/synapse/__init__.py | 1 - src/synapse/workload.py | 1 - .../etc/abuse_report_location.conf | 0 .../etc/main_location.conf | 0 {nginx_rock => synapse_rock}/etc/nginx.conf | 0 .../etc/worker_location.conf | 0 synapse_rock/rockcraft.yaml | 27 +++++++++- tests/conftest.py | 4 -- tests/integration/conftest.py | 14 +---- tests/integration/test_charm.py | 2 - tests/unit/conftest.py | 10 +--- tests/unit/test_charm.py | 52 ++----------------- tests/unit/test_charm_scaling.py | 11 ++-- 19 files changed, 63 insertions(+), 193 deletions(-) delete mode 100644 nginx_rock/rockcraft.yaml rename {nginx_rock => synapse_rock}/etc/abuse_report_location.conf (100%) rename {nginx_rock => synapse_rock}/etc/main_location.conf (100%) rename {nginx_rock => synapse_rock}/etc/nginx.conf (100%) rename {nginx_rock => synapse_rock}/etc/worker_location.conf (100%) diff --git a/docs/explanation/charm-architecture.md b/docs/explanation/charm-architecture.md index 3763f900..5a297d79 100644 --- a/docs/explanation/charm-architecture.md +++ b/docs/explanation/charm-architecture.md @@ -16,7 +16,7 @@ Pebble `services` are configured through [layers](https://github.com/canonical/p and the following containers represent each one a layer forming the effective Pebble configuration, or `plan`: -1. An [NGINX](https://www.nginx.com/) container, which can be used to +1. An [NGINX](https://www.nginx.com/) Pebble layer, which can be used to efficiently serve static resources, as well as be the incoming point for all web traffic to the pod. 2. The [Synapse](https://github.com/matrix-org/synapse) container itself, which @@ -40,31 +40,27 @@ processes startup as explained above. ## OCI images We use [Rockcraft](https://canonical-rockcraft.readthedocs-hosted.com/en/latest/) -to build OCI Images for Synapse and NGINX. -The images are defined in [NGINX rock](https://github.com/canonical/synapse-operator/tree/main/nginx_rock/) -and [Synapse rock](https://github.com/canonical/synapse-operator/tree/main/synapse_rock). -They are published to [Charmhub](https://charmhub.io/), the official repository +to build OCI Image for Synapse. +The image is defined in [Synapse rock](https://github.com/canonical/synapse-operator/tree/main/synapse_rock) and is published to [Charmhub](https://charmhub.io/), the official repository of charms. This is done by publishing a resource to Charmhub as described in the [Juju SDK How-to guides](https://juju.is/docs/sdk/publishing). -## Containers +## Container -Configuration files for the containers can be found in the respective -directories that define the rocks, see the section above. +Configuration files for the container can be found in the respective +directory that define the rock, see the section above. ### NGINX -This container is the entry point for all web traffic to the pod (on port -`8080`). Serves static files directly and forwards non-static requests to -the Synapse container (on port `8008`). +NGINX is configured as a Pebble Layer and is the entry point for all web traffic +to the pod (on port `8080`). Serves static files directly and forwards +non-static requests to the Synapse container (on port `8008`). NGINX provides static content cache, reverse proxy, and load balancer among multiple application servers, as well as other features. It can be used in front of Synapse server to significantly reduce server and network load. -The workload that this container is running is defined in the [NGINX rock](https://github.com/canonical/synapse-operator/tree/main/nginx_rock/). - ### Synapse Synapse is a Python application run by the `start.py` script. diff --git a/docs/how-to/contribute.md b/docs/how-to/contribute.md index 82e5d21a..f9cf8839 100644 --- a/docs/how-to/contribute.md +++ b/docs/how-to/contribute.md @@ -41,12 +41,11 @@ source .tox/unit/bin/activate ### Testing -Note that the [Synapse](synapse_rock/rockcraft.yaml) and [Synapse NGINX](synapse_nginx_rock/rockcraft.yaml) -images need to be built and pushed to microk8s for the tests to run. They should -be tagged as `localhost:32000/synapse:latest` and -`localhost:32000/synapse-nginx:latest` so that Kubernetes knows how to pull them +Note that the [Synapse](synapse_rock/rockcraft.yaml) image need to be built and +pushed to microk8s for the tests to run. It should be tagged as +`localhost:32000/synapse:latest`so that Kubernetes knows how to pull them from the MicroK8s repository. Note that the MicroK8s registry needs to be -enabled using `microk8s enable registry`. More details regarding the OCI images +enabled using `microk8s enable registry`. More details regarding the OCI image below. The following commands can then be used to run the tests: * `tox`: Runs all of the basic checks (`lint`, `unit`, `static`, and `coverage-report`). @@ -73,7 +72,7 @@ Build the charm in this git repository using: charmcraft pack ``` For the integration tests (and also to deploy the charm locally), the synapse -and synapse-nginx images are required in the microk8s registry. To enable it: +image is required in the microk8s registry. To enable it: microk8s enable registry @@ -82,8 +81,6 @@ the registry: cd [project_dir]/synapse_rock && rockcraft pack skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:synapse_1.0_amd64.rock docker://localhost:32000/synapse:latest - cd [project_dir]/nginx_rock && rockcraft pack - skopeo --insecure-policy copy --dest-tls-verify=false oci-archive:synapse-nginx_1.0_amd64.rock docker://localhost:32000/synapse-nginx:latest ### Deploy @@ -94,8 +91,7 @@ juju add-model synapse-dev juju model-config logging-config="=INFO;unit=DEBUG" # Deploy the charm (assuming you're on amd64) juju deploy ./synapse_ubuntu-22.04-amd64.charm \ - --resource synapse-image=localhost:32000/synapse:latest \ - --resource synapse-nginx-image=localhost:32000/synapse-nginx:latest + --resource synapse-image=localhost:32000/synapse:latest ``` ### Configure `server_name` diff --git a/metadata.yaml b/metadata.yaml index 41e0d48d..0d7b5299 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -22,16 +22,11 @@ containers: mounts: - storage: data location: /data - synapse-nginx: - resource: synapse-nginx-image resources: synapse-image: type: oci-image description: OCI image for Synapse - synapse-nginx-image: - type: oci-image - description: OCI image for Synapse NGINX storage: data: diff --git a/nginx_rock/rockcraft.yaml b/nginx_rock/rockcraft.yaml deleted file mode 100644 index 0d36bf85..00000000 --- a/nginx_rock/rockcraft.yaml +++ /dev/null @@ -1,41 +0,0 @@ -# Copyright 2024 Canonical Ltd. -# See LICENSE file for licensing details. - -name: synapse-nginx -summary: synapse nginx rock -description: Nginx OCI image for the synapse charm -version: "1.0" -base: ubuntu@22.04 -build-base: ubuntu@22.04 -license: Apache-2.0 -platforms: - amd64: -parts: - add-user: - plugin: nil - overlay-script: | - chmod 755 $CRAFT_OVERLAY/etc - groupadd -R $CRAFT_OVERLAY --gid 2000 nginx - useradd -R $CRAFT_OVERLAY --system --gid 2000 --uid 2000 --no-create-home nginx - nginx-conf: - plugin: dump - source: etc - organize: - nginx.conf: etc/nginx/nginx.conf - worker_location.conf: etc/nginx/worker_location.conf - abuse_report_location.conf.template: etc/nginx/abuse_report_location.conf.template - abuse_report_location.conf: etc/nginx/abuse_report_location.conf - main_location.conf.template: etc/nginx/main_location.conf.template - main_location.conf: etc/nginx/main_location.conf - nginx: - stage-packages: - - logrotate - - nginx - - sed - plugin: nil - override-build: | - craftctl default - rm $CRAFT_PART_INSTALL/etc/nginx/nginx.conf - override-prime: | - craftctl default - mkdir run diff --git a/src-docs/charm.py.md b/src-docs/charm.py.md index 9d8c3195..cd4509a4 100644 --- a/src-docs/charm.py.md +++ b/src-docs/charm.py.md @@ -74,7 +74,7 @@ Unit that this execution is responsible for. --- - + ### function `build_charm_state` @@ -91,7 +91,7 @@ Build charm state. --- - + ### function `get_main_unit` @@ -108,7 +108,7 @@ Get main unit. --- - + ### function `get_main_unit_address` @@ -125,7 +125,7 @@ Get main unit address. If main unit is None, use unit name. --- - + ### function `get_signing_key` @@ -142,7 +142,7 @@ Get signing key from secret. --- - + ### function `get_unit_number` @@ -166,7 +166,7 @@ Get unit number from unit name. --- - + ### function `instance_map` @@ -183,7 +183,7 @@ Build instance_map config. --- - + ### function `is_main` @@ -201,7 +201,7 @@ Verify if this unit is the main. --- - + ### function `peer_units_total` @@ -218,7 +218,7 @@ Get peer units total. --- - + ### function `reconcile` @@ -238,7 +238,7 @@ This is the main entry for changes that require a restart. --- - + ### function `set_main_unit` @@ -256,7 +256,7 @@ Create/Renew an admin access token and put it in the peer relation. --- - + ### function `set_signing_key` diff --git a/src/charm.py b/src/charm.py index d1d8456a..5bbd80aa 100755 --- a/src/charm.py +++ b/src/charm.py @@ -93,9 +93,6 @@ def __init__(self, *args: typing.Any) -> None: self.on[synapse.SYNAPSE_PEER_RELATION_NAME].relation_changed, self._on_relation_changed ) self.framework.observe(self.on.synapse_pebble_ready, self._on_synapse_pebble_ready) - self.framework.observe( - self.on.synapse_nginx_pebble_ready, self._on_synapse_nginx_pebble_ready - ) self.framework.observe(self.on.register_user_action, self._on_register_user_action) self.framework.observe( self.on.promote_user_admin_action, self._on_promote_user_admin_action @@ -224,6 +221,7 @@ def reconcile(self, charm_state: CharmState) -> None: except (pebble.PebbleServiceError, FileNotFoundError) as exc: self.model.unit.status = ops.BlockedStatus(str(exc)) return + pebble.restart_nginx(container, self.get_main_unit_address()) self._set_unit_status() def _set_unit_status(self) -> None: @@ -250,10 +248,6 @@ def _set_unit_status(self) -> None: self.unit.status = ops.MaintenanceStatus("Waiting for Synapse") return # NGINX checks - container = self.unit.get_container(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - if not container.can_connect(): - self.unit.status = ops.MaintenanceStatus("Waiting for Synapse NGINX pebble") - return nginx_service = container.get_services(synapse.SYNAPSE_NGINX_SERVICE_NAME) nginx_not_active = [ service for service in nginx_service.values() if not service.is_running() @@ -475,26 +469,6 @@ def _on_relation_changed(self, _: ops.HookEvent, charm_state: CharmState) -> Non """ logger.debug("_on_relation_changed emitting reconcile") self.reconcile(charm_state) - # Reload NGINX configuration with new main address - nginx_container = self.unit.get_container(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - if not nginx_container.can_connect(): - logger.warning( - "Relation changed received but NGINX container is not available for reloading." - ) - return - pebble.restart_nginx(nginx_container, self.get_main_unit_address()) - - def _on_synapse_nginx_pebble_ready(self, _: ops.HookEvent) -> None: - """Handle synapse nginx pebble ready event.""" - container = self.unit.get_container(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - if not container.can_connect(): - logger.debug("synapse_nginx_pebble_ready failed to connect") - self.unit.status = ops.MaintenanceStatus("Waiting for Synapse NGINX pebble") - return - logger.debug("synapse_nginx_pebble_ready replanning nginx") - # Replan pebble layer - pebble.restart_nginx(container, self.get_main_unit_address()) - self._set_unit_status() def _on_register_user_action(self, event: ActionEvent) -> None: """Register user and report action result. diff --git a/src/synapse/__init__.py b/src/synapse/__init__.py index f363e41e..fa9c5726 100644 --- a/src/synapse/__init__.py +++ b/src/synapse/__init__.py @@ -51,7 +51,6 @@ SYNAPSE_EXPORTER_PORT, SYNAPSE_FEDERATION_SENDER_SERVICE_NAME, SYNAPSE_GROUP, - SYNAPSE_NGINX_CONTAINER_NAME, SYNAPSE_NGINX_PORT, SYNAPSE_NGINX_SERVICE_NAME, SYNAPSE_PEER_RELATION_NAME, diff --git a/src/synapse/workload.py b/src/synapse/workload.py index f0293cb6..a3e39846 100644 --- a/src/synapse/workload.py +++ b/src/synapse/workload.py @@ -38,7 +38,6 @@ SYNAPSE_DEFAULT_MEDIA_STORE_PATH = "/media_store" SYNAPSE_FEDERATION_SENDER_SERVICE_NAME = "synapse-federation-sender" SYNAPSE_GROUP = "synapse" -SYNAPSE_NGINX_CONTAINER_NAME = "synapse-nginx" SYNAPSE_NGINX_PORT = 8080 SYNAPSE_NGINX_SERVICE_NAME = "synapse-nginx" SYNAPSE_PEER_RELATION_NAME = "synapse-peers" diff --git a/nginx_rock/etc/abuse_report_location.conf b/synapse_rock/etc/abuse_report_location.conf similarity index 100% rename from nginx_rock/etc/abuse_report_location.conf rename to synapse_rock/etc/abuse_report_location.conf diff --git a/nginx_rock/etc/main_location.conf b/synapse_rock/etc/main_location.conf similarity index 100% rename from nginx_rock/etc/main_location.conf rename to synapse_rock/etc/main_location.conf diff --git a/nginx_rock/etc/nginx.conf b/synapse_rock/etc/nginx.conf similarity index 100% rename from nginx_rock/etc/nginx.conf rename to synapse_rock/etc/nginx.conf diff --git a/nginx_rock/etc/worker_location.conf b/synapse_rock/etc/worker_location.conf similarity index 100% rename from nginx_rock/etc/worker_location.conf rename to synapse_rock/etc/worker_location.conf diff --git a/synapse_rock/rockcraft.yaml b/synapse_rock/rockcraft.yaml index 08be1239..000e293f 100644 --- a/synapse_rock/rockcraft.yaml +++ b/synapse_rock/rockcraft.yaml @@ -4,7 +4,7 @@ name: synapse summary: Synapse rock description: Synapse OCI image for the Synapse charm -version: "1.0" +version: "2.0" base: ubuntu@22.04 # renovate: base: ubuntu:22.04@sha256:58b87898e82351c6cf9cf5b9f3c20257bb9e2dcf33af051e12ce532d7f94e3fe build-base: ubuntu@22.04 @@ -41,6 +41,31 @@ parts: overlay-script: | groupadd -R $CRAFT_OVERLAY --gid 991 synapse useradd -R $CRAFT_OVERLAY --system --gid 991 --uid 991 --home /srv/synapse -m synapse + chmod 755 $CRAFT_OVERLAY/etc + groupadd -R $CRAFT_OVERLAY --gid 2000 nginx + useradd -R $CRAFT_OVERLAY --system --gid 2000 --uid 2000 --no-create-home nginx + nginx-conf: + plugin: dump + source: etc + organize: + nginx.conf: etc/nginx/nginx.conf + worker_location.conf: etc/nginx/worker_location.conf + abuse_report_location.conf.template: etc/nginx/abuse_report_location.conf.template + abuse_report_location.conf: etc/nginx/abuse_report_location.conf + main_location.conf.template: etc/nginx/main_location.conf.template + main_location.conf: etc/nginx/main_location.conf + nginx: + stage-packages: + - logrotate + - nginx + - sed + plugin: nil + override-build: | + craftctl default + rm $CRAFT_PART_INSTALL/etc/nginx/nginx.conf + override-prime: | + craftctl default + mkdir run synapse: build-packages: - build-essential diff --git a/tests/conftest.py b/tests/conftest.py index 74449228..4b4b9545 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,6 @@ from pytest import Parser SYNAPSE_IMAGE_PARAM = "--synapse-image" -SYNAPSE_NGINX_IMAGE_PARAM = "--synapse-nginx-image" def pytest_addoption(parser: Parser) -> None: @@ -16,9 +15,6 @@ def pytest_addoption(parser: Parser) -> None: parser: Pytest parser. """ parser.addoption(SYNAPSE_IMAGE_PARAM, action="store", help="Synapse image to be deployed") - parser.addoption( - SYNAPSE_NGINX_IMAGE_PARAM, action="store", help="Synapse NGINX image to be deployed" - ) parser.addoption("--charm-file", action="store", help="Charm file to be deployed") parser.addoption( "--use-existing", diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index ab375aa4..06086c03 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -19,7 +19,7 @@ from pytest import Config from pytest_operator.plugin import OpsTest -from tests.conftest import SYNAPSE_IMAGE_PARAM, SYNAPSE_NGINX_IMAGE_PARAM +from tests.conftest import SYNAPSE_IMAGE_PARAM from tests.integration.helpers import get_access_token, register_user # caused by pytest fixtures, mark does not work in fixtures @@ -75,16 +75,6 @@ def synapse_image_fixture(pytestconfig: Config): return synapse_image -@pytest_asyncio.fixture(scope="module", name="synapse_nginx_image") -def synapse_nginx_image_fixture(pytestconfig: Config): - """Get value from parameter synapse-nginx-image.""" - synapse_nginx_image = pytestconfig.getoption(SYNAPSE_NGINX_IMAGE_PARAM) - use_existing = pytestconfig.getoption("--use-existing", default=False) - if not use_existing: - assert synapse_nginx_image, f"{SYNAPSE_NGINX_IMAGE_PARAM} must be set" - return synapse_nginx_image - - @pytest_asyncio.fixture(scope="module", name="synapse_app_name") def synapse_app_name_fixture() -> str: """Get Synapse application name.""" @@ -104,7 +94,6 @@ async def synapse_app_fixture( synapse_app_name: str, synapse_app_charmhub_name: str, synapse_image: str, - synapse_nginx_image: str, model: Model, server_name: str, synapse_charm: str, @@ -118,7 +107,6 @@ async def synapse_app_fixture( return model.applications[synapse_app_name] resources = { "synapse-image": synapse_image, - "synapse-nginx-image": synapse_nginx_image, } app = await model.deploy( f"./{synapse_charm}", diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index 3ab7a8d7..bbe4ae50 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -396,7 +396,6 @@ async def test_synapse_with_mjolnir_from_refresh_is_up( get_unit_ips: typing.Callable[[str], typing.Awaitable[tuple[str, ...]]], synapse_charm: str, synapse_image: str, - synapse_nginx_image: str, ): """ arrange: build and deploy the Synapse charm from charmhub and enable Mjolnir. @@ -417,7 +416,6 @@ async def test_synapse_with_mjolnir_from_refresh_is_up( resources = { "synapse-image": synapse_image, - "synapse-nginx-image": synapse_nginx_image, } await synapse_charmhub_app.refresh(path=f"./{synapse_charm}", resources=resources) async with ops_test.fast_forward(): diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 9ff1cf28..d1072061 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -127,9 +127,6 @@ def harness_fixture(request, monkeypatch) -> typing.Generator[Harness, None, Non synapse.SYNAPSE_CONTAINER_NAME ) harness.set_can_connect(synapse.SYNAPSE_CONTAINER_NAME, True) - harness.set_can_connect( - harness.model.unit.containers[synapse.SYNAPSE_NGINX_CONTAINER_NAME], True - ) synapse_container.make_dir("/data", make_parents=True) synapse_container.push(f"/data/{TEST_SERVER_NAME}.signing.key", "123") # unused-variable disabled to pass constants values to inner function @@ -178,16 +175,13 @@ def start_cmd_handler(argv: list[str]) -> synapse.ExecResult: executable="/usr/bin/python3", handler=lambda _: synapse.ExecResult(0, "", ""), ) - synapse_nginx_container: ops.Container = harness.model.unit.get_container( - synapse.SYNAPSE_NGINX_CONTAINER_NAME - ) harness.register_command_handler( # type: ignore # pylint: disable=no-member - container=synapse_nginx_container, + container=synapse_container, executable="cp", handler=lambda _: synapse.ExecResult(0, "", ""), ) harness.register_command_handler( # type: ignore # pylint: disable=no-member - container=synapse_nginx_container, + container=synapse_container, executable="sed", handler=lambda _: synapse.ExecResult(0, "", ""), ) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index f02d521c..fbe7dd77 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -123,9 +123,7 @@ def test_restart_nginx_container_down(harness: Harness) -> None: assert: Synapse charm should submit the correct status. """ harness.begin() - harness.set_can_connect( - harness.model.unit.containers[synapse.SYNAPSE_NGINX_CONTAINER_NAME], False - ) + harness.set_can_connect(harness.model.unit.containers[synapse.SYNAPSE_CONTAINER_NAME], False) harness.update_config({"report_stats": True}) assert isinstance(harness.model.unit.status, ops.MaintenanceStatus) assert "Waiting for" in str(harness.model.unit.status) @@ -345,7 +343,6 @@ def test_nginx_replan(harness: Harness, monkeypatch: pytest.MonkeyPatch) -> None monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) harness.container_pebble_ready(synapse.SYNAPSE_CONTAINER_NAME) - harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME) restart_nginx_mock.assert_called_once() @@ -360,10 +357,10 @@ def test_nginx_replan_failure(harness: Harness, monkeypatch: pytest.MonkeyPatch) restart_nginx_mock = MagicMock() monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) - container = harness.model.unit.containers[synapse.SYNAPSE_NGINX_CONTAINER_NAME] + container = harness.model.unit.containers[synapse.SYNAPSE_CONTAINER_NAME] harness.set_can_connect(container, False) # harness.container_pebble_ready cannot be used as it sets the set_can_connect to True - harness.charm.on[synapse.SYNAPSE_NGINX_CONTAINER_NAME].pebble_ready.emit(container) + harness.charm.on[synapse.SYNAPSE_CONTAINER_NAME].pebble_ready.emit(container) restart_nginx_mock.assert_not_called() assert isinstance(harness.model.unit.status, ops.MaintenanceStatus) @@ -378,52 +375,9 @@ def test_nginx_replan_sets_status_to_active(harness: Harness) -> None: harness.begin() harness.container_pebble_ready(synapse.SYNAPSE_CONTAINER_NAME) - harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - assert harness.model.unit.status == ops.ActiveStatus() -def test_nginx_replan_with_synapse_container_down( - harness: Harness, monkeypatch: pytest.MonkeyPatch -) -> None: - """ - arrange: start Synapse charm with Synapse container as down, and mock restart_nginx. - act: Fire that NGINX container is ready. - assert: Pebble Service replan NGINX is called but unit is in maintenance - waiting for Synapse pebble. - """ - harness.begin() - restart_nginx_mock = MagicMock() - monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) - - container = harness.model.unit.containers[synapse.SYNAPSE_CONTAINER_NAME] - harness.set_can_connect(container, False) - - harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - - restart_nginx_mock.assert_called_once() - assert harness.model.unit.status == ops.MaintenanceStatus("Waiting for Synapse pebble") - - -def test_nginx_replan_with_synapse_service_not_existing( - harness: Harness, monkeypatch: pytest.MonkeyPatch -) -> None: - """ - arrange: start Synapse charm with Synapse container but without synapse service, - and mock restart_nginx. - act: Fire that NGINX container is ready. - assert: Pebble Service replan NGINX is called but unit is in maintenance waiting for Synapse. - """ - harness.begin() - restart_nginx_mock = MagicMock() - monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) - - harness.container_pebble_ready(synapse.SYNAPSE_NGINX_CONTAINER_NAME) - - restart_nginx_mock.assert_called_once() - assert harness.model.unit.status == ops.MaintenanceStatus("Waiting for Synapse") - - def test_redis_relation_success(redis_configured: Harness, monkeypatch: pytest.MonkeyPatch): """ arrange: start the Synapse charm, set server_name, mock synapse.enable_redis. diff --git a/tests/unit/test_charm_scaling.py b/tests/unit/test_charm_scaling.py index b844b164..452655b5 100644 --- a/tests/unit/test_charm_scaling.py +++ b/tests/unit/test_charm_scaling.py @@ -250,16 +250,13 @@ def test_scaling_main_unit_changed_nginx_reconfigured( "synapse", app_data={"main_unit_id": "synapse/0"}, ) - harness.begin_with_initial_hooks() - nginx_container = harness.model.unit.containers[synapse.SYNAPSE_NGINX_CONTAINER_NAME] + restart_nginx_mock = MagicMock() + monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) + nginx_container = harness.model.unit.containers[synapse.SYNAPSE_CONTAINER_NAME] harness.set_can_connect(nginx_container, True) + harness.begin_with_initial_hooks() harness.add_relation("redis", "redis", unit_data={"hostname": "redis-host", "port": "1010"}) harness.set_leader(False) - # emit nginx ready - # assert was called with synapse/0 - restart_nginx_mock = MagicMock() - monkeypatch.setattr(pebble, "restart_nginx", restart_nginx_mock) - harness.charm.on.synapse_nginx_pebble_ready.emit(MagicMock()) restart_nginx_mock.assert_called_with(nginx_container, "synapse-0.synapse-endpoints") harness.update_relation_data(