util/chroot: use subprocess.run() for all commands

For consistency, use subprocess.run() with check=True for the calls that
were previously using subprocess.check_call().

Update the affected tests to match.
This commit is contained in:
Achilleas Koutsou 2024-08-27 12:55:07 +02:00 committed by Brian C. Lane
parent 73464ff119
commit 1093b5eeb2
3 changed files with 24 additions and 26 deletions

View file

@ -21,18 +21,17 @@ class Chroot:
print(f"Making missing chroot directory: {d}")
os.makedirs(self.root + d)
subprocess.check_call(["/usr/bin/mount",
"-t", "proc",
"-o", "nosuid,noexec,nodev",
"proc", f"{self.root}/proc"])
subprocess.check_call(["/usr/bin/mount",
"-t", "devtmpfs",
"-o", "mode=0755,noexec,nosuid,strictatime",
"devtmpfs", f"{self.root}/dev"])
subprocess.check_call(["/usr/bin/mount",
"-t", "sysfs",
"-o", "nosuid,noexec,nodev",
"sysfs", f"{self.root}/sys"])
subprocess.run(["/usr/bin/mount", "-t", "proc", "-o", "nosuid,noexec,nodev",
"proc", f"{self.root}/proc"],
check=True)
subprocess.run(["/usr/bin/mount", "-t", "devtmpfs", "-o", "mode=0755,noexec,nosuid,strictatime",
"devtmpfs", f"{self.root}/dev"],
check=True)
subprocess.run(["/usr/bin/mount", "-t", "sysfs", "-o", "nosuid,noexec,nodev",
"sysfs", f"{self.root}/sys"],
check=True)
return self

View file

@ -22,11 +22,9 @@ def test_dracut_with_initoverlayfs(mocked_run, tmp_path, stage_module, with_init
stage_module.main(str(tmp_path), options)
# subprocess.run() gets called 4 times:
# - once for the 'dracut' or 'initoverlayfs-install' call
# - three times for the 'umount' calls to unmount /proc, /dev, and /sys
assert len(mocked_run.call_args_list) == 4
args, kwargs = mocked_run.call_args_list[0]
# We expect 7 calls to run(): 3 mount + chroot + 3 umount
assert len(mocked_run.call_args_list) == 7
args, kwargs = mocked_run.call_args_list[3] # chroot is the 4th call
assert kwargs.get("check") is True
run_argv = args[0]
assert run_argv[0] == "/usr/sbin/chroot"

View file

@ -17,18 +17,19 @@ class RunReturn:
return 0
@patch("subprocess.check_call")
@patch("subprocess.run", return_value=RunReturn())
def test_chroot_context(mocked_run, mocked_check_call):
def test_chroot_context(mocked_run):
with Chroot("") as chroot: # the path doesn't matter since nothing is actually running
chroot.run(["/bin/true"])
assert mocked_run.call_count == 4 # the chroot.run() call + 3 umount calls
assert mocked_run.call_args_list[0].args[0][0] == "/usr/sbin/chroot"
for call_run in mocked_run.call_args_list[1:]:
assert call_run.args[0][0] == "/usr/bin/umount"
# We expect 7 calls to run(): 3 mount + chroot + 3 umount
expected_cmds = ["/usr/bin/mount"] * 3 + ["/usr/sbin/chroot"] + ["/usr/bin/umount"] * 3
assert mocked_run.call_count == len(expected_cmds)
assert mocked_check_call.call_count == 3 # 3 mount calls
for check_run in mocked_check_call.call_args_list:
assert check_run.args[0][0] == "/usr/bin/mount"
cmds = []
for call in mocked_run.call_args_list:
argv = call.args[0]
cmds.append(argv[0])
assert cmds == expected_cmds