From d956ca81bab5b2054dcc64d9e133a7af9c1acf0f Mon Sep 17 00:00:00 2001 From: Omar Selo Date: Mon, 23 Oct 2023 07:23:59 +0000 Subject: [PATCH] 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)