diff --git a/osbuild/util/mnt.py b/osbuild/util/mnt.py index 7169bd39..828659fb 100644 --- a/osbuild/util/mnt.py +++ b/osbuild/util/mnt.py @@ -57,7 +57,16 @@ class MountGuard(contextlib.AbstractContextManager): if options: args += ["-o", ",".join(options)] - subprocess.run(["mount"] + args + [source, target], check=True) + r = subprocess.run(["mount"] + args + [source, target], + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + encoding="utf-8", + check=False) + if r.returncode != 0: + code = r.returncode + msg = r.stdout.strip() + raise RuntimeError(f"{msg} (code: {code})") + self.mounts += [{"source": source, "target": target}] def umount(self): diff --git a/test/mod/test_util_mnt.py b/test/mod/test_util_mnt.py new file mode 100644 index 00000000..ad225016 --- /dev/null +++ b/test/mod/test_util_mnt.py @@ -0,0 +1,22 @@ +import os +import subprocess + +import pytest + +from osbuild.util.mnt import mount, MountGuard + + +@pytest.mark.skipif(os.getuid() != 0, reason="root only") +def test_mount_failure_msg(tmp_path): + with pytest.raises(RuntimeError) as e: + mount("/dev/invalid-src", tmp_path) + assert "special device /dev/invalid-src does not exist" in str(e.value) + + +@pytest.mark.skipif(os.getuid() != 0, reason="root only") +def test_mount_guard_failure_msg(tmp_path): + with pytest.raises(RuntimeError) as e: + with MountGuard() as mg: + mg.mount("/dev/invalid-src", tmp_path) + assert "special device /dev/invalid-src does not exist" in str(e.value) +