From dec163940ea264dbf573becafe8e27ef862b7d42 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Wed, 1 Apr 2020 15:19:59 -0600 Subject: [PATCH] support testing/linting module_utils code This does not directly affect network because network has its own solution for dealing with testing/linting module_utils code, but it gets network to feature/code parity with the template and other repos. Defines a shell function in utils.sh - lsr_setup_module_utils - that allows configuring a tox venv installed ansible to have the repo module_utils code in the default pythonpath for the venv, which allows testing and linting the module_utils code without having to otherwise munge or mock the pythonpath. Projects needing this functionality can set in `.travis/config.sh` the variable `RUN_PYTEST_SETUP_MODULE_UTILS` to setup module_utils/ for pytest, and `RUN_PYLINT_SETUP_MODULE_UTILS` to setup module_utils/ for pylint. --- .travis/config.sh | 4 ++++ .travis/runpylint.sh | 12 +++++++++++ .travis/runpytest.sh | 12 +++++++++++ .travis/utils.sh | 19 +++++++++++++++++ tests/setup_module_utils.sh | 41 +++++++++++++++++++++++++++++++++++++ tox.ini | 2 ++ 6 files changed, 90 insertions(+) create mode 100755 tests/setup_module_utils.sh diff --git a/.travis/config.sh b/.travis/config.sh index dc24337..5619ab3 100644 --- a/.travis/config.sh +++ b/.travis/config.sh @@ -20,6 +20,7 @@ # - RUN_PYLINT_INCLUDE # - RUN_PYLINT_EXCLUDE # - RUN_PYLINT_DISABLED +# - RUN_PYLINT_SETUP_MODULE_UTILS # # * .travis/runblack.sh: # @@ -31,3 +32,6 @@ # # - RUN_FLAKE8_DISABLED # +# * .travis/runpytest.sh: +# +# - RUN_PYTEST_SETUP_MODULE_UTILS diff --git a/.travis/runpylint.sh b/.travis/runpylint.sh index 8ca82b9..b613e18 100755 --- a/.travis/runpylint.sh +++ b/.travis/runpylint.sh @@ -12,6 +12,13 @@ # The given command line arguments are passed to custom_pylint.py. +# Environment variables: +# +# RUN_PYLINT_SETUP_MODULE_UTILS +# if set to an arbitrary non-empty value, the environment will be +# configured so that linting of the module_utils/ code will be run +# correctly + set -e ME=$(basename $0) @@ -19,5 +26,10 @@ SCRIPTDIR=$(readlink -f $(dirname $0)) . ${SCRIPTDIR}/config.sh +if [[ "${RUN_PYLINT_SETUP_MODULE_UTILS}" ]]; then + . ${SCRIPTDIR}/utils.sh + lsr_setup_module_utils +fi + set -x python ${SCRIPTDIR}/custom_pylint.py "$@" diff --git a/.travis/runpytest.sh b/.travis/runpytest.sh index f9a2ce2..67db792 100755 --- a/.travis/runpytest.sh +++ b/.travis/runpytest.sh @@ -10,18 +10,30 @@ # The given command line arguments are passed to pytest. +# Environment variables: +# +# RUN_PYTEST_SETUP_MODULE_UTILS +# if set to an arbitrary non-empty value, the environment will be +# configured so that tests of the module_utils/ code will be run +# correctly + set -e ME=$(basename $0) SCRIPTDIR=$(readlink -f $(dirname $0)) . ${SCRIPTDIR}/utils.sh +. ${SCRIPTDIR}/config.sh if [[ ! -d ${TOPDIR}/tests/unit ]]; then lsr_info "${ME}: No unit tests found. Skipping." exit 0 fi +if [[ "${RUN_PYTEST_SETUP_MODULE_UTILS}" ]]; then + lsr_setup_module_utils +fi + PYTEST_OPTS=() PYTEST_OPTS_NOCOV=() USE_COV=no diff --git a/.travis/utils.sh b/.travis/utils.sh index 9e2c4e0..4db8ffb 100644 --- a/.travis/utils.sh +++ b/.travis/utils.sh @@ -169,6 +169,25 @@ function lsr_venv_python_matches_system_python() { lsr_compare_pythons ${1:-python} -eq $syspython } +## +# lsr_setup_module_utils [$1] [$2] +# +# $1 - path to the ansible/module_utils/ directory in the venv +# assumes ansible has been installed in the venv +# defaults to env var $SRC_MODULE_UTILS_DIR +# $2 - path to the local module_utils/ directory for the role +# defaults to env var $DEST_MODULE_UTILS_DIR +# +# Exit with 0 if virtual environment Python version matches the system Python +# version. +function lsr_setup_module_utils() { + local srcdir=${1:-$SRC_MODULE_UTILS_DIR} + local destdir=${2:-$DEST_MODULE_UTILS_DIR} + if [ -n "$srcdir" -a -d "$srcdir" -a -n "$destdir" -a -d "$destdir" ]; then + bash $TOPDIR/tests/setup_module_utils.sh "$srcdir" "$destdir" + fi +} + # set TOPDIR ME=${ME:-$(basename $0)} SCRIPTDIR=${SCRIPTDIR:-$(readlink -f $(dirname $0))} diff --git a/tests/setup_module_utils.sh b/tests/setup_module_utils.sh new file mode 100755 index 0000000..18d6a00 --- /dev/null +++ b/tests/setup_module_utils.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT + +set -euo pipefail + +if [ -n "${DEBUG:-}" ] ; then + set -x +fi + +if [ ! -d "${1:-}" ] ; then + echo Either ansible is not installed, or there is no ansible/module_utils + echo in $1 - Skipping + exit 0 +fi + +if [ ! -d "${2:-}" ] ; then + echo Role has no module_utils - Skipping + exit 0 +fi + +# we need absolute path for $2 +absmoddir=$( readlink -f "$2" ) + +# clean up old links to module_utils +for item in "$1"/* ; do + if lnitem=$( readlink "$item" ) && test -n "$lnitem" ; then + case "$lnitem" in + *"${2}"*) rm -f "$item" ;; + esac + fi +done + +# add new links to module_utils +for item in "$absmoddir"/* ; do + case "$item" in + *__pycache__) continue;; + *.pyc) continue;; + esac + bnitem=$( basename "$item" ) + ln -s "$item" "$1/$bnitem" +done diff --git a/tox.ini b/tox.ini index 5432fcf..6e9127c 100644 --- a/tox.ini +++ b/tox.ini @@ -22,6 +22,8 @@ passenv = * setenv = PYTHONPATH = {toxinidir}/library:{toxinidir}/module_utils LC_ALL = C + SRC_MODULE_UTILS_DIR = {envsitepackagesdir}/ansible/module_utils + DEST_MODULE_UTILS_DIR = {toxinidir}/module_utils changedir = {toxinidir}/tests covtargets = --cov={toxinidir}/library --cov={toxinidir}/module_utils pytesttarget = unit