Skip to content

Commit

Permalink
Testing with v2 coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
CFSNM committed Dec 11, 2023
1 parent f359549 commit a64b5a6
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 55 deletions.
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
19 changes: 9 additions & 10 deletions src/pytest_ansible/module_dispatcher/v212.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@

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.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
from pytest_ansible.module_dispatcher import BaseModuleDispatcher


class ResultAccumulator(CallbackBase):
Expand Down Expand Up @@ -60,7 +52,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 +66,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
16 changes: 9 additions & 7 deletions src/pytest_ansible/module_dispatcher/v213.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@

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


# pylint: disable=ungrouped-imports, wrong-import-position
if not has_ansible_v213:
msg = "Only supported with ansible-2.13 and newer"
raise ImportError(msg)
from pytest_ansible.module_dispatcher import BaseModuleDispatcher

HAS_CUSTOM_LOADER_SUPPORT = True

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


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

required_kwargs = (
Expand All @@ -76,6 +71,13 @@ class ModuleDispatcherV213(ModuleDispatcherV2):
"loader",
)

def __init__(self, **kwargs) -> None:
"""Fixme."""
super().__init__(**kwargs)
if not has_ansible_v213:
msg = "Only supported with ansible-2.13 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
84 changes: 58 additions & 26 deletions tests/test_host_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@
]


@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_len(hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
assert len(_hosts) == len(ALL_HOSTS) + len(
ALL_EXTRA_HOSTS if include_extra_inventory else [],
)


@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_keys(hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
sorted_keys = _hosts.keys()
Expand All @@ -37,7 +43,10 @@ def test_host_manager_keys(hosts, include_extra_inventory):
("host_pattern", "num_hosts"),
POSITIVE_HOST_PATTERNS + EXTRA_HOST_POSITIVE_PATTERNS,
)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_contains(host_pattern, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
if not include_extra_inventory and host_pattern.startswith("extra"):
Expand All @@ -46,7 +55,10 @@ def test_host_manager_contains(host_pattern, num_hosts, hosts, include_extra_inv
assert host_pattern in _hosts, f"{host_pattern} not in hosts"


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
NEGATIVE_HOST_PATTERNS,
)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
def test_host_manager_not_contains(
host_pattern,
Expand All @@ -62,7 +74,10 @@ def test_host_manager_not_contains(
("host_pattern", "num_hosts"),
POSITIVE_HOST_PATTERNS + EXTRA_HOST_POSITIVE_PATTERNS,
)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_getitem(host_pattern, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
if not include_extra_inventory and host_pattern.startswith("extra"):
Expand All @@ -71,14 +86,15 @@ def test_host_manager_getitem(host_pattern, num_hosts, hosts, include_extra_inve
assert _hosts[host_pattern]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
def test_host_manager_not_getitem(
host_pattern,
num_hosts,
hosts,
include_extra_inventory,
):
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
NEGATIVE_HOST_PATTERNS,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_not_getitem(host_pattern, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
with pytest.raises(KeyError):
assert _hosts[host_pattern]
Expand All @@ -88,7 +104,10 @@ def test_host_manager_not_getitem(
("host_pattern", "num_hosts"),
POSITIVE_HOST_PATTERNS + EXTRA_HOST_POSITIVE_PATTERNS,
)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_getattr(host_pattern, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
if not include_extra_inventory and host_pattern.startswith("extra"):
Expand All @@ -97,8 +116,14 @@ def test_host_manager_getattr(host_pattern, num_hosts, hosts, include_extra_inve
assert hasattr(_hosts, host_pattern)


@pytest.mark.parametrize(("host_slice", "num_hosts"), POSITIVE_HOST_SLICES)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
("host_slice", "num_hosts"),
POSITIVE_HOST_SLICES,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_slice(host_slice, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
assert (
Expand All @@ -107,22 +132,29 @@ def test_host_manager_slice(host_slice, num_hosts, hosts, include_extra_inventor


# pylint: disable=pointless-statement
@pytest.mark.parametrize("host_slice", NEGATIVE_HOST_SLICES)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
"host_slice",
NEGATIVE_HOST_SLICES,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_not_slice(host_slice, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
with pytest.raises(KeyError):
_hosts[host_slice]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
def test_host_manager_not_getattr(
host_pattern,
num_hosts,
hosts,
include_extra_inventory,
):
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
NEGATIVE_HOST_PATTERNS,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_host_manager_not_getattr(host_pattern, num_hosts, hosts, include_extra_inventory):
_hosts = hosts(include_extra_inventory=include_extra_inventory)
assert not hasattr(_hosts, host_pattern)
with pytest.raises(AttributeError):
Expand Down
33 changes: 24 additions & 9 deletions tests/test_module_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,40 @@ def test_importerror_requires_v1():
import pytest_ansible.module_dispatcher.v1 # NOQA


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
POSITIVE_HOST_PATTERNS,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_dispatcher_len(host_pattern, num_hosts, hosts, include_extra_inventory):
hosts = hosts(include_extra_inventory=include_extra_inventory)
assert len(getattr(hosts, host_pattern)) == num_hosts[include_extra_inventory]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), POSITIVE_HOST_PATTERNS)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
POSITIVE_HOST_PATTERNS,
)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_dispatcher_contains(host_pattern, num_hosts, hosts, include_extra_inventory):
hosts = hosts(include_extra_inventory=include_extra_inventory)
assert host_pattern in hosts["all"]


@pytest.mark.parametrize(("host_pattern", "num_hosts"), NEGATIVE_HOST_PATTERNS)
@pytest.mark.parametrize("include_extra_inventory", (True, False))
def test_dispatcher_not_contains(
host_pattern, num_hosts, hosts, include_extra_inventory
):
@pytest.mark.parametrize(
("host_pattern", "num_hosts"),
NEGATIVE_HOST_PATTERNS,)
@pytest.mark.parametrize(
"include_extra_inventory",
(True, False),
)
def test_dispatcher_not_contains(host_pattern, num_hosts, hosts, include_extra_inventory):
hosts = hosts(include_extra_inventory=include_extra_inventory)
assert host_pattern not in hosts["all"]

Expand Down

0 comments on commit a64b5a6

Please sign in to comment.