diff --git a/tests/unit/helpers.py b/tests/unit/helpers.py index fd0259f7..6ee6037e 100644 --- a/tests/unit/helpers.py +++ b/tests/unit/helpers.py @@ -17,28 +17,6 @@ COS_TOOL_URL = "https://github.com/canonical/cos-tool/releases/latest/download/cos-tool-amd64" -def patch_network_get(private_address="10.1.157.116") -> Callable: - def network_get(*args, **kwargs) -> dict: - """Patch for the not-yet-implemented testing backend needed for `bind_address`. - - This patch decorator can be used for cases such as: - self.model.get_binding(event.relation).network.bind_address - """ - return { - "bind-addresses": [ - { - "mac-address": "", - "interface-name": "", - "addresses": [{"hostname": "", "value": private_address, "cidr": ""}], - } - ], - "egress-subnets": ["10.152.183.65/32"], - "ingress-addresses": ["10.152.183.65"], - } - - return patch("ops.testing._TestingModelBackend.network_get", network_get) - - def patch_cos_tool_path(func) -> Callable: """Patch cos tool path. diff --git a/tests/unit/test_charm_status.py b/tests/unit/test_charm_status.py index 98ca1a63..86846aa7 100644 --- a/tests/unit/test_charm_status.py +++ b/tests/unit/test_charm_status.py @@ -8,7 +8,7 @@ from unittest.mock import Mock, patch import ops -from helpers import k8s_resource_multipatch, patch_network_get, prom_multipatch +from helpers import k8s_resource_multipatch, prom_multipatch from ops.model import ActiveStatus, BlockedStatus from ops.pebble import Change, ChangeError, ChangeID from ops.testing import Harness @@ -46,7 +46,6 @@ def setUp(self) -> None: self.mock_capacity.return_value = "1Gi" self.addCleanup(patcher.stop) - @patch_network_get() @k8s_resource_multipatch @patch("lightkube.core.client.GenericSyncClient") def test_unit_is_active_if_deployed_without_relations_or_config(self, *unused): @@ -69,7 +68,6 @@ def test_unit_is_active_if_deployed_without_relations_or_config(self, *unused): self.harness.container_pebble_ready("prometheus") self.assertEqual(self.harness.get_workload_version(), "0.1.0") - @patch_network_get() @k8s_resource_multipatch @patch("lightkube.core.client.GenericSyncClient") def test_unit_is_blocked_if_reload_configuration_fails(self, *unused): diff --git a/tests/unit/test_endpoint_provider.py b/tests/unit/test_endpoint_provider.py index 946ddb20..12651b69 100644 --- a/tests/unit/test_endpoint_provider.py +++ b/tests/unit/test_endpoint_provider.py @@ -21,7 +21,7 @@ from cosl.rules import AlertRules from deepdiff import DeepDiff from fs.tempfs import TempFS -from helpers import PROJECT_DIR, UNITTEST_DIR, patch_network_get +from helpers import PROJECT_DIR, UNITTEST_DIR from ops.charm import CharmBase from ops.framework import StoredState from ops.testing import Harness @@ -190,7 +190,6 @@ def test_provider_default_scrape_relation_wrong_role(self): ) self.assertRaises(RelationRoleMismatchError, harness.begin) - @patch_network_get() def test_provider_sets_scrape_metadata(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -206,8 +205,8 @@ def test_provider_sets_scrape_metadata(self): self.assertIn("model_uuid", scrape_metadata) self.assertIn("application", scrape_metadata) - @patch_network_get() def test_provider_unit_sets_address_on_pebble_ready(self): + self.harness.add_network("10.1.157.116") rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.container_pebble_ready("prometheus-tester") @@ -219,8 +218,8 @@ def test_provider_unit_sets_address_on_pebble_ready(self): self.assertIn("prometheus_scrape_unit_address", data) self.assertEqual(data["prometheus_scrape_unit_address"], "10.1.157.116") - @patch_network_get() def test_provider_unit_sets_address_on_relation_joined(self): + self.harness.add_network("10.1.157.116") rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -233,7 +232,6 @@ def test_provider_unit_sets_address_on_relation_joined(self): self.assertEqual(data["prometheus_scrape_unit_address"], "10.1.157.116") self.assertIn("prometheus_scrape_unit_name", data) - @patch_network_get() def test_provider_sets_external_url(self): harness = Harness(EndpointProviderCharmExternalUrl, meta=PROVIDER_META) harness.set_model_name("MyUUID") @@ -251,7 +249,6 @@ def test_provider_sets_external_url(self): self.assertEqual(data["prometheus_scrape_unit_address"], "9.12.20.18") self.assertIn("prometheus_scrape_unit_name", data) - @patch_network_get() def test_provider_can_set_lookaside_data(self): harness = Harness(EndpointProviderCharmWithLookaside, meta=PROVIDER_META) harness.set_model_name("MyUUID") @@ -270,7 +267,7 @@ def test_provider_can_set_lookaside_data(self): self.assertIn("dynamic-job", names) @patch("socket.getfqdn", new=lambda *args: "some.host") - @patch_network_get(private_address=None) + @patch("ops.Network.bind_address", new="not-an-ip") def test_provider_unit_sets_fqdn_if_not_address_on_relation_joined(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -284,7 +281,6 @@ def test_provider_unit_sets_fqdn_if_not_address_on_relation_joined(self): self.assertEqual(data["prometheus_scrape_unit_address"], "some.host") self.assertIn("prometheus_scrape_unit_name", data) - @patch_network_get() def test_provider_supports_multiple_jobs(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -301,7 +297,6 @@ def test_provider_supports_multiple_jobs(self): job_names = [job["job_name"] for job in JOBS] self.assertListEqual(names, job_names) - @patch_network_get() def test_provider_sanitizes_jobs(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -317,7 +312,6 @@ def test_provider_sanitizes_jobs(self): keys = set(job.keys()) self.assertTrue(keys.issubset(ALLOWED_KEYS)) - @patch_network_get() def test_each_alert_rule_is_topology_labeled(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -354,7 +348,6 @@ def test_each_alert_rule_is_topology_labeled(self): self.assertIn("juju_unit", rule["labels"]) self.assertIn("juju_unit=", rule["expr"]) - @patch_network_get() def test_each_alert_expression_is_topology_labeled(self): rel_id = self.harness.add_relation(RELATION_NAME, "provider") self.harness.add_relation_unit(rel_id, "provider/0") @@ -408,7 +401,6 @@ def setup(self, **kwargs): self.harness.set_leader(True) self.harness.begin() - @patch_network_get() def test_a_bad_alert_expression_logs_an_error(self): self.setup(alert_rules_path=str(UNITTEST_DIR / "bad_alert_expressions")) @@ -420,7 +412,6 @@ def test_a_bad_alert_expression_logs_an_error(self): self.assertEqual(len(messages), 1) self.assertIn("Invalid rules file: missing_expr.rule", messages[0]) - @patch_network_get() def test_a_bad_alert_rules_logs_an_error(self): self.setup(alert_rules_path=str(UNITTEST_DIR / "bad_alert_rules")) @@ -750,7 +741,6 @@ def setup(self, **kwargs): self.harness.set_leader(True) self.harness.begin() - @patch_network_get() def test_unit_label_is_retained_if_hard_coded(self): self.setup(alert_rules_path=str(UNITTEST_DIR / "alert_rules_with_unit_topology")) rel_id = self.harness.add_relation("metrics-endpoint", "provider") @@ -779,7 +769,6 @@ def setUp(self): self.harness.set_leader(False) self.harness.begin_with_initial_hooks() - @patch_network_get() def test_alert_rules(self): """Verify alert rules are added when leader is elected after the relation is created.""" rel_id = self.harness.add_relation(RELATION_NAME, "provider") @@ -819,7 +808,6 @@ def setUp(self): self.harness.set_leader(True) self.harness.begin_with_initial_hooks() - @patch_network_get() def test_alert_rules(self): """Verify alert rules are added when leader is elected after the relation is created.""" rel_id = self.harness.add_relation(RELATION_NAME, "provider") diff --git a/tests/unit/test_remote_write.py b/tests/unit/test_remote_write.py index e21d4244..aeb62554 100644 --- a/tests/unit/test_remote_write.py +++ b/tests/unit/test_remote_write.py @@ -18,7 +18,6 @@ UNITTEST_DIR, k8s_resource_multipatch, patch_cos_tool_path, - patch_network_get, prom_multipatch, ) from ops import framework @@ -231,7 +230,6 @@ def setUp(self, *unused): @patch("lightkube.core.client.GenericSyncClient") @patch.object(Prometheus, "reload_configuration", new=lambda _: True) @patch("socket.getfqdn", new=lambda *args: "fqdn") - @patch_network_get() def test_port_is_set(self, *unused): self.harness.begin_with_initial_hooks() self.harness.container_pebble_ready("prometheus") @@ -248,7 +246,6 @@ def test_port_is_set(self, *unused): @k8s_resource_multipatch @patch("lightkube.core.client.GenericSyncClient") @patch.object(Prometheus, "reload_configuration", new=lambda _: True) - @patch_network_get() def test_alert_rules(self, *unused): self.harness.begin_with_initial_hooks() @@ -269,7 +266,6 @@ def test_alert_rules(self, *unused): @k8s_resource_multipatch @patch("lightkube.core.client.GenericSyncClient") @patch.object(Prometheus, "reload_configuration", new=lambda _: True) - @patch_network_get() def test_address_is_updated_on_upgrade(self, *unused): rel_id = self.harness.add_relation(RELATION_NAME, "consumer") self.harness.add_relation_unit(rel_id, "consumer/0") diff --git a/tests/unit/test_tls.py b/tests/unit/test_tls.py index 22fe5650..dc9fdc2c 100644 --- a/tests/unit/test_tls.py +++ b/tests/unit/test_tls.py @@ -6,7 +6,6 @@ from helpers import ( k8s_resource_multipatch, - patch_network_get, prom_multipatch, ) from ops.testing import Harness @@ -32,7 +31,6 @@ def setUp(self, *unused): @patch("lightkube.core.client.GenericSyncClient") @patch.object(Prometheus, "reload_configuration", new=lambda _: True) @patch("socket.getfqdn", new=lambda *args: "fqdn") - @patch_network_get() def test_tls_relation_without_certs(self, *unused): self.harness.begin_with_initial_hooks() @@ -48,7 +46,6 @@ def test_tls_relation_without_certs(self, *unused): @patch("lightkube.core.client.GenericSyncClient") @patch.object(Prometheus, "reload_configuration", new=lambda _: True) @patch("socket.getfqdn", new=lambda *args: "fqdn") - @patch_network_get() @patch.multiple( "charm.PrometheusCharm", _is_tls_ready=lambda *_: True,