Debian Codegraph @

test/units/_internal/_yaml/test_dumper.py

raw
1 from __future__ import annotations
2
3 import pytest
4
5 from ansible.errors import AnsibleUndefinedVariable
6 from ansible.parsing.utils.yaml import from_yaml
7 from ansible.parsing.vault import EncryptedString
8 from ansible.template import Templar, trust_as_template, is_trusted_as_template
9 from units.mock.vault_helper import VaultTestHelper
10
11 undecryptable_value = EncryptedString(
12 ciphertext="$ANSIBLE_VAULT;1.1;AES256\n"
13 "35323961353038346165643738646465376139363061353835303739663538343266303232326635336535366264623635666532313563363065623"
14 "8316530640a663362363763633436373439663031336634333830373964386564646364336538373763613136383663623330373239613163643633"
15 "633835616438623261650a6361643765343766613931343266623263623231313739643139616233653833",
16 )
17
18
19 @pytest.mark.parametrize("filter_name", (
20 "to_yaml",
21 "to_nice_yaml",
22 ))
23 def test_yaml_dump(filter_name: str, _vault_secrets_context: VaultTestHelper) -> None:
24 """Verify YAML dumping round-trips only values which are expected to be supported."""
25 payload = dict(
26 trusted=trust_as_template('trusted'),
27 untrusted="untrusted",
28 decryptable=VaultTestHelper().make_encrypted_string("hi mom"),
29 undecryptable=undecryptable_value,
30 )
31
32 original = dict(a_list=[payload])
33 templar = Templar(variables=dict(original=original))
34 result = templar.template(trust_as_template(f"{{{{ original | {filter_name}(vault_behavior='keep_encrypted') }}}}"))
35 data = from_yaml(trust_as_template(result))
36
37 assert len(data) == len(original)
38
39 result_item = data['a_list'][0]
40 original_item = original['a_list'][0]
41
42 assert result_item['trusted'] == original_item['trusted']
43 assert result_item['untrusted'] == original_item['untrusted']
44 assert result_item['decryptable'] == original_item['decryptable']
45
46 assert is_trusted_as_template(result_item['trusted'])
47 assert is_trusted_as_template(result_item['untrusted']) # round-tripping trust is NOT supported
48
49 assert not is_trusted_as_template(result_item['decryptable'])
50
51 assert result_item['decryptable']._ciphertext == original_item['decryptable']._ciphertext
52 assert result_item['undecryptable']._ciphertext == original_item['undecryptable']._ciphertext
53
54 assert not is_trusted_as_template(result_item['undecryptable'])
55
56
57 def test_yaml_dump_undefined() -> None:
58 templar = Templar(variables=dict(dict_with_undefined=dict(undefined_value=trust_as_template("{{ bogus }}"))))
59
60 with pytest.raises(AnsibleUndefinedVariable):
61 templar.template(trust_as_template("{{ dict_with_undefined | to_yaml }}"))
62
63
64 @pytest.mark.parametrize("value, expected", (
65 ((1, 2, 3), "[1, 2, 3]\n"),
66 ({1, 2, 3}, "!!set {1: null, 2: null, 3: null}\n"),
67 ("abc", "abc\n"),
68 (b"abc", "!!binary |\n YWJj\n"),
69 ))
70 def test_yaml_dump_iterables(value: object, expected: object) -> None:
71 result = Templar(variables=dict(value=value)).template(trust_as_template("{{ value | to_yaml }}"))
72
73 assert result == expected