Skip to content

Commit

Permalink
Fix charm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Oct 23, 2023
1 parent 994913f commit d956ca8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
29 changes: 17 additions & 12 deletions frontend/charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"""Test Observer frontend charm."""

import logging
import sys
from typing import Tuple

import ops
Expand Down Expand Up @@ -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"]
Expand Down
24 changes: 4 additions & 20 deletions frontend/charm/tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d956ca8

Please sign in to comment.