diff --git a/requirements-dev.txt b/requirements-dev.txt index 6d3b190826..77a802fe9e 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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 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 8dad7cdee5..caa296d372 100644 --- a/tests/test_crud.py +++ b/tests/test_crud.py @@ -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) 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 093f6c4e52..e4aecd679f 100644 --- a/tests/test_playbooks/tasks/inventory_plugin.yml +++ b/tests/test_playbooks/tasks/inventory_plugin.yml @@ -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 always: - name: remove containers docker_container: diff --git a/tests/test_playbooks/tasks/inventory_plugin_tests.yml b/tests/test_playbooks/tasks/inventory_plugin_tests.yml index ad2e522c90..8bf8cb302a 100644 --- a/tests/test_playbooks/tasks/inventory_plugin_tests.yml +++ b/tests/test_playbooks/tasks/inventory_plugin_tests.yml @@ -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: >