Add missing cache plugin test scenarios (#85154)

pull/85155/head
Matt Clay 8 months ago committed by GitHub
parent e5476972da
commit 567fda6906
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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

@ -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

@ -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:

@ -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 "$@"

@ -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

Loading…
Cancel
Save