From c3af3173ebdf07950c7a870cd32dce925cfa36cc Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Thu, 8 Feb 2024 09:45:34 +0100 Subject: [PATCH] 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. --- stages/org.osbuild.container-deploy | 22 +++++++++++----------- stages/test/test_container_deploy.py | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/stages/org.osbuild.container-deploy b/stages/org.osbuild.container-deploy index 7f419da4..df914a3b 100755 --- a/stages/org.osbuild.container-deploy +++ b/stages/org.osbuild.container-deploy @@ -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( diff --git a/stages/test/test_container_deploy.py b/stages/test/test_container_deploy.py index 285e82a7..b280b002 100644 --- a/stages/test/test_container_deploy.py +++ b/stages/test/test_container_deploy.py @@ -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)