Skip to content

Commit bd688be

Browse files
authored
Deprecate features no longer supported with ansible-core 2.19 (#467)
* Add function to skip failing tests on ansible 2.19+ * Fail using ansible-host and ansible-group on 2.19+ * Mark code and use as deprecated
1 parent 4dda011 commit bd688be

File tree

12 files changed

+92
-13
lines changed

12 files changed

+92
-13
lines changed

src/pytest_ansible/has_version.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
has_ansible_v2 = parse_version(ansible.__version__) >= parse_version("2.0.0")
1111
has_ansible_v24 = parse_version(ansible.__version__) >= parse_version("2.4.0")
12-
has_ansible_v28 = parse_version(ansible.__version__) >= parse_version(
13-
"2.8.0.dev0",
14-
) or parse_version(ansible.__version__) >= parse_version("2.8.0")
12+
has_ansible_v28 = parse_version(ansible.__version__) >= parse_version("2.8.0.dev0")
1513
has_ansible_v29 = parse_version(ansible.__version__) >= parse_version("2.9.0")
1614
has_ansible_v212 = parse_version(ansible.__version__) >= parse_version("2.12.0")
1715
has_ansible_v213 = parse_version(ansible.__version__) >= parse_version("2.13.0")
16+
has_ansible_v219 = parse_version(ansible.__version__) >= parse_version("2.19.0.dev0")

src/pytest_ansible/host_manager/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import ansible
66

7+
from typing_extensions import deprecated
78

9+
10+
@deprecated("Host management is deprecated and will be removed in a future release")
811
class BaseHostManager:
912
"""The BaseHostManager class provides a base class for managing ansible inventory hosts.
1013

src/pytest_ansible/plugin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import contextlib
66
import logging
77
import subprocess
8+
import warnings
89

910
from typing import TYPE_CHECKING
1011

@@ -15,12 +16,15 @@
1516
import ansible.utils.display
1617
import pytest
1718

19+
from typing_extensions import deprecated
20+
1821
from pytest_ansible.fixtures import (
1922
ansible_facts,
2023
fixture_ansible_adhoc,
2124
fixture_ansible_module,
2225
localhost,
2326
)
27+
from pytest_ansible.has_version import has_ansible_v219
2428
from pytest_ansible.host_manager.utils import get_host_manager
2529

2630
from .molecule import HAS_MOLECULE, MoleculeFile, MoleculeScenario
@@ -233,13 +237,33 @@ def pytest_collect_file(
233237
return None
234238

235239

240+
def warn_or_fail(fixture_name: str) -> None:
241+
"""Give the appropriate feedback to the user when a deprecated fixture is used.
242+
243+
Args:
244+
fixture_name: The fixture that has been used.
245+
"""
246+
if has_ansible_v219:
247+
pytest.exit(
248+
f"{fixture_name} fixture not supported on Ansible 2.19+. See https://github.com/ansible/pytest-ansible/issues/468."
249+
)
250+
else:
251+
warnings.warn(
252+
f"{fixture_name} fixture is deprecated and will be removed in a future release. See https://github.com/ansible/pytest-ansible/issues/468.",
253+
DeprecationWarning,
254+
stacklevel=2,
255+
)
256+
257+
236258
def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
237259
"""Generate tests when specific `ansible_*` fixtures are used by tests.
238260
239261
Raises:
240262
pytest.UsageError: If the required --ansible-* parameters were not provided.
241263
"""
242264
if "ansible_host" in metafunc.fixturenames:
265+
warn_or_fail("ansible_host")
266+
243267
# assert required --ansible-* parameters were used
244268
PyTestAnsiblePlugin.assert_required_ansible_parameters(metafunc.config) # type: ignore[no-untyped-call]
245269
try:
@@ -255,6 +279,8 @@ def pytest_generate_tests(metafunc): # type: ignore[no-untyped-def] # noqa: AN
255279
metafunc.parametrize("ansible_host", iter(hosts[h] for h in hosts))
256280

257281
if "ansible_group" in metafunc.fixturenames:
282+
warn_or_fail("ansible_group")
283+
258284
# assert required --ansible-* parameters were used
259285
PyTestAnsiblePlugin.assert_required_ansible_parameters(metafunc.config) # type: ignore[no-untyped-call]
260286
try:
@@ -402,6 +428,7 @@ def _load_request_config(self, request): # type: ignore[no-untyped-def] # noqa
402428

403429
return kwargs
404430

431+
@deprecated("Host management is deprecated and will be removed in a future release")
405432
def initialize(self, config=None, request=None, **kwargs): # type: ignore[no-untyped-def] # noqa: ANN001, ANN003, ANN201
406433
"""Return an initialized Ansible Host Manager instance."""
407434
ansible_cfg = {}

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from pytest_ansible.has_version import has_ansible_v219
78
from pytest_ansible.host_manager.utils import get_host_manager
89

910

@@ -173,3 +174,8 @@ def create_host_manager(include_extra_inventory=False): # type: ignore[no-untyp
173174
return get_host_manager(**kwargs)
174175

175176
return create_host_manager
177+
178+
179+
skip_ansible_219 = pytest.mark.skipif(
180+
has_ansible_v219, reason="Functionality is unsupported on Ansible 2.19+"
181+
)

tests/test_adhoc.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
from .conftest import skip_ansible_219
8+
79

810
# pylint: disable=unused-import
911
try:
@@ -24,7 +26,7 @@
2426
EXIT_NOTESTSCOLLECTED = ExitCode.NO_TESTS_COLLECTED
2527

2628

27-
@pytest.mark.old
29+
@skip_ansible_219
2830
def test_contacted_with_params(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
2931
"""FIXME."""
3032
src = """
@@ -54,7 +56,7 @@ def test_func(ansible_module):
5456
assert result.parseoutcomes()["passed"] == 1
5557

5658

57-
@pytest.mark.old
59+
@skip_ansible_219
5860
def test_contacted_with_params_and_inventory_marker(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
5961
"""FIXME."""
6062
src = f"""
@@ -80,7 +82,7 @@ def test_func(ansible_module):
8082
assert result.parseoutcomes()["passed"] == 1
8183

8284

83-
@pytest.mark.old
85+
@skip_ansible_219
8486
def test_contacted_with_params_and_host_pattern_marker(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
8587
"""FIXME."""
8688
src = """
@@ -111,7 +113,7 @@ def test_func(ansible_module):
111113
assert result.parseoutcomes()["passed"] == 1
112114

113115

114-
@pytest.mark.old
116+
@skip_ansible_219
115117
def test_contacted_with_params_and_inventory_host_pattern_marker(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
116118
"""FIXME."""
117119
src = f"""
@@ -142,7 +144,7 @@ def test_func(ansible_module):
142144
assert result.parseoutcomes()["passed"] == 1
143145

144146

145-
@pytest.mark.old
147+
@skip_ansible_219
146148
def test_become(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
147149
"""Test --ansible-become* parameters. This test doesn't actually 'sudo',
148150
but verifies that 'sudo' was attempted by asserting
@@ -200,7 +202,7 @@ def test_func(ansible_module):
200202
assert result.parseoutcomes()["passed"] == 1
201203

202204

203-
@pytest.mark.old
205+
@skip_ansible_219
204206
def test_dark_with_params(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
205207
"""FIXME."""
206208
src = """
@@ -230,7 +232,7 @@ def test_func(ansible_module):
230232
assert result.parseoutcomes()["passed"] == 1
231233

232234

233-
@pytest.mark.old
235+
@skip_ansible_219
234236
def test_dark_with_params_and_inventory_marker(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
235237
"""FIXME."""
236238
src = f"""
@@ -256,7 +258,7 @@ def test_func(ansible_module):
256258
assert result.parseoutcomes()["passed"] == 1
257259

258260

259-
@pytest.mark.old
261+
@skip_ansible_219
260262
def test_dark_with_params_and_host_pattern_marker(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201
261263
"""FIXME."""
262264
src = """

tests/test_adhoc_result.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from pytest_ansible.results import ModuleResult
1010

11-
from .conftest import ALL_EXTRA_HOSTS, ALL_HOSTS
11+
from .conftest import ALL_EXTRA_HOSTS, ALL_HOSTS, skip_ansible_219
1212

1313

1414
invalid_hosts = [
@@ -28,20 +28,23 @@ def create_hosts(): # type: ignore[no-untyped-def] # noqa: ANN202
2828
return create_hosts
2929

3030

31+
@skip_ansible_219
3132
def test_len(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
3233
adhoc_result_ret, include_extra_inv = adhoc_result()
3334
assert len(adhoc_result_ret) == len(ALL_HOSTS) + len(
3435
ALL_EXTRA_HOSTS if include_extra_inv else [],
3536
)
3637

3738

39+
@skip_ansible_219
3840
def test_keys(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
3941
adhoc_result_ret, include_extra_inv = adhoc_result()
4042
assert set(adhoc_result_ret) == set(
4143
ALL_HOSTS + (ALL_EXTRA_HOSTS if include_extra_inv else []),
4244
)
4345

4446

47+
@skip_ansible_219
4548
def test_items(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
4649
adhoc_result_ret, include_extra_inv = adhoc_result()
4750
items = adhoc_result_ret.items()
@@ -54,6 +57,7 @@ def test_items(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001, A
5457
assert count == len(ALL_HOSTS + (ALL_EXTRA_HOSTS if include_extra_inv else []))
5558

5659

60+
@skip_ansible_219
5761
def test_values(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
5862
adhoc_result_ret, include_extra_inv = adhoc_result()
5963
values = adhoc_result_ret.values()
@@ -67,6 +71,7 @@ def test_values(adhoc_result): # type: ignore[no-untyped-def] # noqa: ANN001,
6771

6872

6973
@pytest.mark.parametrize("host", ALL_HOSTS + ALL_EXTRA_HOSTS)
74+
@skip_ansible_219
7075
def test_contains(adhoc_result, host): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
7176
adhoc_result_ret, include_extra_inv = adhoc_result()
7277
if not include_extra_inv and host in ALL_EXTRA_HOSTS:
@@ -76,12 +81,14 @@ def test_contains(adhoc_result, host): # type: ignore[no-untyped-def] # noqa:
7681

7782

7883
@pytest.mark.parametrize("host", invalid_hosts)
84+
@skip_ansible_219
7985
def test_not_contains(adhoc_result, host): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
8086
adhoc_result_ret, dummy = adhoc_result()
8187
assert host not in adhoc_result_ret
8288

8389

8490
@pytest.mark.parametrize("host_pattern", ALL_HOSTS + ALL_EXTRA_HOSTS)
91+
@skip_ansible_219
8592
def test_getitem(adhoc_result, host_pattern): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
8693
adhoc_result_ret, include_extra_inv = adhoc_result()
8794
if not include_extra_inv and host_pattern in ALL_EXTRA_HOSTS:
@@ -93,13 +100,15 @@ def test_getitem(adhoc_result, host_pattern): # type: ignore[no-untyped-def] #
93100

94101

95102
@pytest.mark.parametrize("host_pattern", invalid_hosts)
103+
@skip_ansible_219
96104
def test_not_getitem(adhoc_result, host_pattern): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
97105
adhoc_result_ret, dummy = adhoc_result()
98106
with pytest.raises(KeyError):
99107
assert adhoc_result_ret[host_pattern]
100108

101109

102110
@pytest.mark.parametrize("host_pattern", ALL_HOSTS + ALL_EXTRA_HOSTS)
111+
@skip_ansible_219
103112
def test_getattr(adhoc_result, host_pattern): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
104113
adhoc_result_ret, include_extra_inv = adhoc_result()
105114
if not include_extra_inv and host_pattern in ALL_EXTRA_HOSTS:
@@ -110,6 +119,7 @@ def test_getattr(adhoc_result, host_pattern): # type: ignore[no-untyped-def] #
110119

111120

112121
@pytest.mark.parametrize("host_pattern", invalid_hosts)
122+
@skip_ansible_219
113123
def test_not_getattr(adhoc_result, host_pattern): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
114124
adhoc_result_ret, dummy = adhoc_result()
115125
assert not hasattr(adhoc_result_ret, host_pattern)
@@ -118,6 +128,7 @@ def test_not_getattr(adhoc_result, host_pattern): # type: ignore[no-untyped-def
118128

119129

120130
@pytest.mark.requires_ansible_v2
131+
@skip_ansible_219
121132
def test_connection_failure_v2(): # type: ignore[no-untyped-def] # noqa: ANN201, D103
122133
from pytest_ansible.errors import AnsibleConnectionFailure
123134
from pytest_ansible.host_manager.utils import get_host_manager
@@ -142,6 +153,7 @@ def test_connection_failure_v2(): # type: ignore[no-untyped-def] # noqa: ANN20
142153

143154

144155
@pytest.mark.requires_ansible_v2
156+
@skip_ansible_219
145157
def test_connection_failure_extra_inventory_v2(): # type: ignore[no-untyped-def] # noqa: ANN201, D103
146158
from pytest_ansible.errors import AnsibleConnectionFailure
147159
from pytest_ansible.host_manager.utils import get_host_manager

tests/test_fixtures.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from __future__ import annotations
44

5+
from .conftest import skip_ansible_219
6+
57

68
try:
79
from _pytest.main import EXIT_OK # type: ignore # noqa: PGH003
@@ -34,6 +36,7 @@ def test_func(ansible_adhoc):
3436
assert result.parseoutcomes()["passed"] == 1
3537

3638

39+
@skip_ansible_219
3740
def test_ansible_module(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
3841
src = """
3942
import pytest
@@ -55,6 +58,7 @@ def test_func(ansible_module):
5558
assert result.parseoutcomes()["passed"] == 1
5659

5760

61+
@skip_ansible_219
5862
def test_ansible_facts(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
5963
src = """
6064
import pytest
@@ -76,6 +80,7 @@ def test_func(ansible_facts):
7680
assert result.parseoutcomes()["passed"] == 1
7781

7882

83+
@skip_ansible_219
7984
def test_localhost(pytester, option): # type: ignore[no-untyped-def] # noqa: ANN001, ANN201, D103
8085
src = """
8186
import pytest

tests/test_func.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
import pytest
88

9+
from .conftest import skip_ansible_219
10+
911

1012
@pytest.mark.ansible(inventory="local,", connection="local", host_pattern="all")
13+
@skip_ansible_219
1114
def test_func(ansible_module: Any) -> None: # noqa: ANN401
1215
"""Sample test for ansible module.
1316

0 commit comments

Comments
 (0)