community.docker/tests/unit/plugins/module_utils/test__image_archive.py
Felix Fontein 741c318b1d
Python code modernization, 2/n (#1156)
* Adjust all __future__ imports:

for i in $(grep -REl "__future__.*absolute_import" plugins/ tests/); do
  sed -e 's/from __future__ import .*/from __future__ import annotations/g' -i $i;
done

* Remove all UTF-8 encoding specifications for Python source files:

for i in $(grep -REl '[-][*]- coding: utf-8 -[*]-' plugins/ tests/); do
  sed -e '/^# -\*- coding: utf-8 -\*-/d' -i $i;
done

* Reformat.
2025-10-09 20:46:48 +02:00

92 lines
2.6 KiB
Python

# Copyright 2022 Red Hat | Ansible
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations
__metaclass__ = type
import tarfile
import pytest
from ansible_collections.community.docker.plugins.module_utils._image_archive import (
ImageArchiveInvalidException,
api_image_id,
archived_image_manifest,
)
from ..test_support.docker_image_archive_stubbing import (
write_imitation_archive,
write_imitation_archive_with_manifest,
write_irrelevant_tar,
)
@pytest.fixture
def tar_file_name(tmpdir):
"""
Return the name of a non-existing tar file in an existing temporary directory.
"""
# Cast to str required by Python 2.x
return str(tmpdir.join("foo.tar"))
@pytest.mark.parametrize(
"expected, value", [("sha256:foo", "foo"), ("sha256:bar", "bar")]
)
def test_api_image_id_from_archive_id(expected, value):
assert api_image_id(value) == expected
def test_archived_image_manifest_extracts(tar_file_name):
expected_id = "abcde12345"
expected_tags = ["foo:latest", "bar:v1"]
write_imitation_archive(tar_file_name, expected_id, expected_tags)
actual = archived_image_manifest(tar_file_name)
assert actual.image_id == expected_id
assert actual.repo_tags == expected_tags
def test_archived_image_manifest_extracts_nothing_when_file_not_present(tar_file_name):
image_id = archived_image_manifest(tar_file_name)
assert image_id is None
def test_archived_image_manifest_raises_when_file_not_a_tar():
try:
archived_image_manifest(__file__)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, tarfile.ReadError)
assert str(__file__) in str(e)
def test_archived_image_manifest_raises_when_tar_missing_manifest(tar_file_name):
write_irrelevant_tar(tar_file_name)
try:
archived_image_manifest(tar_file_name)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, KeyError)
assert "manifest.json" in str(e.__cause__)
def test_archived_image_manifest_raises_when_manifest_missing_id(tar_file_name):
manifest = [{"foo": "bar"}]
write_imitation_archive_with_manifest(tar_file_name, manifest)
try:
archived_image_manifest(tar_file_name)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, KeyError)
assert "Config" in str(e.__cause__)