Skip to content

Commit

Permalink
Added tests for module dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
CFSNM committed Oct 18, 2023
1 parent 50d87a3 commit 21520ff
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 120 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ cython_debug/

# Version created and populated by setuptools_scm
/src/pytest_ansible/_version.py


# JetBrains
.idea
5 changes: 5 additions & 0 deletions extra_inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[extrahosts]
extra-host-1.example.com ansible_connection=local ansible_python_interpreter='/usr/bin/env python'
extra-host-2.example.com ansible_connection=local ansible_python_interpreter='/usr/bin/env python'
extra-host-3.example.com ansible_connection=local ansible_python_interpreter='/usr/bin/env python'
extra-host-4.example.com ansible_connection=local ansible_python_interpreter='/usr/bin/env python'
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 @@ -24,7 +24,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 @@ -24,7 +24,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/v29.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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
62 changes: 57 additions & 5 deletions src/pytest_ansible/module_dispatcher/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,28 @@ def _run(self, *module_args, **complex_args):

# Assert hosts matching the provided pattern exist
hosts = self.options["inventory_manager"].list_hosts()
if self.options.get('extra_inventory_manager', None):
extra_hosts = self.options["extra_inventory_manager"].list_hosts()
else:
extra_hosts = []
no_hosts = False
if len(hosts) == 0:
if len(hosts + extra_hosts) == 0:
no_hosts = True
warnings.warn("provided hosts list is empty, only localhost is available")

self.options["inventory_manager"].subset(self.options.get("subset"))
hosts = self.options["inventory_manager"].list_hosts(
self.options["host_pattern"],
)
if len(hosts) == 0 and not no_hosts:
msg = "Specified hosts and/or --limit does not match any hosts"

if self.options.get('extra_inventory_manager', None):
self.options["extra_inventory_manager"].subset(self.options.get("subset"))
extra_hosts = self.options["extra_inventory_manager"].list_hosts()
else:
extra_hosts = []
if len(hosts + extra_hosts) == 0 and not no_hosts:
raise ansible.errors.AnsibleError(
msg,
"Specified hosts and/or --limit does not match any hosts.",
)

# pylint: disable=no-member
Expand Down Expand Up @@ -133,6 +142,19 @@ def _run(self, *module_args, **complex_args):
"passwords": {"conn_pass": None, "become_pass": None},
}

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

kwargs_extra = {
"inventory": self.options["extra_inventory_manager"],
"variable_manager": self.options["extra_variable_manager"],
"loader": self.options["extra_loader"],
"stdout_callback": callback_extra,
"passwords": {"conn_pass": None, "become_pass": None},
}

# create a pseudo-play to execute the specified module via a single task
play_ds = {
"name": "pytest-ansible",
Expand All @@ -154,6 +176,14 @@ def _run(self, *module_args, **complex_args):
loader=self.options["loader"],
)

play_extra = None
if self.options.get('extra_inventory_manager', None):
play_extra = Play().load(
play_ds,
variable_manager=self.options["extra_variable_manager"],
loader=self.options["extra_loader"],
)

# now create a task queue manager to execute the play
tqm = None
try:
Expand All @@ -163,6 +193,15 @@ def _run(self, *module_args, **complex_args):
if tqm:
tqm.cleanup()

if self.options.get('extra_inventory_manager', None):
tqm_extra = None
try:
tqm_extra = TaskQueueManager(**kwargs_extra)
tqm_extra.run(play_extra)
finally:
if tqm_extra:
tqm_extra.cleanup()

# Raise exception if host(s) unreachable
if callback.unreachable:
msg = "Host unreachable"
Expand All @@ -172,5 +211,18 @@ def _run(self, *module_args, **complex_args):
contacted=callback.contacted,
)

if self.options.get('extra_inventory_manager', None) and callback_extra.unreachable:
raise AnsibleConnectionFailure(
"Host unreachable in the extra inventory",
dark=callback_extra.unreachable,
contacted=callback_extra.contacted,
)

# Success!
return AdHocResult(contacted=callback.contacted)
return AdHocResult(
contacted=(
{**callback.contacted, **callback_extra.contacted}
if self.options.get('extra_inventory_manager', None)
else callback.contacted
),
)
11 changes: 6 additions & 5 deletions src/pytest_ansible/module_dispatcher/v212.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,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 @@ -190,7 +190,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 @@ -206,7 +206,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 @@ -223,7 +223,8 @@ 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 @@ -235,7 +236,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
),
)
26 changes: 13 additions & 13 deletions src/pytest_ansible/module_dispatcher/v213.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _run(self, *module_args, **complex_args):

# Assert hosts matching the provided pattern exist
hosts = self.options["inventory_manager"].list_hosts()
if "extra_inventory_manager" in self.options:
if self.options.get('extra_inventory_manager', None):
extra_hosts = self.options["extra_inventory_manager"].list_hosts()
else:
extra_hosts = []
Expand All @@ -106,7 +106,7 @@ def _run(self, *module_args, **complex_args):
hosts = self.options["inventory_manager"].list_hosts(
self.options["host_pattern"],
)
if "extra_inventory_manager" in self.options:
if self.options.get('extra_inventory_manager', None):
self.options["extra_inventory_manager"].subset(self.options.get("subset"))
extra_hosts = self.options["extra_inventory_manager"].list_hosts()
else:
Expand Down Expand Up @@ -167,7 +167,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 @@ -201,7 +201,7 @@ def _run(self, *module_args, **complex_args):
loader=self.options["loader"],
)
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 @@ -221,7 +221,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 @@ -238,19 +238,19 @@ def _run(self, *module_args, **complex_args):
dark=callback.unreachable,
contacted=callback.contacted,
)
if "extra_inventory_manager" in self.options and callback_extra.unreachable:
msg = "Host unreachable in the extra inventory"
raise AnsibleConnectionFailure(
msg,
dark=callback_extra.unreachable,
contacted=callback_extra.contacted,
)
if self.options.get('extra_inventory_manager', None):
if callback_extra.unreachable:
raise AnsibleConnectionFailure(
"Host unreachable in the extra inventory",
dark=callback_extra.unreachable,
contacted=callback_extra.contacted,
)

# Success!
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
),
)
16 changes: 7 additions & 9 deletions src/pytest_ansible/module_dispatcher/v29.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,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 @@ -183,7 +183,7 @@ def _run(self, *module_args, **complex_args):
loader=self.options["loader"],
)
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 @@ -199,7 +199,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 @@ -210,16 +210,14 @@ def _run(self, *module_args, **complex_args):

# Raise exception if host(s) unreachable
if callback.unreachable:
msg = "Host unreachable in the inventory"
raise AnsibleConnectionFailure(
msg,
"Host unreachable in the inventory",
dark=callback.unreachable,
contacted=callback.contacted,
)
if "extra_inventory_manager" in self.options and callback_extra.unreachable:
msg = "Host unreachable in the extra inventory"
if self.options.get('extra_inventory_manager', None) and callback_extra.unreachable:
raise AnsibleConnectionFailure(
msg,
"Host unreachable in the extra inventory",
dark=callback_extra.unreachable,
contacted=callback_extra.contacted,
)
Expand All @@ -228,7 +226,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 21520ff

Please sign in to comment.