tests: Fix ansible_python_interpreter & discovered_interpreter_python tests on macOS

Should account for fiddling in mitogen.parent.Connection._first_stage() and
symlinks. I won't be surprised if it breaks again soon and often.
pull/1032/head
Alex Willmer 9 months ago
parent c2ad52e54e
commit e2f4d9275c

@ -1409,6 +1409,9 @@ class Connection(object):
# their respective values.
# * CONTEXT_NAME must be prefixed with the name of the Python binary in
# order to allow virtualenvs to detect their install prefix.
#
# macOS tweaks for Python 2.7 must be kept in sync with the the Ansible
# module test_echo_module, used by the integration tests.
# * macOS <= 10.14 (Darwin <= 18) install an unreliable Python version
# switcher as /usr/bin/python, which introspects argv0. To workaround
# it we redirect attempts to call /usr/bin/python with an explicit
@ -1417,7 +1420,8 @@ class Connection(object):
# do something slightly different. The Python executable is patched to
# perform an extra execvp(). I don't fully understand the details, but
# setting PYTHON_LAUNCHED_FROM_WRAPPER=1 avoids it.
# * macOS 13.x (Darwin 22?) may remove python 2.x entirely.
# * macOS 12.3+ (Darwin 21.4+, Monterey) doesn't ship Python.
# https://developer.apple.com/documentation/macos-release-notes/macos-12_3-release-notes#Python
#
# Locals:
# R: read side of interpreter stdin.

@ -37,6 +37,7 @@
vars:
ansible_python_interpreter: auto
test_echo_module:
facts_copy: "{{ ansible_facts }}"
register: echoout
# can't test this assertion:
@ -44,11 +45,15 @@
# because Mitogen's ansible_python_interpreter is a connection-layer configurable that
# "must be extracted during each task execution to form the complete connection-layer configuration".
# Discovery won't be reran though; the ansible_python_interpreter is read from the cache if already discovered
- assert:
- name: assert discovered python matches invoked python
assert:
that:
- auto_out.ansible_facts.discovered_interpreter_python is defined
- echoout.running_python_interpreter == auto_out.ansible_facts.discovered_interpreter_python
fail_msg: auto_out={{auto_out}} echoout={{echoout}}
- auto_out.ansible_facts.discovered_interpreter_python == echoout.discovered_python.as_seen
- echoout.discovered_python.resolved == echoout.running_python.sys.executable.resolved
fail_msg:
- "auto_out: {{ auto_out }}"
- "echoout: {{ echoout }}"
- name: test that auto_legacy gives a dep warning when /usr/bin/python present but != auto result
@ -128,7 +133,8 @@
- name: ensure modules can't set discovered_interpreter_X or ansible_X_interpreter
block:
- test_echo_module:
facts:
facts_copy: "{{ ansible_facts }}"
facts_to_override:
ansible_discovered_interpreter_bogus: from module
discovered_interpreter_bogus: from_module
ansible_bogus_interpreter: from_module

@ -9,28 +9,51 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import os
import platform
import sys
from ansible.module_utils.basic import AnsibleModule
def main():
result = dict(changed=False)
module = AnsibleModule(argument_spec=dict(
facts=dict(type=dict, default={})
facts_copy=dict(type=dict, default={}),
facts_to_override=dict(type=dict, default={})
))
result['ansible_facts'] = module.params['facts']
# revert the Mitogen OSX tweak since discover_interpreter() doesn't return this info
if sys.platform == 'darwin' and sys.executable != '/usr/bin/python':
if int(platform.release()[:2]) < 19:
sys.executable = sys.executable[:-3]
else:
# NB This must be synced with mitogen.parent.Connection.get_boot_command()
if sys.modules.get('mitogen') and sys.platform == 'darwin':
darwin_major = int(platform.release().partition('.')[0])
if darwin_major < 19 and sys.executable == '/usr/bin/python2.7':
sys.executable = '/usr/bin/python'
if darwin_major in (20, 21) and sys.version_info[:2] == (2, 7):
# only for tests to check version of running interpreter -- Mac 10.15+ changed python2
# so it looks like it's /usr/bin/python but actually it's /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
sys.executable = "/usr/bin/python"
result['running_python_interpreter'] = sys.executable
facts_copy = module.params['facts_copy']
discovered_interpreter_python = facts_copy['discovered_interpreter_python']
result = {
'changed': False,
'ansible_facts': module.params['facts_to_override'],
'discovered_and_running_samefile': os.path.samefile(
os.path.realpath(discovered_interpreter_python),
os.path.realpath(sys.executable),
),
'discovered_python': {
'as_seen': discovered_interpreter_python,
'resolved': os.path.realpath(discovered_interpreter_python),
},
'running_python': {
'sys': {
'executable': {
'as_seen': sys.executable,
'resolved': os.path.realpath(sys.executable),
},
},
},
}
module.exit_json(**result)

Loading…
Cancel
Save