Skip to content

Commit a92f321

Browse files
Fix unit test mocks
1 parent 6db28d2 commit a92f321

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

tests/unit/conftest.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Copyright 2023 Canonical Ltd.
22
# See LICENSE file for licensing details.
3-
3+
import pathlib
4+
import platform
45

56
import ops
67
import pytest
8+
import tomli
9+
710
from charms.tempo_coordinator_k8s.v0.charm_tracing import charm_tracing_disabled
811

912
import snap
@@ -29,6 +32,28 @@ def disable_tenacity_retry(monkeypatch):
2932
monkeypatch.setattr(f"tenacity.{retry_class}.__call__", lambda *args, **kwargs: False)
3033

3134

35+
class _MockRefresh:
36+
in_progress = False
37+
next_unit_allowed_to_refresh = True
38+
workload_allowed_to_start = True
39+
app_status_higher_priority = None
40+
unit_status_higher_priority = None
41+
42+
def __init__(self, _, /):
43+
pass
44+
45+
def update_snap_revision(self):
46+
pass
47+
48+
@property
49+
def pinned_snap_revision(self):
50+
with pathlib.Path("refresh_versions.toml").open("rb") as file:
51+
return tomli.load(file)["snap"]["revisions"][platform.machine()]
52+
53+
def unit_status_lower_priority(self, *, workload_is_running=True):
54+
return None
55+
56+
3257
@pytest.fixture(autouse=True)
3358
def patch(monkeypatch):
3459
monkeypatch.setattr(
@@ -41,9 +66,8 @@ def patch(monkeypatch):
4166
"mysql_shell.Shell.get_mysql_router_user_for_unit", lambda *args, **kwargs: None
4267
)
4368
monkeypatch.setattr("mysql_shell.Shell.is_router_in_cluster_set", lambda *args, **kwargs: True)
44-
monkeypatch.setattr("upgrade.Upgrade.in_progress", False)
45-
monkeypatch.setattr("upgrade.Upgrade.versions_set", True)
46-
monkeypatch.setattr("upgrade.Upgrade.is_compatible", True)
69+
monkeypatch.setattr("charm_refresh.Machines", _MockRefresh)
70+
monkeypatch.setattr("relations.database_requires.RelationEndpoint.does_relation_exist", lambda *args, **kwargs: True)
4771

4872

4973
# flake8: noqa: C901
@@ -102,6 +126,7 @@ def restart(self, services: list[str] = []):
102126
"snap.Snap._run_command",
103127
lambda *args, **kwargs: "null", # Use "null" for `json.loads()`
104128
)
129+
monkeypatch.setattr("snap.Snap.install", lambda *args, **kwargs: None)
105130
monkeypatch.setattr("snap._Path.read_text", lambda *args, **kwargs: "")
106131
monkeypatch.setattr("snap._Path.write_text", lambda *args, **kwargs: None)
107132
monkeypatch.setattr("snap._Path.unlink", lambda *args, **kwargs: None)

tests/unit/scenario_/database_relations/test_database_relations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def output_states(*, relations: list[scenario.Relation]) -> typing.Iterable[scen
2323
"""
2424
context = scenario.Context(charm.MachineSubordinateRouterCharm)
2525
input_state = scenario.State(
26-
relations=[*relations, scenario.PeerRelation(endpoint="upgrade-version-a")],
26+
relations=[*relations, scenario.PeerRelation(endpoint="refresh-v-three")],
2727
leader=True,
2828
)
2929
events = []

tests/unit/scenario_/database_relations/test_database_relations_breaking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def output_state(
1717
) -> scenario.State:
1818
context = scenario.Context(charm.MachineSubordinateRouterCharm)
1919
input_state = scenario.State(
20-
relations=[*relations, scenario.PeerRelation(endpoint="upgrade-version-a")],
20+
relations=[*relations, scenario.PeerRelation(endpoint="refresh-v-three")],
2121
secrets=secrets,
2222
leader=True,
2323
)

tests/unit/scenario_/test_start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_start_sets_status_if_no_relations(leader):
1313
context = scenario.Context(charm.MachineSubordinateRouterCharm)
1414
input_state = scenario.State(
1515
leader=leader,
16-
relations=[scenario.PeerRelation(endpoint="upgrade-version-a")],
16+
relations=[scenario.PeerRelation(endpoint="refresh-v-three")],
1717
)
1818
output_state = context.run("start", input_state)
1919
if leader:

0 commit comments

Comments
 (0)