| 1 |
# (c) 2017, Toshio Kuratomi <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import os.path |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
import ansible.errors |
| 25 |
|
| 26 |
from ansible.executor import module_common as amc |
| 27 |
from ansible.executor.interpreter_discovery import InterpreterDiscoveryRequiredError |
| 28 |
|
| 29 |
|
| 30 |
class TestStripComments: |
| 31 |
def test_no_changes(self): |
| 32 |
no_comments = u"""def some_code(): |
| 33 |
return False""" |
| 34 |
assert amc._strip_comments(no_comments) == no_comments |
| 35 |
|
| 36 |
def test_all_comments(self): |
| 37 |
all_comments = u"""# This is a test |
| 38 |
# Being as it is |
| 39 |
# To be |
| 40 |
""" |
| 41 |
assert amc._strip_comments(all_comments) == u"\n\n\n" |
| 42 |
|
| 43 |
def test_all_whitespace(self): |
| 44 |
all_whitespace = ( |
| 45 |
'\n' |
| 46 |
' \n' |
| 47 |
'\n' |
| 48 |
' \n' |
| 49 |
'\t\t\r\n' |
| 50 |
'\n' |
| 51 |
' ' |
| 52 |
) |
| 53 |
|
| 54 |
assert amc._strip_comments(all_whitespace) == u"\n\n\n\n\n\n" |
| 55 |
|
| 56 |
def test_somewhat_normal(self): |
| 57 |
mixed = u"""#!/usr/bin/python |
| 58 |
|
| 59 |
# here we go |
| 60 |
def test(arg): |
| 61 |
# this is a thing |
| 62 |
thing = '# test' |
| 63 |
return thing |
| 64 |
# End |
| 65 |
""" |
| 66 |
mixed_results = u""" |
| 67 |
|
| 68 |
|
| 69 |
def test(arg): |
| 70 |
|
| 71 |
thing = '# test' |
| 72 |
return thing |
| 73 |
""" |
| 74 |
assert amc._strip_comments(mixed) == mixed_results |
| 75 |
|
| 76 |
|
| 77 |
class TestSlurp: |
| 78 |
def test_slurp_nonexistent(self, mocker): |
| 79 |
mocker.patch('os.path.exists', side_effect=lambda x: False) |
| 80 |
with pytest.raises(ansible.errors.AnsibleError): |
| 81 |
amc._slurp('no_file') |
| 82 |
|
| 83 |
def test_slurp_file(self, mocker): |
| 84 |
mocker.patch('os.path.exists', side_effect=lambda x: True) |
| 85 |
m = mocker.mock_open(read_data='This is a test') |
| 86 |
mocker.patch('builtins.open', m) |
| 87 |
assert amc._slurp('some_file') == 'This is a test' |
| 88 |
|
| 89 |
def test_slurp_file_with_newlines(self, mocker): |
| 90 |
mocker.patch('os.path.exists', side_effect=lambda x: True) |
| 91 |
m = mocker.mock_open(read_data='#!/usr/bin/python\ndef test(args):\nprint("hi")\n') |
| 92 |
mocker.patch('builtins.open', m) |
| 93 |
assert amc._slurp('some_file') == '#!/usr/bin/python\ndef test(args):\nprint("hi")\n' |
| 94 |
|
| 95 |
|
| 96 |
class TestGetShebang: |
| 97 |
"""Note: We may want to change the API of this function in the future. It isn't a great API""" |
| 98 |
def test_no_interpreter_set(self, templar): |
| 99 |
# normally this would return /usr/bin/python, but so long as we're defaulting to auto python discovery, we'll get |
| 100 |
# an InterpreterDiscoveryRequiredError here instead |
| 101 |
with pytest.raises(InterpreterDiscoveryRequiredError): |
| 102 |
amc._get_shebang(u'/usr/bin/python', {}, templar) |
| 103 |
|
| 104 |
def test_python_interpreter(self, templar): |
| 105 |
assert amc._get_shebang(u'/usr/bin/python3.8', {}, templar) == ('#!/usr/bin/python3.8', u'/usr/bin/python3.8') |
| 106 |
|
| 107 |
def test_non_python_interpreter(self, templar): |
| 108 |
assert amc._get_shebang(u'/usr/bin/ruby', {}, templar) == ('#!/usr/bin/ruby', u'/usr/bin/ruby') |
| 109 |
|
| 110 |
def test_interpreter_set_in_task_vars(self, templar): |
| 111 |
assert amc._get_shebang(u'/usr/bin/python', {u'ansible_python_interpreter': u'/usr/bin/pypy'}, templar) == \ |
| 112 |
(u'#!/usr/bin/pypy', u'/usr/bin/pypy') |
| 113 |
|
| 114 |
def test_non_python_interpreter_in_task_vars(self, templar): |
| 115 |
assert amc._get_shebang(u'/usr/bin/ruby', {u'ansible_ruby_interpreter': u'/usr/local/bin/ruby'}, templar) == \ |
| 116 |
(u'#!/usr/local/bin/ruby', u'/usr/local/bin/ruby') |
| 117 |
|
| 118 |
def test_with_args(self, templar): |
| 119 |
assert amc._get_shebang(u'/usr/bin/python', {u'ansible_python_interpreter': u'/usr/bin/python3'}, templar, args=('-tt', '-OO')) == \ |
| 120 |
(u'#!/usr/bin/python3 -tt -OO', u'/usr/bin/python3') |
| 121 |
|
| 122 |
def test_python_via_env(self, templar): |
| 123 |
assert amc._get_shebang(u'/usr/bin/python', {u'ansible_python_interpreter': u'/usr/bin/env python'}, templar) == \ |
| 124 |
(u'#!/usr/bin/env python', u'/usr/bin/env python') |
| 125 |
|
| 126 |
|
| 127 |
class TestDetectionRegexes: |
| 128 |
ANSIBLE_MODULE_UTIL_STRINGS = ( |
| 129 |
# Absolute collection imports |
| 130 |
b'import ansible_collections.my_ns.my_col.plugins.module_utils.my_util', |
| 131 |
b'from ansible_collections.my_ns.my_col.plugins.module_utils import my_util', |
| 132 |
b'from ansible_collections.my_ns.my_col.plugins.module_utils.my_util import my_func', |
| 133 |
# Absolute core imports |
| 134 |
b'import ansible.module_utils.basic', |
| 135 |
b'from ansible.module_utils import basic', |
| 136 |
b'from ansible.module_utils.basic import AnsibleModule', |
| 137 |
# Relative imports |
| 138 |
b'from ..module_utils import basic', |
| 139 |
b'from .. module_utils import basic', |
| 140 |
b'from ....module_utils import basic', |
| 141 |
b'from ..module_utils.basic import AnsibleModule', |
| 142 |
) |
| 143 |
NOT_ANSIBLE_MODULE_UTIL_STRINGS = ( |
| 144 |
b'from ansible import release', |
| 145 |
b'from ..release import __version__', |
| 146 |
b'from .. import release', |
| 147 |
b'from ansible.modules.system import ping', |
| 148 |
b'from ansible_collecitons.my_ns.my_col.plugins.modules import function', |
| 149 |
) |
| 150 |
|
| 151 |
OFFSET = os.path.dirname(os.path.dirname(amc.__file__)) |
| 152 |
CORE_PATHS = ( |
| 153 |
('%s/modules/from_role.py' % OFFSET, 'ansible/modules/from_role'), |
| 154 |
('%s/modules/system/ping.py' % OFFSET, 'ansible/modules/system/ping'), |
| 155 |
('%s/modules/cloud/amazon/s3.py' % OFFSET, 'ansible/modules/cloud/amazon/s3'), |
| 156 |
) |
| 157 |
|
| 158 |
COLLECTION_PATHS = ( |
| 159 |
('/root/ansible_collections/ns/col/plugins/modules/ping.py', |
| 160 |
'ansible_collections/ns/col/plugins/modules/ping'), |
| 161 |
('/root/ansible_collections/ns/col/plugins/modules/subdir/ping.py', |
| 162 |
'ansible_collections/ns/col/plugins/modules/subdir/ping'), |
| 163 |
) |
| 164 |
|
| 165 |
@pytest.mark.parametrize('testcase', ANSIBLE_MODULE_UTIL_STRINGS) |
| 166 |
def test_detect_new_style_python_module_re(self, testcase): |
| 167 |
assert amc.NEW_STYLE_PYTHON_MODULE_RE.search(testcase) |
| 168 |
|
| 169 |
@pytest.mark.parametrize('testcase', NOT_ANSIBLE_MODULE_UTIL_STRINGS) |
| 170 |
def test_no_detect_new_style_python_module_re(self, testcase): |
| 171 |
assert not amc.NEW_STYLE_PYTHON_MODULE_RE.search(testcase) |
| 172 |
|
| 173 |
@pytest.mark.parametrize('testcase, result', CORE_PATHS) |
| 174 |
def test_detect_core_library_path_re(self, testcase, result): |
| 175 |
assert amc.CORE_LIBRARY_PATH_RE.search(testcase).group('path') == result |
| 176 |
|
| 177 |
@pytest.mark.parametrize('testcase', [p[0] for p in COLLECTION_PATHS]) |
| 178 |
def test_no_detect_core_library_path_re(self, testcase): |
| 179 |
assert not amc.CORE_LIBRARY_PATH_RE.search(testcase) |
| 180 |
|
| 181 |
@pytest.mark.parametrize('testcase, result', COLLECTION_PATHS) |
| 182 |
def test_detect_collection_path_re(self, testcase, result): |
| 183 |
assert amc.COLLECTION_PATH_RE.search(testcase).group('path') == result |
| 184 |
|
| 185 |
@pytest.mark.parametrize('testcase', [p[0] for p in CORE_PATHS]) |
| 186 |
def test_no_detect_collection_path_re(self, testcase): |
| 187 |
assert not amc.COLLECTION_PATH_RE.search(testcase) |