Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for inventory cache #1386

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rpm-py-installer
rstcheck==3.3.1 # from https://github.com/ansible/ansible/raw/devel/test/sanity/code-smell/rstcheck.requirements.txt
docker
ruamel.yaml.clib<0.2.3; python_version < '3.6'
cryptography<3.1; python_version < '3.6'
cryptography<3.1; python_version < '3.7'
-r requirements.txt
pylint==2.6.0; python_version >= '3.6'
voluptuous==0.12.1 # from https://github.com/ansible/ansible/raw/devel/test/lib/ansible_test/_data/requirements/sanity.validate-modules.txt
Expand Down
4 changes: 4 additions & 0 deletions tests/inventory/inventory_plugin.foreman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ validate_certs: False
want_params: True
want_facts: True
use_reports_api: False

cache: true
cache_plugin: jsonfile
cache_timeout: 3600
4 changes: 4 additions & 0 deletions tests/inventory/inventory_plugin_ansible.foreman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ want_facts: True
want_content_facet_attributes: False
want_hostcollections: False
use_reports_api: True

cache: true
cache_plugin: jsonfile
cache_timeout: 3600
4 changes: 3 additions & 1 deletion tests/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def test_check_mode(tmpdir, module):
@pytest.mark.parametrize('module', INVENTORY_PLAYBOOKS)
def test_inventory(tmpdir, module):
inventory = [os.path.join(os.getcwd(), 'tests', 'inventory', inv) for inv in ['hosts', "{}.foreman.yml".format(module)]]
run = run_playbook(module, inventory=inventory)
extra_env = {}
extra_env['ANSIBLE_INVENTORY_CACHE_CONNECTION'] = tmpdir.strpath
run = run_playbook(module, inventory=inventory, extra_env=extra_env)
assert run.rc == 0

_assert_no_warnings(run)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_playbooks/inventory_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,31 @@
tasks:
- name: test inventory
include_tasks: tasks/inventory_plugin.yml

- name: Find cache file
find:
paths: "{{ lookup('env', 'ANSIBLE_INVENTORY_CACHE_CONNECTION') }}"
patterns: 'ansible_inventory_*'
register: cache_files

- name: Assert exactly one cache file was found
assert:
that: cache_files['files'] | length == 1

- name: Fetch cache file
slurp:
src: "{{ cache_files['files'][0]['path'] }}"
register: cache_slurp

- name: Assert cache content
assert:
that:
- cache_content_host['hostgroup_name'] == 'group_a'
- cache_content_host['all_parameters'] != []
- cache_content_facts['testhost1.example.com']['operatingsystem'] == 'CentOS'
vars:
cache_content_host: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3000//api/v2/hosts/1') }}"
cache_content_facts: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3000//api/v2/hosts/1/facts') }}"
Comment on lines +49 to +50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

turns out, the host id is not strictly stable, fuck

- name: Decode cache content from JSON
set_fact:
cache_content: "{{ cache_slurp['content'] | b64decode | from_json }}"
23 changes: 23 additions & 0 deletions tests/test_playbooks/inventory_plugin_ansible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,26 @@
tasks:
- name: test inventory
include_tasks: tasks/inventory_plugin.yml

- name: Find cache file
find:
paths: "{{ lookup('env', 'ANSIBLE_INVENTORY_CACHE_CONNECTION') }}"
patterns: 'ansible_inventory_*'
register: cache_files

- name: Assert exactly one cache file was found
assert:
that: cache_files['files'] | length == 1

- name: Fetch cache file
slurp:
src: "{{ cache_files['files'][0]['path'] }}"
register: cache_slurp

- name: Assert cache content
assert:
that:
- "{{ cache_content | selectattr('name', 'equalto', 'testhost1.example.com') | map(attribute='name') | list }} == ['testhost1.example.com']"
- "{{ cache_content | selectattr('name', 'equalto', 'testhost1.example.com') | map(attribute='facts', default=[]) | list | first }} != []"
vars:
cache_content: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3001//ansible/api/v2/ansible_inventories/schedule') }}"
5 changes: 2 additions & 3 deletions tests/test_playbooks/tasks/inventory_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
- name: setup
include_tasks: inventory_plugin_setup.yml

- name: Refresh inventory to ensure new instances exist in inventory
meta: refresh_inventory

- name: execute tests
block:
- name: execute tests
include_tasks: inventory_plugin_tests.yml
- name: execute tests again, now fed from cache
include_tasks: inventory_plugin_tests.yml
Comment on lines +9 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to work… I would expect the facts asserts to fail for the report api codepath, but they do not.

always:
- name: remove containers
docker_container:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_playbooks/tasks/inventory_plugin_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
- name: Refresh inventory to ensure new instances exist in inventory
meta: refresh_inventory

- name: test that all groups are present
assert:
that: >
Expand Down