Add ensure-nox role

This is an alternative to tox.

Change-Id: I394774836e303584473f0e7d64d496baa393037a
This commit is contained in:
James E. Blair 2022-12-15 13:22:49 -08:00
parent 41153f0653
commit 77b1b24911
6 changed files with 238 additions and 0 deletions

View File

@ -5,6 +5,7 @@ Python Roles
.. zuul:autorole:: build-releasenotes
.. zuul:autorole:: ensure-babel
.. zuul:autorole:: ensure-if-python
.. zuul:autorole:: ensure-nox
.. zuul:autorole:: ensure-pip
.. zuul:autorole:: ensure-python
.. zuul:autorole:: ensure-sphinx

View File

@ -0,0 +1,26 @@
Ensure nox is installed
Look for ``nox``, and if not found, install it via ``pip`` into a
virtual environment for the current user.
**Role Variables**
.. zuul:rolevar:: ensure_nox_version
:default: ''
Version specifier to select the version of nox. The default is the
latest version.
**Output Variables**
.. zuul:rolevar:: nox_executable
:default: nox
After running this role, ``nox_executable`` will be set as the path
to a valid ``nox``.
At role runtime, look for an existing ``nox`` at this specific
path. Note the default (``nox``) effectively means to find tox in
the current ``$PATH``. For example, if your base image
pre-installs tox in an out-of-path environment, set this so the
role does not attempt to install the user version.

View File

@ -0,0 +1,3 @@
nox_executable: nox
ensure_nox_version: ''
nox_venv_path: '{{ ansible_user_dir }}/.local/nox'

View File

@ -0,0 +1,34 @@
- name: Install pip
include_role:
name: ensure-pip
- name: Check if nox is installed
shell: |
command -v {{ nox_executable }} {{ nox_venv_path }}/bin/nox || exit 1
args:
executable: /bin/bash
register: nox_preinstalled
failed_when: false
- name: Export preinstalled nox_exectuable
set_fact:
nox_executable: '{{ nox_preinstalled.stdout_lines[0] }}'
cacheable: true
when: nox_preinstalled.rc == 0
- name: Install nox to local env
when: nox_preinstalled.rc != 0
block:
- name: Create local venv
command: '{{ ensure_pip_virtualenv_command }} {{ nox_venv_path }}'
- name: Install nox to local venv
command: '{{ nox_venv_path }}/bin/pip install nox{{ ensure_nox_version }}'
- name: Export installed nox_executable path
set_fact:
nox_executable: '{{ nox_venv_path }}/bin/nox'
cacheable: true
- name: Output nox version
command: "{{ nox_executable }} --version"

View File

@ -0,0 +1,55 @@
- hosts: all
name: Remove any pre-installed nox
tasks:
- name: Remove nox package with pip
shell: pip uninstall -y nox
args:
warn: false
become: true
failed_when: false
- name: Remove nox package with pip3
shell: pip3 uninstall -y nox
args:
warn: false
become: true
failed_when: false
- name: Verify nox is not installed
command: "nox --version"
register: result
failed_when: result.rc == 0
- hosts: all
name: Test ensure-nox installs into user environment
tasks:
- name: Verify nox is not installed
command: "nox --version"
register: result
failed_when: result.rc == 0
- name: Run ensure-nox with nox not installed
include_role:
name: ensure-nox
- name: Verify nox_executable is set
assert:
that:
- nox_executable == "{{ ansible_user_dir }}/.local/nox/bin/nox"
- name: Verify nox is installed
command: "{{ nox_executable }} --version"
register: result
failed_when: result.rc != 0
- hosts: all
name: Test ensure-nox when nox_executable is set to an already installed nox
tasks:
- name: Create a virtualenv
command: '{{ ensure_pip_virtualenv_command }} {{ ansible_user_dir }}/nox-venv'
- name: Install nox to local venv
command: '{{ ansible_user_dir }}/nox-venv/bin/pip install nox'
- name: Run ensure-nox pointing to an already installed nox
include_role:
name: ensure-nox
vars:
nox_executable: "{{ ansible_user_dir }}/nox-venv/bin/nox"
- name: Verify nox_executable is set to the virtualenv nox
assert:
that:
- nox_executable == '{{ ansible_user_dir }}/nox-venv/bin/nox'

View File

@ -1,3 +1,112 @@
- job:
name: zuul-jobs-test-ensure-nox
description: Test the ensure-nox role
files:
- roles/ensure-nox/.*
- test-playbooks/ensure-nox.yaml
run: test-playbooks/ensure-nox.yaml
tags: all-platforms
- job:
name: zuul-jobs-test-ensure-nox-centos-7
description: Test the ensure-nox role on centos-7
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: centos-7
label: centos-7
- job:
name: zuul-jobs-test-ensure-nox-centos-8-stream
description: Test the ensure-nox role on centos-8-stream
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: centos-8-stream
label: centos-8-stream
- job:
name: zuul-jobs-test-ensure-nox-centos-9-stream
description: Test the ensure-nox role on centos-9-stream
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: centos-9-stream
label: centos-9-stream
- job:
name: zuul-jobs-test-ensure-nox-debian-bullseye
description: Test the ensure-nox role on debian-bullseye
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: debian-bullseye
label: debian-bullseye
- job:
name: zuul-jobs-test-ensure-nox-debian-buster
description: Test the ensure-nox role on debian-buster
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: debian-buster
label: debian-buster
- job:
name: zuul-jobs-test-ensure-nox-fedora-36
description: Test the ensure-nox role on fedora-36
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: fedora-36
label: fedora-36
- job:
name: zuul-jobs-test-ensure-nox-opensuse-15
description: Test the ensure-nox role on opensuse-15
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: opensuse-15
label: opensuse-15
- job:
name: zuul-jobs-test-ensure-nox-ubuntu-bionic
description: Test the ensure-nox role on ubuntu-bionic
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-bionic
label: ubuntu-bionic
- job:
name: zuul-jobs-test-ensure-nox-ubuntu-focal
description: Test the ensure-nox role on ubuntu-focal
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-focal
label: ubuntu-focal
- job:
name: zuul-jobs-test-ensure-nox-ubuntu-jammy
description: Test the ensure-nox role on ubuntu-jammy
parent: zuul-jobs-test-ensure-nox
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-jammy
label: ubuntu-jammy
- job:
name: zuul-jobs-test-ensure-pip
description: Test the ensure-pip role
@ -458,6 +567,16 @@
- project:
check:
jobs: &id001
- zuul-jobs-test-ensure-nox-centos-7
- zuul-jobs-test-ensure-nox-centos-8-stream
- zuul-jobs-test-ensure-nox-centos-9-stream
- zuul-jobs-test-ensure-nox-debian-bullseye
- zuul-jobs-test-ensure-nox-debian-buster
- zuul-jobs-test-ensure-nox-fedora-36
- zuul-jobs-test-ensure-nox-opensuse-15
- zuul-jobs-test-ensure-nox-ubuntu-bionic
- zuul-jobs-test-ensure-nox-ubuntu-focal
- zuul-jobs-test-ensure-nox-ubuntu-jammy
- zuul-jobs-test-ensure-pip-centos-7
- zuul-jobs-test-ensure-pip-centos-8-stream
- zuul-jobs-test-ensure-pip-centos-9-stream