2017-01-27 20:53:02 +01:00
|
|
|
# (c) 2017, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible 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.
|
|
|
|
#
|
|
|
|
# Ansible 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 Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2019-08-28 03:11:21 +02:00
|
|
|
import os
|
2017-05-30 19:05:19 +02:00
|
|
|
import pytest
|
2017-01-27 20:53:02 +01:00
|
|
|
import zipfile
|
2017-05-30 19:05:19 +02:00
|
|
|
|
2017-01-27 20:53:02 +01:00
|
|
|
from collections import namedtuple
|
2019-04-25 17:28:18 +02:00
|
|
|
from io import BytesIO
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
import ansible.errors
|
|
|
|
|
|
|
|
from ansible.executor.module_common import recursive_finder
|
2017-03-23 21:35:05 +01:00
|
|
|
from ansible.module_utils.six import PY2
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
|
2018-08-24 05:54:40 +02:00
|
|
|
# These are the modules that are brought in by module_utils/basic.py This may need to be updated
|
|
|
|
# when basic.py gains new imports
|
|
|
|
# We will remove these when we modify AnsiBallZ to store its args in a separate file instead of in
|
|
|
|
# basic.py
|
2020-07-17 02:57:47 +02:00
|
|
|
|
|
|
|
MODULE_UTILS_BASIC_FILES = frozenset(('ansible/__init__.py',
|
|
|
|
'ansible/module_utils/__init__.py',
|
|
|
|
'ansible/module_utils/_text.py',
|
2018-08-24 05:54:40 +02:00
|
|
|
'ansible/module_utils/basic.py',
|
2019-02-28 22:43:19 +01:00
|
|
|
'ansible/module_utils/six/__init__.py',
|
|
|
|
'ansible/module_utils/_text.py',
|
2019-01-01 00:10:51 +01:00
|
|
|
'ansible/module_utils/common/_collections_compat.py',
|
2019-03-21 14:40:19 +01:00
|
|
|
'ansible/module_utils/common/_json_compat.py',
|
2019-02-28 22:43:19 +01:00
|
|
|
'ansible/module_utils/common/collections.py',
|
|
|
|
'ansible/module_utils/common/parameters.py',
|
2020-01-28 18:12:14 +01:00
|
|
|
'ansible/module_utils/common/warnings.py',
|
2019-02-28 22:43:19 +01:00
|
|
|
'ansible/module_utils/parsing/convert_bool.py',
|
|
|
|
'ansible/module_utils/common/__init__.py',
|
2018-08-24 05:54:40 +02:00
|
|
|
'ansible/module_utils/common/file.py',
|
2019-01-01 00:10:51 +01:00
|
|
|
'ansible/module_utils/common/process.py',
|
2018-12-07 19:21:11 +01:00
|
|
|
'ansible/module_utils/common/sys_info.py',
|
2019-03-21 14:40:19 +01:00
|
|
|
'ansible/module_utils/common/text/__init__.py',
|
|
|
|
'ansible/module_utils/common/text/converters.py',
|
|
|
|
'ansible/module_utils/common/text/formatters.py',
|
2019-03-15 02:29:55 +01:00
|
|
|
'ansible/module_utils/common/validation.py',
|
2019-01-01 00:10:51 +01:00
|
|
|
'ansible/module_utils/common/_utils.py',
|
2020-05-14 17:46:34 +02:00
|
|
|
'ansible/module_utils/compat/__init__.py',
|
|
|
|
'ansible/module_utils/compat/_selectors2.py',
|
|
|
|
'ansible/module_utils/compat/selectors.py',
|
2018-12-11 02:04:14 +01:00
|
|
|
'ansible/module_utils/distro/__init__.py',
|
|
|
|
'ansible/module_utils/distro/_distro.py',
|
2019-01-01 00:10:51 +01:00
|
|
|
'ansible/module_utils/parsing/__init__.py',
|
|
|
|
'ansible/module_utils/parsing/convert_bool.py',
|
2018-08-24 05:54:40 +02:00
|
|
|
'ansible/module_utils/pycompat24.py',
|
2019-01-01 00:10:51 +01:00
|
|
|
'ansible/module_utils/six/__init__.py',
|
2018-08-24 05:54:40 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
ONLY_BASIC_FILE = frozenset(('ansible/module_utils/basic.py',))
|
|
|
|
|
2019-08-28 03:11:21 +02:00
|
|
|
ANSIBLE_LIB = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))), 'lib', 'ansible')
|
|
|
|
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def finder_containers():
|
2020-07-17 02:57:47 +02:00
|
|
|
FinderContainers = namedtuple('FinderContainers', ['zf'])
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
zipoutput = BytesIO()
|
|
|
|
zf = zipfile.ZipFile(zipoutput, mode='w', compression=zipfile.ZIP_STORED)
|
|
|
|
|
2020-07-17 02:57:47 +02:00
|
|
|
return FinderContainers(zf)
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestRecursiveFinder(object):
|
|
|
|
def test_no_module_utils(self, finder_containers):
|
|
|
|
name = 'ping'
|
|
|
|
data = b'#!/usr/bin/python\nreturn \'{\"changed\": false}\''
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'ping.py'), data, *finder_containers)
|
2018-08-24 05:54:40 +02:00
|
|
|
assert frozenset(finder_containers.zf.namelist()) == MODULE_UTILS_BASIC_FILES
|
2017-01-27 20:53:02 +01:00
|
|
|
|
2019-01-04 06:03:37 +01:00
|
|
|
def test_module_utils_with_syntax_error(self, finder_containers):
|
|
|
|
name = 'fake_module'
|
|
|
|
data = b'#!/usr/bin/python\ndef something(:\n pass\n'
|
|
|
|
with pytest.raises(ansible.errors.AnsibleError) as exec_info:
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'fake_module.py'), data, *finder_containers)
|
2019-08-09 20:58:46 +02:00
|
|
|
assert 'Unable to import fake_module due to invalid syntax' in str(exec_info.value)
|
2019-01-04 06:03:37 +01:00
|
|
|
|
|
|
|
def test_module_utils_with_identation_error(self, finder_containers):
|
|
|
|
name = 'fake_module'
|
|
|
|
data = b'#!/usr/bin/python\n def something():\n pass\n'
|
|
|
|
with pytest.raises(ansible.errors.AnsibleError) as exec_info:
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'fake_module.py'), data, *finder_containers)
|
2019-08-09 20:58:46 +02:00
|
|
|
assert 'Unable to import fake_module due to unexpected indent' in str(exec_info.value)
|
2019-01-04 06:03:37 +01:00
|
|
|
|
2017-01-27 20:53:02 +01:00
|
|
|
#
|
|
|
|
# Test importing six with many permutations because it is not a normal module
|
|
|
|
#
|
|
|
|
def test_from_import_six(self, finder_containers):
|
|
|
|
name = 'ping'
|
|
|
|
data = b'#!/usr/bin/python\nfrom ansible.module_utils import six'
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'ping.py'), data, *finder_containers)
|
2018-08-24 05:54:40 +02:00
|
|
|
assert frozenset(finder_containers.zf.namelist()) == frozenset(('ansible/module_utils/six/__init__.py', )).union(MODULE_UTILS_BASIC_FILES)
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
def test_import_six(self, finder_containers):
|
|
|
|
name = 'ping'
|
|
|
|
data = b'#!/usr/bin/python\nimport ansible.module_utils.six'
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'ping.py'), data, *finder_containers)
|
2018-08-24 05:54:40 +02:00
|
|
|
assert frozenset(finder_containers.zf.namelist()) == frozenset(('ansible/module_utils/six/__init__.py', )).union(MODULE_UTILS_BASIC_FILES)
|
2017-01-27 20:53:02 +01:00
|
|
|
|
|
|
|
def test_import_six_from_many_submodules(self, finder_containers):
|
|
|
|
name = 'ping'
|
|
|
|
data = b'#!/usr/bin/python\nfrom ansible.module_utils.six.moves.urllib.parse import urlparse'
|
2019-08-28 03:11:21 +02:00
|
|
|
recursive_finder(name, os.path.join(ANSIBLE_LIB, 'modules', 'system', 'ping.py'), data, *finder_containers)
|
2018-08-24 05:54:40 +02:00
|
|
|
assert frozenset(finder_containers.zf.namelist()) == frozenset(('ansible/module_utils/six/__init__.py',)).union(MODULE_UTILS_BASIC_FILES)
|