From 567fda6906e22ff06e1547110ddba5821b19f38e Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 13 May 2025 14:18:32 -0700 Subject: [PATCH] Add missing cache plugin test scenarios (#85154) --- .../targets/cache-plugins/inspect_cache.yml | 2 ++ .../cache-plugins/inspect_inventory_cache.yml | 11 +++++++++++ .../inventory_plugins/test_inventoryconfig.py | 17 ++++++++++++++++- test/integration/targets/cache-plugins/runme.sh | 10 +++++++++- .../cache-plugins/test_inventory_cache.yml | 2 -- 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 test/integration/targets/cache-plugins/inspect_inventory_cache.yml diff --git a/test/integration/targets/cache-plugins/inspect_cache.yml b/test/integration/targets/cache-plugins/inspect_cache.yml index 42a4de59107..2ea138e58e3 100644 --- a/test/integration/targets/cache-plugins/inspect_cache.yml +++ b/test/integration/targets/cache-plugins/inspect_cache.yml @@ -6,6 +6,7 @@ that: - ansible_facts | length == 1 - ansible_facts.foo == 'bar' + - ansible_facts.foo is ansible._protomatter.tagged_with "Origin" tags: - set_fact @@ -15,6 +16,7 @@ - ansible_facts | length > 1 - ansible_facts.foo == 'bar' - ansible_facts.distribution is defined + - ansible_facts.foo is ansible._protomatter.tagged_with "Origin" tags: - additive_gather_facts diff --git a/test/integration/targets/cache-plugins/inspect_inventory_cache.yml b/test/integration/targets/cache-plugins/inspect_inventory_cache.yml new file mode 100644 index 00000000000..ac635fdc2c7 --- /dev/null +++ b/test/integration/targets/cache-plugins/inspect_inventory_cache.yml @@ -0,0 +1,11 @@ +- hosts: all + gather_facts: no + tasks: + - name: verify a template retrieved from the cache renders + debug: + var: my_template + + - name: verify the values retrieved from the cache are correct + assert: + that: + - my_template == verify diff --git a/test/integration/targets/cache-plugins/inventory_plugins/test_inventoryconfig.py b/test/integration/targets/cache-plugins/inventory_plugins/test_inventoryconfig.py index 7fa88079882..5d1177150f9 100644 --- a/test/integration/targets/cache-plugins/inventory_plugins/test_inventoryconfig.py +++ b/test/integration/targets/cache-plugins/inventory_plugins/test_inventoryconfig.py @@ -12,6 +12,7 @@ DOCUMENTATION = """ """ from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable +from ansible.template import trust_as_template class InventoryModule(BaseInventoryPlugin, Cacheable): @@ -21,11 +22,23 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): def populate(self, hosts): for host in list(hosts.keys()): self.inventory.add_host(host, group='all') + for hostvar, hostval in hosts[host].items(): self.inventory.set_variable(host, hostvar, hostval) def get_hosts(self): - return {'host1': {'one': 'two'}, 'host2': {'three': 'four'}} + return dict( + host1=dict( + one='two', + my_template=trust_as_template("{{ one }}"), + verify='two', + ), + host2=dict( + three='four', + my_template=trust_as_template("{{ three }}"), + verify='four', + ), + ) def verify_file(self, path): return path.endswith('.inventoryconfig.yml') @@ -44,6 +57,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): attempt_to_read_cache = cache_setting and cache cache_needs_update = cache_setting and not cache + results = {} + # attempt to read the cache if inventory isn't being refreshed and the user has caching enabled if attempt_to_read_cache: try: diff --git a/test/integration/targets/cache-plugins/runme.sh b/test/integration/targets/cache-plugins/runme.sh index a62c28ae150..65350e1dfb7 100755 --- a/test/integration/targets/cache-plugins/runme.sh +++ b/test/integration/targets/cache-plugins/runme.sh @@ -2,7 +2,8 @@ set -eux -source virtualenv.sh +# Test Fact Caching + export ANSIBLE_CACHE_PLUGIN="dummy_cache" # test set_fact with cacheable: true @@ -19,6 +20,13 @@ ansible-playbook inspect_cache.yml --tags gather_facts "$@" unset ANSIBLE_CACHE_PLUGIN +# Test Inventory Caching + +export ANSIBLE_INVENTORY_CACHE=true +export ANSIBLE_INVENTORY_CACHE_PLUGIN=dummy_cache + # Legacy cache plugins need to be updated to use set_options/get_option to be compatible with inventory plugins. # Inventory plugins load cache options with the config manager. ansible-playbook test_inventory_cache.yml "$@" + +ansible-playbook inspect_inventory_cache.yml -i test.inventoryconfig.yml "$@" diff --git a/test/integration/targets/cache-plugins/test_inventory_cache.yml b/test/integration/targets/cache-plugins/test_inventory_cache.yml index ea08851043d..3c0e86d69d8 100644 --- a/test/integration/targets/cache-plugins/test_inventory_cache.yml +++ b/test/integration/targets/cache-plugins/test_inventory_cache.yml @@ -5,8 +5,6 @@ no_parse_message: "No inventory was parsed, only implicit localhost is available" expected_host_name: host1 base_environment: - ANSIBLE_INVENTORY_CACHE: true - ANSIBLE_INVENTORY_CACHE_PLUGIN: dummy_cache ANSIBLE_FORCE_COLOR: 0 legacy_cache: DUMMY_CACHE_SKIP_SUPER: 1