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:
parent
73464ff119
commit
1093b5eeb2
3 changed files with 24 additions and 26 deletions
|
|
@ -21,18 +21,17 @@ class Chroot:
|
||||||
print(f"Making missing chroot directory: {d}")
|
print(f"Making missing chroot directory: {d}")
|
||||||
os.makedirs(self.root + d)
|
os.makedirs(self.root + d)
|
||||||
|
|
||||||
subprocess.check_call(["/usr/bin/mount",
|
subprocess.run(["/usr/bin/mount", "-t", "proc", "-o", "nosuid,noexec,nodev",
|
||||||
"-t", "proc",
|
"proc", f"{self.root}/proc"],
|
||||||
"-o", "nosuid,noexec,nodev",
|
check=True)
|
||||||
"proc", f"{self.root}/proc"])
|
|
||||||
subprocess.check_call(["/usr/bin/mount",
|
subprocess.run(["/usr/bin/mount", "-t", "devtmpfs", "-o", "mode=0755,noexec,nosuid,strictatime",
|
||||||
"-t", "devtmpfs",
|
"devtmpfs", f"{self.root}/dev"],
|
||||||
"-o", "mode=0755,noexec,nosuid,strictatime",
|
check=True)
|
||||||
"devtmpfs", f"{self.root}/dev"])
|
|
||||||
subprocess.check_call(["/usr/bin/mount",
|
subprocess.run(["/usr/bin/mount", "-t", "sysfs", "-o", "nosuid,noexec,nodev",
|
||||||
"-t", "sysfs",
|
"sysfs", f"{self.root}/sys"],
|
||||||
"-o", "nosuid,noexec,nodev",
|
check=True)
|
||||||
"sysfs", f"{self.root}/sys"])
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,9 @@ def test_dracut_with_initoverlayfs(mocked_run, tmp_path, stage_module, with_init
|
||||||
|
|
||||||
stage_module.main(str(tmp_path), options)
|
stage_module.main(str(tmp_path), options)
|
||||||
|
|
||||||
# subprocess.run() gets called 4 times:
|
# We expect 7 calls to run(): 3 mount + chroot + 3 umount
|
||||||
# - once for the 'dracut' or 'initoverlayfs-install' call
|
assert len(mocked_run.call_args_list) == 7
|
||||||
# - three times for the 'umount' calls to unmount /proc, /dev, and /sys
|
args, kwargs = mocked_run.call_args_list[3] # chroot is the 4th call
|
||||||
assert len(mocked_run.call_args_list) == 4
|
|
||||||
args, kwargs = mocked_run.call_args_list[0]
|
|
||||||
assert kwargs.get("check") is True
|
assert kwargs.get("check") is True
|
||||||
run_argv = args[0]
|
run_argv = args[0]
|
||||||
assert run_argv[0] == "/usr/sbin/chroot"
|
assert run_argv[0] == "/usr/sbin/chroot"
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,19 @@ class RunReturn:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@patch("subprocess.check_call")
|
|
||||||
@patch("subprocess.run", return_value=RunReturn())
|
@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
|
with Chroot("") as chroot: # the path doesn't matter since nothing is actually running
|
||||||
chroot.run(["/bin/true"])
|
chroot.run(["/bin/true"])
|
||||||
|
|
||||||
assert mocked_run.call_count == 4 # the chroot.run() call + 3 umount calls
|
# We expect 7 calls to run(): 3 mount + chroot + 3 umount
|
||||||
assert mocked_run.call_args_list[0].args[0][0] == "/usr/sbin/chroot"
|
expected_cmds = ["/usr/bin/mount"] * 3 + ["/usr/sbin/chroot"] + ["/usr/bin/umount"] * 3
|
||||||
for call_run in mocked_run.call_args_list[1:]:
|
assert mocked_run.call_count == len(expected_cmds)
|
||||||
assert call_run.args[0][0] == "/usr/bin/umount"
|
|
||||||
|
|
||||||
assert mocked_check_call.call_count == 3 # 3 mount calls
|
cmds = []
|
||||||
for check_run in mocked_check_call.call_args_list:
|
for call in mocked_run.call_args_list:
|
||||||
assert check_run.args[0][0] == "/usr/bin/mount"
|
argv = call.args[0]
|
||||||
|
cmds.append(argv[0])
|
||||||
|
|
||||||
|
assert cmds == expected_cmds
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue