diff options
Diffstat (limited to 'test/integration/targets/plugin_config_for_inventory')
6 files changed, 176 insertions, 0 deletions
diff --git a/test/integration/targets/plugin_config_for_inventory/aliases b/test/integration/targets/plugin_config_for_inventory/aliases new file mode 100644 index 0000000..1d28bdb --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/aliases @@ -0,0 +1,2 @@ +shippable/posix/group5 +context/controller diff --git a/test/integration/targets/plugin_config_for_inventory/cache_plugins/none.py b/test/integration/targets/plugin_config_for_inventory/cache_plugins/none.py new file mode 100644 index 0000000..62a91c8 --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/cache_plugins/none.py @@ -0,0 +1,62 @@ +# (c) 2014, Brian Coca, Josh Drake, et al +# (c) 2017 Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible.plugins.cache import BaseCacheModule + +DOCUMENTATION = ''' + cache: none + short_description: write-only cache (no cache) + description: + - No caching at all + version_added: historical + author: core team (@ansible-core) + options: + _timeout: + default: 86400 + description: Expiration timeout for the cache plugin data + env: + - name: ANSIBLE_CACHE_PLUGIN_TIMEOUT + ini: + - key: fact_caching_timeout + section: defaults + type: integer +''' + + +class CacheModule(BaseCacheModule): + def __init__(self, *args, **kwargs): + super(CacheModule, self).__init__(*args, **kwargs) + self.empty = {} + self._timeout = self.get_option('_timeout') + + def get(self, key): + return self.empty.get(key) + + def set(self, key, value): + return value + + def keys(self): + return self.empty.keys() + + def contains(self, key): + return key in self.empty + + def delete(self, key): + del self.emtpy[key] + + def flush(self): + self.empty = {} + + def copy(self): + return self.empty.copy() + + def __getstate__(self): + return self.copy() + + def __setstate__(self, data): + self.empty = data diff --git a/test/integration/targets/plugin_config_for_inventory/config_with_parameter.yml b/test/integration/targets/plugin_config_for_inventory/config_with_parameter.yml new file mode 100644 index 0000000..b9e367b --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/config_with_parameter.yml @@ -0,0 +1,5 @@ +plugin: test_inventory +departments: + - paris +cache: yes +cache_timeout: 0 diff --git a/test/integration/targets/plugin_config_for_inventory/config_without_parameter.yml b/test/integration/targets/plugin_config_for_inventory/config_without_parameter.yml new file mode 100644 index 0000000..787cf96 --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/config_without_parameter.yml @@ -0,0 +1 @@ +plugin: test_inventory diff --git a/test/integration/targets/plugin_config_for_inventory/runme.sh b/test/integration/targets/plugin_config_for_inventory/runme.sh new file mode 100755 index 0000000..2a22325 --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/runme.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o xtrace + +export ANSIBLE_INVENTORY_PLUGINS=./ +export ANSIBLE_INVENTORY_ENABLED=test_inventory + +# check default values +ansible-inventory --list -i ./config_without_parameter.yml --export | \ + env python -c "import json, sys; inv = json.loads(sys.stdin.read()); \ + assert set(inv['_meta']['hostvars']['test_host']['departments']) == set(['seine-et-marne', 'haute-garonne'])" + +# check values +ansible-inventory --list -i ./config_with_parameter.yml --export | \ + env python -c "import json, sys; inv = json.loads(sys.stdin.read()); \ + assert set(inv['_meta']['hostvars']['test_host']['departments']) == set(['paris'])" + +export ANSIBLE_CACHE_PLUGINS=cache_plugins/ +export ANSIBLE_CACHE_PLUGIN=none +ansible-inventory --list -i ./config_with_parameter.yml --export | \ + env python -c "import json, sys; inv = json.loads(sys.stdin.read()); \ + assert inv['_meta']['hostvars']['test_host']['given_timeout'] == inv['_meta']['hostvars']['test_host']['cache_timeout']" diff --git a/test/integration/targets/plugin_config_for_inventory/test_inventory.py b/test/integration/targets/plugin_config_for_inventory/test_inventory.py new file mode 100644 index 0000000..f937c03 --- /dev/null +++ b/test/integration/targets/plugin_config_for_inventory/test_inventory.py @@ -0,0 +1,84 @@ +from __future__ import (absolute_import, division, print_function) + +__metaclass__ = type + +DOCUMENTATION = ''' + name: test_inventory + plugin_type: inventory + authors: + - Pierre-Louis Bonicoli (@pilou-) + short_description: test inventory + description: + - test inventory (fetch parameters using config API) + options: + departments: + description: test parameter + type: list + default: + - seine-et-marne + - haute-garonne + required: False + cache: + description: cache + type: bool + default: false + required: False + cache_plugin: + description: cache plugin + type: str + default: none + required: False + cache_timeout: + description: test cache parameter + type: integer + default: 7 + required: False + cache_connection: + description: cache connection + type: str + default: /tmp/foo + required: False + cache_prefix: + description: cache prefix + type: str + default: prefix_ + required: False +''' + +EXAMPLES = ''' +# Example command line: ansible-inventory --list -i test_inventory.yml + +plugin: test_inventory +departments: + - paris +''' + +from ansible.plugins.inventory import BaseInventoryPlugin + + +class InventoryModule(BaseInventoryPlugin): + NAME = 'test_inventory' + + def verify_file(self, path): + return True + + def parse(self, inventory, loader, path, cache=True): + super(InventoryModule, self).parse(inventory, loader, path) + config_data = self._read_config_data(path=path) + self._consume_options(config_data) + + departments = self.get_option('departments') + + group = 'test_group' + host = 'test_host' + + self.inventory.add_group(group) + self.inventory.add_host(group=group, host=host) + self.inventory.set_variable(host, 'departments', departments) + + # Ensure the timeout we're given gets sent to the cache plugin + if self.get_option('cache'): + given_timeout = self.get_option('cache_timeout') + cache_timeout = self._cache._plugin.get_option('_timeout') + self.inventory.set_variable(host, 'given_timeout', given_timeout) + self.inventory.set_variable(host, 'cache_timeout', cache_timeout) |