Skip to content

Commit a03211f

Browse files
Fix unit tests [alternative]
1 parent ebce78a commit a03211f

File tree

3 files changed

+59
-75
lines changed

3 files changed

+59
-75
lines changed

poetry.lock

Lines changed: 43 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package-mode = false
66
requires-poetry = ">=2.0.0"
77

88
[tool.poetry.dependencies]
9-
python = "^3.9.0" # ^3.9.0 required by tomli-w
9+
python = "^3.8.6" # ^3.8.6 required by juju
1010
# breaking change in ops 2.10.0: https://github.com/canonical/operator/pull/1091#issuecomment-1888644075
1111
ops = "^2.9.0, <2.10"
1212
tenacity = "^9.0.0"
@@ -42,13 +42,10 @@ ruff = "^0.4.5"
4242
codespell = "^2.3.0"
4343

4444
[tool.poetry.group.unit.dependencies]
45-
filelock ="^3.18.0"
4645
pytest = "^8.2.2"
4746
pytest-xdist = "^3.6.1"
4847
pytest-cov = "^5.0.0"
4948
ops-scenario = "^6.0.3, <6.0.4" # 6.0.4 requires ops >= 2.12
50-
tomli = "^2.2.1"
51-
tomli-w = "^1.2.0"
5249

5350
[tool.poetry.group.integration.dependencies]
5451
pytest = "^8.2.2"

tests/unit/conftest.py

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,13 @@
33

44
import pathlib
55
import platform
6-
import shutil
76

8-
import filelock
97
import ops
108
import pytest
119
import tomli
12-
import tomli_w
1310
from charms.tempo_coordinator_k8s.v0.charm_tracing import charm_tracing_disabled
1411

1512

16-
def _mock_charm_version(origin_path: pathlib.Path, backup_path: pathlib.Path) -> None:
17-
"""Add charm version to refresh_versions.toml."""
18-
shutil.copy(origin_path, backup_path)
19-
20-
with origin_path.open("rb") as file:
21-
versions = tomli.load(file)
22-
23-
versions["charm"] = "8.0/0.0.0"
24-
with origin_path.open("wb") as file:
25-
tomli_w.dump(versions, file)
26-
27-
28-
@pytest.fixture(scope="session", autouse=True)
29-
def mock_charm_version(tmp_path_factory, worker_id):
30-
origin_path = pathlib.Path("refresh_versions.toml")
31-
backup_path = pathlib.Path("refresh_versions.toml.backup")
32-
33-
# When not running with pytest-xdist
34-
if worker_id == "master":
35-
_mock_charm_version(origin_path, backup_path)
36-
yield
37-
origin_path.unlink()
38-
shutil.move(backup_path, origin_path)
39-
40-
# When running with pytest-xdist
41-
else:
42-
root_tmp_dir = tmp_path_factory.getbasetemp().parent
43-
lock_file = root_tmp_dir / "refresh_versions.lock"
44-
45-
# The first worker to acquire the lock mocks the file
46-
with filelock.FileLock(lock_file):
47-
_mock_charm_version(origin_path, backup_path)
48-
yield
49-
origin_path.unlink()
50-
shutil.move(backup_path, origin_path)
51-
52-
5313
@pytest.fixture(autouse=True)
5414
def disable_tenacity_retry(monkeypatch):
5515
for retry_class in (
@@ -94,6 +54,20 @@ def unit_status_lower_priority(self, *, workload_is_running=True):
9454

9555
@pytest.fixture(autouse=True)
9656
def patch(monkeypatch):
57+
def _tomli_load(*args, **kwargs) -> dict:
58+
return {
59+
"charm_major": 1,
60+
"workload": "8.0.0",
61+
"charm": "v8.0/1.0.0",
62+
"snap": {
63+
"name": "charmed-mysql",
64+
"revisions": {
65+
"x86_64": "1",
66+
"aarch64": "1",
67+
},
68+
},
69+
}
70+
9771
monkeypatch.setattr(
9872
"charm.MachineSubordinateRouterCharm.wait_until_mysql_router_ready",
9973
lambda *args, **kwargs: None,
@@ -105,6 +79,7 @@ def patch(monkeypatch):
10579
)
10680
monkeypatch.setattr("mysql_shell.Shell.is_router_in_cluster_set", lambda *args, **kwargs: True)
10781
monkeypatch.setattr("charm_refresh.Machines", _MockRefresh)
82+
monkeypatch.setattr("charm_refresh._main.tomli.load", _tomli_load)
10883
monkeypatch.setattr(
10984
"relations.database_requires.RelationEndpoint.does_relation_exist",
11085
lambda *args, **kwargs: True,

0 commit comments

Comments
 (0)