Skip to content

Commit

Permalink
Fix tests to work with ops > 2.16 (#352)
Browse files Browse the repository at this point in the history
* Fix most utests
* Chore: Remove fake networks from failing unit tests

---------

Signed-off-by: Michael Thamm <[email protected]>
Co-authored-by: sed-i <[email protected]>
  • Loading branch information
MichaelThamm and sed-i authored Oct 11, 2024
1 parent a40184b commit d122274
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 31 deletions.
4 changes: 1 addition & 3 deletions tests/scenario/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import patch, MagicMock
from unittest.mock import patch

import pytest
from scenario import Context
Expand All @@ -23,8 +23,6 @@ def ctx():
is_ready=tautology,
),
patch.object(GrafanaCharm, "grafana_version", "0.1.0"),
patch("ops.testing._TestingModelBackend.network_get"),
patch("ops.testing._TestingPebbleClient.exec", MagicMock()),
)
for p in patches:
p.__enter__()
Expand Down
30 changes: 5 additions & 25 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def cli_arg(plan, cli_opt):
class BaseTestCharm(unittest.TestCase):
def setUp(self, *unused):
self.harness = Harness(GrafanaCharm)
self.harness.handle_exec("grafana", [], result=0)
self.addCleanup(self.harness.cleanup)

for p in [
Expand All @@ -125,8 +126,6 @@ def setUp(self, *unused):
patch("socket.gethostbyname", new=lambda *args: "1.2.3.4"),
k8s_resource_multipatch,
patch.object(GrafanaCharm, "grafana_version", "0.1.0"),
patch("ops.testing._TestingModelBackend.network_get"),
patch("ops.testing._TestingPebbleClient.exec", MagicMock()),
]:
p.start()
self.addCleanup(p.stop)
Expand Down Expand Up @@ -379,13 +378,13 @@ def test_config_is_updated_with_authentication_config(self):
class TestCharmReplication(unittest.TestCase):
def setUp(self, *unused):
self.harness = Harness(GrafanaCharm)
self.harness.handle_exec("grafana", [], result=0)
self.addCleanup(self.harness.cleanup)

for p in [
patch("lightkube.core.client.GenericSyncClient"),
k8s_resource_multipatch,
patch.object(GrafanaCharm, "grafana_version", "0.1.0"),
patch("ops.testing._TestingPebbleClient.exec", MagicMock()),
]:
p.start()
self.addCleanup(p.stop)
Expand All @@ -399,17 +398,7 @@ def setUp(self, *unused):
).hexdigest()

@patch("socket.getfqdn", lambda: "1.2.3.4")
@patch("ops.testing._TestingModelBackend.network_get")
def test_primary_sets_correct_peer_data(self, mock_unit_ip):
fake_network = {
"bind-addresses": [
{
"interface-name": "eth0",
"addresses": [{"hostname": "grafana-0", "value": "1.2.3.4"}],
}
]
}
mock_unit_ip.return_value = fake_network
def test_primary_sets_correct_peer_data(self):

self.harness.begin_with_initial_hooks()
self.harness.container_pebble_ready("grafana")
Expand All @@ -419,23 +408,14 @@ def test_primary_sets_correct_peer_data(self, mock_unit_ip):
rel = self.harness.model.get_relation("grafana")
self.harness.add_relation_unit(rel.id, "grafana-k8s/1")

self.harness.add_network("1.2.3.4", endpoint="grafana")
unit_ip = str(self.harness.charm.model.get_binding("grafana").network.bind_address)
replica_address = self.harness.charm.get_peer_data("replica_primary")

self.assertEqual(unit_ip, replica_address)

@patch("socket.getfqdn", lambda: "2.3.4.5")
@patch("ops.testing._TestingModelBackend.network_get")
def test_replicas_get_correct_environment_variables(self, mock_unit_ip):
fake_network = {
"bind-addresses": [
{
"interface-name": "eth0",
"addresses": [{"hostname": "grafana-0", "value": "2.3.4.5"}],
}
]
}
mock_unit_ip.return_value = fake_network
def test_replicas_get_correct_environment_variables(self):

self.harness.begin_with_initial_hooks()
self.harness.container_pebble_ready("grafana")
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/test_external_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import unittest
from typing import Dict
from unittest.mock import MagicMock, patch
from unittest.mock import patch

import ops
from charms.traefik_k8s.v0.traefik_route import TraefikRouteRequirer
Expand Down Expand Up @@ -33,6 +33,7 @@ class TestExternalUrl(unittest.TestCase):

def setUp(self, *unused):
self.harness = Harness(GrafanaCharm)
self.harness.handle_exec("grafana", [], result=0)
self.addCleanup(self.harness.cleanup)

model_name = "testmodel"
Expand All @@ -45,8 +46,6 @@ def setUp(self, *unused):
patch("socket.gethostbyname", new=lambda *args: "1.2.3.4"),
k8s_resource_multipatch,
patch.object(GrafanaCharm, "grafana_version", "0.0.0"),
patch("ops.testing._TestingModelBackend.network_get"),
patch("ops.testing._TestingPebbleClient.exec", MagicMock()),
]:
p.start()
self.addCleanup(p.stop)
Expand Down

0 comments on commit d122274

Please sign in to comment.