| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import typing as t |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from ansible._internal import _display_utils |
| 8 |
from ansible._internal._datatag._tags import Origin |
| 9 |
from ansible.module_utils._internal._datatag import AnsibleTagHelper |
| 10 |
from ansible.parsing.vault import EncryptedString |
| 11 |
from ansible.parsing.yaml.objects import _AnsibleMapping, _AnsibleUnicode, _AnsibleSequence |
| 12 |
from ansible.parsing.yaml import objects |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture(autouse=True, scope='function') |
| 16 |
def suppress_warnings() -> t.Generator[None]: |
| 17 |
with _display_utils.DeferredWarningContext(variables={}): |
| 18 |
yield |
| 19 |
|
| 20 |
|
| 21 |
def test_ansible_mapping() -> None: |
| 22 |
from ansible.parsing.yaml.objects import AnsibleMapping |
| 23 |
|
| 24 |
value = dict(a=1) |
| 25 |
result = AnsibleMapping(value) |
| 26 |
|
| 27 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 28 |
assert result == value |
| 29 |
|
| 30 |
|
| 31 |
def test_tagged_ansible_mapping() -> None: |
| 32 |
from ansible.parsing.yaml.objects import AnsibleMapping |
| 33 |
|
| 34 |
value = Origin(description='test').tag(dict(a=1)) |
| 35 |
result = AnsibleMapping(value) |
| 36 |
|
| 37 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 38 |
assert result == value |
| 39 |
assert AnsibleTagHelper.tags(result) == AnsibleTagHelper.tags(value) |
| 40 |
|
| 41 |
|
| 42 |
def test_ansible_unicode() -> None: |
| 43 |
from ansible.parsing.yaml.objects import AnsibleUnicode |
| 44 |
|
| 45 |
value = 'hello' |
| 46 |
result = AnsibleUnicode(value) |
| 47 |
|
| 48 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 49 |
assert result == value |
| 50 |
|
| 51 |
|
| 52 |
def test_tagged_ansible_unicode() -> None: |
| 53 |
from ansible.parsing.yaml.objects import AnsibleUnicode |
| 54 |
|
| 55 |
value = Origin(description='test').tag('hello') |
| 56 |
result = AnsibleUnicode(value) |
| 57 |
|
| 58 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 59 |
assert result == value |
| 60 |
assert AnsibleTagHelper.tags(result) == AnsibleTagHelper.tags(value) |
| 61 |
|
| 62 |
|
| 63 |
def test_ansible_sequence() -> None: |
| 64 |
from ansible.parsing.yaml.objects import AnsibleSequence |
| 65 |
|
| 66 |
value = [1, 2, 3] |
| 67 |
result = AnsibleSequence(value) |
| 68 |
|
| 69 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 70 |
assert result == value |
| 71 |
|
| 72 |
|
| 73 |
def test_tagged_ansible_sequence() -> None: |
| 74 |
from ansible.parsing.yaml.objects import AnsibleSequence |
| 75 |
|
| 76 |
value = Origin(description='test').tag([1, 2, 3]) |
| 77 |
result = AnsibleSequence(value) |
| 78 |
|
| 79 |
assert type(result) is type(value) # pylint: disable=unidiomatic-typecheck |
| 80 |
assert result == value |
| 81 |
assert AnsibleTagHelper.tags(result) == AnsibleTagHelper.tags(value) |
| 82 |
|
| 83 |
|
| 84 |
def test_ansible_vault_encrypted_unicode() -> None: |
| 85 |
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode |
| 86 |
|
| 87 |
value = 'ciphertext' |
| 88 |
result = AnsibleVaultEncryptedUnicode(value) |
| 89 |
|
| 90 |
assert type(result) is EncryptedString # pylint: disable=unidiomatic-typecheck |
| 91 |
assert result._ciphertext == value |
| 92 |
|
| 93 |
|
| 94 |
def test_tagged_ansible_vault_encrypted_unicode() -> None: |
| 95 |
from ansible.parsing.yaml.objects import AnsibleVaultEncryptedUnicode |
| 96 |
|
| 97 |
value = Origin(description='test').tag('ciphertext') |
| 98 |
result = AnsibleVaultEncryptedUnicode(value) |
| 99 |
|
| 100 |
assert type(result) is EncryptedString # pylint: disable=unidiomatic-typecheck |
| 101 |
assert result._ciphertext == value |
| 102 |
assert AnsibleTagHelper.tags(result) == AnsibleTagHelper.tags(value) |
| 103 |
|
| 104 |
|
| 105 |
def test_invalid_attribute() -> None: |
| 106 |
with pytest.raises(ImportError, match="cannot import name 'bogus' from 'ansible.parsing.yaml.objects'"): |
| 107 |
from ansible.parsing.yaml.objects import bogus |
| 108 |
|
| 109 |
with pytest.raises(AttributeError, match="module 'ansible.parsing.yaml.objects' has no attribute 'bogus'"): |
| 110 |
assert objects.bogus |
| 111 |
|
| 112 |
|
| 113 |
def test_non_ansible_attribute() -> None: |
| 114 |
with pytest.raises(ImportError, match="cannot import name 't' from 'ansible.parsing.yaml.objects'"): |
| 115 |
from ansible.parsing.yaml.objects import t |
| 116 |
|
| 117 |
with pytest.raises(AttributeError, match="module 'ansible.parsing.yaml.objects' has no attribute 't'"): |
| 118 |
assert objects.t |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize("target_type,args,kwargs,expected", ( |
| 122 |
(_AnsibleMapping, (), {}, {}), |
| 123 |
(_AnsibleMapping, (dict(a=1),), {}, dict(a=1)), |
| 124 |
(_AnsibleMapping, (dict(a=1),), dict(b=2), dict(a=1, b=2)), |
| 125 |
(_AnsibleUnicode, (), {}, ''), |
| 126 |
(_AnsibleUnicode, ('Hello',), {}, 'Hello'), |
| 127 |
(_AnsibleUnicode, (), dict(object='Hello'), 'Hello'), |
| 128 |
(_AnsibleUnicode, (b'Hello',), {}, str(b'Hello')), |
| 129 |
(_AnsibleUnicode, (b'Hello',), dict(encoding='utf-8', errors='strict'), 'Hello'), |
| 130 |
(_AnsibleSequence, (), {}, []), |
| 131 |
(_AnsibleSequence, ([1, 2],), {}, [1, 2]), |
| 132 |
)) |
| 133 |
def test_objects(target_type: type, args: tuple, kwargs: dict, expected: object) -> None: |
| 134 |
"""Verify legacy objects support the same constructor args as their base types.""" |
| 135 |
result = target_type(*args, **kwargs) |
| 136 |
|
| 137 |
assert isinstance(result, type(expected)) |
| 138 |
assert result == expected |