| 1 |
# DTFIX5: review this test for removal, should have been superseded by test_serialization_profiles.py |
| 2 |
# some tests will need to be under module_utils to verify target-only Python versions |
| 3 |
# if kept, de-dupe |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import json |
| 7 |
import typing as t |
| 8 |
import collections.abc as c |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from ansible.module_utils._internal._datatag import AnsibleTaggedObject, _untaggable_types, AnsibleTagHelper |
| 13 |
from ansible.module_utils._internal._datatag._tags import Deprecated |
| 14 |
from ansible._internal._datatag._tags import Origin, TrustedAsTemplate |
| 15 |
from ansible.module_utils.common.json import get_encoder, get_decoder |
| 16 |
from ansible.module_utils._internal._json._profiles import _module_legacy_m2c, _tagless, _module_legacy_c2m, _module_modern_c2m, _module_modern_m2c |
| 17 |
from ansible._internal._json._profiles import _legacy |
| 18 |
|
| 19 |
_simple_json_values = ( |
| 20 |
'hello', |
| 21 |
1.1, |
| 22 |
1, |
| 23 |
True, |
| 24 |
None, |
| 25 |
[1], |
| 26 |
dict(k='v'), |
| 27 |
) |
| 28 |
|
| 29 |
_converted_json_values = ( |
| 30 |
((1,), [1]), |
| 31 |
({1}, [1]), |
| 32 |
) |
| 33 |
|
| 34 |
origin = Origin(path='/absolute/path/for/testing') |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.parametrize("value, expected", _converted_json_values, ids=str) |
| 38 |
def test_modern_controller_to_module_converted_types(value: t.Any, expected: t.Any) -> None: |
| 39 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 40 |
profile = _module_modern_c2m |
| 41 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 42 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 43 |
|
| 44 |
untagged_value = result['untagged_value'] |
| 45 |
tagged_value = result['tagged_value'] |
| 46 |
|
| 47 |
assert untagged_value == expected |
| 48 |
assert tagged_value == expected |
| 49 |
|
| 50 |
assert_has_no_tags(result) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize("value, expected", _converted_json_values, ids=str) |
| 54 |
def test_legacy_controller_to_module_converted_types(value: t.Any, expected: t.Any) -> None: |
| 55 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 56 |
profile = _module_legacy_c2m |
| 57 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 58 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 59 |
|
| 60 |
untagged_value = result['untagged_value'] |
| 61 |
tagged_value = result['tagged_value'] |
| 62 |
|
| 63 |
assert untagged_value == expected |
| 64 |
assert tagged_value == expected |
| 65 |
|
| 66 |
assert_has_no_tags(result) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.mark.parametrize("value, expected", _converted_json_values, ids=str) |
| 70 |
def test_modern_module_to_controller_converted_types(value: t.Any, expected: t.Any) -> None: |
| 71 |
deprecation_tag = Deprecated(msg='go away') |
| 72 |
args = dict(untagged_value=value, tagged_value=deprecation_tag.tag(value)) |
| 73 |
profile = _module_modern_m2c |
| 74 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 75 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 76 |
|
| 77 |
untagged_value = result['untagged_value'] |
| 78 |
tagged_value = result['tagged_value'] |
| 79 |
|
| 80 |
assert untagged_value == expected |
| 81 |
assert tagged_value == expected |
| 82 |
|
| 83 |
assert_has_no_tags(untagged_value) |
| 84 |
|
| 85 |
if type(value) not in _untaggable_types: |
| 86 |
tags = AnsibleTagHelper.tags(tagged_value) |
| 87 |
|
| 88 |
assert tags == {deprecation_tag} |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.parametrize("value, expected", _converted_json_values, ids=str) |
| 92 |
def test_legacy_module_to_controller_converted_types(value: t.Any, expected: t.Any) -> None: |
| 93 |
args = dict(untagged_value=value) |
| 94 |
profile = _module_modern_m2c |
| 95 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 96 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 97 |
|
| 98 |
untagged_value = result['untagged_value'] |
| 99 |
|
| 100 |
assert untagged_value == expected |
| 101 |
|
| 102 |
assert_has_no_tags(result) |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 106 |
def test_modern_controller_to_module(value: t.Any) -> None: |
| 107 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 108 |
profile = _module_modern_c2m |
| 109 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 110 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 111 |
|
| 112 |
untagged_value = result['untagged_value'] |
| 113 |
tagged_value = result['tagged_value'] |
| 114 |
|
| 115 |
assert untagged_value == value |
| 116 |
assert tagged_value == value |
| 117 |
|
| 118 |
assert_has_no_tags(result) |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 122 |
def test_legacy_controller_to_module(value: t.Any) -> None: |
| 123 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 124 |
profile = _module_legacy_c2m |
| 125 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 126 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 127 |
|
| 128 |
untagged_value = result['untagged_value'] |
| 129 |
tagged_value = result['tagged_value'] |
| 130 |
|
| 131 |
assert untagged_value == value |
| 132 |
assert tagged_value == value |
| 133 |
|
| 134 |
assert_has_no_tags(result) |
| 135 |
|
| 136 |
|
| 137 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 138 |
def test_modern_module_to_controller(value: t.Any) -> None: |
| 139 |
deprecation_tag = Deprecated(msg='go away') |
| 140 |
args = dict( |
| 141 |
untagged_value=value, |
| 142 |
tagged_value=deprecation_tag.tag(value), |
| 143 |
# tagged_with_unwanted_tag_only=origin.tag(value), |
| 144 |
) |
| 145 |
profile = _module_modern_m2c |
| 146 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 147 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 148 |
|
| 149 |
untagged_value = result['untagged_value'] |
| 150 |
tagged_value = result['tagged_value'] |
| 151 |
# tagged_with_unwanted_tag_only = result['tagged_with_unwanted_tag_only'] |
| 152 |
|
| 153 |
assert untagged_value == value |
| 154 |
assert tagged_value == value |
| 155 |
# assert tagged_with_unwanted_tag_only == value |
| 156 |
|
| 157 |
assert_has_no_tags(untagged_value) |
| 158 |
# assert_has_no_tags(tagged_with_unwanted_tag_only) |
| 159 |
|
| 160 |
if type(value) not in _untaggable_types: |
| 161 |
tags = AnsibleTagHelper.tags(tagged_value) |
| 162 |
|
| 163 |
assert tags == {deprecation_tag} |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 167 |
def test_legacy_module_to_controller(value: t.Any) -> None: |
| 168 |
# deprecation_tag = Deprecated(msg='go away') |
| 169 |
args = dict( |
| 170 |
untagged_value=value, |
| 171 |
# tagged_value=deprecation_tag.tag(value), |
| 172 |
# tagged_with_unwanted_tag_only=origin.tag(value), |
| 173 |
) |
| 174 |
profile = _module_legacy_m2c |
| 175 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 176 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 177 |
|
| 178 |
untagged_value = result['untagged_value'] |
| 179 |
# tagged_value = result['tagged_value'] |
| 180 |
# tagged_with_unwanted_tag_only = result['tagged_with_unwanted_tag_only'] |
| 181 |
|
| 182 |
assert untagged_value == value |
| 183 |
# assert tagged_value == value |
| 184 |
# assert tagged_with_unwanted_tag_only == value |
| 185 |
|
| 186 |
assert_has_no_tags(untagged_value) |
| 187 |
# assert_has_no_tags(tagged_value) |
| 188 |
# assert_has_no_tags(tagged_with_unwanted_tag_only) |
| 189 |
|
| 190 |
|
| 191 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 192 |
def test_legacy(value: t.Any) -> None: |
| 193 |
args = dict( |
| 194 |
untagged_value=value, |
| 195 |
tagged_value=origin.tag(value), |
| 196 |
trusted_value=apply_trust(value), |
| 197 |
) |
| 198 |
profile = _legacy |
| 199 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 200 |
payload = TrustedAsTemplate().tag(payload) |
| 201 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 202 |
|
| 203 |
untagged_value = result['untagged_value'] |
| 204 |
tagged_value = result['tagged_value'] |
| 205 |
trusted_value = result['trusted_value'] |
| 206 |
|
| 207 |
assert untagged_value == value |
| 208 |
assert tagged_value == value |
| 209 |
assert trusted_value == value |
| 210 |
|
| 211 |
assert_has_no_tags(untagged_value) |
| 212 |
assert_has_no_tags(tagged_value) |
| 213 |
assert_has_trusted_strings(trusted_value) |
| 214 |
|
| 215 |
|
| 216 |
@pytest.mark.parametrize("value, expected", ( |
| 217 |
((1,), [1]), |
| 218 |
({1}, [1]), |
| 219 |
), ids=str) |
| 220 |
def test_legacy_converted_types(value: t.Any, expected: t.Any) -> None: |
| 221 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 222 |
profile = _legacy |
| 223 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 224 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 225 |
|
| 226 |
untagged_value = result['untagged_value'] |
| 227 |
tagged_value = result['tagged_value'] |
| 228 |
|
| 229 |
assert untagged_value == expected |
| 230 |
assert tagged_value == expected |
| 231 |
|
| 232 |
assert_has_no_tags(result) |
| 233 |
|
| 234 |
|
| 235 |
@pytest.mark.parametrize("value", _simple_json_values, ids=str) |
| 236 |
def test_tagless(value: t.Any) -> None: |
| 237 |
args = dict( |
| 238 |
untagged_value=value, |
| 239 |
tagged_value=origin.tag(value), |
| 240 |
trusted_value=apply_trust(value), |
| 241 |
) |
| 242 |
profile = _tagless |
| 243 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 244 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 245 |
|
| 246 |
untagged_value = result['untagged_value'] |
| 247 |
tagged_value = result['tagged_value'] |
| 248 |
trusted_value = result['trusted_value'] |
| 249 |
|
| 250 |
assert untagged_value == value |
| 251 |
assert tagged_value == value |
| 252 |
assert trusted_value == value |
| 253 |
|
| 254 |
assert_has_no_tags(result) |
| 255 |
|
| 256 |
|
| 257 |
@pytest.mark.parametrize("value, expected", _converted_json_values, ids=str) |
| 258 |
def test_tagless_converted_types(value: t.Any, expected: t.Any) -> None: |
| 259 |
args = dict(untagged_value=value, tagged_value=origin.tag(value)) |
| 260 |
profile = _tagless |
| 261 |
payload = json.dumps(args, cls=get_encoder(profile)) |
| 262 |
result = json.loads(payload, cls=get_decoder(profile)) |
| 263 |
|
| 264 |
untagged_value = result['untagged_value'] |
| 265 |
tagged_value = result['tagged_value'] |
| 266 |
|
| 267 |
assert untagged_value == expected |
| 268 |
assert tagged_value == expected |
| 269 |
|
| 270 |
assert_has_no_tags(result) |
| 271 |
|
| 272 |
|
| 273 |
def apply_trust(o: t.Any) -> t.Any: |
| 274 |
if isinstance(o, str): |
| 275 |
return TrustedAsTemplate().tag(o) |
| 276 |
|
| 277 |
if isinstance(o, dict): |
| 278 |
return {key: apply_trust(value) for key, value in o.items()} |
| 279 |
|
| 280 |
if isinstance(o, (set, list, tuple)): |
| 281 |
return type(o)(apply_trust(item) for item in o) |
| 282 |
|
| 283 |
return o |
| 284 |
|
| 285 |
|
| 286 |
def assert_has_trusted_strings(o: t.Any) -> None: |
| 287 |
assert not isinstance(o, str) or TrustedAsTemplate.is_tagged_on(o) |
| 288 |
|
| 289 |
if isinstance(o, c.Mapping): |
| 290 |
for key, value in o.items(): |
| 291 |
assert_has_no_tags(key) |
| 292 |
assert_has_trusted_strings(value) |
| 293 |
elif not isinstance(o, str) and isinstance(o, c.Iterable): |
| 294 |
for item in o: |
| 295 |
assert_has_trusted_strings(item) |
| 296 |
|
| 297 |
|
| 298 |
def assert_has_no_tags(o: t.Any) -> None: |
| 299 |
assert not isinstance(o, AnsibleTaggedObject) |
| 300 |
|
| 301 |
if isinstance(o, c.Mapping): |
| 302 |
for key, value in o.items(): |
| 303 |
assert_has_no_tags(key) |
| 304 |
assert_has_no_tags(value) |
| 305 |
elif not isinstance(o, str) and isinstance(o, c.Iterable): |
| 306 |
for item in o: |
| 307 |
assert_has_no_tags(item) |