| 1 |
# Copyright 2022 Red Hat | Ansible |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import typing as t |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from ansible_collections.community.docker.plugins.module_utils._image_archive import ( |
| 12 |
api_image_id, |
| 13 |
) |
| 14 |
from ansible_collections.community.docker.plugins.modules.docker_image import ( |
| 15 |
ImageManager, |
| 16 |
) |
| 17 |
|
| 18 |
from ..test_support.docker_image_archive_stubbing import ( |
| 19 |
write_imitation_archive, |
| 20 |
write_irrelevant_tar, |
| 21 |
) |
| 22 |
|
| 23 |
if t.TYPE_CHECKING: |
| 24 |
from collections.abc import Callable |
| 25 |
|
| 26 |
|
| 27 |
def assert_no_logging(msg: str) -> t.NoReturn: |
| 28 |
raise AssertionError(f"Should not have logged anything but logged {msg}") |
| 29 |
|
| 30 |
|
| 31 |
def capture_logging(messages: list[str]) -> Callable[[str], None]: |
| 32 |
def capture(msg: str) -> None: |
| 33 |
messages.append(msg) |
| 34 |
|
| 35 |
return capture |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture(name="tar_file_name") |
| 39 |
def tar_file_name_fixture(tmpdir: t.Any) -> str: |
| 40 |
""" |
| 41 |
Return the name of a non-existing tar file in an existing temporary directory. |
| 42 |
""" |
| 43 |
|
| 44 |
return tmpdir.join("foo.tar") |
| 45 |
|
| 46 |
|
| 47 |
def test_archived_image_action_when_missing(tar_file_name: str) -> None: |
| 48 |
fake_name = "a:latest" |
| 49 |
fake_id = "a1" |
| 50 |
|
| 51 |
expected = f"Archived image {fake_name} to {tar_file_name}, since none present" |
| 52 |
|
| 53 |
actual = ImageManager.archived_image_action( |
| 54 |
assert_no_logging, tar_file_name, fake_name, api_image_id(fake_id) |
| 55 |
) |
| 56 |
|
| 57 |
assert actual == expected |
| 58 |
|
| 59 |
|
| 60 |
def test_archived_image_action_when_current(tar_file_name: str) -> None: |
| 61 |
fake_name = "b:latest" |
| 62 |
fake_id = "b2" |
| 63 |
|
| 64 |
write_imitation_archive(tar_file_name, fake_id, [fake_name]) |
| 65 |
|
| 66 |
actual = ImageManager.archived_image_action( |
| 67 |
assert_no_logging, tar_file_name, fake_name, api_image_id(fake_id) |
| 68 |
) |
| 69 |
|
| 70 |
assert actual is None |
| 71 |
|
| 72 |
|
| 73 |
def test_archived_image_action_when_invalid(tar_file_name: str) -> None: |
| 74 |
fake_name = "c:1.2.3" |
| 75 |
fake_id = "c3" |
| 76 |
|
| 77 |
write_irrelevant_tar(tar_file_name) |
| 78 |
|
| 79 |
expected = f"Archived image {fake_name} to {tar_file_name}, overwriting an unreadable archive file" |
| 80 |
|
| 81 |
actual_log: list[str] = [] |
| 82 |
actual = ImageManager.archived_image_action( |
| 83 |
capture_logging(actual_log), tar_file_name, fake_name, api_image_id(fake_id) |
| 84 |
) |
| 85 |
|
| 86 |
assert actual == expected |
| 87 |
|
| 88 |
assert len(actual_log) == 1 |
| 89 |
assert actual_log[0].startswith("Unable to extract manifest summary from archive") |
| 90 |
|
| 91 |
|
| 92 |
def test_archived_image_action_when_obsolete_by_id(tar_file_name: str) -> None: |
| 93 |
fake_name = "d:0.0.1" |
| 94 |
old_id = "e5" |
| 95 |
new_id = "d4" |
| 96 |
|
| 97 |
write_imitation_archive(tar_file_name, old_id, [fake_name]) |
| 98 |
|
| 99 |
expected = f"Archived image {fake_name} to {tar_file_name}, overwriting archive with image {old_id} named {fake_name}" |
| 100 |
actual = ImageManager.archived_image_action( |
| 101 |
assert_no_logging, tar_file_name, fake_name, api_image_id(new_id) |
| 102 |
) |
| 103 |
|
| 104 |
assert actual == expected |
| 105 |
|
| 106 |
|
| 107 |
def test_archived_image_action_when_obsolete_by_name(tar_file_name: str) -> None: |
| 108 |
old_name = "hi" |
| 109 |
new_name = "d:0.0.1" |
| 110 |
fake_id = "d4" |
| 111 |
|
| 112 |
write_imitation_archive(tar_file_name, fake_id, [old_name]) |
| 113 |
|
| 114 |
expected = f"Archived image {new_name} to {tar_file_name}, overwriting archive with image {fake_id} named {old_name}" |
| 115 |
actual = ImageManager.archived_image_action( |
| 116 |
assert_no_logging, tar_file_name, new_name, api_image_id(fake_id) |
| 117 |
) |
| 118 |
|
| 119 |
print(f"actual : {actual}") |
| 120 |
print(f"expected : {expected}") |
| 121 |
assert actual == expected |