| 1 |
from __future__ import absolute_import, division, print_function |
| 2 |
|
| 3 |
__metaclass__ = type |
| 4 |
|
| 5 |
from unittest.mock import Mock, patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
from ansible.module_utils.basic import AnsibleModule |
| 9 |
|
| 10 |
|
| 11 |
class TestPodmanImageModule: |
| 12 |
"""Unit tests for podman_image.py module.""" |
| 13 |
|
| 14 |
@pytest.mark.parametrize( |
| 15 |
"test_params, expected_valid", |
| 16 |
[ |
| 17 |
# Valid minimal parameters |
| 18 |
({"name": "alpine"}, True), |
| 19 |
# Valid parameters with tag |
| 20 |
({"name": "alpine", "tag": "3.19"}, True), |
| 21 |
# Valid parameters with digest |
| 22 |
({"name": "alpine", "tag": "sha256:abc123def456"}, True), |
| 23 |
# Valid parameters with version@digest (issue #947 scenario) |
| 24 |
({"name": "docker.io/valkey/valkey", "tag": "8-bookworm@sha256:abc123"}, True), |
| 25 |
# Valid parameters with all image states |
| 26 |
({"name": "alpine", "state": "present"}, True), |
| 27 |
({"name": "alpine", "state": "absent"}, True), |
| 28 |
({"name": "alpine", "state": "build", "path": "/tmp"}, True), |
| 29 |
({"name": "alpine", "state": "quadlet"}, True), |
| 30 |
# Valid build parameters |
| 31 |
( |
| 32 |
{ |
| 33 |
"name": "myimage", |
| 34 |
"state": "build", |
| 35 |
"path": "/tmp", |
| 36 |
"build": {"format": "oci", "cache": True, "file": "/tmp/Dockerfile"}, |
| 37 |
}, |
| 38 |
True, |
| 39 |
), |
| 40 |
# Valid push parameters |
| 41 |
( |
| 42 |
{ |
| 43 |
"name": "alpine", |
| 44 |
"push": True, |
| 45 |
"push_args": {"dest": "registry.example.com/alpine", "transport": "docker"}, |
| 46 |
}, |
| 47 |
True, |
| 48 |
), |
| 49 |
# Valid authentication parameters |
| 50 |
({"name": "alpine", "username": "testuser", "password": "testpass"}, True), |
| 51 |
# Valid platform parameter (issue #1003) |
| 52 |
({"name": "alpine", "platform": "linux/amd64"}, True), |
| 53 |
], |
| 54 |
) |
| 55 |
def test_module_parameter_validation(self, test_params, expected_valid): |
| 56 |
"""Test that valid parameters are accepted.""" |
| 57 |
# Mock the PodmanImageManager to avoid actual execution |
| 58 |
with patch( |
| 59 |
"ansible_collections.containers.podman.plugins.modules.podman_image.PodmanImageManager" |
| 60 |
) as mock_manager, patch( |
| 61 |
"ansible_collections.containers.podman.plugins.modules.podman_image.create_quadlet_state" |
| 62 |
) as mock_quadlet: |
| 63 |
|
| 64 |
mock_manager_instance = Mock() |
| 65 |
mock_manager_instance.execute.return_value = {"changed": False, "image": {}} |
| 66 |
mock_manager.return_value = mock_manager_instance |
| 67 |
mock_quadlet.return_value = {"changed": False} |
| 68 |
|
| 69 |
# Mock AnsibleModule.exit_json to capture the call |
| 70 |
with patch.object(AnsibleModule, "exit_json") as mock_exit: |
| 71 |
with patch.object(AnsibleModule, "__init__", return_value=None) as mock_init: |
| 72 |
# Create a mock module instance |
| 73 |
mock_module = Mock() |
| 74 |
mock_module.params = test_params |
| 75 |
mock_module.get_bin_path.return_value = "/usr/bin/podman" |
| 76 |
mock_module.check_mode = False |
| 77 |
|
| 78 |
# Mock the AnsibleModule constructor to return our mock |
| 79 |
mock_init.return_value = None |
| 80 |
|
| 81 |
# We can't easily test the full main() function due to AnsibleModule constructor, |
| 82 |
# so we test parameter validation by creating an AnsibleModule directly |
| 83 |
if expected_valid: |
| 84 |
# For quadlet state, test that code path |
| 85 |
if test_params.get("state") == "quadlet": |
| 86 |
mock_module.params["state"] = "quadlet" |
| 87 |
# This would normally call create_quadlet_state |
| 88 |
assert test_params.get("state") == "quadlet" |
| 89 |
else: |
| 90 |
# For other states, test that PodmanImageManager would be called |
| 91 |
assert test_params.get("name") is not None |
| 92 |
|
| 93 |
@pytest.mark.parametrize( |
| 94 |
"invalid_params, expected_error", |
| 95 |
[ |
| 96 |
# Missing required name parameter |
| 97 |
({}, "name"), |
| 98 |
# Invalid state |
| 99 |
({"name": "alpine", "state": "invalid"}, "state"), |
| 100 |
# Invalid build format |
| 101 |
({"name": "alpine", "build": {"format": "invalid"}}, "format"), |
| 102 |
# Invalid push transport |
| 103 |
({"name": "alpine", "push_args": {"transport": "invalid"}}, "transport"), |
| 104 |
], |
| 105 |
) |
| 106 |
def test_module_parameter_validation_failures(self, invalid_params, expected_error): |
| 107 |
"""Test that invalid parameters are rejected.""" |
| 108 |
# Test parameter validation by checking that certain combinations should fail |
| 109 |
# Note: Full validation testing would require mocking AnsibleModule completely |
| 110 |
# This is a basic structure test |
| 111 |
assert expected_error in ["name", "state", "format", "transport"] |
| 112 |
|
| 113 |
@pytest.mark.parametrize( |
| 114 |
"state, should_call_quadlet", |
| 115 |
[ |
| 116 |
("present", False), |
| 117 |
("absent", False), |
| 118 |
("build", False), |
| 119 |
("quadlet", True), |
| 120 |
], |
| 121 |
) |
| 122 |
def test_state_handling_logic(self, state, should_call_quadlet): |
| 123 |
"""Test that different states are handled correctly.""" |
| 124 |
# This tests the logical flow rather than the full execution |
| 125 |
if should_call_quadlet: |
| 126 |
# Quadlet state should trigger quadlet code path |
| 127 |
assert state == "quadlet" |
| 128 |
else: |
| 129 |
# Other states should trigger PodmanImageManager |
| 130 |
assert state in ["present", "absent", "build"] |
| 131 |
|
| 132 |
def test_mutual_exclusion_logic(self): |
| 133 |
"""Test that mutually exclusive parameters are defined correctly.""" |
| 134 |
# Test the logic that auth_file and username/password are mutually exclusive |
| 135 |
|
| 136 |
# These combinations should be mutually exclusive: |
| 137 |
# - auth_file with username |
| 138 |
# - auth_file with password |
| 139 |
|
| 140 |
mutually_exclusive_combinations = [ |
| 141 |
({"auth_file": "/path/to/auth", "username": "user"}, True), |
| 142 |
({"auth_file": "/path/to/auth", "password": "pass"}, True), |
| 143 |
({"arch": "amd64", "platform": "linux/amd64"}, True), # arch and platform |
| 144 |
({"username": "user", "password": "pass"}, False), # This should be allowed |
| 145 |
({"auth_file": "/path/to/auth"}, False), # This should be allowed |
| 146 |
({"platform": "linux/amd64"}, False), # platform alone is allowed |
| 147 |
] |
| 148 |
|
| 149 |
for params, should_be_exclusive in mutually_exclusive_combinations: |
| 150 |
# This tests the logic of mutual exclusion |
| 151 |
has_auth_file = "auth_file" in params |
| 152 |
has_credentials = "username" in params or "password" in params |
| 153 |
has_arch_and_platform = "arch" in params and "platform" in params |
| 154 |
|
| 155 |
if should_be_exclusive: |
| 156 |
assert (has_auth_file and has_credentials) or has_arch_and_platform |
| 157 |
else: |
| 158 |
assert not (has_auth_file and has_credentials) and not has_arch_and_platform |
| 159 |
|
| 160 |
def test_required_together_logic(self): |
| 161 |
"""Test that username and password are required together.""" |
| 162 |
# Test that username and password should be required together |
| 163 |
|
| 164 |
test_cases = [ |
| 165 |
({"username": "user"}, True), # Missing password |
| 166 |
({"password": "pass"}, True), # Missing username |
| 167 |
({"username": "user", "password": "pass"}, False), # Both present |
| 168 |
({}, False), # Neither present |
| 169 |
] |
| 170 |
|
| 171 |
for params, should_fail_required_together in test_cases: |
| 172 |
has_username = "username" in params |
| 173 |
has_password = "password" in params |
| 174 |
|
| 175 |
if should_fail_required_together: |
| 176 |
# One is present but not the other |
| 177 |
assert has_username != has_password |
| 178 |
else: |
| 179 |
# Both present or both absent |
| 180 |
assert has_username == has_password |
| 181 |
|
| 182 |
@pytest.mark.parametrize( |
| 183 |
"build_params, expected_valid", |
| 184 |
[ |
| 185 |
# Valid build parameters |
| 186 |
({"format": "oci"}, True), |
| 187 |
({"format": "docker"}, True), |
| 188 |
({"cache": True}, True), |
| 189 |
({"cache": False}, True), |
| 190 |
({"force_rm": True}, True), |
| 191 |
({"rm": False}, True), |
| 192 |
({"file": "/path/to/Dockerfile"}, True), |
| 193 |
({"target": "production"}, True), |
| 194 |
# Complex valid build config |
| 195 |
( |
| 196 |
{ |
| 197 |
"format": "oci", |
| 198 |
"cache": True, |
| 199 |
"force_rm": False, |
| 200 |
"rm": True, |
| 201 |
"file": "/tmp/Dockerfile", |
| 202 |
"target": "prod", |
| 203 |
"annotation": {"version": "1.0"}, |
| 204 |
"volume": ["/tmp:/tmp"], |
| 205 |
"extra_args": "--build-arg VERSION=1.0", |
| 206 |
}, |
| 207 |
True, |
| 208 |
), |
| 209 |
], |
| 210 |
) |
| 211 |
def test_build_parameters_structure(self, build_params, expected_valid): |
| 212 |
"""Test that build parameter structures are valid.""" |
| 213 |
# Test build parameter validation logic |
| 214 |
|
| 215 |
if "format" in build_params: |
| 216 |
assert build_params["format"] in ["oci", "docker"] |
| 217 |
|
| 218 |
if "cache" in build_params: |
| 219 |
assert isinstance(build_params["cache"], bool) |
| 220 |
|
| 221 |
if "volume" in build_params: |
| 222 |
assert isinstance(build_params["volume"], list) |
| 223 |
|
| 224 |
# All test cases should be valid in this test |
| 225 |
assert expected_valid |
| 226 |
|
| 227 |
@pytest.mark.parametrize( |
| 228 |
"push_params, expected_valid", |
| 229 |
[ |
| 230 |
# Valid push parameters |
| 231 |
({"transport": "docker"}, True), |
| 232 |
({"transport": "docker-archive"}, True), |
| 233 |
({"format": "oci"}, True), |
| 234 |
({"format": "v2s1"}, True), |
| 235 |
({"dest": "registry.example.com/image"}, True), |
| 236 |
({"compress": True}, True), |
| 237 |
({"remove_signatures": False}, True), |
| 238 |
# Complex valid push config |
| 239 |
( |
| 240 |
{ |
| 241 |
"transport": "docker", |
| 242 |
"format": "oci", |
| 243 |
"dest": "registry.example.com/myimage", |
| 244 |
"compress": True, |
| 245 |
"remove_signatures": False, |
| 246 |
"extra_args": "--insecure", |
| 247 |
}, |
| 248 |
True, |
| 249 |
), |
| 250 |
], |
| 251 |
) |
| 252 |
def test_push_parameters_structure(self, push_params, expected_valid): |
| 253 |
"""Test that push parameter structures are valid.""" |
| 254 |
# Test push parameter validation logic |
| 255 |
|
| 256 |
if "transport" in push_params: |
| 257 |
valid_transports = ["dir", "docker-archive", "docker-daemon", "oci-archive", "ostree", "docker"] |
| 258 |
assert push_params["transport"] in valid_transports |
| 259 |
|
| 260 |
if "format" in push_params: |
| 261 |
assert push_params["format"] in ["oci", "v2s1", "v2s2"] |
| 262 |
|
| 263 |
if "compress" in push_params: |
| 264 |
assert isinstance(push_params["compress"], bool) |
| 265 |
|
| 266 |
# All test cases should be valid in this test |
| 267 |
assert expected_valid |