test: return container_id in make_container
The current `make_container()` helper is a bit silly (which is entirely my fault). It requires a container tag as input but all tests end up creating a random number for this input. So instead just remove the input and return the container_id from the podman build in the contextmanager and use that.
This commit is contained in:
parent
df224fb32b
commit
fd0167f130
5 changed files with 49 additions and 24 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}"
|
||||
|
|
|
|||
|
|
@ -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([
|
||||
|
|
|
|||
29
test/mod/test_testutil_make_container.py
Normal file
29
test/mod/test_testutil_make_container.py
Normal file
|
|
@ -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
|
||||
Loading…
Add table
Add a link
Reference in a new issue