summaryrefslogtreecommitdiff
path: root/test/units/module_utils/common/arg_spec/test_module_validate.py
blob: 5041d521bd2caad9bfe1477bbb0c63985eaebe54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- 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

from ansible.module_utils.common import warnings

from ansible.module_utils.common.arg_spec import ModuleArgumentSpecValidator, ValidationResult


def test_module_validate():
    arg_spec = {'name': {}}
    parameters = {'name': 'larry'}
    expected = {'name': 'larry'}

    v = ModuleArgumentSpecValidator(arg_spec)
    result = v.validate(parameters)

    assert isinstance(result, ValidationResult)
    assert result.error_messages == []
    assert result._deprecations == []
    assert result._warnings == []
    assert result.validated_parameters == expected


def test_module_alias_deprecations_warnings(monkeypatch):
    monkeypatch.setattr(warnings, '_global_deprecations', [])

    arg_spec = {
        'path': {
            'aliases': ['source', 'src', 'flamethrower'],
            'deprecated_aliases': [{'name': 'flamethrower', 'date': '2020-03-04'}],
        },
    }
    parameters = {'flamethrower': '/tmp', 'source': '/tmp'}
    expected = {
        'path': '/tmp',
        'flamethrower': '/tmp',
        'source': '/tmp',
    }

    v = ModuleArgumentSpecValidator(arg_spec)
    result = v.validate(parameters)

    assert result.validated_parameters == expected
    assert result._deprecations == [
        {
            'collection_name': None,
            'date': '2020-03-04',
            'name': 'flamethrower',
            'version': None,
        }
    ]
    assert "Alias 'flamethrower' is deprecated" in warnings._global_deprecations[0]['msg']
    assert result._warnings == [{'alias': 'flamethrower', 'option': 'path'}]
    assert "Both option path and its alias flamethrower are set" in warnings._global_warnings[0]