diff --git a/changelogs/fragments/ansible-test-venv-virtualenv-fallback.yml b/changelogs/fragments/ansible-test-venv-virtualenv-fallback.yml new file mode 100644 index 00000000000..b22fc5101ef --- /dev/null +++ b/changelogs/fragments/ansible-test-venv-virtualenv-fallback.yml @@ -0,0 +1,3 @@ +minor_changes: + - ansible-test - virtualenv helper scripts now prefer ``venv`` on Python 3 over ``virtualenv`` if the ``ANSIBLE_TEST_PREFER_VENV`` environment variable is set + - ansible-test - remote macOS instances no longer install ``virtualenv`` during provisioning diff --git a/test/lib/ansible_test/_data/injector/virtualenv-isolated.sh b/test/lib/ansible_test/_data/injector/virtualenv-isolated.sh index 82f799809eb..af92a0562eb 100644 --- a/test/lib/ansible_test/_data/injector/virtualenv-isolated.sh +++ b/test/lib/ansible_test/_data/injector/virtualenv-isolated.sh @@ -2,7 +2,13 @@ # Create and activate a fresh virtual environment with `source virtualenv-isolated.sh`. rm -rf "${OUTPUT_DIR}/venv" -"${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m virtualenv --python "${ANSIBLE_TEST_PYTHON_INTERPRETER}" "${OUTPUT_DIR}/venv" + +# Try to use 'venv' if it is available, then fallback to 'virtualenv' since some systems provide 'venv' although it is non-functional. +if [ -z "${ANSIBLE_TEST_PREFER_VENV:-}" ] || [[ "${ANSIBLE_TEST_PYTHON_VERSION}" =~ ^2\. ]] || ! "${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m venv "${OUTPUT_DIR}/venv" > /dev/null 2>&1; then + rm -rf "${OUTPUT_DIR}/venv" + "${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m virtualenv --python "${ANSIBLE_TEST_PYTHON_INTERPRETER}" "${OUTPUT_DIR}/venv" +fi + set +ux source "${OUTPUT_DIR}/venv/bin/activate" set -ux diff --git a/test/lib/ansible_test/_data/injector/virtualenv.sh b/test/lib/ansible_test/_data/injector/virtualenv.sh index ccde29748c0..282e6074cc0 100644 --- a/test/lib/ansible_test/_data/injector/virtualenv.sh +++ b/test/lib/ansible_test/_data/injector/virtualenv.sh @@ -2,7 +2,13 @@ # Create and activate a fresh virtual environment with `source virtualenv.sh`. rm -rf "${OUTPUT_DIR}/venv" -"${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m virtualenv --system-site-packages --python "${ANSIBLE_TEST_PYTHON_INTERPRETER}" "${OUTPUT_DIR}/venv" + +# Try to use 'venv' if it is available, then fallback to 'virtualenv' since some systems provide 'venv' although it is non-functional. +if [ -z "${ANSIBLE_TEST_PREFER_VENV:-}" ] || [[ "${ANSIBLE_TEST_PYTHON_VERSION}" =~ ^2\. ]] || ! "${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m venv --system-site-packages "${OUTPUT_DIR}/venv" > /dev/null 2>&1; then + rm -rf "${OUTPUT_DIR}/venv" + "${ANSIBLE_TEST_PYTHON_INTERPRETER}" -m virtualenv --system-site-packages --python "${ANSIBLE_TEST_PYTHON_INTERPRETER}" "${OUTPUT_DIR}/venv" +fi + set +ux source "${OUTPUT_DIR}/venv/bin/activate" set -ux diff --git a/test/lib/ansible_test/_data/setup/remote.sh b/test/lib/ansible_test/_data/setup/remote.sh index 654f678d385..93dead5d7e3 100644 --- a/test/lib/ansible_test/_data/setup/remote.sh +++ b/test/lib/ansible_test/_data/setup/remote.sh @@ -88,18 +88,10 @@ elif [ "${platform}" = "centos" ]; then done install_pip -elif [ "${platform}" = "macos" ]; then - while true; do - pip3 install --disable-pip-version-check --quiet \ - 'virtualenv<20' \ - && break - echo "Failed to install packages. Sleeping before trying again..." - sleep 10 - done elif [ "${platform}" = "osx" ]; then while true; do pip install --disable-pip-version-check --quiet \ - 'virtualenv<20' \ + 'virtualenv==16.7.10' \ && break echo "Failed to install packages. Sleeping before trying again..." sleep 10