Fix unit tests for NG-UI charm 99/12999/5
authorDaniel Arndt <daniel.arndt@canonical.com>
Fri, 24 Feb 2023 13:41:14 +0000 (09:41 -0400)
committerDaniel Arndt <daniel.arndt@canonical.com>
Fri, 10 Mar 2023 18:36:23 +0000 (14:36 -0400)
Change-Id: If5b98446bb589a3346bcaf1d260a3ad2c5affd3b
Signed-off-by: Daniel Arndt <daniel.arndt@canonical.com>
installers/charm/osm-ng-ui/src/charm.py
installers/charm/osm-ng-ui/tests/unit/test_charm.py
installers/charm/osm-ng-ui/tox.ini

index 235461f..ca517b3 100755 (executable)
@@ -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)
index 006da99..f4d4571 100644 (file)
@@ -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
index 94974de..5287f5c 100644 (file)
@@ -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