Debian Codegraph @

test/units/parsing/yaml/test_dumper.py

raw
1 # coding: utf-8
2 # This file is part of Ansible
3 #
4 # Ansible is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # Ansible is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
16
17 from __future__ import annotations
18
19 import io
20 import pytest
21 import typing as t
22 import unittest
23
24 import yaml
25
26 from ansible.module_utils._internal._datatag import Tripwire
27 from ansible.module_utils._internal._datatag._tags import Deprecated
28 from ansible.parsing import vault
29 from ansible._internal._datatag._tags import TrustedAsTemplate
30 from ansible.parsing.yaml.loader import AnsibleLoader
31 from ansible.parsing.yaml.dumper import AnsibleDumper
32 from ansible._internal._templating._jinja_bits import _DEFAULT_UNDEF
33 from ansible._internal._templating._jinja_common import MarkerError
34
35 from ...mock.custom_types import CustomMapping, CustomSequence
36 from units.mock.yaml_helper import YamlTestUtils
37 from units.mock.vault_helper import TextVaultSecret
38
39
40 class TestAnsibleDumper(unittest.TestCase, YamlTestUtils):
41 def setUp(self):
42 self.vault_password = "hunter42"
43 vault_secret = TextVaultSecret(self.vault_password)
44 self.vault_secrets = [('vault_secret', vault_secret)]
45 self.good_vault = vault.VaultLib(self.vault_secrets)
46 self.vault = self.good_vault
47 self.stream = self._build_stream()
48 self.dumper = AnsibleDumper
49
50 def _build_stream(self, yaml_text=None):
51 text = yaml_text or u''
52 stream = io.StringIO(text)
53 return stream
54
55 def _loader(self, stream):
56 return AnsibleLoader(stream)
57
58 def test_bytes(self):
59 b_text = u'tréma'.encode('utf-8')
60 unsafe_object = TrustedAsTemplate().tag(b_text)
61 yaml_out = self._dump_string(unsafe_object, dumper=self.dumper)
62
63 stream = self._build_stream(yaml_out)
64
65 data_from_yaml = yaml.load(stream, Loader=AnsibleLoader)
66
67 result = b_text
68
69 self.assertEqual(result, data_from_yaml)
70
71 def test_unicode(self):
72 u_text = u'nöel'
73 unsafe_object = TrustedAsTemplate().tag(u_text)
74 yaml_out = self._dump_string(unsafe_object, dumper=self.dumper)
75
76 stream = self._build_stream(yaml_out)
77
78 data_from_yaml = yaml.load(stream, Loader=AnsibleLoader)
79
80 self.assertEqual(u_text, data_from_yaml)
81
82 def test_undefined(self):
83 with pytest.raises(MarkerError):
84 self._dump_string(_DEFAULT_UNDEF, dumper=self.dumper)
85
86
87 _test_tag = Deprecated(msg="test")
88
89
90 @pytest.mark.parametrize("value, expected", (
91 (CustomMapping(dict(a=1)), "a: 1"),
92 (CustomSequence([1]), "- 1"),
93 (_test_tag.tag(dict(a=1)), "a: 1"),
94 (_test_tag.tag([1]), "- 1"),
95 (_test_tag.tag(1), "1"),
96 (_test_tag.tag("Ansible"), "Ansible"),
97 ))
98 def test_dump(value: t.Any, expected: str) -> None:
99 """Verify supported types can be dumped."""
100 result = yaml.dump(value, Dumper=AnsibleDumper).strip()
101
102 assert result == expected
103
104
105 def test_dump_tripwire() -> None:
106 """Verify dumping a tripwire trips it."""
107 class Tripped(Exception):
108 pass
109
110 class CustomTripwire(Tripwire):
111 def trip(self) -> t.NoReturn:
112 raise Tripped()
113
114 with pytest.raises(Tripped):
115 yaml.dump(CustomTripwire(), Dumper=AnsibleDumper)