From bafc1f8a4103390f74a9c717e7c61fd82d7d92b3 Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Tue, 27 Nov 2018 13:01:56 -0500 Subject: [PATCH] fix constructed functionality in openstack inventory plugin (#48833) * test for openstack inventory constructed functionality this adds unit tests for the compose, groups, and keyed_var features of the openstack inventory plugin * fix constructed functionality in openstack inventory plugin The compose, groups, and keyed_groups functionality of the openstack inventory plugin was broken: - the plugin was not passing the correct variables to the Constructable methods for compose and groups - the plugin was simply never calling the appropriate method for implementing keyed_groups This commit fixes both issues. --- lib/ansible/plugins/inventory/openstack.py | 8 +- .../units/plugins/inventory/test_openstack.py | 90 +++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 test/units/plugins/inventory/test_openstack.py diff --git a/lib/ansible/plugins/inventory/openstack.py b/lib/ansible/plugins/inventory/openstack.py index 74fb68ee24c..9c9a1e03001 100644 --- a/lib/ansible/plugins/inventory/openstack.py +++ b/lib/ansible/plugins/inventory/openstack.py @@ -240,7 +240,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # create composite vars self._set_composite_vars( - self._config_data.get('compose'), hostvars, host) + self._config_data.get('compose'), hostvars[host], host) # actually update inventory for key in hostvars[host]: @@ -248,7 +248,11 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # constructed groups based on conditionals self._add_host_to_composed_groups( - self._config_data.get('groups'), hostvars, host) + self._config_data.get('groups'), hostvars[host], host) + + # constructed groups based on jinja expressions + self._add_host_to_keyed_groups( + self._config_data.get('keyed_groups'), hostvars[host], host) for group_name, group_hosts in groups.items(): self.inventory.add_group(group_name) diff --git a/test/units/plugins/inventory/test_openstack.py b/test/units/plugins/inventory/test_openstack.py new file mode 100644 index 00000000000..fec7ae580c4 --- /dev/null +++ b/test/units/plugins/inventory/test_openstack.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- + +# Copyright 2018 Lars Kellogg-Stedman +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import pytest + +from ansible.plugins.inventory.openstack import InventoryModule +from ansible.inventory.data import InventoryData +from ansible.template import Templar + + +config_data = { + 'plugin': 'openstack', + 'compose': { + 'composed_var': '"testvar-" + testvar', + }, + 'groups': { + 'testgroup': '"host" in inventory_hostname', + }, + 'keyed_groups': + [{ + 'prefix': 'keyed', + 'key': 'testvar', + }] +} + +hostvars = { + 'host0': { + 'inventory_hostname': 'host0', + 'testvar': '0', + }, + 'host1': { + 'inventory_hostname': 'host1', + 'testvar': '1', + }, +} + + +@pytest.fixture(scope="module") +def inventory(): + inventory = InventoryModule() + inventory._config_data = config_data + inventory.inventory = InventoryData() + inventory.templar = Templar(loader=None) + + for host in hostvars: + inventory.inventory.add_host(host) + + return inventory + + +def test_simple_groups(inventory): + inventory._set_variables(hostvars, {}) + groups = inventory.inventory.get_groups_dict() + assert 'testgroup' in groups + assert len(groups['testgroup']) == len(hostvars) + + +def test_keyed_groups(inventory): + inventory._set_variables(hostvars, {}) + assert 'keyed_0' in inventory.inventory.groups + assert 'keyed_1' in inventory.inventory.groups + + +def test_composed_vars(inventory): + inventory._set_variables(hostvars, {}) + + for host in hostvars: + assert host in inventory.inventory.hosts + host = inventory.inventory.get_host(host) + assert host.vars['composed_var'] == 'testvar-{testvar}'.format(**hostvars[host.name])