test/systemd_unit_create: mount unit type creation

This commit is contained in:
Achilleas Koutsou 2024-05-13 17:22:06 +02:00
parent f085714a7e
commit e92b683e11

View file

@ -248,3 +248,80 @@ def test_systemd_unit_create(tmp_path, stage_module, unit_type, unit_path, expec
RequiredBy=multi-user.target
""")
@pytest.mark.parametrize("unit_type,unit_path,expected_prefix", [
("system", "usr", "usr/lib/systemd/system"),
("system", "etc", "etc/systemd/system"),
("global", "usr", "usr/lib/systemd/user"),
("global", "etc", "etc/systemd/user"),
])
def test_systemd_unit_create_mount(tmp_path, stage_module, unit_type, unit_path, expected_prefix):
options = {
"filename": "data-test.mount",
"unit-type": unit_type,
"unit-path": unit_path,
"config": {
"Unit": {
"DefaultDependencies": True,
"ConditionPathExists": [
"/data"
],
"ConditionPathIsDirectory": [
"/data"
],
"Before": [
"local-fs.target",
],
},
"Mount": {
"What": "/dev/sda2",
"Where": "/data/test",
"Type": "xfs",
"Options": "rw,noatime"
}
}
}
# create units in the tree for systemd-analyze verify
systemd_dir = "usr/lib/systemd/system/"
# if a unit file is empty, it gets detected as "masked" and fails verification
testutil.make_fake_tree(tmp_path, {
systemd_dir + "basic.target": "[Unit]\n",
systemd_dir + "sysinit.target": "[Unit]\n",
systemd_dir + "paths.target": "[Unit]\n",
systemd_dir + "sockets.target": "[Unit]\n",
"usr/bin/mkdir": "fake-mkdir",
"usr/bin/touch": "fake-touch",
})
for bin_name in ["mkdir", "touch"]:
os.chmod(tmp_path / "usr/bin" / bin_name, mode=0o755)
expected_unit_path = tmp_path / expected_prefix / "data-test.mount"
expected_unit_path.parent.mkdir(parents=True, exist_ok=True)
stage_module.main(tmp_path, options)
assert os.path.exists(expected_unit_path)
if unit_type == "system":
# When verifying user units, systemd expects runtime directories and more generally a booted system. Setting
# something up like that to verify it is more trouble than it's worth, especially since the system units are
# identical.
subprocess.run(["systemd-analyze", "verify",
"--man=no", # disable manpage checks
f"--root={tmp_path}", # verify commands and units in the root tree
expected_unit_path],
check=True)
assert expected_unit_path.read_text(encoding="utf-8") == textwrap.dedent("""\
[Unit]
DefaultDependencies=True
ConditionPathExists=/data
ConditionPathIsDirectory=/data
Before=local-fs.target
[Mount]
What=/dev/sda2
Where=/data/test
Type=xfs
Options=rw,noatime
""")