diff --git a/roles/ensure-tox/README.rst b/roles/ensure-tox/README.rst index 5dd3d59fb..4213a2b9e 100644 --- a/roles/ensure-tox/README.rst +++ b/roles/ensure-tox/README.rst @@ -1,14 +1,19 @@ Ensure tox is installed -If tox is not already installed, it will be installed via pip in the -user install directory (i.e., "pip install --user"). +Look for ``tox``, and if not found, install it via ``pip`` in the user +install directory (i.e., ``pip install --user``). + +After running this role, ``tox_executable`` will be set as the path to +a valid ``tox``. **Role Variables** .. zuul:rolevar:: tox_executable :default: ``tox`` - Optional path to point tox executable + Look for an existing ``tox`` at this specific path. Note the + default (``tox``) effectively means to find tox in the current + ``$PATH``. .. zuul:rolevar:: tox_prefer_python2 :default: ``false`` diff --git a/roles/ensure-tox/tasks/main.yaml b/roles/ensure-tox/tasks/main.yaml index ec44cc1d6..940b455aa 100644 --- a/roles/ensure-tox/tasks/main.yaml +++ b/roles/ensure-tox/tasks/main.yaml @@ -16,17 +16,23 @@ fi {% endif %} - type tox || $PIP install --user tox + type {{ tox_executable }} || $PIP install --user tox args: executable: /bin/bash register: result changed_when: "'Successfully installed' in result.stdout" -- name: Set tox_executable fact +- name: Set tox_executable fact to pip installed set_fact: tox_executable: "{{ ansible_user_dir }}/.local/bin/tox" cacheable: true when: result is changed +- name: Set tox_exectuable fact to found tox + set_fact: + tox_executable: "{{ tox_executable }}" + cacheable: true + when: result is not changed + - name: Output tox version command: "{{ tox_executable }} --version" diff --git a/test-playbooks/ensure-tox.yaml b/test-playbooks/ensure-tox.yaml index 2f561b0c4..1f790e1ef 100644 --- a/test-playbooks/ensure-tox.yaml +++ b/test-playbooks/ensure-tox.yaml @@ -1,35 +1,5 @@ - hosts: all - name: Test ensure-tox when tox is installed in system default - tasks: - - name: Verify tox is pre-installed on OpenDev images - command: "tox --version" - - name: Run ensure-tox with tox already installed - include_role: - name: ensure-tox - - name: Verify tox_executable is not set by ensure-tox - assert: - that: - - tox_executable is not defined - -- hosts: all - name: Test ensure-tox when tox_executable is already set and tox is installed - tasks: - - name: Install tox inside a virtualenv - pip: - name: tox - virtualenv: "{{ ansible_user_dir }}/tox-venv" - - name: Run ensure-tox with tox not installed - include_role: - name: ensure-tox - vars: - tox_executable: "{{ ansible_user_dir }}/tox-venv/bin/tox" - - name: Verify tox_executable is not set by ensure-tox - assert: - that: - - tox_executable is not defined - -- hosts: all - name: Remove pre-installed tox + name: Remove any pre-installed tox tasks: - name: Remove tox package with pip shell: pip uninstall -y tox @@ -45,12 +15,8 @@ failed_when: result.rc == 0 - hosts: all - name: Test ensure-tox when tox is not installed + name: Test ensure-tox installs into user environment tasks: - - name: Remove tox package - shell: pip uninstall tox || pip3 uninstall tox - failed_when: false - become: true - name: Verify tox is not installed command: "tox --version" register: result @@ -66,3 +32,21 @@ command: "{{ tox_executable }} --version" register: result failed_when: result.rc != 0 + +- hosts: all + name: Test ensure-tox when tox_executable is set to an already installed tox + tasks: + - name: Install tox inside a virtualenv + pip: + name: tox + virtualenv: "{{ ansible_user_dir }}/tox-venv" + - name: Run ensure-tox pointing to an already installed tox + include_role: + name: ensure-tox + vars: + tox_executable: "{{ ansible_user_dir }}/tox-venv/bin/tox" + - name: Verify tox_executable is set to the virtualenv tox + assert: + that: + - tox_executable == '{{ ansible_user_dir}}/tox-venv/bin/tox' +