test: add/use new testutil.make_fake_service_fd()

All inputs/sources tests need a fake service fd to instanciate
their services. Consolidate the creation in a single helper.
This commit is contained in:
Michael Vogt 2024-02-27 20:33:19 +01:00 committed by Achilleas Koutsou
parent cccdf8b784
commit 5f31ccf9f2
4 changed files with 33 additions and 19 deletions

View file

@ -1,11 +1,11 @@
#!/usr/bin/python3
import os
import socket
import subprocess
import pytest
from osbuild import testutil
from osbuild.testutil import has_executable, make_container
SOURCES_NAME = "org.osbuild.containers-storage"
@ -20,8 +20,8 @@ def test_containers_storage_integration(tmp_path, sources_module):
image_id = subprocess.check_output(["podman", "inspect", "-f", "{{ .Id }}", base_tag],
universal_newlines=True).strip()
checksum = f"sha256:{image_id}"
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(sock.fileno())])
fd = testutil.make_fake_service_fd()
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(fd)])
assert cnt_storage.exists(checksum, None)
@ -29,8 +29,8 @@ def test_containers_storage_integration(tmp_path, sources_module):
@pytest.mark.skipif(os.getuid() != 0, reason="root only")
def test_containers_storage_integration_missing(sources_module):
checksum = "sha256:1234567890123456789012345678901234567890909b14ffb032aa20fa23d9ad6"
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(sock.fileno())])
fd = testutil.make_fake_service_fd()
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(fd)])
assert not cnt_storage.exists(checksum, None)
@ -40,8 +40,8 @@ def test_containers_storage_integration_invalid(sources_module):
# put an invalid reference into the source to ensure skopeo errors with
# a different error than image not found
checksum = "sha256:["
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(sock.fileno())])
fd = testutil.make_fake_service_fd()
cnt_storage = sources_module.ContainersStorageSource.from_args(["--service-fd", str(fd)])
with pytest.raises(RuntimeError) as exc:
cnt_storage.exists(checksum, None)
assert "unknown skopeo error:" in str(exc)