From e116222308b4142888bfb1c77c345b23f8a03d80 Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Thu, 26 Mar 2020 15:28:28 +0100 Subject: [PATCH] Allow to specify python interpter used for testing IR plugin Change-Id: I2ea68efed1fe1c0dc22b3c49118cfc5c93c8a037 --- roles/plugin.spec | 15 ++++- roles/tobiko-common/defaults/main.yaml | 1 - roles/tobiko-tox/defaults/main.yaml | 7 ++- roles/tobiko-tox/tasks/main.yaml | 26 ++++++-- tools/ci/activate | 2 +- tools/setup_infrared.py | 85 ++++++++++++++++++++++++++ tox.ini | 33 +++++----- zuul.d/infrared.yaml | 3 +- 8 files changed, 148 insertions(+), 24 deletions(-) create mode 100644 tools/setup_infrared.py diff --git a/roles/plugin.spec b/roles/plugin.spec index f26c02751..2675d2354 100644 --- a/roles/plugin.spec +++ b/roles/plugin.spec @@ -20,6 +20,11 @@ subparsers: default: localhost help: Target host where test cases are deployed and executed ansible_variable: test_host + ansible-python-interpreter: + type: Value + default: python3 + help: Python interpreter executed by ansible on target hosts + ansible_variable: ansible_python_interpreter - title: Control flow options: @@ -89,14 +94,22 @@ subparsers: type: Value help: command to be executed for tox (typically tox) ansible_variable: tox_command + tox-environment: + type: Value + help: envitonment variables to be set when running test cases + ansible_variable: tox_environment tox-envlist: type: Value - help: tox environment list to be executed + help: Tox environment names to be executed ansible_variable: tox_envlist tox-extra-args: type: Value help: extra options to be passed to Tox ansible_variable: tox_extra_args + tox-python: + type: Value + help: Python interpreter to be used for executing test cases + ansible_variable: tox_python test-report-dir: type: Value help: directory where to store test report files diff --git a/roles/tobiko-common/defaults/main.yaml b/roles/tobiko-common/defaults/main.yaml index 55093f087..f7ec997b5 100644 --- a/roles/tobiko-common/defaults/main.yaml +++ b/roles/tobiko-common/defaults/main.yaml @@ -19,7 +19,6 @@ test_git_repo: '{{ tobiko_git_repo | realpath }}' test_git_refspec: '' test_git_version: '' - # --- Test configuration options ---------------------------------------------- # Configuration options diff --git a/roles/tobiko-tox/defaults/main.yaml b/roles/tobiko-tox/defaults/main.yaml index f4e43f385..731d9c383 100644 --- a/roles/tobiko-tox/defaults/main.yaml +++ b/roles/tobiko-tox/defaults/main.yaml @@ -4,9 +4,14 @@ tox_dir: '{{ test_dir | realpath }}' tox_command: tox tox_environment: {} -tox_envlist: py36 +tox_envlist: tox_extra_args: '' +tox_command_line: > + {{ tox_command }} + {% if tox_envlist %} -e {{ tox_envlist | quote }} {% endif %} + {{ tox_extra_args }} +tox_python: 'python3' tox_report_dir: '{{ test_report_dir | realpath }}' tox_report_name: '{{ test_report_name }}_{{ tox_envlist }}' tox_report_env: diff --git a/roles/tobiko-tox/tasks/main.yaml b/roles/tobiko-tox/tasks/main.yaml index ba512b8af..6221810ea 100644 --- a/roles/tobiko-tox/tasks/main.yaml +++ b/roles/tobiko-tox/tasks/main.yaml @@ -1,10 +1,26 @@ --- -- name: "run test Tox envlist '{{ tox_envlist }}' on direcory '{{ tox_dir }}'" - shell: - chdir: "{{ tox_dir }}" - cmd: | - {{ tox_command }} -e "{{ tox_envlist }}" {{ tox_extra_args }} +- name: "set 'PYTHON={{ tox_python }}' in Tox environment variables" + set_fact: + tox_environment: > + {{ tox_environment | combine({ "PYTHON": (tox_python or "") }) }} + when: + - tox_python is defined + + +- name: "show tox environment" + debug: var=tox_environment + + +- name: "normalize white spaces from Tox command line" + set_fact: + tox_command_line: '{{ tox_command_line.split() | join(" ") }}' + + +- name: "run Tox on direcory '{{ tox_dir }}': '{{ tox_command_line }}'" + command: + chdir: '{{ tox_dir }}' + cmd: '{{ tox_command_line }}' register: run_tox environment: '{{ tox_environment }}' diff --git a/tools/ci/activate b/tools/ci/activate index 4f374dd0e..c70799dca 100644 --- a/tools/ci/activate +++ b/tools/ci/activate @@ -14,7 +14,7 @@ function get_realpath { ( export PATH=${CI_TOOLS_ORIGINAL_PATH:-${PATH}} local tools_dir=$(dirname "${BASH_SOURCE[0]}") - local python=$(which python3 || which python2 || wich python) + local python=$(which python3 || which python2 || which python) if [ -x "${python}" ] && "${python}" --version > /dev/null; then local script="${tools_dir}/realpath.py" local script=$("${python}" "${script}" "${script}") diff --git a/tools/setup_infrared.py b/tools/setup_infrared.py new file mode 100644 index 000000000..79be6eef1 --- /dev/null +++ b/tools/setup_infrared.py @@ -0,0 +1,85 @@ +# Copyright 2018 Red Hat +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +from __future__ import absolute_import + +import logging +import os +import subprocess +import sys + + +LOG = logging.getLogger(__name__) + + +def main(): + setup_logging() + add_plugin('tobiko', os.environ.get('IR_TOBIKO_PLUGIN')) + import_workspace(os.environ.get('IR_WORKSPACE_FILE')) + + +def setup_logging(level=logging.DEBUG): + logging.basicConfig( + level=level, stream=sys.stderr, + format='%(name)-s: %(levelname)-7s %(asctime)-15s | %(message)s') + + +def add_plugin(name, path): + path = path or os.environ.get('IR_TOBIKO_PLUGIN') + if path: + path = normalize_path(path) + if os.path.isdir(path): + remove_plugin(name) + execute('ir plugin add "{}"', path) + + +def remove_plugin(name): + try: + execute('ir plugin remove "{}"', name) + except subprocess.CalledProcessError: + return False + else: + return True + + +def import_workspace(filename): + if filename: + filename = normalize_path(filename) + if os.path.isfile(filename): + try: + execute('ir workspace import "{}"', filename) + except subprocess.CalledProcessError: + # If file was already imported before we checkout to its + # workspace + workspace = name_from_path(filename) + execute('ir workspace checkout "{}"', workspace) + + +def normalize_path(path): + return os.path.realpath(os.path.expanduser(path)) + + +def execute(command, *args, **kwargs): + if args or kwargs: + command = command.format(*args, **kwargs) + LOG.info('%s', command) + return subprocess.check_output(command, shell=True) + + +def name_from_path(path): + return os.path.splitext(os.path.basename(path))[0] + + +if __name__ == '__main__': + LOG = logging.getLogger(name_from_path(__file__)) + main() diff --git a/tox.ini b/tox.ini index d465e13b9..5dc4e2d3b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] -envlist = linters,py37,docs +envlist = bindep,linters,cover minversion = 3.4.0 @@ -177,11 +177,11 @@ commands = {posargs:bash} [testenv:infrared] -# Package libselinux-python3 still not available on centos-7 -basepython = python -usedevelop = False -skipdist = True -skip_install = True +basepython = {env:PYTHON:python3} +usedevelop = false +skipdist = true +skip_install = true +sitepackages = true deps = -r infrared-requirements.txt passenv = @@ -193,15 +193,20 @@ passenv = setenv = {[testenv:venv]setenv} - IR_COMMAND={env:IR_COMMAND:{toxinidir}/tools/ci/ir} - IR_HOME={env:IR_HOME:{envdir}/home/infrared} ANSIBLE_CONFIG={env:ANSIBLE_CONFIG:{toxinidir}/ansible.cfg} + ANSIBLE_PYTHON_INTERPRETER={env:ANSIBLE_PYTHON_INTERPRETER:python3} + IR_HOME={env:IR_HOME:{envdir}/home/infrared} + IR_TOBIKO_PLUGIN={env:IR_TOBIKO_PLUGIN:{toxinidir}/roles} + IR_WORKSPACE_FILE={env:IR_WORKSPACE_FILE:{toxinidir}/workspace.tgz} + PYTHON={env:PYTHON:python3} + commands_pre = - -ir plugin remove tobiko - ir plugin add {toxinidir}/roles - -ir workspace import {env:IR_WORKSPACE_FILE:{toxinidir}/workspace.tgz} + {envpython} {toxinidir}/tools/setup_infrared.py + commands = - ir tobiko --tobiko-src-dir {toxinidir} {posargs} + ir tobiko \ + --ansible-python-interpreter {env:ANSIBLE_PYTHON_INTERPRETER} \ + --tobiko-src-dir {toxinidir} {posargs} # --- documentation environments ---------------------------------------------- @@ -257,10 +262,10 @@ commands = [testenv:bindep] -basepython = python3 +basepython = {env:PYTHON:python3} # Do not install any requirements. We want this to be fast and work even if # system dependencies are missing, since it's used to tell you what system # dependencies are missing! This also means that bindep must be installed # separately, outside of the requirements files. deps = bindep -commands = bindep test +commands = bindep {posargs:test} diff --git a/zuul.d/infrared.yaml b/zuul.d/infrared.yaml index c641d5ff4..2f4f0b9ec 100644 --- a/zuul.d/infrared.yaml +++ b/zuul.d/infrared.yaml @@ -12,7 +12,8 @@ test_results_dir: '{{ zuul_output_dir }}/logs' tox_envlist: infrared tox_extra_args: > - -- --collect-dir '{{ test_results_dir }}' + -- + --collect-dir '{{ test_results_dir }}' pre-run: playbooks/infrared/pre.yaml post-run: playbooks/infrared/post.yaml