Skip to content

Commit

Permalink
add tests for inventory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeni committed Apr 22, 2022
1 parent 57af01f commit 14056df
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 1 deletion.
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 @@ -58,7 +58,9 @@ def test_inventory(tmpdir, module):
if LooseVersion(ansible_version) < LooseVersion('2.9'):
pytest.skip("This module should not be tested on Ansible before 2.9")
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
19 changes: 19 additions & 0 deletions tests/test_playbooks/inventory_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,22 @@
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: Decode cache content from JSON
set_fact:
cache_content: "{{ cache_slurp['content'] | b64decode | from_json }}"
24 changes: 24 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,27 @@
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
- "{{ 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') }}"
2 changes: 2 additions & 0 deletions tests/test_playbooks/tasks/inventory_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
block:
- name: execute tests
include_tasks: inventory_plugin_tests.yml
- name: execute tests again, now fed from cache
include_tasks: inventory_plugin_tests.yml
always:
- name: remove containers
docker_container:
Expand Down

0 comments on commit 14056df

Please sign in to comment.