Add inventory filter capability (#698)

* Add inventory filter capability.

* Use community.library_inventory_filtering_v1 collection.

* Bump dependency to 1.0.0.

* Mention the new dependency in the changelog.
This commit is contained in:
Felix Fontein
2024-01-13 15:51:02 +01:00
committed by GitHub
parent 97a0610f25
commit f429017d94
11 changed files with 137 additions and 18 deletions
@@ -9,14 +9,24 @@ __metaclass__ = type
import pytest
from ansible.inventory.data import InventoryData
from ansible.parsing.dataloader import DataLoader
from ansible.template import Templar
from ansible_collections.community.docker.plugins.inventory.docker_containers import InventoryModule
from ansible_collections.community.docker.tests.unit.compat.mock import create_autospec
@pytest.fixture(scope="module")
def inventory():
def templar():
dataloader = create_autospec(DataLoader, instance=True)
return Templar(loader=dataloader)
@pytest.fixture(scope="module")
def inventory(templar):
r = InventoryModule()
r.inventory = InventoryData()
r.templar = templar
return r
@@ -114,6 +124,7 @@ def test_populate(inventory, mocker):
'compose': {},
'groups': {},
'keyed_groups': {},
'filters': None,
}))
inventory._populate(client)
@@ -145,6 +156,7 @@ def test_populate_service(inventory, mocker):
'groups': {},
'keyed_groups': {},
'docker_host': 'unix://var/run/docker.sock',
'filters': None,
}))
inventory._populate(client)
@@ -186,6 +198,7 @@ def test_populate_stack(inventory, mocker):
'docker_host': 'unix://var/run/docker.sock',
'default_ip': '127.0.0.1',
'private_ssh_port': 22,
'filters': None,
}))
inventory._populate(client)
@@ -212,3 +225,46 @@ def test_populate_stack(inventory, mocker):
assert len(inventory.inventory.groups['unix://var/run/docker.sock'].hosts) == 1
assert len(inventory.inventory.groups) == 10
assert len(inventory.inventory.hosts) == 1
def test_populate_filter_none(inventory, mocker):
client = FakeClient(LOVING_THARP)
inventory.get_option = mocker.MagicMock(side_effect=create_get_option({
'verbose_output': True,
'connection_type': 'docker-api',
'add_legacy_groups': False,
'compose': {},
'groups': {},
'keyed_groups': {},
'filters': [
{'exclude': True},
],
}))
inventory._populate(client)
assert len(inventory.inventory.hosts) == 0
def test_populate_filter(inventory, mocker):
client = FakeClient(LOVING_THARP)
inventory.get_option = mocker.MagicMock(side_effect=create_get_option({
'verbose_output': True,
'connection_type': 'docker-api',
'add_legacy_groups': False,
'compose': {},
'groups': {},
'keyed_groups': {},
'filters': [
{'include': 'docker_state.Running is true'},
{'exclude': True},
],
}))
inventory._populate(client)
host_1 = inventory.inventory.get_host('loving_tharp')
host_1_vars = host_1.get_vars()
assert host_1_vars['ansible_host'] == 'loving_tharp'
assert len(inventory.inventory.hosts) == 1
+1
View File
@@ -5,3 +5,4 @@
collections:
- community.internal_test_tools
- community.library_inventory_filtering_v1