diff --git a/inputs/test/test_containers.py b/inputs/test/test_containers.py index 72dfc45c..5417e69a 100644 --- a/inputs/test/test_containers.py +++ b/inputs/test/test_containers.py @@ -2,9 +2,7 @@ import os import pathlib -import random import socket -import string import subprocess import tempfile @@ -29,8 +27,7 @@ class FakeStoreClient: @pytest.mark.skipif(not has_executable("podman"), reason="no podman executable") @pytest.mark.skipif(os.getuid() != 0, reason="root only") def test_containers_local_inputs_integration(tmp_path, inputs_module): - base_tag = "container-" + "".join(random.choices(string.digits, k=12)) - with make_container(tmp_path, base_tag, {"file1": "file1 content"}): + with make_container(tmp_path, {"file1": "file1 content"}) as base_tag: image_id = subprocess.check_output( ["podman", "inspect", "-f", "{{ .Id }}", base_tag], universal_newlines=True).strip() diff --git a/osbuild/testutil/__init__.py b/osbuild/testutil/__init__.py index 3c947804..f6c4eb6b 100644 --- a/osbuild/testutil/__init__.py +++ b/osbuild/testutil/__init__.py @@ -81,8 +81,9 @@ def mock_command(cmd_name: str, script: str): @contextlib.contextmanager -def make_container(tmp_path, tag, fake_content, base="scratch"): +def make_container(tmp_path, fake_content, base="scratch"): fake_container_src = tmp_path / "fake-container-src" + fake_container_src.mkdir(exist_ok=True) make_fake_tree(fake_container_src, fake_content) fake_containerfile_path = fake_container_src / "Containerfile" container_file_content = f""" @@ -90,16 +91,22 @@ def make_container(tmp_path, tag, fake_content, base="scratch"): COPY . . """ fake_containerfile_path.write_text(container_file_content, encoding="utf8") - subprocess.check_call([ + p = subprocess.Popen([ "podman", "build", "--no-cache", "-f", os.fspath(fake_containerfile_path), - "-t", tag, - ]) + ], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + while True: + line = p.stdout.readline() + if line == "": + break + print(line) + container_id = line.strip() + p.wait() try: - yield + yield container_id finally: - subprocess.check_call(["podman", "image", "rm", tag]) + subprocess.check_call(["podman", "image", "rm", container_id]) @contextlib.contextmanager diff --git a/sources/test/test_container_storage_source.py b/sources/test/test_container_storage_source.py index 5acc7ba8..7eeec1ce 100644 --- a/sources/test/test_container_storage_source.py +++ b/sources/test/test_container_storage_source.py @@ -1,9 +1,7 @@ #!/usr/bin/python3 import os -import random import socket -import string import subprocess import pytest @@ -16,10 +14,9 @@ SOURCES_NAME = "org.osbuild.containers-storage" @pytest.mark.skipif(not has_executable("podman"), reason="no podman executable") @pytest.mark.skipif(os.getuid() != 0, reason="root only") def test_containers_storage_integration(tmp_path, sources_module): - base_tag = "container-" + "".join(random.choices(string.digits, k=12)) - with make_container(tmp_path, base_tag, { + with make_container(tmp_path, { "file1": "file1 content", - }): + }) as base_tag: image_id = subprocess.check_output(["podman", "inspect", "-f", "{{ .Id }}", base_tag], universal_newlines=True).strip() checksum = f"sha256:{image_id}" diff --git a/stages/test/test_container_deploy.py b/stages/test/test_container_deploy.py index fb18c438..02a11795 100644 --- a/stages/test/test_container_deploy.py +++ b/stages/test/test_container_deploy.py @@ -2,8 +2,6 @@ import os import os.path -import random -import string import subprocess import textwrap from unittest.mock import call, patch @@ -21,10 +19,8 @@ STAGE_NAME = "org.osbuild.container-deploy" def test_container_deploy_integration(tmp_path, stage_module): # build two containers and overlay files to test for # https://github.com/containers/storage/issues/1779 - base_tag = "cont-base-" + "".join(random.choices(string.digits, k=12)) - with make_container(tmp_path, base_tag, {"file1": "file1 from base"}): - cont_tag = "cont" + "".join(random.choices(string.digits, k=12)) - with make_container(tmp_path, cont_tag, {"file1": "file1 from final layer"}, base_tag): + with make_container(tmp_path, {"file1": "file1 from base"}) as base_tag: + with make_container(tmp_path, {"file1": "file1 from final layer"}, base_tag) as cont_tag: # export for the container-deploy stage fake_container_dst = tmp_path / "fake-container" subprocess.check_call([ @@ -63,13 +59,12 @@ def test_container_deploy_integration(tmp_path, stage_module): @pytest.mark.skipif(os.getuid() != 0, reason="needs root") @pytest.mark.skipif(not has_executable("podman"), reason="no podman executable") def test_container_deploy_exclude(tmp_path, stage_module): - base_tag = "cont-base-" + "".join(random.choices(string.digits, k=12)) - with make_container(tmp_path, base_tag, { + with make_container(tmp_path, { "file1": "file1 content", "file2": "file2 content", "dir1/file3": "dir1/file3 content", "dir2/file4": "dir2/file4 content", - }): + }) as base_tag: # export for the container-deploy stage fake_container_dst = tmp_path / "fake-container" subprocess.check_call([ diff --git a/test/mod/test_testutil_make_container.py b/test/mod/test_testutil_make_container.py new file mode 100644 index 00000000..2c8a7717 --- /dev/null +++ b/test/mod/test_testutil_make_container.py @@ -0,0 +1,29 @@ +# +# Tests for the 'osbuild.util.testutil.make_container'. +# +import subprocess +import textwrap + +import pytest + +from osbuild.testutil import has_executable, make_container, mock_command + + +def test_make_container_bad_podman_prints_podman_output(tmp_path, capsys): + fake_broken_podman = textwrap.dedent("""\ + #!/bin/sh + echo fake-broken-podman + exit 1 + """) + with mock_command("podman", fake_broken_podman): + with pytest.raises(subprocess.CalledProcessError): + with make_container(tmp_path, {}) as _: + pass + assert "fake-broken-podman" in capsys.readouterr().out + + +@pytest.mark.skipif(not has_executable("podman"), reason="no podman executable") +def test_make_container_integration(tmp_path, capsys): + with make_container(tmp_path, {"/etc/foo": "foo-content"}) as cref: + assert len(cref) == 64 + assert "COMMIT" in capsys.readouterr().out