testinfra: pass inventory and zuul data

Create a zuul_data fixture for testinfra.

The fixture directly loads the inventory from the inventory YAML file
written out.  This lets you get easy access to the IP addresses of the
hosts.

We pass in the "zuul" variable by writing it out to a YAML file on
disk, and then passing an environment variable to this.  This is
useful for things like determining which job is running.  Additional
arbitrary data could be added to this if required.

Change-Id: I8adb7601f7eec6d48509f8f1a42840beca70120c
This commit is contained in:
Ian Wienand 2020-05-20 09:06:40 +10:00
parent 728f8a9ee5
commit 0d004ea73d
4 changed files with 43 additions and 0 deletions

View File

@ -110,6 +110,18 @@
- name: Run test playbook - name: Run test playbook
when: run_test_playbook is defined when: run_test_playbook is defined
shell: "ANSIBLE_ROLES_PATH=/home/zuul/src/opendev.org/opendev/system-config/playbooks/roles ansible-playbook -v /home/zuul/src/opendev.org/opendev/system-config/{{ run_test_playbook }}" shell: "ANSIBLE_ROLES_PATH=/home/zuul/src/opendev.org/opendev/system-config/playbooks/roles ansible-playbook -v /home/zuul/src/opendev.org/opendev/system-config/{{ run_test_playbook }}"
- name: Generate testinfra extra data fixture
set_fact:
testinfra_extra_data:
zuul_job: '{{ zuul.job }}'
zuul: '{{ zuul }}'
- name: Write out testinfra extra data fixture
copy:
content: '{{ testinfra_extra_data | to_nice_yaml }}'
dest: '/home/zuul/testinfra_extra_data_fixture.yaml'
- name: Run testinfra to validate configuration - name: Run testinfra to validate configuration
include_role: include_role:
name: tox name: tox
@ -117,4 +129,6 @@
tox_envlist: testinfra tox_envlist: testinfra
# This allows us to run from external projects (like testinfra # This allows us to run from external projects (like testinfra
# itself) # itself)
tox_environment:
TESTINFRA_EXTRA_DATA: '/home/zuul/testinfra_extra_data_fixture.yaml'
zuul_work_dir: src/opendev.org/opendev/system-config zuul_work_dir: src/opendev.org/opendev/system-config

20
testinfra/conftest.py Normal file
View File

@ -0,0 +1,20 @@
import os
import pytest
import yaml
@pytest.fixture
def zuul_data():
data = {}
with open('/etc/ansible/hosts/inventory.yaml') as f:
inventory = yaml.load(f)
data['inventory'] = inventory
zuul_extra_data_file = os.environ.get('TESTINFRA_EXTRA_DATA')
if os.path.exists(zuul_extra_data_file):
with open(zuul_extra_data_file, 'r') as f:
extra = yaml.load(f)
data['extra'] = extra
return data

View File

@ -17,6 +17,13 @@ import pytest
testinfra_hosts = ['bridge.openstack.org'] testinfra_hosts = ['bridge.openstack.org']
def test_zuul_data(host, zuul_data):
# Test the zuul_data fixture that picks up things set by Zuul
assert 'inventory' in zuul_data
assert 'extra' in zuul_data
assert 'zuul' in zuul_data['extra']
def test_clouds_yaml(host): def test_clouds_yaml(host):
clouds_yaml = host.file('/etc/openstack/clouds.yaml') clouds_yaml = host.file('/etc/openstack/clouds.yaml')
assert clouds_yaml.exists assert clouds_yaml.exists

View File

@ -33,6 +33,8 @@ deps = -r{toxinidir}/doc/requirements.txt
commands = sphinx-build -W -E -b html doc/source doc/build/html commands = sphinx-build -W -E -b html doc/source doc/build/html
[testenv:testinfra] [testenv:testinfra]
passenv =
TESTINFRA_EXTRA_DATA
commands = py.test --junit-xml junit.xml --connection=ansible --ansible-inventory=/etc/ansible/hosts/inventory.yaml -v testinfra {posargs} commands = py.test --junit-xml junit.xml --connection=ansible --ansible-inventory=/etc/ansible/hosts/inventory.yaml -v testinfra {posargs}
[flake8] [flake8]