stages(container-deploy): podman mount failure test/tweaks

This commit adds a test that ensures that the output of
podman mount is part of the error message. While writing
the test I also tweaked the code slightly so that we only
try to `podman umount` if we managed to successfully mount.
This commit is contained in:
Michael Vogt 2024-02-08 09:45:34 +01:00 committed by Brian C. Lane
parent 7b5d6e4bd9
commit c3af3173eb
2 changed files with 28 additions and 11 deletions

View file

@ -44,18 +44,18 @@ SCHEMA_2 = r"""
@contextlib.contextmanager
def mount_container(image_tag):
result = subprocess.run(
["podman", "image", "mount", image_tag],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
check=False,
)
if result.returncode != 0:
code = result.returncode
msg = result.stderr.strip()
raise RuntimeError(f"Failed to mount image ({code}): {msg}")
try:
result = subprocess.run(
["podman", "image", "mount", image_tag],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
check=False,
)
if result.returncode != 0:
code = result.returncode
msg = result.stderr.strip()
raise RuntimeError(f"Failed to mount image ({code}): {msg}")
yield result.stdout.strip()
finally:
subprocess.run(

View file

@ -5,10 +5,12 @@ import os.path
import random
import string
import subprocess
import textwrap
from unittest.mock import call, patch
import pytest
import osbuild.testutil
from osbuild.testutil import has_executable, make_fake_tree
STAGE_NAME = "org.osbuild.container-deploy"
@ -127,3 +129,18 @@ def test_container_deploy_exclude(tmp_path, stage_module):
assert (output_dir / "dir1/file3").read_bytes() == b"dir1/file3 content"
assert not (output_dir / "dir2/file4").exists()
assert not (output_dir / "dir2").exists()
def test_container_deploy_error(stage_module):
fake_podman = textwrap.dedent("""\
#!/bin/sh
echo "some msg on stdout"
echo "other error on stderr" >&2
exit 1
""")
with osbuild.testutil.mock_command("podman", fake_podman):
with pytest.raises(RuntimeError) as exp:
with stage_module.mount_container("some-image-tag"):
pass
assert "some msg on stdout" not in str(exp.value)
assert "other error on stderr" in str(exp.value)