mirror of https://github.com/ansible/ansible.git
ansible-test validate-modules: report bad-return-value-key for return values that cannot be accessed with Jinja's dot notation (#86079)
* Report bad-return-value-key for return values that cannot be accessed with Jinja's dot notation. * Move constants into separate module. * Add test to check FORBIDDEN_DICTIONARY_KEYS against current Python's key list. * Remove unused constant. * Apply suggestions from code review. Co-authored-by: Matt Clay <matt@mystile.com> * Add type annotations. * Simplify typing. Co-authored-by: Matt Clay <matt@mystile.com> --------- Co-authored-by: Matt Clay <matt@mystile.com>pull/86186/head
parent
76f07034b3
commit
222f786f23
@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- "ansible-test validate-modules sanity test - now reports bad return value keys that cannot be used with the dot notation in Jinja expressions (https://github.com/ansible/ansible/issues/86079)."
|
||||
@ -0,0 +1,67 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2015 Matt Martz <matt@sivel.net>
|
||||
# Copyright (C) 2015 Rackspace US, Inc.
|
||||
#
|
||||
# This program 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.
|
||||
#
|
||||
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
|
||||
REJECTLIST_DIRS = frozenset(('.git', 'test', '.github', '.idea'))
|
||||
SYS_EXIT_REGEX = re.compile(r'[^#]*sys.exit\s*\(.*')
|
||||
NO_LOG_REGEX = re.compile(r'(?:pass(?!ive)|secret|token|key)', re.I)
|
||||
|
||||
# Everything that should not be used in a dictionary of a return value,
|
||||
# since it will make user's life harder.
|
||||
FORBIDDEN_DICTIONARY_KEYS = frozenset([
|
||||
'clear',
|
||||
'copy',
|
||||
'fromkeys',
|
||||
'get',
|
||||
'items',
|
||||
'keys',
|
||||
'pop',
|
||||
'popitem',
|
||||
'setdefault',
|
||||
'update',
|
||||
'values',
|
||||
])
|
||||
|
||||
|
||||
REJECTLIST_IMPORTS = {
|
||||
'requests': {
|
||||
'new_only': True,
|
||||
'error': {
|
||||
'code': 'use-module-utils-urls',
|
||||
'msg': ('requests import found, should use '
|
||||
'ansible.module_utils.urls instead')
|
||||
}
|
||||
},
|
||||
r'boto(?:\.|$)': {
|
||||
'new_only': True,
|
||||
'error': {
|
||||
'code': 'use-boto3',
|
||||
'msg': 'boto import found, new modules should use boto3'
|
||||
}
|
||||
},
|
||||
}
|
||||
SUBPROCESS_REGEX = re.compile(r'subprocess\.Po.*')
|
||||
OS_CALL_REGEX = re.compile(r'os\.call.*')
|
||||
|
||||
|
||||
PLUGINS_WITH_RETURN_VALUES = ('module', )
|
||||
PLUGINS_WITH_EXAMPLES = ('module', )
|
||||
PLUGINS_WITH_YAML_EXAMPLES = ('module', )
|
||||
@ -0,0 +1,17 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope='session')
|
||||
def inject_ansible_test_validate_modules() -> None:
|
||||
"""Make ansible_test's validate-modules available on `sys.path` for unit testing ansible-test."""
|
||||
test_lib = (
|
||||
Path(__file__).parent / ".." / ".." / ".." / ".." / ".." / ".." / ".."
|
||||
/ "lib" / "ansible_test" / "_util" / "controller" / "sanity" / "validate-modules"
|
||||
)
|
||||
sys.path.insert(0, str(test_lib))
|
||||
@ -0,0 +1,10 @@
|
||||
"""Tests for validate-module's main module."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def test_dict_members() -> None:
|
||||
from validate_modules.constants import FORBIDDEN_DICTIONARY_KEYS # type: ignore[import-not-found]
|
||||
|
||||
expected_keys = [key for key in dict.__dict__ if not key.startswith("__")]
|
||||
|
||||
assert FORBIDDEN_DICTIONARY_KEYS == frozenset(expected_keys)
|
||||
Loading…
Reference in New Issue