debian-forge/test/mod/test_util_chroot.py
Michael Vogt a3e32f3823 util: drop absolute path from Chroot.run() calls
We currently use the absolute path of these binaries in the
helper. This has some advantages but given that we control the
inputs for PATH in general it seems unnecessary.

We are also slightly inconsistent about this in the codebase but
favor the non absolute path version. A quick count:
```
$ git grep '"chroot"'|wc -l
13
$ git grep '"/usr/sbin/chroot"'|grep -v test_|wc -l
8
```
for `mount` and `umount` it seems this is the only place that uses
the absolute path.

It's not an important change but it has the nice property that it
allows us to use e.g. `testutil.mock_command()` in our tests and
it would be nice to be consistent.
2024-10-24 10:06:46 +02:00

43 lines
1.4 KiB
Python

#
# Test for util/chroot.py
#
import os
from unittest.mock import call, patch
from osbuild.util.chroot import Chroot
class RunReturn:
"""
Class to be returned from mocked run() call so that the returncode is always 0.
"""
@property
def returncode(self):
return 0
@patch("subprocess.run", return_value=RunReturn())
def test_chroot_context(mocked_run, tmp_path):
with Chroot(os.fspath(tmp_path)) as chroot:
ret = chroot.run(["/bin/true"], check=True)
assert isinstance(ret, RunReturn)
chroot.run(["/bin/false"], check=False)
assert mocked_run.call_args_list == [
call(["mount", "-t", "proc", "-o", "nosuid,noexec,nodev",
"proc", os.fspath(tmp_path / "proc")], check=True),
call(["mount", "-t", "devtmpfs", "-o", "mode=0755,noexec,nosuid,strictatime",
"devtmpfs", os.fspath(tmp_path / "dev")], check=True),
call(["mount", "-t", "sysfs", "-o", "nosuid,noexec,nodev",
"sysfs", os.fspath(tmp_path / "sys")], check=True),
call(["chroot", os.fspath(tmp_path), "/bin/true"], check=True),
call(["chroot", os.fspath(tmp_path), "/bin/false"], check=False),
call(["umount", "--lazy", os.fspath(tmp_path / "proc")], check=False),
call(["umount", "--lazy", os.fspath(tmp_path / "dev")], check=False),
call(["umount", "--lazy", os.fspath(tmp_path / "sys")], check=False),
]