From 15d22c79ed68dab7acc475eb679082fc5370a70b Mon Sep 17 00:00:00 2001 From: Evgeni Golov Date: Thu, 21 Apr 2022 14:43:40 +0200 Subject: [PATCH] add tests for inventory cache --- tests/inventory/inventory_plugin.foreman.yml | 4 +++ .../inventory_plugin_ansible.foreman.yml | 4 +++ tests/test_crud.py | 4 ++- tests/test_playbooks/inventory_plugin.yml | 28 +++++++++++++++++++ .../inventory_plugin_ansible.yml | 23 +++++++++++++++ .../test_playbooks/tasks/inventory_plugin.yml | 2 ++ 6 files changed, 64 insertions(+), 1 deletion(-) 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..89ee73e286 100644 --- a/tests/test_playbooks/inventory_plugin.yml +++ b/tests/test_playbooks/inventory_plugin.yml @@ -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') }}" + - 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..ee954ed9f7 100644 --- a/tests/test_playbooks/inventory_plugin_ansible.yml +++ b/tests/test_playbooks/inventory_plugin_ansible.yml @@ -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') }}" 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: