3
3
4
4
import pathlib
5
5
import platform
6
- import shutil
7
6
8
- import filelock
9
7
import ops
10
8
import pytest
11
9
import tomli
12
- import tomli_w
13
10
from charms .tempo_coordinator_k8s .v0 .charm_tracing import charm_tracing_disabled
14
11
15
12
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
-
53
13
@pytest .fixture (autouse = True )
54
14
def disable_tenacity_retry (monkeypatch ):
55
15
for retry_class in (
@@ -94,6 +54,20 @@ def unit_status_lower_priority(self, *, workload_is_running=True):
94
54
95
55
@pytest .fixture (autouse = True )
96
56
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
+
97
71
monkeypatch .setattr (
98
72
"charm.MachineSubordinateRouterCharm.wait_until_mysql_router_ready" ,
99
73
lambda * args , ** kwargs : None ,
@@ -105,6 +79,7 @@ def patch(monkeypatch):
105
79
)
106
80
monkeypatch .setattr ("mysql_shell.Shell.is_router_in_cluster_set" , lambda * args , ** kwargs : True )
107
81
monkeypatch .setattr ("charm_refresh.Machines" , _MockRefresh )
82
+ monkeypatch .setattr ("charm_refresh._main.tomli.load" , _tomli_load )
108
83
monkeypatch .setattr (
109
84
"relations.database_requires.RelationEndpoint.does_relation_exist" ,
110
85
lambda * args , ** kwargs : True ,
0 commit comments