From 0d004ea73dad305b8be57cf2d7d4d58f229566a2 Mon Sep 17 00:00:00 2001 From: Ian Wienand Date: Wed, 20 May 2020 09:06:40 +1000 Subject: [PATCH] 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 --- playbooks/zuul/run-base.yaml | 14 ++++++++++++++ testinfra/conftest.py | 20 ++++++++++++++++++++ testinfra/test_bridge.py | 7 +++++++ tox.ini | 2 ++ 4 files changed, 43 insertions(+) create mode 100644 testinfra/conftest.py diff --git a/playbooks/zuul/run-base.yaml b/playbooks/zuul/run-base.yaml index 033c2c400c..e1ef7ac806 100644 --- a/playbooks/zuul/run-base.yaml +++ b/playbooks/zuul/run-base.yaml @@ -110,6 +110,18 @@ - name: Run test playbook 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 }}" + + - 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 include_role: name: tox @@ -117,4 +129,6 @@ tox_envlist: testinfra # This allows us to run from external projects (like testinfra # itself) + tox_environment: + TESTINFRA_EXTRA_DATA: '/home/zuul/testinfra_extra_data_fixture.yaml' zuul_work_dir: src/opendev.org/opendev/system-config diff --git a/testinfra/conftest.py b/testinfra/conftest.py new file mode 100644 index 0000000000..be545d7395 --- /dev/null +++ b/testinfra/conftest.py @@ -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 diff --git a/testinfra/test_bridge.py b/testinfra/test_bridge.py index 3947bff963..61fff1dc8a 100644 --- a/testinfra/test_bridge.py +++ b/testinfra/test_bridge.py @@ -17,6 +17,13 @@ import pytest 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): clouds_yaml = host.file('/etc/openstack/clouds.yaml') assert clouds_yaml.exists diff --git a/tox.ini b/tox.ini index 4f4e5c83e0..8cbe0a8a08 100644 --- a/tox.ini +++ b/tox.ini @@ -33,6 +33,8 @@ deps = -r{toxinidir}/doc/requirements.txt commands = sphinx-build -W -E -b html doc/source doc/build/html [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} [flake8]