diff --git a/tests/inventory/inventory_plugin.foreman.yml b/tests/inventory/inventory_plugin.foreman.yml index c87fa8694a..9742ae35bc 100644 --- a/tests/inventory/inventory_plugin.foreman.yml +++ b/tests/inventory/inventory_plugin.foreman.yml @@ -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 diff --git a/tests/inventory/inventory_plugin_ansible.foreman.yml b/tests/inventory/inventory_plugin_ansible.foreman.yml index ba1b40f334..254470b004 100644 --- a/tests/inventory/inventory_plugin_ansible.foreman.yml +++ b/tests/inventory/inventory_plugin_ansible.foreman.yml @@ -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 diff --git a/tests/test_crud.py b/tests/test_crud.py index cd95c768d5..cdb841278d 100644 --- a/tests/test_crud.py +++ b/tests/test_crud.py @@ -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) diff --git a/tests/test_playbooks/inventory_plugin.yml b/tests/test_playbooks/inventory_plugin.yml index d9cf9915b8..04bb851f87 100644 --- a/tests/test_playbooks/inventory_plugin.yml +++ b/tests/test_playbooks/inventory_plugin.yml @@ -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 }}" diff --git a/tests/test_playbooks/inventory_plugin_ansible.yml b/tests/test_playbooks/inventory_plugin_ansible.yml index 40029a3b37..076be482cc 100644 --- a/tests/test_playbooks/inventory_plugin_ansible.yml +++ b/tests/test_playbooks/inventory_plugin_ansible.yml @@ -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') }}" diff --git a/tests/test_playbooks/tasks/inventory_plugin.yml b/tests/test_playbooks/tasks/inventory_plugin.yml index 1ed3c3dda7..e4aecd679f 100644 --- a/tests/test_playbooks/tasks/inventory_plugin.yml +++ b/tests/test_playbooks/tasks/inventory_plugin.yml @@ -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: