2018-02-22 23:27:53 +01:00
|
|
|
# Copyright (c) 2018 Matt Martz <matt@sivel.net>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from ansible.executor.module_common import modify_module
|
|
|
|
from ansible.module_utils.six import PY2
|
|
|
|
|
|
|
|
from test_module_common import templar
|
|
|
|
|
|
|
|
|
|
|
|
FAKE_OLD_MODULE = b'''#!/usr/bin/python
|
|
|
|
import sys
|
|
|
|
print('{"result": "%s"}' % sys.executable)
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_old_module_open(mocker):
|
|
|
|
m = mocker.mock_open(read_data=FAKE_OLD_MODULE)
|
|
|
|
if PY2:
|
|
|
|
mocker.patch('__builtin__.open', m)
|
|
|
|
else:
|
|
|
|
mocker.patch('builtins.open', m)
|
|
|
|
|
2019-02-28 08:52:02 +01:00
|
|
|
# this test no longer makes sense, since a Python module will always either have interpreter discovery run or
|
|
|
|
# an explicit interpreter passed (so we'll never default to the module shebang)
|
|
|
|
# def test_shebang(fake_old_module_open, templar):
|
|
|
|
# (data, style, shebang) = modify_module('fake_module', 'fake_path', {}, templar)
|
|
|
|
# assert shebang == '#!/usr/bin/python'
|
2018-02-22 23:27:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_shebang_task_vars(fake_old_module_open, templar):
|
|
|
|
task_vars = {
|
|
|
|
'ansible_python_interpreter': '/usr/bin/python3'
|
|
|
|
}
|
|
|
|
|
module_common: handle None value for templar (#36651)
* module_common: set required parameter templar
Fix the following error (related to b455901):
$ ./hacking/test-module -m ./lib/ansible/modules/system/ping.py -I ansible_python_interpreter=/usr/bin/python
Traceback (most recent call last):
File "./hacking/test-module", line 268, in <module>
main()
File "./hacking/test-module", line 249, in main
(modfile, modname, module_style) = boilerplate_module(options.module_path, options.module_args, interpreters, options.check, options.filename)
File "./hacking/test-module", line 152, in boilerplate_module
task_vars=task_vars
File "ansible/lib/ansible/executor/module_common.py", line 910, in modify_module
environment=environment)
File "ansible/lib/ansible/executor/module_common.py", line 736, in _find_module_utils
shebang, interpreter = _get_shebang(u'/usr/bin/python', task_vars, templar)
File "ansible/lib/ansible/executor/module_common.py", line 452, in _get_shebang
interpreter = templar.template(task_vars[interpreter_config].strip())
AttributeError: 'NoneType' object has no attribute 'template'
* module_common.modify_module: templar is required
2018-03-29 19:54:48 +02:00
|
|
|
(data, style, shebang) = modify_module('fake_module', 'fake_path', {}, templar, task_vars=task_vars)
|
2018-02-22 23:27:53 +01:00
|
|
|
assert shebang == '#!/usr/bin/python3'
|