ff47d3f766
* Make sure collection is a list if a str is given * Call field validation early on collections Because we are doing work on modifying the collections value before it is actually validated, we can validate it ourselves early to make sure the user supplies either a string or list. Dicts are not valid. The new validation allows us to simplify the _ensure_default_collection() function. And since the field is now static, we no longer need to specify a default for it, which also allows us to simplify the function. Since the default is now removed, we can also remove the sanity/ignore.txt entry for collectionsearch.py. New unit tests are added (and the existing one modified) that allow us to make sure that we throw a parser error if a user specifies something other than a string or list for the collections value everywhere it can be specified. * Revert removing the collection default The default is actually used, so restore it. * Fix unit tests in test_helpers.py affected by early collection validation
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
# (c) 2020 Ansible Project
|
|
#
|
|
# This file is part of Ansible
|
|
#
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
__metaclass__ = type
|
|
|
|
from ansible.errors import AnsibleParserError
|
|
from ansible.playbook.play import Play
|
|
from ansible.playbook.task import Task
|
|
from ansible.playbook.block import Block
|
|
from ansible.playbook.collectionsearch import CollectionSearch
|
|
|
|
import pytest
|
|
|
|
|
|
def test_collection_static_warning(capsys):
|
|
"""Test that collection name is not templated.
|
|
|
|
Also, make sure that users see the warning message for the referenced name.
|
|
"""
|
|
collection_name = "foo.{{bar}}"
|
|
p = Play.load(dict(
|
|
name="test play",
|
|
hosts=['foo'],
|
|
gather_facts=False,
|
|
connection='local',
|
|
collections=collection_name,
|
|
))
|
|
assert collection_name in p.collections
|
|
std_out, std_err = capsys.readouterr()
|
|
assert '[WARNING]: "collections" is not templatable, but we found: %s' % collection_name in std_err
|
|
assert '' == std_out
|
|
|
|
|
|
def test_collection_invalid_data_play():
|
|
"""Test that collection as a dict at the play level fails with parser error"""
|
|
collection_name = {'name': 'foo'}
|
|
with pytest.raises(AnsibleParserError):
|
|
Play.load(dict(
|
|
name="test play",
|
|
hosts=['foo'],
|
|
gather_facts=False,
|
|
connection='local',
|
|
collections=collection_name,
|
|
))
|
|
|
|
|
|
def test_collection_invalid_data_task():
|
|
"""Test that collection as a dict at the task level fails with parser error"""
|
|
collection_name = {'name': 'foo'}
|
|
with pytest.raises(AnsibleParserError):
|
|
Task.load(dict(
|
|
name="test task",
|
|
collections=collection_name,
|
|
))
|
|
|
|
|
|
def test_collection_invalid_data_block():
|
|
"""Test that collection as a dict at the block level fails with parser error"""
|
|
collection_name = {'name': 'foo'}
|
|
with pytest.raises(AnsibleParserError):
|
|
Block.load(dict(
|
|
block=[dict(name="test task", collections=collection_name)]
|
|
))
|