ansible-test - Fix collection loader import. (#76986)

* ansible-test - Fix collection loader import.

Resolves https://github.com/ansible/ansible/issues/76960
pull/76991/head
Matt Clay 2 years ago committed by GitHub
parent 699ecb8308
commit 0d40423f1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Fix the ``validate-modules`` sanity test to avoid double-loading the collection loader and possibly failing on import of the ``packaging`` module.

@ -0,0 +1,8 @@
function Validate {
<#
.SYNOPSIS
validate
#>
}
Export-ModuleMember -Function "Validate"

@ -0,0 +1,8 @@
#!powershell
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell ..module_utils.validate
$module = [Ansible.Basic.AnsibleModule]::Create($args, @{})
$module.ExitJson()

@ -0,0 +1,14 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = r'''
module: validate
short_description: validate
description: validate
author: "validate (@validate)"
'''
EXAMPLES = r'''
'''
RETURN = r'''
'''

@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -eux -o pipefail
cp -a "${TEST_DIR}/ansible_collections" "${WORK_DIR}"
cd "${WORK_DIR}/ansible_collections/ns/ps_only"
if ! command -V pwsh; then
echo "skipping test since pwsh is not available"
exit 0
fi
# Use a PowerShell-only collection to verify that validate-modules does not load the collection loader multiple times.
ansible-test sanity --test validate-modules --color --truncate 0 "${@}"

@ -29,32 +29,57 @@ import subprocess
import sys import sys
import tempfile import tempfile
import traceback import traceback
import warnings
from collections import OrderedDict from collections import OrderedDict
from contextlib import contextmanager from contextlib import contextmanager
from ansible.module_utils.compat.version import StrictVersion, LooseVersion
from fnmatch import fnmatch from fnmatch import fnmatch
import yaml import yaml
from voluptuous.humanize import humanize_error
def setup_collection_loader():
"""
Configure the collection loader if a collection is being tested.
This must be done before the plugin loader is imported.
"""
if '--collection' not in sys.argv:
return
# noinspection PyProtectedMember
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
collections_paths = os.environ.get('ANSIBLE_COLLECTIONS_PATH', '').split(os.pathsep)
collection_loader = _AnsibleCollectionFinder(collections_paths)
# noinspection PyProtectedMember
collection_loader._install() # pylint: disable=protected-access
warnings.filterwarnings(
"ignore",
"AnsibleCollectionFinder has already been configured")
setup_collection_loader()
from ansible import __version__ as ansible_version from ansible import __version__ as ansible_version
from ansible.executor.module_common import REPLACER_WINDOWS, NEW_STYLE_PYTHON_MODULE_RE from ansible.executor.module_common import REPLACER_WINDOWS, NEW_STYLE_PYTHON_MODULE_RE
from ansible.module_utils.common._collections_compat import Mapping from ansible.module_utils.common._collections_compat import Mapping
from ansible.module_utils.common.parameters import DEFAULT_TYPE_VALIDATORS from ansible.module_utils.common.parameters import DEFAULT_TYPE_VALIDATORS
from ansible.module_utils.compat.version import StrictVersion, LooseVersion
from ansible.module_utils.basic import to_bytes
from ansible.module_utils.six import PY3, with_metaclass, string_types
from ansible.plugins.loader import fragment_loader from ansible.plugins.loader import fragment_loader
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
from ansible.utils.plugin_docs import REJECTLIST, add_collection_to_versions_and_dates, add_fragments, get_docstring from ansible.utils.plugin_docs import REJECTLIST, add_collection_to_versions_and_dates, add_fragments, get_docstring
from ansible.utils.version import SemanticVersion from ansible.utils.version import SemanticVersion
from ansible.module_utils.basic import to_bytes
from .module_args import AnsibleModuleImportError, AnsibleModuleNotInitialized, get_argument_spec from .module_args import AnsibleModuleImportError, AnsibleModuleNotInitialized, get_argument_spec
from .schema import ansible_module_kwargs_schema, doc_schema, return_schema from .schema import ansible_module_kwargs_schema, doc_schema, return_schema
from .utils import CaptureStd, NoArgsAnsibleModule, compare_unordered_lists, is_empty, parse_yaml, parse_isodate from .utils import CaptureStd, NoArgsAnsibleModule, compare_unordered_lists, is_empty, parse_yaml, parse_isodate
from voluptuous.humanize import humanize_error
from ansible.module_utils.six import PY3, with_metaclass, string_types
if PY3: if PY3:
# Because there is no ast.TryExcept in Python 3 ast module # Because there is no ast.TryExcept in Python 3 ast module
@ -2246,11 +2271,6 @@ class PythonPackageValidator(Validator):
) )
def setup_collection_loader():
collections_paths = os.environ.get('ANSIBLE_COLLECTIONS_PATH', '').split(os.pathsep)
_AnsibleCollectionFinder(collections_paths)
def re_compile(value): def re_compile(value):
""" """
Argparse expects things to raise TypeError, re.compile raises an re.error Argparse expects things to raise TypeError, re.compile raises an re.error
@ -2303,7 +2323,6 @@ def run():
routing = None routing = None
if args.collection: if args.collection:
setup_collection_loader()
routing_file = 'meta/runtime.yml' routing_file = 'meta/runtime.yml'
# Load meta/runtime.yml if it exists, as it may contain deprecation information # Load meta/runtime.yml if it exists, as it may contain deprecation information
if os.path.isfile(routing_file): if os.path.isfile(routing_file):

Loading…
Cancel
Save