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.
This commit is contained in:
Michael Vogt 2024-02-26 11:49:49 +01:00 committed by Ondřej Budai
parent 5ab0b41456
commit 82f2414637
2 changed files with 20 additions and 3 deletions

View file

@ -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(

View file

@ -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)