|
15 | 15 | from psutil import Process |
16 | 16 |
|
17 | 17 | from tests.testlib.site import Site |
| 18 | +from tests.testlib.utils import run |
18 | 19 | from tests.testlib.web_session import CMKWebSession |
19 | 20 |
|
20 | 21 | from cmk.utils.paths import mkbackup_lock_dir |
@@ -74,25 +75,27 @@ def backup_path_fixture(site: Site) -> Iterator[str]: |
74 | 75 | {"exists": False}, |
75 | 76 | ], |
76 | 77 | ) |
77 | | -def backup_lock_dir_fixture(site: Site, request: pytest.FixtureRequest) -> None: |
| 78 | +def backup_lock_dir_fixture(site: Site, request: pytest.FixtureRequest) -> Iterator[None]: |
78 | 79 | # This fixture should prepare two possible scenarios: |
79 | 80 | # 1) The folder for the backup locks does already exist *and* has the correct permissions |
80 | 81 | # 2) The folder does not yet exist. |
81 | 82 | # --> In both scenarios mkbackup must not fail |
82 | 83 |
|
83 | | - # In the second case the "omd" command executed as root ensures that the directory is created. |
84 | | - # This functionality has been added to the "omd" command, because it is the only command which |
85 | | - # can reliably create the directory when started as root. |
86 | | - if not request.param["exists"]: |
87 | | - subprocess.check_call(["sudo", "rm", "-r", str(mkbackup_lock_dir)]) |
88 | | - assert not mkbackup_lock_dir.exists() |
89 | | - |
| 84 | + def _initialize_lock_dir() -> None: |
90 | 85 | # This omd call triggers the creation of the lock dir with the correct permissions. In |
91 | 86 | # production there is always at least one command executed before being able to execute |
92 | 87 | # the backup code. So we can assume it has been executed before. |
93 | | - site.omd("status") |
| 88 | + run(["omd", "status", site.id], sudo=True) |
| 89 | + assert mkbackup_lock_dir.exists() |
94 | 90 |
|
95 | | - assert mkbackup_lock_dir.exists() |
| 91 | + if request.param["exists"]: |
| 92 | + _initialize_lock_dir() |
| 93 | + yield |
| 94 | + else: |
| 95 | + run(["rm", "-r", str(mkbackup_lock_dir)], sudo=True) |
| 96 | + assert not mkbackup_lock_dir.exists(), f"Expected '{mkbackup_lock_dir}' to be deleted!" |
| 97 | + yield |
| 98 | + _initialize_lock_dir() |
96 | 99 |
|
97 | 100 |
|
98 | 101 | @pytest.fixture(name="test_cfg", scope="function") |
@@ -250,7 +253,7 @@ def test_mkbackup_help(site: Site) -> None: |
250 | 253 | stdout, stderr = p.communicate() |
251 | 254 | assert stderr == "ERROR: Missing operation mode\n" |
252 | 255 | assert stdout.startswith("Usage:") |
253 | | - assert p.wait() == 3 |
| 256 | + assert p.wait() == 3 # noqa: PLR2004 |
254 | 257 |
|
255 | 258 |
|
256 | 259 | @pytest.mark.usefixtures("test_cfg") |
@@ -293,7 +296,7 @@ def test_mkbackup_list_backups_invalid_target(site: Site) -> None: |
293 | 296 | ) |
294 | 297 | stdout, stderr = p.communicate() |
295 | 298 | assert stderr.startswith("This backup target does not exist") |
296 | | - assert p.wait() == 3 |
| 299 | + assert p.wait() == 3 # noqa: PLR2004 |
297 | 300 | assert stdout == "" |
298 | 301 |
|
299 | 302 |
|
|
0 commit comments