| 1 |
# coding: utf-8 |
| 2 |
# (c) 2015, Toshio Kuratomi <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import collections.abc as c |
| 22 |
import datetime |
| 23 |
import typing as t |
| 24 |
|
| 25 |
import pytest |
| 26 |
import yaml |
| 27 |
|
| 28 |
from collections.abc import Sequence, Set, Mapping |
| 29 |
from io import StringIO |
| 30 |
|
| 31 |
import unittest |
| 32 |
|
| 33 |
from ansible import errors |
| 34 |
from ansible.parsing.utils.yaml import from_yaml |
| 35 |
from ansible.parsing.yaml.dumper import AnsibleDumper |
| 36 |
from ansible.parsing.yaml.loader import AnsibleLoader |
| 37 |
from ansible._internal._yaml._errors import AnsibleYAMLParserError |
| 38 |
from ansible.parsing import vault |
| 39 |
from ansible._internal._datatag._tags import Origin, TrustedAsTemplate |
| 40 |
from ansible.module_utils._internal._datatag import _untaggable_types |
| 41 |
|
| 42 |
from units.mock.yaml_helper import YamlTestUtils |
| 43 |
from units.mock.vault_helper import TextVaultSecret |
| 44 |
|
| 45 |
from yaml.parser import ParserError |
| 46 |
from yaml.scanner import ScannerError |
| 47 |
|
| 48 |
file_name = '/some/test/path/myfile.yml' |
| 49 |
|
| 50 |
|
| 51 |
class TestAnsibleLoaderBasic(unittest.TestCase): |
| 52 |
|
| 53 |
def test_parse_number(self): |
| 54 |
stream = StringIO(u""" |
| 55 |
1 |
| 56 |
""") |
| 57 |
stream.name = file_name |
| 58 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 59 |
self.assertEqual(data, 1) |
| 60 |
# No line/column info saved yet |
| 61 |
|
| 62 |
def test_parse_string(self): |
| 63 |
stream = StringIO(u""" |
| 64 |
Ansible |
| 65 |
""") |
| 66 |
stream.name = file_name |
| 67 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 68 |
self.assertEqual(data, u'Ansible') |
| 69 |
self.assertIsInstance(data, str) |
| 70 |
|
| 71 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=2, col_num=17)) |
| 72 |
|
| 73 |
def test_parse_utf8_string(self): |
| 74 |
stream = StringIO(u""" |
| 75 |
Cafè Eñyei |
| 76 |
""") |
| 77 |
stream.name = file_name |
| 78 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 79 |
self.assertEqual(data, u'Cafè Eñyei') |
| 80 |
self.assertIsInstance(data, str) |
| 81 |
|
| 82 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=2, col_num=17)) |
| 83 |
|
| 84 |
def test_parse_dict(self): |
| 85 |
stream = StringIO(u""" |
| 86 |
webster: daniel |
| 87 |
oed: oxford |
| 88 |
""") |
| 89 |
stream.name = file_name |
| 90 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 91 |
self.assertEqual(data, {'webster': 'daniel', 'oed': 'oxford'}) |
| 92 |
self.assertEqual(len(data), 2) |
| 93 |
self.assertIsInstance(list(data.keys())[0], str) |
| 94 |
self.assertIsInstance(list(data.values())[0], str) |
| 95 |
|
| 96 |
# Beginning of the first key |
| 97 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=2, col_num=17)) |
| 98 |
|
| 99 |
self.assertEqual(Origin.get_tag(data[u'webster']), Origin(path=file_name, line_num=2, col_num=26)) |
| 100 |
self.assertEqual(Origin.get_tag(data[u'oed']), Origin(path=file_name, line_num=3, col_num=22)) |
| 101 |
|
| 102 |
def test_parse_list(self): |
| 103 |
stream = StringIO(u""" |
| 104 |
- a |
| 105 |
- b |
| 106 |
""") |
| 107 |
stream.name = file_name |
| 108 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 109 |
self.assertEqual(data, [u'a', u'b']) |
| 110 |
self.assertEqual(len(data), 2) |
| 111 |
self.assertIsInstance(data[0], str) |
| 112 |
|
| 113 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=2, col_num=17)) |
| 114 |
|
| 115 |
self.assertEqual(Origin.get_tag(data[0]), Origin(path=file_name, line_num=2, col_num=19)) |
| 116 |
self.assertEqual(Origin.get_tag(data[1]), Origin(path=file_name, line_num=3, col_num=19)) |
| 117 |
|
| 118 |
def test_parse_short_dict(self): |
| 119 |
stream = StringIO(u"""{"foo": "bar"}""") |
| 120 |
stream.name = file_name |
| 121 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 122 |
self.assertEqual(data, dict(foo=u'bar')) |
| 123 |
|
| 124 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=1, col_num=1)) |
| 125 |
self.assertEqual(Origin.get_tag(data[u'foo']), Origin(path=file_name, line_num=1, col_num=9)) |
| 126 |
|
| 127 |
stream = StringIO(u"""foo: bar""") |
| 128 |
stream.name = file_name |
| 129 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 130 |
self.assertEqual(data, dict(foo=u'bar')) |
| 131 |
|
| 132 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=1, col_num=1)) |
| 133 |
self.assertEqual(Origin.get_tag(data[u'foo']), Origin(path=file_name, line_num=1, col_num=6)) |
| 134 |
|
| 135 |
def test_error_conditions(self): |
| 136 |
stream = StringIO(u"""{""") |
| 137 |
stream.name = file_name |
| 138 |
with self.assertRaises(ParserError): |
| 139 |
yaml.load(stream, Loader=AnsibleLoader) |
| 140 |
|
| 141 |
def test_tab_error(self): |
| 142 |
stream = StringIO(u"""---\nhosts: localhost\nvars:\n foo: bar\n\tblip: baz""") |
| 143 |
stream.name = file_name |
| 144 |
with self.assertRaises(ScannerError): |
| 145 |
yaml.load(stream, Loader=AnsibleLoader) |
| 146 |
|
| 147 |
def test_front_matter(self): |
| 148 |
stream = StringIO(u"""---\nfoo: bar""") |
| 149 |
stream.name = file_name |
| 150 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 151 |
self.assertEqual(data, dict(foo=u'bar')) |
| 152 |
|
| 153 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=2, col_num=1)) |
| 154 |
self.assertEqual(Origin.get_tag(data[u'foo']), Origin(path=file_name, line_num=2, col_num=6)) |
| 155 |
|
| 156 |
# Initial indent (See: #6348) |
| 157 |
stream = StringIO(u""" - foo: bar\n baz: qux""") |
| 158 |
stream.name = file_name |
| 159 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 160 |
self.assertEqual(data, [{u'foo': u'bar', u'baz': u'qux'}]) |
| 161 |
|
| 162 |
self.assertEqual(Origin.get_tag(data), Origin(path=file_name, line_num=1, col_num=2)) |
| 163 |
self.assertEqual(Origin.get_tag(data[0]), Origin(path=file_name, line_num=1, col_num=4)) |
| 164 |
self.assertEqual(Origin.get_tag(data[0][u'foo']), Origin(path=file_name, line_num=1, col_num=9)) |
| 165 |
self.assertEqual(Origin.get_tag(data[0][u'baz']), Origin(path=file_name, line_num=2, col_num=9)) |
| 166 |
|
| 167 |
|
| 168 |
class TestAnsibleLoaderVault(unittest.TestCase, YamlTestUtils): |
| 169 |
def setUp(self): |
| 170 |
self.vault_password = "hunter42" |
| 171 |
vault_secret = TextVaultSecret(self.vault_password) |
| 172 |
self.vault_secrets = [('vault_secret', vault_secret), |
| 173 |
('default', vault_secret)] |
| 174 |
self.vault = vault.VaultLib(self.vault_secrets) |
| 175 |
|
| 176 |
@property |
| 177 |
def vault_secret(self): |
| 178 |
return vault.match_encrypt_secret(self.vault_secrets)[1] |
| 179 |
|
| 180 |
def test_wrong_password(self): |
| 181 |
plaintext = u"Ansible" |
| 182 |
bob_password = "this is a different password" |
| 183 |
|
| 184 |
bobs_secret = TextVaultSecret(bob_password) |
| 185 |
bobs_secrets = [('default', bobs_secret)] |
| 186 |
|
| 187 |
bobs_vault = vault.VaultLib(bobs_secrets) |
| 188 |
|
| 189 |
ciphertext = bobs_vault.encrypt(plaintext, vault.match_encrypt_secret(bobs_secrets)[1]) |
| 190 |
|
| 191 |
try: |
| 192 |
self.vault.decrypt(ciphertext) |
| 193 |
except Exception as e: |
| 194 |
self.assertIsInstance(e, errors.AnsibleError) |
| 195 |
self.assertEqual(e.message, 'Decryption failed (no vault secrets were found that could decrypt).') |
| 196 |
|
| 197 |
def _encrypt_plaintext(self, plaintext): |
| 198 |
# Construct a yaml repr of a vault by hand |
| 199 |
vaulted_var_bytes = self.vault.encrypt(plaintext, self.vault_secret) |
| 200 |
|
| 201 |
# add yaml tag |
| 202 |
vaulted_var = vaulted_var_bytes.decode() |
| 203 |
lines = vaulted_var.splitlines() |
| 204 |
lines2 = [] |
| 205 |
for line in lines: |
| 206 |
lines2.append(' %s' % line) |
| 207 |
|
| 208 |
vaulted_var = '\n'.join(lines2) |
| 209 |
tagged_vaulted_var = u"""!vault |\n%s""" % vaulted_var |
| 210 |
return tagged_vaulted_var |
| 211 |
|
| 212 |
def _build_stream(self, yaml_text): |
| 213 |
stream = StringIO(yaml_text) |
| 214 |
stream.name = 'my.yml' |
| 215 |
return stream |
| 216 |
|
| 217 |
def _load_yaml(self, yaml_text, password): |
| 218 |
stream = self._build_stream(yaml_text) |
| 219 |
data = yaml.load(stream, Loader=AnsibleLoader) |
| 220 |
|
| 221 |
return data |
| 222 |
|
| 223 |
|
| 224 |
class TestAnsibleLoaderPlay(unittest.TestCase): |
| 225 |
|
| 226 |
def setUp(self): |
| 227 |
stream = StringIO(""" |
| 228 |
- hosts: localhost |
| 229 |
vars: |
| 230 |
number: 1 |
| 231 |
string: Ansible |
| 232 |
utf8_string: Cafè Eñyei |
| 233 |
dictionary: |
| 234 |
webster: daniel |
| 235 |
oed: oxford |
| 236 |
list: |
| 237 |
- a |
| 238 |
- b |
| 239 |
- 1 |
| 240 |
- 2 |
| 241 |
tasks: |
| 242 |
- name: Test case |
| 243 |
ping: |
| 244 |
data: "{{ utf8_string }}" |
| 245 |
|
| 246 |
- name: Test 2 |
| 247 |
ping: |
| 248 |
data: "Cafè Eñyei" |
| 249 |
|
| 250 |
- name: Test 3 |
| 251 |
command: "printf 'Cafè Eñyei\\n'" |
| 252 |
vars: |
| 253 |
not_safe: !unsafe "{{ sorry }}" |
| 254 |
also_not_safe: !unsafe ["{{ sorry }}"] |
| 255 |
""") |
| 256 |
self.play_filename = '/path/to/myplay.yml' |
| 257 |
stream.name = self.play_filename |
| 258 |
self.data = yaml.load(stream, Loader=AnsibleLoader) |
| 259 |
|
| 260 |
def tearDown(self): |
| 261 |
pass |
| 262 |
|
| 263 |
def test_data_complete(self): |
| 264 |
self.assertEqual(len(self.data), 1) |
| 265 |
self.assertIsInstance(self.data, list) |
| 266 |
self.assertEqual(frozenset(self.data[0].keys()), frozenset((u'hosts', u'vars', u'tasks'))) |
| 267 |
|
| 268 |
self.assertEqual(self.data[0][u'hosts'], u'localhost') |
| 269 |
|
| 270 |
self.assertEqual(self.data[0][u'vars'][u'number'], 1) |
| 271 |
self.assertEqual(self.data[0][u'vars'][u'string'], u'Ansible') |
| 272 |
self.assertEqual(self.data[0][u'vars'][u'utf8_string'], u'Cafè Eñyei') |
| 273 |
self.assertEqual(self.data[0][u'vars'][u'dictionary'], { |
| 274 |
u'webster': u'daniel', |
| 275 |
u'oed': u'oxford' |
| 276 |
}) |
| 277 |
self.assertEqual(self.data[0][u'vars'][u'list'], [u'a', u'b', 1, 2]) |
| 278 |
|
| 279 |
self.assertEqual(self.data[0][u'tasks'], [ |
| 280 |
{u'name': u'Test case', u'ping': {u'data': u'{{ utf8_string }}'}}, |
| 281 |
{u'name': u'Test 2', u'ping': {u'data': u'Cafè Eñyei'}}, |
| 282 |
{u'name': u'Test 3', u'command': u'printf \'Cafè Eñyei\n\'', 'vars': {'not_safe': "{{ sorry }}", 'also_not_safe': ["{{ sorry }}"]}}, |
| 283 |
]) |
| 284 |
|
| 285 |
def walk(self, data): |
| 286 |
# Make sure there's no str in the data |
| 287 |
self.assertNotIsInstance(data, bytes) |
| 288 |
|
| 289 |
# Descend into various container types |
| 290 |
if isinstance(data, str): |
| 291 |
# strings are a sequence so we have to be explicit here |
| 292 |
return |
| 293 |
elif isinstance(data, (Sequence, Set)): |
| 294 |
for element in data: |
| 295 |
self.walk(element) |
| 296 |
elif isinstance(data, Mapping): |
| 297 |
for k, v in data.items(): |
| 298 |
self.walk(k) |
| 299 |
self.walk(v) |
| 300 |
|
| 301 |
# Scalars were all checked so we're good to go |
| 302 |
return |
| 303 |
|
| 304 |
def test_no_str_in_data(self): |
| 305 |
# Checks that no strings are str type |
| 306 |
self.walk(self.data) |
| 307 |
|
| 308 |
def check_vars(self): |
| 309 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'number']), Origin(path=self.play_filename, line_num=4, col_num=29)) |
| 310 |
|
| 311 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'string']), Origin(path=self.play_filename, line_num=5, col_num=29)) |
| 312 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'utf8_string']), Origin(path=self.play_filename, line_num=6, col_num=34)) |
| 313 |
|
| 314 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'dictionary']), Origin(path=self.play_filename, line_num=8, col_num=23)) |
| 315 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'dictionary'][u'webster']), |
| 316 |
Origin(path=self.play_filename, line_num=8, col_num=32)) |
| 317 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'dictionary'][u'oed']), |
| 318 |
Origin(path=self.play_filename, line_num=9, col_num=28)) |
| 319 |
|
| 320 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'list']), Origin(path=self.play_filename, line_num=11, col_num=23)) |
| 321 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'list'][0]), Origin(path=self.play_filename, line_num=11, col_num=25)) |
| 322 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'list'][1]), Origin(path=self.play_filename, line_num=12, col_num=25)) |
| 323 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'list'][2]), Origin(path=self.play_filename, line_num=13, col_num=25)) |
| 324 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars'][u'list'][3]), Origin(path=self.play_filename, line_num=14, col_num=25)) |
| 325 |
|
| 326 |
def check_tasks(self): |
| 327 |
# |
| 328 |
# First Task |
| 329 |
# |
| 330 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][0]), Origin(path=self.play_filename, line_num=16, col_num=23)) |
| 331 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][0][u'name']), Origin(path=self.play_filename, line_num=16, col_num=29)) |
| 332 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][0][u'ping']), Origin(path=self.play_filename, line_num=18, col_num=25)) |
| 333 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][0][u'ping'][u'data']), |
| 334 |
Origin(path=self.play_filename, line_num=18, col_num=31)) |
| 335 |
|
| 336 |
# |
| 337 |
# Second Task |
| 338 |
# |
| 339 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][1]), Origin(path=self.play_filename, line_num=20, col_num=23)) |
| 340 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][1][u'name']), Origin(path=self.play_filename, line_num=20, col_num=29)) |
| 341 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][1][u'ping']), Origin(path=self.play_filename, line_num=22, col_num=25)) |
| 342 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][1][u'ping'][u'data']), |
| 343 |
Origin(path=self.play_filename, line_num=22, col_num=31)) |
| 344 |
|
| 345 |
# |
| 346 |
# Third Task |
| 347 |
# |
| 348 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][2]), Origin(path=self.play_filename, line_num=24, col_num=23)) |
| 349 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][2][u'name']), Origin(path=self.play_filename, line_num=24, col_num=29)) |
| 350 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks'][2][u'command']), Origin(path=self.play_filename, line_num=25, col_num=32)) |
| 351 |
|
| 352 |
not_safe = self.data[0][u'tasks'][2][u'vars']['not_safe'] |
| 353 |
also_not_safe = self.data[0][u'tasks'][2][u'vars']['also_not_safe'] |
| 354 |
|
| 355 |
assert Origin.get_tag(not_safe) == Origin(path=self.play_filename, line_num=27, col_num=35) |
| 356 |
assert Origin.get_tag(also_not_safe) == Origin(path=self.play_filename, line_num=28, col_num=40) |
| 357 |
assert Origin.get_tag(also_not_safe) == Origin(path=self.play_filename, line_num=28, col_num=40) |
| 358 |
|
| 359 |
assert not TrustedAsTemplate.is_tagged_on(not_safe) |
| 360 |
assert not TrustedAsTemplate.is_tagged_on(also_not_safe) |
| 361 |
assert not TrustedAsTemplate.is_tagged_on(also_not_safe[0]) |
| 362 |
|
| 363 |
def test_line_numbers(self): |
| 364 |
# Check the line/column numbers are correct |
| 365 |
# Note: Remember, currently dicts begin at the start of their first entry |
| 366 |
self.assertEqual(Origin.get_tag(self.data[0]), Origin(path=self.play_filename, line_num=2, col_num=19)) |
| 367 |
self.assertEqual(Origin.get_tag(self.data[0][u'hosts']), Origin(path=self.play_filename, line_num=2, col_num=26)) |
| 368 |
self.assertEqual(Origin.get_tag(self.data[0][u'vars']), Origin(path=self.play_filename, line_num=4, col_num=21)) |
| 369 |
|
| 370 |
self.check_vars() |
| 371 |
|
| 372 |
self.assertEqual(Origin.get_tag(self.data[0][u'tasks']), Origin(path=self.play_filename, line_num=16, col_num=21)) |
| 373 |
|
| 374 |
self.check_tasks() |
| 375 |
|
| 376 |
|
| 377 |
@pytest.mark.parametrize("value", ( |
| 378 |
"[1]", |
| 379 |
"{a: 1}", |
| 380 |
"1", |
| 381 |
"1.1", |
| 382 |
"true", |
| 383 |
"", |
| 384 |
"~", |
| 385 |
)) |
| 386 |
def test_vault_tag_type_validation(value: str) -> None: |
| 387 |
with pytest.raises(AnsibleYAMLParserError) as error: |
| 388 |
from_yaml(f"!vault {value}") |
| 389 |
|
| 390 |
assert "requires a string value" in str(error.value) |
| 391 |
|
| 392 |
|
| 393 |
class CustomMapping(c.Mapping): |
| 394 |
def __init__(self, data: dict) -> None: |
| 395 |
self._data = data |
| 396 |
|
| 397 |
def __getitem__(self, __key): |
| 398 |
return self._data[__key] |
| 399 |
|
| 400 |
def __len__(self): |
| 401 |
return len(self._data) |
| 402 |
|
| 403 |
def __iter__(self): |
| 404 |
return iter(self._data) |
| 405 |
|
| 406 |
def __repr__(self): |
| 407 |
return f'{type(self).__name__}({self._data!r})' |
| 408 |
|
| 409 |
|
| 410 |
YAML_STRINGS_AND_VALUES = ( |
| 411 |
("test", "test"), |
| 412 |
("123", 123), |
| 413 |
("1.234", 1.234), |
| 414 |
("true", True), |
| 415 |
("false", False), |
| 416 |
("{foo: bar}", dict(foo="bar")), |
| 417 |
("[1, 2, 3]", [1, 2, 3]), |
| 418 |
("!!set {bar: null, foo: null}", {"foo", "bar"}), |
| 419 |
("2024-01-01", datetime.date(2024, 1, 1)), |
| 420 |
("2024-01-01 12:01:01", datetime.datetime(2024, 1, 1, 12, 1, 1)), |
| 421 |
("null", None), |
| 422 |
("!!binary |\n aGVsbG8=", b'hello'), |
| 423 |
) |
| 424 |
"""These values can be round-tripped through YAML.""" |
| 425 |
|
| 426 |
YAML_STRINGS_FROM_VALUES = ( |
| 427 |
("{a: 1}", CustomMapping(dict(a=1))), |
| 428 |
) |
| 429 |
"""These values can be dumped to YAML, but are converted to another type in the process, so they can't be loaded as the original type that was dumped.""" |
| 430 |
|
| 431 |
|
| 432 |
@pytest.mark.parametrize("value, expected", YAML_STRINGS_AND_VALUES) |
| 433 |
def test_load_data_types(value: str, expected: t.Any) -> None: |
| 434 |
"""Verify supported data types can be YAML loaded.""" |
| 435 |
value = Origin(path=file_name).tag(value) |
| 436 |
result = from_yaml(value) |
| 437 |
origin = Origin.get_tag(result) |
| 438 |
|
| 439 |
assert result == expected |
| 440 |
|
| 441 |
if type(result) in _untaggable_types: |
| 442 |
assert origin is None |
| 443 |
else: |
| 444 |
assert origin.path == file_name |
| 445 |
|
| 446 |
|
| 447 |
@pytest.mark.parametrize("expected, value", YAML_STRINGS_AND_VALUES + YAML_STRINGS_FROM_VALUES) |
| 448 |
def test_dump_data_types(value: str, expected: t.Any) -> None: |
| 449 |
"""Verify supported data types can be YAML dumped.""" |
| 450 |
result = yaml.dump(value, Dumper=AnsibleDumper, default_flow_style=True).rstrip() |
| 451 |
|
| 452 |
assert result == expected |
| 453 |
|
| 454 |
|
| 455 |
@pytest.mark.parametrize("trust_input_str", ( |
| 456 |
True, |
| 457 |
False, |
| 458 |
)) |
| 459 |
def test_string_trust_propagation(trust_input_str: bool) -> None: |
| 460 |
""" |
| 461 |
Verify that input trust propagation behaves as expected. An explicit boolean `trusted_as_template` arg to the loader is always |
| 462 |
respected; if not specified, the presence of trust on the input string determines if trust is applied to outputs. |
| 463 |
""" |
| 464 |
data = "foo: bar" |
| 465 |
|
| 466 |
if trust_input_str: |
| 467 |
data = TrustedAsTemplate().tag(data) |
| 468 |
|
| 469 |
res = yaml.load(data, Loader=AnsibleLoader) # type: ignore[arg-type] |
| 470 |
|
| 471 |
assert trust_input_str == TrustedAsTemplate.is_tagged_on(res['foo']) |