Skip to content

Commit

Permalink
Remove workaround for old ops version. (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer authored Nov 29, 2024
1 parent 0953ac0 commit 6ad2b42
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 48 deletions.
22 changes: 0 additions & 22 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/test_charm_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
20 changes: 4 additions & 16 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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")

Expand All @@ -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")

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"))

Expand All @@ -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"))

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 0 additions & 4 deletions tests/unit/test_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
UNITTEST_DIR,
k8s_resource_multipatch,
patch_cos_tool_path,
patch_network_get,
prom_multipatch,
)
from ops import framework
Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand All @@ -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")
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from helpers import (
k8s_resource_multipatch,
patch_network_get,
prom_multipatch,
)
from ops.testing import Harness
Expand All @@ -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()

Expand All @@ -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,
Expand Down

0 comments on commit 6ad2b42

Please sign in to comment.