Debian Codegraph @

test/units/playbook/test_collectionsearch.py

raw
1 # (c) 2020 Ansible Project
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import annotations
19
20 from ansible.errors import AnsibleParserError
21 from ansible._internal._datatag._tags import TrustedAsTemplate
22 from ansible.playbook.play import Play
23 from ansible.playbook.task import Task
24 from ansible.playbook.block import Block
25 from ansible._internal._templating._engine import TemplateEngine
26
27 import pytest
28
29
30 def test_collection_static_warning(capsys):
31 """Test that collection name is not templated.
32
33 Also, make sure that users see the warning message for the referenced name.
34 """
35 collection_name = TrustedAsTemplate().tag("foo.{{bar}}")
36 p = Play.load(dict(
37 name="test play",
38 hosts=['foo'],
39 gather_facts=False,
40 connection='local',
41 collections=collection_name,
42 ))
43 templar = TemplateEngine(None, {})
44 p.post_validate(templar)
45 assert collection_name in p.collections
46 std_out, std_err = capsys.readouterr()
47 assert '[WARNING]: "collections" is not templatable, but we found' in std_err
48 assert collection_name in std_err
49 assert '' == std_out
50
51
52 def test_collection_invalid_data_play():
53 """Test that collection as a dict at the play level fails with parser error"""
54 collection_name = {'name': 'foo'}
55 with pytest.raises(AnsibleParserError):
56 Play.load(dict(
57 name="test play",
58 hosts=['foo'],
59 gather_facts=False,
60 connection='local',
61 collections=collection_name,
62 ))
63
64
65 def test_collection_invalid_data_task():
66 """Test that collection as a dict at the task level fails with parser error"""
67 collection_name = {'name': 'foo'}
68 with pytest.raises(AnsibleParserError):
69 Task.load(dict(
70 name="test task",
71 collections=collection_name,
72 ))
73
74
75 def test_collection_invalid_data_block():
76 """Test that collection as a dict at the block level fails with parser error"""
77 collection_name = {'name': 'foo'}
78 with pytest.raises(AnsibleParserError):
79 Block.load(dict(
80 block=[dict(name="test task", collections=collection_name)]
81 ))