Skip to content

Commit e3a397a

Browse files
committed
integration-tests: Fix bug in test-design.
mkbackup_lock_dir` always existed, even if not desired. Referred to `changeid: I0c2b406` for original test-design. CMK-24644 Change-Id: If286d48c33a9d5a89454f37c39db7517f61b65a5
1 parent af28ad6 commit e3a397a

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

tests/integration/bin/test_mkbackup.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from psutil import Process
1616

1717
from tests.testlib.site import Site
18+
from tests.testlib.utils import run
1819
from tests.testlib.web_session import CMKWebSession
1920

2021
from cmk.utils.paths import mkbackup_lock_dir
@@ -74,25 +75,27 @@ def backup_path_fixture(site: Site) -> Iterator[str]:
7475
{"exists": False},
7576
],
7677
)
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]:
7879
# This fixture should prepare two possible scenarios:
7980
# 1) The folder for the backup locks does already exist *and* has the correct permissions
8081
# 2) The folder does not yet exist.
8182
# --> In both scenarios mkbackup must not fail
8283

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:
9085
# This omd call triggers the creation of the lock dir with the correct permissions. In
9186
# production there is always at least one command executed before being able to execute
9287
# 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()
9490

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()
9699

97100

98101
@pytest.fixture(name="test_cfg", scope="function")
@@ -250,7 +253,7 @@ def test_mkbackup_help(site: Site) -> None:
250253
stdout, stderr = p.communicate()
251254
assert stderr == "ERROR: Missing operation mode\n"
252255
assert stdout.startswith("Usage:")
253-
assert p.wait() == 3
256+
assert p.wait() == 3 # noqa: PLR2004
254257

255258

256259
@pytest.mark.usefixtures("test_cfg")
@@ -293,7 +296,7 @@ def test_mkbackup_list_backups_invalid_target(site: Site) -> None:
293296
)
294297
stdout, stderr = p.communicate()
295298
assert stderr.startswith("This backup target does not exist")
296-
assert p.wait() == 3
299+
assert p.wait() == 3 # noqa: PLR2004
297300
assert stdout == ""
298301

299302

0 commit comments

Comments
 (0)