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 os
|
||||||
import pathlib
|
import pathlib
|
||||||
import random
|
|
||||||
import socket
|
import socket
|
||||||
import string
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
|
@ -29,8 +27,7 @@ class FakeStoreClient:
|
||||||
@pytest.mark.skipif(not has_executable("podman"), reason="no podman executable")
|
@pytest.mark.skipif(not has_executable("podman"), reason="no podman executable")
|
||||||
@pytest.mark.skipif(os.getuid() != 0, reason="root only")
|
@pytest.mark.skipif(os.getuid() != 0, reason="root only")
|
||||||
def test_containers_local_inputs_integration(tmp_path, inputs_module):
|
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, {"file1": "file1 content"}) as base_tag:
|
||||||
with make_container(tmp_path, base_tag, {"file1": "file1 content"}):
|
|
||||||
image_id = subprocess.check_output(
|
image_id = subprocess.check_output(
|
||||||
["podman", "inspect", "-f", "{{ .Id }}", base_tag],
|
["podman", "inspect", "-f", "{{ .Id }}", base_tag],
|
||||||
universal_newlines=True).strip()
|
universal_newlines=True).strip()
|
||||||
|
|
|
||||||
|
|
@ -81,8 +81,9 @@ def mock_command(cmd_name: str, script: str):
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@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 = tmp_path / "fake-container-src"
|
||||||
|
fake_container_src.mkdir(exist_ok=True)
|
||||||
make_fake_tree(fake_container_src, fake_content)
|
make_fake_tree(fake_container_src, fake_content)
|
||||||
fake_containerfile_path = fake_container_src / "Containerfile"
|
fake_containerfile_path = fake_container_src / "Containerfile"
|
||||||
container_file_content = f"""
|
container_file_content = f"""
|
||||||
|
|
@ -90,16 +91,22 @@ def make_container(tmp_path, tag, fake_content, base="scratch"):
|
||||||
COPY . .
|
COPY . .
|
||||||
"""
|
"""
|
||||||
fake_containerfile_path.write_text(container_file_content, encoding="utf8")
|
fake_containerfile_path.write_text(container_file_content, encoding="utf8")
|
||||||
subprocess.check_call([
|
p = subprocess.Popen([
|
||||||
"podman", "build",
|
"podman", "build",
|
||||||
"--no-cache",
|
"--no-cache",
|
||||||
"-f", os.fspath(fake_containerfile_path),
|
"-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:
|
try:
|
||||||
yield
|
yield container_id
|
||||||
finally:
|
finally:
|
||||||
subprocess.check_call(["podman", "image", "rm", tag])
|
subprocess.check_call(["podman", "image", "rm", container_id])
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import random
|
|
||||||
import socket
|
import socket
|
||||||
import string
|
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
import pytest
|
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(not has_executable("podman"), reason="no podman executable")
|
||||||
@pytest.mark.skipif(os.getuid() != 0, reason="root only")
|
@pytest.mark.skipif(os.getuid() != 0, reason="root only")
|
||||||
def test_containers_storage_integration(tmp_path, sources_module):
|
def test_containers_storage_integration(tmp_path, sources_module):
|
||||||
base_tag = "container-" + "".join(random.choices(string.digits, k=12))
|
with make_container(tmp_path, {
|
||||||
with make_container(tmp_path, base_tag, {
|
|
||||||
"file1": "file1 content",
|
"file1": "file1 content",
|
||||||
}):
|
}) as base_tag:
|
||||||
image_id = subprocess.check_output(["podman", "inspect", "-f", "{{ .Id }}", base_tag],
|
image_id = subprocess.check_output(["podman", "inspect", "-f", "{{ .Id }}", base_tag],
|
||||||
universal_newlines=True).strip()
|
universal_newlines=True).strip()
|
||||||
checksum = f"sha256:{image_id}"
|
checksum = f"sha256:{image_id}"
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import textwrap
|
import textwrap
|
||||||
from unittest.mock import call, patch
|
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):
|
def test_container_deploy_integration(tmp_path, stage_module):
|
||||||
# build two containers and overlay files to test for
|
# build two containers and overlay files to test for
|
||||||
# https://github.com/containers/storage/issues/1779
|
# https://github.com/containers/storage/issues/1779
|
||||||
base_tag = "cont-base-" + "".join(random.choices(string.digits, k=12))
|
with make_container(tmp_path, {"file1": "file1 from base"}) as base_tag:
|
||||||
with make_container(tmp_path, base_tag, {"file1": "file1 from base"}):
|
with make_container(tmp_path, {"file1": "file1 from final layer"}, base_tag) as cont_tag:
|
||||||
cont_tag = "cont" + "".join(random.choices(string.digits, k=12))
|
|
||||||
with make_container(tmp_path, cont_tag, {"file1": "file1 from final layer"}, base_tag):
|
|
||||||
# export for the container-deploy stage
|
# export for the container-deploy stage
|
||||||
fake_container_dst = tmp_path / "fake-container"
|
fake_container_dst = tmp_path / "fake-container"
|
||||||
subprocess.check_call([
|
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(os.getuid() != 0, reason="needs root")
|
||||||
@pytest.mark.skipif(not has_executable("podman"), reason="no podman executable")
|
@pytest.mark.skipif(not has_executable("podman"), reason="no podman executable")
|
||||||
def test_container_deploy_exclude(tmp_path, stage_module):
|
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, {
|
||||||
with make_container(tmp_path, base_tag, {
|
|
||||||
"file1": "file1 content",
|
"file1": "file1 content",
|
||||||
"file2": "file2 content",
|
"file2": "file2 content",
|
||||||
"dir1/file3": "dir1/file3 content",
|
"dir1/file3": "dir1/file3 content",
|
||||||
"dir2/file4": "dir2/file4 content",
|
"dir2/file4": "dir2/file4 content",
|
||||||
}):
|
}) as base_tag:
|
||||||
# export for the container-deploy stage
|
# export for the container-deploy stage
|
||||||
fake_container_dst = tmp_path / "fake-container"
|
fake_container_dst = tmp_path / "fake-container"
|
||||||
subprocess.check_call([
|
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