ansible/test/integration/targets/module_utils_distro/runme.sh
Rick Elrod fa0bccf6a1
[module_utils.distro] Fall back to bundled (#74229)
Change:
- When a "distro" package exists in PYTHONPATH but isn't what we expect,
  fall back to our own vendored one and use it. This prevents a
  traceback if someone has some random thing that provides "distro" but
  isn't actually the "distro" library we need.

Test Plan:
- new tests

Tickets:
- Fixes #74228

Signed-off-by: Rick Elrod <rick@elrod.me>

* nuke playbook test file

Signed-off-by: Rick Elrod <rick@elrod.me>

* test fixes

Signed-off-by: Rick Elrod <rick@elrod.me>
2021-04-13 11:27:52 -04:00

24 lines
788 B
Bash
Executable file

#!/usr/bin/env bash
set -eux
# Ensure that when a non-distro 'distro' package is in PYTHONPATH, we fallback
# to our bundled one.
new_pythonpath="$OUTPUT_DIR/pythonpath"
mkdir -p "$new_pythonpath/distro"
touch "$new_pythonpath/distro/__init__.py"
export PYTHONPATH="$new_pythonpath:$PYTHONPATH"
# Sanity test to make sure the above worked
set +e
distro_id_fail="$(python -c 'import distro; distro.id' 2>&1)"
set -e
grep -q "AttributeError:.*has no attribute 'id'" <<< "$distro_id_fail"
# ansible.module_utils.common.sys_info imports distro, and itself gets imported
# in DataLoader, so all we have to do to test the fallback is run `ansible`.
ansirun="$(ansible -i ../../inventory -a "echo \$PYTHONPATH" localhost)"
grep -q "$new_pythonpath" <<< "$ansirun"
rm -rf "$new_pythonpath"