diff --git a/src/charm.py b/src/charm.py index d257de91..474510df 100755 --- a/src/charm.py +++ b/src/charm.py @@ -737,11 +737,11 @@ def _get_gnb_subnet_config(self) -> Optional[str]: def _get_external_upf_hostname_config(self) -> Optional[str]: return self.model.config.get("external-upf-hostname") - def _upf_load_balancer_service_hostname(self) -> str: + def _upf_load_balancer_service_hostname(self) -> Optional[str]: """Returns the hostname of UPF's LoadBalancer service. Returns: - str: Hostname of UPF's LoadBalancer service + str/None: Hostname of UPF's LoadBalancer service if available else None """ client = Client() service = client.get( @@ -749,13 +749,13 @@ def _upf_load_balancer_service_hostname(self) -> str: ) try: return service.status.loadBalancer.ingress[0].hostname # type: ignore[attr-defined] - except AttributeError: + except (AttributeError, TypeError): logger.error( "Service '%s-external' does not have a hostname:\n%s", self.model.app.name, service, ) - return "" + return None @property def _upf_hostname(self) -> str: diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index e80381f2..a6f471b2 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -544,6 +544,26 @@ def test_given_external_upf_hostname_config_not_set_and_external_upf_service_hos upf_n4_port=TEST_PFCP_PORT, ) + @patch("lightkube.core.client.GenericSyncClient", new=Mock) + @patch("lightkube.core.client.Client.get") + @patch("charms.sdcore_upf.v0.fiveg_n4.N4Provides.publish_upf_n4_information") + @patch("charm.PFCP_PORT", TEST_PFCP_PORT) + def test_given_external_upf_hostname_config_not_set_and_metallb_not_available_and_fiveg_n4_relation_created_when_fiveg_n4_request_then_upf_hostname_and_n4_port_is_published( # noqa: E501 + self, patched_publish_upf_n4_information, patched_lightkube_client_get + ): + service = Mock(status=Mock(loadBalancer=Mock(ingress=None))) + patched_lightkube_client_get.return_value = service + + n4_relation_id = self.harness.add_relation("fiveg_n4", "n4_requirer_app") + self.harness.add_relation_unit(n4_relation_id, "n4_requirer_app/0") + + patched_publish_upf_n4_information.assert_called_once_with( + relation_id=n4_relation_id, + upf_hostname=f"{self.harness.charm.app.name}-external.{self.namespace}" + ".svc.cluster.local", + upf_n4_port=TEST_PFCP_PORT, + ) + @patch("charms.sdcore_upf.v0.fiveg_n4.N4Provides.publish_upf_n4_information") @patch(f"{MULTUS_LIBRARY_PATH}.KubernetesMultusCharmLib.is_ready") @patch(f"{HUGEPAGES_LIBRARY_PATH}.KubernetesHugePagesPatchCharmLib.is_patched")