debian-forge/test/mod/test_util_chroot.py
Achilleas Koutsou 73464ff119 test: add test for chroot context
Add a test for the chroot context that mocks subprocess.run() and
subprocess.check_call().  The test verifies that the functions are
called the expected number of times with the expected command (first
arg).
2024-08-28 16:45:48 -07:00

34 lines
983 B
Python

#
# Test for util/chroot.py
#
from unittest.mock import 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.check_call")
@patch("subprocess.run", return_value=RunReturn())
def test_chroot_context(mocked_run, mocked_check_call):
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"
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"