| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2021 Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from ansible.module_utils.errors import AnsibleValidationError, AnsibleValidationErrorMultiple |
| 10 |
from ansible.module_utils.common.arg_spec import ArgumentSpecValidator, ValidationResult |
| 11 |
|
| 12 |
# id, argument spec, parameters, expected parameters, deprecation, warning |
| 13 |
ALIAS_TEST_CASES = [ |
| 14 |
( |
| 15 |
"alias", |
| 16 |
{'path': {'aliases': ['dir', 'directory']}}, |
| 17 |
{'dir': '/tmp'}, |
| 18 |
{ |
| 19 |
'dir': '/tmp', |
| 20 |
'path': '/tmp', |
| 21 |
}, |
| 22 |
"", |
| 23 |
"", |
| 24 |
), |
| 25 |
( |
| 26 |
"alias-duplicate-warning", |
| 27 |
{'path': {'aliases': ['dir', 'directory']}}, |
| 28 |
{ |
| 29 |
'dir': '/tmp', |
| 30 |
'directory': '/tmp', |
| 31 |
}, |
| 32 |
{ |
| 33 |
'dir': '/tmp', |
| 34 |
'directory': '/tmp', |
| 35 |
'path': '/tmp', |
| 36 |
}, |
| 37 |
"", |
| 38 |
{'alias': 'directory', 'option': 'path'}, |
| 39 |
), |
| 40 |
( |
| 41 |
"deprecated-alias", |
| 42 |
{ |
| 43 |
'path': { |
| 44 |
'aliases': ['not_yo_path'], |
| 45 |
'deprecated_aliases': [ |
| 46 |
{ |
| 47 |
'name': 'not_yo_path', |
| 48 |
'version': '1.7', |
| 49 |
} |
| 50 |
] |
| 51 |
} |
| 52 |
}, |
| 53 |
{'not_yo_path': '/tmp'}, |
| 54 |
{ |
| 55 |
'path': '/tmp', |
| 56 |
'not_yo_path': '/tmp', |
| 57 |
}, |
| 58 |
{ |
| 59 |
'version': '1.7', |
| 60 |
'date': None, |
| 61 |
'collection_name': None, |
| 62 |
'msg': "Alias 'not_yo_path' is deprecated. See the module docs for more information", |
| 63 |
}, |
| 64 |
"", |
| 65 |
) |
| 66 |
] |
| 67 |
|
| 68 |
|
| 69 |
# id, argument spec, parameters, expected parameters, error |
| 70 |
ALIAS_TEST_CASES_INVALID: list[tuple[str, dict, dict, dict, str]] = [ |
| 71 |
( |
| 72 |
"alias-invalid", |
| 73 |
{'path': {'aliases': 'bad'}}, |
| 74 |
{}, |
| 75 |
{'path': None}, |
| 76 |
"internal error: aliases must be a list or tuple", |
| 77 |
), |
| 78 |
( |
| 79 |
# This isn't related to aliases, but it exists in the alias handling code |
| 80 |
"default-and-required", |
| 81 |
{'name': {'default': 'ray', 'required': True}}, |
| 82 |
{}, |
| 83 |
{'name': 'ray'}, |
| 84 |
"internal error: required and default are mutually exclusive for name", |
| 85 |
), |
| 86 |
] |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize( |
| 90 |
('arg_spec', 'parameters', 'expected', 'deprecation', 'warning'), |
| 91 |
[i[1:] for i in ALIAS_TEST_CASES], |
| 92 |
ids=[i[0] for i in ALIAS_TEST_CASES] |
| 93 |
) |
| 94 |
def test_aliases(arg_spec, parameters, expected, deprecation, warning): |
| 95 |
v = ArgumentSpecValidator(arg_spec) |
| 96 |
result = v.validate(parameters) |
| 97 |
|
| 98 |
assert isinstance(result, ValidationResult) |
| 99 |
assert result.validated_parameters == expected |
| 100 |
assert result.error_messages == [] |
| 101 |
assert result._aliases == { |
| 102 |
alias: param |
| 103 |
for param, value in arg_spec.items() |
| 104 |
for alias in value.get("aliases", []) |
| 105 |
} |
| 106 |
|
| 107 |
if deprecation: |
| 108 |
assert deprecation == result._deprecations[0] |
| 109 |
else: |
| 110 |
assert result._deprecations == [] |
| 111 |
|
| 112 |
if warning: |
| 113 |
assert warning == result._warnings[0] |
| 114 |
else: |
| 115 |
assert result._warnings == [] |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize( |
| 119 |
('arg_spec', 'parameters', 'expected', 'error'), |
| 120 |
[i[1:] for i in ALIAS_TEST_CASES_INVALID], |
| 121 |
ids=[i[0] for i in ALIAS_TEST_CASES_INVALID] |
| 122 |
) |
| 123 |
def test_aliases_invalid(arg_spec, parameters, expected, error): |
| 124 |
v = ArgumentSpecValidator(arg_spec) |
| 125 |
result = v.validate(parameters) |
| 126 |
|
| 127 |
assert isinstance(result, ValidationResult) |
| 128 |
assert error in result.error_messages |
| 129 |
assert isinstance(result.errors.errors[0], AnsibleValidationError) |
| 130 |
assert isinstance(result.errors, AnsibleValidationErrorMultiple) |