From 82f24146375a21ca5bf93078760a0cd20e4ca3dd Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 26 Feb 2024 11:49:49 +0100 Subject: [PATCH] sources: tweak ContainersStorageSources.exists to return False When an images does not exist just return `False` instead of raising a RuntimeError. If anything else goes wrong (unknown output or hash mismatch) keep the RuntimeError as this is an unexpected exception. --- sources/org.osbuild.containers-storage | 8 +++++++- sources/test/test_container_storage_source.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/sources/org.osbuild.containers-storage b/sources/org.osbuild.containers-storage index f7c5e50d..1bfea583 100755 --- a/sources/org.osbuild.containers-storage +++ b/sources/org.osbuild.containers-storage @@ -81,10 +81,16 @@ class ContainersStorageSource(sources.SourceService): # fail early if the user hasn't imported the container into # containers-storage if res.returncode != 0: - raise RuntimeError(f"Container does not exist in local containers storage: {res.stderr}") + # string not matching not ideal - this is ErrNotAnImage + # which is unchanged since 2016 (added in ee99172905 in + # containers/storage) + if "identifier is not an image" in res.stderr: + return False + raise RuntimeError(f"unknown skopeo error: {res.stderr}") # NOTE: this is a bit redundant because we're checking the content digest of the thing we retrieved via its # id (which is the content digest), but let's keep it in case we change to retrieving local containers by name + # See also https://github.com/containers/skopeo/pull/2236 local_id = "sha256:" + hashlib.sha256(res.stdout.encode()).hexdigest() if local_id != checksum: raise RuntimeError( diff --git a/sources/test/test_container_storage_source.py b/sources/test/test_container_storage_source.py index 00f70304..5acc7ba8 100644 --- a/sources/test/test_container_storage_source.py +++ b/sources/test/test_container_storage_source.py @@ -34,6 +34,17 @@ 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())]) - # this is not ideal, consider to just return "False" here - with pytest.raises(RuntimeError): + assert not cnt_storage.exists(checksum, None) + + +@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_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())]) + with pytest.raises(RuntimeError) as exc: cnt_storage.exists(checksum, None) + assert "unknown skopeo error:" in str(exc)