From 994913f963b43279f775bbfe3762275ab96bb11c Mon Sep 17 00:00:00 2001 From: Omar Selo Date: Tue, 17 Oct 2023 13:23:26 +0000 Subject: [PATCH 1/2] Fix frontend not connecting to api --- frontend/charm/src/charm.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/frontend/charm/src/charm.py b/frontend/charm/src/charm.py index 93b6fbee..2912c3de 100755 --- a/frontend/charm/src/charm.py +++ b/frontend/charm/src/charm.py @@ -31,7 +31,7 @@ def __init__(self, *args): self.pebble_service_name = "test-observer-frontend" self.container = self.unit.get_container("frontend") - self.framework.observe(self.on.frontend_pebble_ready, self._on_frontend_pebble_ready) + self.framework.observe(self.on.frontend_pebble_ready, self._update_layer_and_restart) self.framework.observe(self.on.config_changed, self._on_config_changed) self.framework.observe( self.on.test_observer_rest_api_relation_joined, @@ -56,12 +56,6 @@ def _setup_ingress(self): service_port=int(self.config["port"]), ) - def _on_frontend_pebble_ready(self, event: ops.PebbleReadyEvent): - container = event.workload - container.add_layer("frontend", self._pebble_layer, combine=True) - container.replan() - self.unit.status = ops.ActiveStatus() - def _on_config_changed(self, event): is_valid, reason = self._config_is_valid(self.config) @@ -168,7 +162,7 @@ def _update_layer_and_restart(self, event): make_dirs=True, ) self.container.add_layer(self.pebble_service_name, self._pebble_layer, combine=True) - self.container.restart(self.pebble_service_name) + self.container.replan() self.unit.status = ActiveStatus() else: self.unit.status = WaitingStatus("Waiting for Pebble for API to set available state") From d956ca81bab5b2054dcc64d9e133a7af9c1acf0f Mon Sep 17 00:00:00 2001 From: Omar Selo Date: Mon, 23 Oct 2023 07:23:59 +0000 Subject: [PATCH 2/2] Fix charm tests --- frontend/charm/src/charm.py | 29 +++++++++++++++---------- frontend/charm/tests/unit/test_charm.py | 24 ++++---------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/frontend/charm/src/charm.py b/frontend/charm/src/charm.py index 2912c3de..98880e6f 100755 --- a/frontend/charm/src/charm.py +++ b/frontend/charm/src/charm.py @@ -7,7 +7,6 @@ """Test Observer frontend charm.""" import logging -import sys from typing import Tuple import ops @@ -156,29 +155,35 @@ def _update_layer_and_restart(self, event): self.unit.status = MaintenanceStatus(f"Updating {self.pebble_service_name} layer") if self.container.can_connect(): - self.container.push( - "/etc/nginx/sites-available/test-observer-frontend", - self.nginx_config(base_uri=self._api_url), - make_dirs=True, - ) - self.container.add_layer(self.pebble_service_name, self._pebble_layer, combine=True) - self.container.replan() - self.unit.status = ActiveStatus() + api_url = self._api_url + if api_url: + self.container.push( + "/etc/nginx/sites-available/test-observer-frontend", + self.nginx_config(base_uri=api_url), + make_dirs=True, + ) + self.container.add_layer( + self.pebble_service_name, self._pebble_layer, combine=True + ) + self.container.replan() + self.unit.status = ActiveStatus() + else: + self._handle_no_api_relation() else: self.unit.status = WaitingStatus("Waiting for Pebble for API to set available state") @property - def _api_url(self): + def _api_url(self) -> str | None: api_relation = self.model.get_relation("test-observer-rest-api") if api_relation is None: self._handle_no_api_relation() - sys.exit() + return relation_data = api_relation.data[api_relation.app] if not relation_data: self.unit.status = WaitingStatus("Waiting for test observer api relation data") - sys.exit() + return hostname = relation_data["hostname"] port = relation_data["port"] diff --git a/frontend/charm/tests/unit/test_charm.py b/frontend/charm/tests/unit/test_charm.py index 18b32953..6a3330c8 100644 --- a/frontend/charm/tests/unit/test_charm.py +++ b/frontend/charm/tests/unit/test_charm.py @@ -16,28 +16,12 @@ def setUp(self): self.addCleanup(self.harness.cleanup) self.harness.begin() - def test_pebble_ready(self): - expected_plan = { - "services": { - "test-observer-frontend": { - "override": "replace", - "summary": "nginx", - "command": "nginx -g 'daemon off;'", - "startup": "enabled", - } - }, - } - + def test_pebble_ready_no_relation(self): self.harness.container_pebble_ready("frontend") - updated_plan = self.harness.get_container_pebble_plan("frontend").to_dict() - - self.assertEqual(expected_plan, updated_plan) - - service = self.harness.model.unit.get_container("frontend").get_service( - "test-observer-frontend" + self.assertEqual( + self.harness.model.unit.status, + ops.MaintenanceStatus("test-observer-rest-api relation not connected."), ) - self.assertTrue(service.is_running()) - self.assertEqual(self.harness.model.unit.status, ops.ActiveStatus()) def test_relating(self): harness = ops.testing.Harness(TestObserverFrontendCharm)