diff --git a/osbuild/util/chroot.py b/osbuild/util/chroot.py index d092a3b4..682988e5 100644 --- a/osbuild/util/chroot.py +++ b/osbuild/util/chroot.py @@ -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 diff --git a/stages/test/test_dracut.py b/stages/test/test_dracut.py index 6aed287a..c8c1d7ed 100644 --- a/stages/test/test_dracut.py +++ b/stages/test/test_dracut.py @@ -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" diff --git a/test/mod/test_util_chroot.py b/test/mod/test_util_chroot.py index c02fb06a..07e91802 100644 --- a/test/mod/test_util_chroot.py +++ b/test/mod/test_util_chroot.py @@ -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