| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2023, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
"""Tests for depresolver dataclass objects.""" |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import typing as t |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible.errors import AnsibleError |
| 14 |
from ansible.galaxy.dependency_resolution.dataclasses import Requirement, _validate_v1_source_info_schema |
| 15 |
|
| 16 |
|
| 17 |
NO_LEADING_WHITESPACES = pytest.mark.xfail( |
| 18 |
reason='Does not yet support leading whitespaces', |
| 19 |
strict=True, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.parametrize( |
| 24 |
('collection_version_spec', 'expected_is_pinned_outcome'), |
| 25 |
( |
| 26 |
('1.2.3-dev4', True), |
| 27 |
(' 1.2.3-dev4', True), |
| 28 |
('=1.2.3', True), |
| 29 |
('= 1.2.3', True), |
| 30 |
(' = 1.2.3', True), |
| 31 |
(' =1.2.3', True), |
| 32 |
('==1.2.3', True), |
| 33 |
('== 1.2.3', True), |
| 34 |
(' == 1.2.3', True), |
| 35 |
(' ==1.2.3', True), |
| 36 |
('!=1.0.0', False), |
| 37 |
('!= 1.0.0', False), |
| 38 |
pytest.param(' != 1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 39 |
pytest.param(' !=1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 40 |
('>1.0.0', False), |
| 41 |
('> 1.0.0', False), |
| 42 |
pytest.param(' > 1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 43 |
pytest.param(' >1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 44 |
('>=1.0.0', False), |
| 45 |
('>= 1.0.0', False), |
| 46 |
pytest.param(' >= 1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 47 |
pytest.param(' >=1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 48 |
('<1.0.0', False), |
| 49 |
('< 1.0.0', False), |
| 50 |
pytest.param(' < 1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 51 |
pytest.param(' <1.0.0', False, marks=NO_LEADING_WHITESPACES), |
| 52 |
('*', False), |
| 53 |
('* ', False), |
| 54 |
pytest.param(' * ', False, marks=NO_LEADING_WHITESPACES), |
| 55 |
pytest.param(' *', False, marks=NO_LEADING_WHITESPACES), |
| 56 |
('=1.2.3,!=1.2.3rc5', True), |
| 57 |
), |
| 58 |
) |
| 59 |
def test_requirement_is_pinned_logic( |
| 60 |
collection_version_spec: str, |
| 61 |
expected_is_pinned_outcome: bool, |
| 62 |
) -> None: |
| 63 |
"""Test how Requirement's is_pinned property detects pinned spec.""" |
| 64 |
assert Requirement( |
| 65 |
'namespace.collection', collection_version_spec, |
| 66 |
None, None, None, |
| 67 |
).is_pinned is expected_is_pinned_outcome |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
("namespace", "name", "version", "provided_arguments", "expected_errors"), |
| 72 |
[ |
| 73 |
( |
| 74 |
# Empty data |
| 75 |
"foo", |
| 76 |
"bar", |
| 77 |
"1.2.3", |
| 78 |
{}, |
| 79 |
[], |
| 80 |
), |
| 81 |
( |
| 82 |
# Correct data |
| 83 |
"foo", |
| 84 |
"bar", |
| 85 |
"1.2.3", |
| 86 |
{ |
| 87 |
"format_version": "1.0.0", |
| 88 |
"download_url": "asdf", |
| 89 |
"version_url": "asdf", |
| 90 |
"server": "asdf", |
| 91 |
"signatures": [ |
| 92 |
{ |
| 93 |
"signature": "asdf", |
| 94 |
"pubkey_fingerprint": "asdf", |
| 95 |
"signing_service": "asdf", |
| 96 |
"pulp_created": "asdf", |
| 97 |
}, |
| 98 |
], |
| 99 |
"name": "bar", |
| 100 |
"namespace": "foo", |
| 101 |
"version": "1.2.3", |
| 102 |
}, |
| 103 |
[], |
| 104 |
), |
| 105 |
( |
| 106 |
# Random data that is convertible to string, but not a string |
| 107 |
"foo", |
| 108 |
"bar", |
| 109 |
"1.2.3", |
| 110 |
{ |
| 111 |
"format_version": "1.0.0", |
| 112 |
"download_url": 123, |
| 113 |
"version_url": 1.23, |
| 114 |
"server": True, |
| 115 |
"signatures": [ |
| 116 |
{ |
| 117 |
"signature": [], |
| 118 |
"pubkey_fingerprint": {}, |
| 119 |
"signing_service": None, |
| 120 |
"pulp_created": 42, |
| 121 |
}, |
| 122 |
], |
| 123 |
"name": "bar", |
| 124 |
"namespace": "foo", |
| 125 |
"version": "1.2.3", |
| 126 |
}, |
| 127 |
[], |
| 128 |
), |
| 129 |
( |
| 130 |
# Invalid data 1 |
| 131 |
"foo", |
| 132 |
"bar", |
| 133 |
"1.2.3", |
| 134 |
{ |
| 135 |
"format_version": 123, |
| 136 |
"signatures": "a,b,c", |
| 137 |
"name": "asdf", |
| 138 |
"namespace": "fdsa", |
| 139 |
"version": "asdffdsa", |
| 140 |
}, |
| 141 |
[ |
| 142 |
'dictionary requested, could not parse JSON or key=value', |
| 143 |
"Elements value for option 'signatures' is of type str and we were unable to convert " |
| 144 |
"to dict: dictionary requested, could not parse JSON or key=value", |
| 145 |
"Elements value for option 'signatures' is of type str and we were unable to convert " |
| 146 |
"to dict: dictionary requested, could not parse JSON or key=value", |
| 147 |
"Elements value for option 'signatures' is of type str and we were unable to convert " |
| 148 |
"to dict: dictionary requested, could not parse JSON or key=value", |
| 149 |
'value of format_version must be one of: 1.0.0, got: 123', |
| 150 |
'value of name must be one of: bar, got: asdf', |
| 151 |
'value of namespace must be one of: foo, got: fdsa', |
| 152 |
'value of version must be one of: 1.2.3, got: asdffdsa', |
| 153 |
], |
| 154 |
), |
| 155 |
( |
| 156 |
# Invalid data 2 |
| 157 |
"foo", |
| 158 |
"bar", |
| 159 |
"1.2.3", |
| 160 |
{ |
| 161 |
"format_version": 123, |
| 162 |
"signatures": {}, |
| 163 |
"name": "asdf", |
| 164 |
"namespace": "fdsa", |
| 165 |
"version": "asdffdsa", |
| 166 |
}, |
| 167 |
[ |
| 168 |
"argument 'signatures' is of type dict and we were unable to convert to list: <class 'dict'> cannot be converted to a list", |
| 169 |
'value of format_version must be one of: 1.0.0, got: 123', |
| 170 |
'value of name must be one of: bar, got: asdf', |
| 171 |
'value of namespace must be one of: foo, got: fdsa', |
| 172 |
'value of version must be one of: 1.2.3, got: asdffdsa', |
| 173 |
], |
| 174 |
), |
| 175 |
( |
| 176 |
# Invalid data 3 |
| 177 |
"foo", |
| 178 |
"bar", |
| 179 |
"1.2.3", |
| 180 |
{ |
| 181 |
"signatures": [ |
| 182 |
{ |
| 183 |
"foo": 1, |
| 184 |
}, |
| 185 |
{ |
| 186 |
"signature": None, |
| 187 |
}, |
| 188 |
True, |
| 189 |
], |
| 190 |
}, |
| 191 |
[ |
| 192 |
"Value 'True' in the sub parameter field 'signatures' must be a list, not 'bool'", |
| 193 |
"Elements value for option 'signatures' is of type bool and we were unable " |
| 194 |
"to convert to dict: <class 'bool'> cannot be converted to a dict", |
| 195 |
'signatures.foo. Supported parameters include: pubkey_fingerprint, pulp_created, signature, signing_service.', |
| 196 |
], |
| 197 |
), |
| 198 |
( |
| 199 |
# Unknown parameters |
| 200 |
"foo", |
| 201 |
"bar", |
| 202 |
"1.2.3", |
| 203 |
{ |
| 204 |
"asdf": 123, |
| 205 |
"meh": {}, |
| 206 |
"a": [], |
| 207 |
}, |
| 208 |
[ |
| 209 |
'a, asdf, meh. Supported parameters include: download_url, format_version, ' |
| 210 |
'name, namespace, server, signatures, version, version_url.', |
| 211 |
], |
| 212 |
), |
| 213 |
], |
| 214 |
) |
| 215 |
def test__validate_v1_source_info_schema( |
| 216 |
namespace: str, |
| 217 |
name: str, |
| 218 |
version: str, |
| 219 |
provided_arguments: dict[str, object], |
| 220 |
expected_errors: list[str], |
| 221 |
) -> None: |
| 222 |
result = _validate_v1_source_info_schema(namespace, name, version, provided_arguments) |
| 223 |
assert result == expected_errors |
| 224 |
|
| 225 |
|
| 226 |
@pytest.mark.parametrize( |
| 227 |
("provided_arguments",), |
| 228 |
[ |
| 229 |
(None,), |
| 230 |
("asdf",), |
| 231 |
(123,), |
| 232 |
(1.2,), |
| 233 |
(True,), |
| 234 |
([],), |
| 235 |
], |
| 236 |
) |
| 237 |
def test__validate_v1_source_info_schema_fail(provided_arguments: t.Any) -> None: |
| 238 |
with pytest.raises(AnsibleError, match="^Invalid offline source info for foo.bar:1.2.3, expected a dict and got "): |
| 239 |
_validate_v1_source_info_schema("foo", "bar", "1.2.3", provided_arguments) |