Skip to content

Commit 15d22c7

Browse files
committed
add tests for inventory cache
1 parent 57af01f commit 15d22c7

6 files changed

Lines changed: 64 additions & 1 deletion

File tree

tests/inventory/inventory_plugin.foreman.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ validate_certs: False
66
want_params: True
77
want_facts: True
88
use_reports_api: False
9+
10+
cache: true
11+
cache_plugin: jsonfile
12+
cache_timeout: 3600

tests/inventory/inventory_plugin_ansible.foreman.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ want_facts: True
88
want_content_facet_attributes: False
99
want_hostcollections: False
1010
use_reports_api: True
11+
12+
cache: true
13+
cache_plugin: jsonfile
14+
cache_timeout: 3600

tests/test_crud.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def test_inventory(tmpdir, module):
5858
if LooseVersion(ansible_version) < LooseVersion('2.9'):
5959
pytest.skip("This module should not be tested on Ansible before 2.9")
6060
inventory = [os.path.join(os.getcwd(), 'tests', 'inventory', inv) for inv in ['hosts', "{}.foreman.yml".format(module)]]
61-
run = run_playbook(module, inventory=inventory)
61+
extra_env = {}
62+
extra_env['ANSIBLE_INVENTORY_CACHE_CONNECTION'] = tmpdir.strpath
63+
run = run_playbook(module, inventory=inventory, extra_env=extra_env)
6264
assert run.rc == 0
6365

6466
_assert_no_warnings(run)

tests/test_playbooks/inventory_plugin.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,31 @@
2323
tasks:
2424
- name: test inventory
2525
include_tasks: tasks/inventory_plugin.yml
26+
27+
- name: Find cache file
28+
find:
29+
paths: "{{ lookup('env', 'ANSIBLE_INVENTORY_CACHE_CONNECTION') }}"
30+
patterns: 'ansible_inventory_*'
31+
register: cache_files
32+
33+
- name: Assert exactly one cache file was found
34+
assert:
35+
that: cache_files['files'] | length == 1
36+
37+
- name: Fetch cache file
38+
slurp:
39+
src: "{{ cache_files['files'][0]['path'] }}"
40+
register: cache_slurp
41+
42+
- name: Assert cache content
43+
assert:
44+
that:
45+
- cache_content_host['hostgroup_name'] == 'group_a'
46+
- cache_content_host['all_parameters'] != []
47+
- cache_content_facts['testhost1.example.com']['operatingsystem'] == 'CentOS'
48+
vars:
49+
cache_content_host: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3000//api/v2/hosts/1') }}"
50+
cache_content_facts: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3000//api/v2/hosts/1/facts') }}"
51+
- name: Decode cache content from JSON
52+
set_fact:
53+
cache_content: "{{ cache_slurp['content'] | b64decode | from_json }}"

tests/test_playbooks/inventory_plugin_ansible.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,26 @@
2323
tasks:
2424
- name: test inventory
2525
include_tasks: tasks/inventory_plugin.yml
26+
27+
- name: Find cache file
28+
find:
29+
paths: "{{ lookup('env', 'ANSIBLE_INVENTORY_CACHE_CONNECTION') }}"
30+
patterns: 'ansible_inventory_*'
31+
register: cache_files
32+
33+
- name: Assert exactly one cache file was found
34+
assert:
35+
that: cache_files['files'] | length == 1
36+
37+
- name: Fetch cache file
38+
slurp:
39+
src: "{{ cache_files['files'][0]['path'] }}"
40+
register: cache_slurp
41+
42+
- name: Assert cache content
43+
assert:
44+
that:
45+
- "{{ cache_content | selectattr('name', 'equalto', 'testhost1.example.com') | map(attribute='name') | list }} == ['testhost1.example.com']"
46+
- "{{ cache_content | selectattr('name', 'equalto', 'testhost1.example.com') | map(attribute='facts', default=[]) | list | first }} != []"
47+
vars:
48+
cache_content: "{{ (cache_slurp['content'] | b64decode | from_json).get('http://localhost:3001//ansible/api/v2/ansible_inventories/schedule') }}"

tests/test_playbooks/tasks/inventory_plugin.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
block:
77
- name: execute tests
88
include_tasks: inventory_plugin_tests.yml
9+
- name: execute tests again, now fed from cache
10+
include_tasks: inventory_plugin_tests.yml
911
always:
1012
- name: remove containers
1113
docker_container:

0 commit comments

Comments
 (0)