Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Fix failing tests after change from ops lib (#18)
Browse files Browse the repository at this point in the history
The tests were depending on private methods from ops,
which could change at any time.
In the latest release of ops (2.16.0 at time of writing),
`ops.main._get_charm_dir()` was removed,
and the signature of `ops.main._emit_charm_event()` changed.

Update the charm test code to stop depending on these private methods,
replacing with public stable methods,
fixing the tests.
  • Loading branch information
samuelallan72 authored Sep 3, 2024
1 parent e44fa5a commit d05506f
Showing 1 changed file with 3 additions and 23 deletions.
26 changes: 3 additions & 23 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Charm unit tests."""
import os
import unittest
from pathlib import Path

import mock

Expand All @@ -12,7 +11,6 @@

from charm import KubernetesServiceChecksCharm # noqa:I100

import ops.main
from ops.testing import Harness

TEST_KUBE_CONTOL_RELATION_DATA = {
Expand Down Expand Up @@ -68,7 +66,7 @@ def setUp(self):
"""Prepare tests."""
self.harness = Harness(KubernetesServiceChecksCharm)
# Mock config_get to return default config
with open(ops.main._get_charm_dir() / Path("config.yaml"), "r") as config_file:
with open("config.yaml", "r") as config_file:
config = yaml.safe_load(config_file)
charm_config = {}

Expand Down Expand Up @@ -176,7 +174,8 @@ def test_nrpe_external_master_relation_departed(self, mock_relation_data):
mock_relation_data.return_value.__getitem__.return_value = {}
self.harness.begin()
self.harness.charm.check_charm_status = mock.MagicMock()
self.emit("nrpe_external_master_relation_departed")
relation = self.harness.model.get_relation("nrpe-external-master", 1)
self.harness.charm.on["nrpe-external-master"].relation_departed.emit(relation)
self.harness.charm.check_charm_status.assert_called_once()

self.assertFalse(self.harness.charm.state.nrpe_configured)
Expand Down Expand Up @@ -239,25 +238,6 @@ def test_check_charm_status_configured(self):
self.harness.charm.helper.configure.assert_called_once()
self.assertTrue(self.harness.charm.state.configured)

def emit(self, event):
"""Emit the named hook on the charm."""
self.harness.charm.framework.reemit()

if "_relation_" in event:
relation_name = event.split("_relation")[0].replace("_", "-")
with mock.patch.dict(
"os.environ",
{
"JUJU_RELATION": relation_name,
"JUJU_RELATION_ID": "1",
"JUJU_REMOTE_APP": "mock",
"JUJU_REMOTE_UNIT": "mock/0",
},
):
ops.main._emit_charm_event(self.harness.charm, event)
else:
ops.main._emit_charm_event(self.harness.charm, event)


if __name__ == "__main__":
unittest.main()

0 comments on commit d05506f

Please sign in to comment.