From e64d8cfde60d376724327dddd17f343d1db0388d Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 24 Feb 2023 09:41:14 -0400 Subject: [PATCH] Fix unit tests for NG-UI charm Change-Id: If5b98446bb589a3346bcaf1d260a3ad2c5affd3b Signed-off-by: Daniel Arndt --- installers/charm/osm-ng-ui/src/charm.py | 10 +++++----- .../charm/osm-ng-ui/tests/unit/test_charm.py | 17 +++++++++-------- installers/charm/osm-ng-ui/tox.ini | 4 +--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/installers/charm/osm-ng-ui/src/charm.py b/installers/charm/osm-ng-ui/src/charm.py index 235461fe..ca517b31 100755 --- a/installers/charm/osm-ng-ui/src/charm.py +++ b/installers/charm/osm-ng-ui/src/charm.py @@ -43,7 +43,7 @@ from lightkube.models.core_v1 import ServicePort from ops.charm import CharmBase from ops.framework import StoredState from ops.main import main -from ops.model import ActiveStatus, Container +from ops.model import ActiveStatus, BlockedStatus, Container SERVICE_PORT = 80 @@ -113,8 +113,8 @@ class OsmNgUiCharm(CharmBase): logger.debug(e.message) self.unit.status = e.status - def _on_required_relation_broken(self, _) -> None: - """Handler for the kafka-broken event.""" + def _on_nbi_relation_broken(self, _) -> None: + """Handler for the nbi relation broken event.""" # Check Pebble has started in the container try: check_container_ready(self.container) @@ -124,7 +124,7 @@ class OsmNgUiCharm(CharmBase): except CharmError: pass finally: - self._on_update_status() + self.unit.status = BlockedStatus("need nbi relation") # --------------------------------------------------------------------------- # Validation and configuration and more @@ -142,7 +142,7 @@ class OsmNgUiCharm(CharmBase): self.on.update_status: self._on_update_status, # Relation events self.on["nbi"].relation_changed: self._on_config_changed, - self.on["nbi"].relation_broken: self._on_required_relation_broken, + self.on["nbi"].relation_broken: self._on_nbi_relation_broken, } for event, handler in event_handler_mapping.items(): self.framework.observe(event, handler) diff --git a/installers/charm/osm-ng-ui/tests/unit/test_charm.py b/installers/charm/osm-ng-ui/tests/unit/test_charm.py index 006da99e..f4d45711 100644 --- a/installers/charm/osm-ng-ui/tests/unit/test_charm.py +++ b/installers/charm/osm-ng-ui/tests/unit/test_charm.py @@ -57,9 +57,9 @@ def harness(mocker: MockerFixture): mocker.patch("charm.KubernetesServicePatch", lambda x, y: None) harness = Harness(OsmNgUiCharm) harness.begin() - harness.charm.unit.get_container("ng-ui").push( - "/etc/nginx/sites-available/default", sites_default, make_dirs=True - ) + container = harness.charm.unit.get_container("ng-ui") + harness.set_can_connect(container, True) + container.push("/etc/nginx/sites-available/default", sites_default, make_dirs=True) yield harness harness.cleanup() @@ -71,23 +71,24 @@ def test_missing_relations(harness: Harness): def test_ready(harness: Harness): - _add_relation(harness) + _add_nbi_relation(harness) assert harness.charm.unit.status == ActiveStatus() def test_container_stops_after_relation_broken(harness: Harness): harness.charm.on[container_name].pebble_ready.emit(container_name) container = harness.charm.unit.get_container(container_name) - relation_id = _add_relation(harness) + relation_id = _add_nbi_relation(harness) check_service_active(container, service_name) harness.remove_relation(relation_id) with pytest.raises(CharmError): check_service_active(container, service_name) + assert type(harness.charm.unit.status) == BlockedStatus + assert harness.charm.unit.status.message == "need nbi relation" -def _add_relation(harness: Harness): - # Add nbi relation +def _add_nbi_relation(harness: Harness): relation_id = harness.add_relation("nbi", "nbi") harness.add_relation_unit(relation_id, "nbi/0") - harness.update_relation_data(relation_id, "nbi", {"host": "nbi", "port": 9999}) + harness.update_relation_data(relation_id, "nbi", {"host": "nbi", "port": "9999"}) return relation_id diff --git a/installers/charm/osm-ng-ui/tox.ini b/installers/charm/osm-ng-ui/tox.ini index 94974def..5287f5c0 100644 --- a/installers/charm/osm-ng-ui/tox.ini +++ b/installers/charm/osm-ng-ui/tox.ini @@ -26,7 +26,6 @@ envlist = lint, unit, integration [vars] src_path = {toxinidir}/src tst_path = {toxinidir}/tests -lib_path = {toxinidir}/lib/charms/osm_ng_ui all_path = {[vars]src_path} {[vars]tst_path} [testenv] @@ -62,7 +61,6 @@ deps = codespell commands = # uncomment the following line if this charm owns a lib - codespell {[vars]lib_path} codespell {toxinidir} --skip {toxinidir}/.git --skip {toxinidir}/.tox \ --skip {toxinidir}/build --skip {toxinidir}/lib --skip {toxinidir}/venv \ --skip {toxinidir}/.mypy_cache --skip {toxinidir}/icon.svg @@ -79,7 +77,7 @@ deps = coverage[toml] -r{toxinidir}/requirements.txt commands = - coverage run --source={[vars]src_path},{[vars]lib_path} \ + coverage run --source={[vars]src_path} \ -m pytest {[vars]tst_path}/unit -v --tb native -s {posargs} coverage report coverage xml -- 2.25.1