Skip to content

Commit

Permalink
Fix duplicated host bug with extra_inventory (#180)
Browse files Browse the repository at this point in the history
* Support extra inventory in the module dispatcher

* Added tests for module dispatcher

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Testing with v2 coverage

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* chore: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
CFSNM and pre-commit-ci[bot] authored Dec 11, 2023
1 parent 655fb9c commit a546059
Show file tree
Hide file tree
Showing 13 changed files with 352 additions and 347 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,6 @@ cython_debug/

.DS_Store
_readthedocs

# JetBrains
.idea
3 changes: 0 additions & 3 deletions src/pytest_ansible/host_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ansible

from pytest_ansible.has_version import (
has_ansible_v2,
has_ansible_v212,
has_ansible_v213,
)
Expand Down Expand Up @@ -144,8 +143,6 @@ def get_host_manager(*args, **kwargs):
from pytest_ansible.host_manager.v213 import HostManagerV213 as HostManager
elif has_ansible_v212:
from pytest_ansible.host_manager.v212 import HostManagerV212 as HostManager
elif has_ansible_v2:
from pytest_ansible.host_manager.v2 import HostManagerV2 as HostManager
else:
raise RuntimeError("Unable to find any supported HostManager")

Expand Down
29 changes: 0 additions & 29 deletions src/pytest_ansible/host_manager/v2.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/pytest_ansible/host_manager/v212.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize_inventory(self):
loader=self.options["loader"],
inventory=self.options["inventory_manager"],
)
if "extra_inventory" in self.options:
if self.options.get("extra_inventory", None):
self.options["extra_loader"] = DataLoader()
self.options["extra_inventory_manager"] = InventoryManager(
loader=self.options["extra_loader"],
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_ansible/host_manager/v213.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize_inventory(self):
loader=self.options["loader"],
inventory=self.options["inventory_manager"],
)
if "extra_inventory" in self.options:
if self.options.get("extra_inventory", None):
self.options["extra_loader"] = DataLoader()
self.options["extra_inventory_manager"] = InventoryManager(
loader=self.options["extra_loader"],
Expand Down
21 changes: 18 additions & 3 deletions src/pytest_ansible/module_dispatcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ def __init__(self, **kwargs) -> None:

def __len__(self) -> int:
"""Return the number of hosts that match the `host_pattern`."""
try:
extra_inventory_hosts = self.options["extra_inventory_manager"].list_hosts(
self.options["host_pattern"],
)
except KeyError:
extra_inventory_hosts = []
return len(
self.options["inventory_manager"].list_hosts(self.options["host_pattern"]),
)
) + len(extra_inventory_hosts)

def __contains__(self, item) -> bool:
"""Return the whether the inventory contains a host matching the provided `item`."""
return len(self.options["inventory_manager"].list_hosts(item)) > 0
"""Return the whether the inventory or extra_inventory contains a host matching the provided `item`."""
try:
extra_inventory_hosts = self.options["extra_inventory_manager"].list_hosts(
item,
)
except KeyError:
extra_inventory_hosts = []
return (
len(self.options["inventory_manager"].list_hosts(item))
+ len(extra_inventory_hosts)
) > 0

def __getattr__(self, name):
"""Run the ansible module matching the provided `name`.
Expand Down
181 changes: 0 additions & 181 deletions src/pytest_ansible/module_dispatcher/v2.py

This file was deleted.

34 changes: 18 additions & 16 deletions src/pytest_ansible/module_dispatcher/v212.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,10 @@

from pytest_ansible.errors import AnsibleConnectionFailure
from pytest_ansible.has_version import has_ansible_v212
from pytest_ansible.module_dispatcher.v2 import ModuleDispatcherV2
from pytest_ansible.module_dispatcher import BaseModuleDispatcher
from pytest_ansible.results import AdHocResult


# pylint: disable=ungrouped-imports, wrong-import-position
if not has_ansible_v212:
msg = "Only supported with ansible-2.12 and newer"
raise ImportError(msg)


# pylint: enable=ungrouped-imports


class ResultAccumulator(CallbackBase):
"""Fixme."""

Expand Down Expand Up @@ -60,7 +51,7 @@ def results(self):
return {"contacted": self.contacted, "unreachable": self.unreachable}


class ModuleDispatcherV212(ModuleDispatcherV2):
class ModuleDispatcherV212(BaseModuleDispatcher):
"""Pass."""

if TYPE_CHECKING:
Expand All @@ -74,6 +65,13 @@ class ModuleDispatcherV212(ModuleDispatcherV2):
"loader",
)

def __init__(self, **kwargs) -> None:
"""Fixme."""
super().__init__(**kwargs)
if not has_ansible_v212:
msg = "Only supported with ansible-2.12 and newer"
raise ImportError(msg)

def has_module(self, name):
"""Fixme."""
# Make sure we parse module_path and pass it to the loader,
Expand Down Expand Up @@ -161,7 +159,7 @@ def _run(self, *module_args, **complex_args):

kwargs_extra = {}
# If we have an extra inventory, do the same that we did for the inventory
if "extra_inventory_manager" in self.options:
if self.options.get("extra_inventory_manager", None):
callback_extra = ResultAccumulator()

kwargs_extra = {
Expand Down Expand Up @@ -196,7 +194,7 @@ def _run(self, *module_args, **complex_args):
)

play_extra = None
if "extra_inventory_manager" in self.options:
if self.options.get("extra_inventory_manager", None):
play_extra = Play().load(
play_ds,
variable_manager=self.options["extra_variable_manager"],
Expand All @@ -212,7 +210,7 @@ def _run(self, *module_args, **complex_args):
if tqm:
tqm.cleanup()

if "extra_inventory_manager" in self.options:
if self.options.get("extra_inventory_manager", None):
tqm_extra = None
try:
tqm_extra = TaskQueueManager(**kwargs_extra)
Expand All @@ -229,7 +227,11 @@ def _run(self, *module_args, **complex_args):
dark=callback.unreachable,
contacted=callback.contacted,
)
if "extra_inventory_manager" in self.options and callback_extra.unreachable:

if (
self.options.get("extra_inventory_manager", None)
and callback_extra.unreachable
):
msg = "Host unreachable in the extra inventory"
raise AnsibleConnectionFailure(
msg,
Expand All @@ -241,7 +243,7 @@ def _run(self, *module_args, **complex_args):
return AdHocResult(
contacted=(
{**callback.contacted, **callback_extra.contacted}
if "extra_inventory_manager" in self.options
if self.options.get("extra_inventory_manager", None)
else callback.contacted
),
)
Loading

0 comments on commit a546059

Please sign in to comment.