2021-02-26 15:30:16 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2021 Ansible Project
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
from ansible.module_utils.errors import AnsibleValidationError, AnsibleValidationErrorMultiple
|
|
|
|
from ansible.module_utils.common.arg_spec import ArgumentSpecValidator, ValidationResult
|
2021-02-26 15:30:16 +01:00
|
|
|
from ansible.module_utils.common.warnings import get_deprecation_messages, get_warning_messages
|
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
# id, argument spec, parameters, expected parameters, deprecation, warning
|
2021-02-26 15:30:16 +01:00
|
|
|
ALIAS_TEST_CASES = [
|
|
|
|
(
|
|
|
|
"alias",
|
|
|
|
{'path': {'aliases': ['dir', 'directory']}},
|
|
|
|
{'dir': '/tmp'},
|
|
|
|
{
|
|
|
|
'dir': '/tmp',
|
|
|
|
'path': '/tmp',
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"alias-duplicate-warning",
|
|
|
|
{'path': {'aliases': ['dir', 'directory']}},
|
|
|
|
{
|
|
|
|
'dir': '/tmp',
|
|
|
|
'directory': '/tmp',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'dir': '/tmp',
|
|
|
|
'directory': '/tmp',
|
|
|
|
'path': '/tmp',
|
|
|
|
},
|
|
|
|
"",
|
2021-03-19 20:09:18 +01:00
|
|
|
{'alias': 'directory', 'option': 'path'},
|
2021-02-26 15:30:16 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"deprecated-alias",
|
|
|
|
{
|
|
|
|
'path': {
|
|
|
|
'aliases': ['not_yo_path'],
|
|
|
|
'deprecated_aliases': [
|
|
|
|
{
|
|
|
|
'name': 'not_yo_path',
|
|
|
|
'version': '1.7',
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{'not_yo_path': '/tmp'},
|
|
|
|
{
|
|
|
|
'path': '/tmp',
|
|
|
|
'not_yo_path': '/tmp',
|
|
|
|
},
|
2021-03-19 20:09:18 +01:00
|
|
|
{'version': '1.7', 'date': None, 'collection_name': None, 'name': 'not_yo_path'},
|
2021-02-26 15:30:16 +01:00
|
|
|
"",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
# id, argument spec, parameters, expected parameters, error
|
|
|
|
ALIAS_TEST_CASES_INVALID = [
|
|
|
|
(
|
|
|
|
"alias-invalid",
|
|
|
|
{'path': {'aliases': 'bad'}},
|
|
|
|
{},
|
|
|
|
{'path': None},
|
|
|
|
"internal error: aliases must be a list or tuple",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
# This isn't related to aliases, but it exists in the alias handling code
|
|
|
|
"default-and-required",
|
|
|
|
{'name': {'default': 'ray', 'required': True}},
|
|
|
|
{},
|
|
|
|
{'name': 'ray'},
|
|
|
|
"internal error: required and default are mutually exclusive for name",
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-02-26 15:30:16 +01:00
|
|
|
@pytest.mark.parametrize(
|
2021-03-19 20:09:18 +01:00
|
|
|
('arg_spec', 'parameters', 'expected', 'deprecation', 'warning'),
|
|
|
|
((i[1:]) for i in ALIAS_TEST_CASES),
|
2021-02-26 15:30:16 +01:00
|
|
|
ids=[i[0] for i in ALIAS_TEST_CASES]
|
|
|
|
)
|
2021-03-19 20:09:18 +01:00
|
|
|
def test_aliases(arg_spec, parameters, expected, deprecation, warning):
|
|
|
|
v = ArgumentSpecValidator(arg_spec)
|
|
|
|
result = v.validate(parameters)
|
2021-02-26 15:30:16 +01:00
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
assert isinstance(result, ValidationResult)
|
|
|
|
assert result.validated_parameters == expected
|
|
|
|
assert result.error_messages == []
|
2021-02-26 15:30:16 +01:00
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
if deprecation:
|
|
|
|
assert deprecation == result._deprecations[0]
|
2021-02-26 15:30:16 +01:00
|
|
|
else:
|
2021-03-19 20:09:18 +01:00
|
|
|
assert result._deprecations == []
|
2021-02-26 15:30:16 +01:00
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
if warning:
|
|
|
|
assert warning == result._warnings[0]
|
2021-02-26 15:30:16 +01:00
|
|
|
else:
|
2021-03-19 20:09:18 +01:00
|
|
|
assert result._warnings == []
|
2021-02-26 15:30:16 +01:00
|
|
|
|
2021-03-19 20:09:18 +01:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
('arg_spec', 'parameters', 'expected', 'error'),
|
|
|
|
((i[1:]) for i in ALIAS_TEST_CASES_INVALID),
|
|
|
|
ids=[i[0] for i in ALIAS_TEST_CASES_INVALID]
|
|
|
|
)
|
|
|
|
def test_aliases_invalid(arg_spec, parameters, expected, error):
|
|
|
|
v = ArgumentSpecValidator(arg_spec)
|
|
|
|
result = v.validate(parameters)
|
|
|
|
|
|
|
|
assert isinstance(result, ValidationResult)
|
|
|
|
assert error in result.error_messages
|
|
|
|
assert isinstance(result.errors.errors[0], AnsibleValidationError)
|
|
|
|
assert isinstance(result.errors, AnsibleValidationErrorMultiple)
|