Skip to content

Commit

Permalink
Added deprecated decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-baillargeon committed Jan 19, 2025
1 parent f2def95 commit 4737b1f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion anta/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

DEFAULT_NOFILE = 16384

@deprecated("This function is deprecated and will be removed in ANTA v2.0.0.")
@deprecated("This function is deprecated and will be removed in ANTA v2.0.0. Use AntaRunner class instead.", category=DeprecationWarning)
def adjust_rlimit_nofile() -> tuple[int, int]:
"""Adjust the maximum number of open file descriptors for the ANTA process.
Expand Down Expand Up @@ -61,6 +61,7 @@ def adjust_rlimit_nofile() -> tuple[int, int]:
logger = logging.getLogger(__name__)


@deprecated("This function is deprecated and will be removed in ANTA v2.0.0. Use AntaRunner class instead.", category=DeprecationWarning)
def log_cache_statistics(devices: list[AntaDevice]) -> None:
"""Log cache statistics for each device in the inventory.
Expand All @@ -81,6 +82,7 @@ def log_cache_statistics(devices: list[AntaDevice]) -> None:
logger.info("Caching is not enabled on %s", device.name)


@deprecated("This function is deprecated and will be removed in ANTA v2.0.0. Use AntaRunner class instead.", category=DeprecationWarning)
async def setup_inventory(inventory: AntaInventory, tags: set[str] | None, devices: set[str] | None, *, established_only: bool) -> AntaInventory | None:
"""Set up the inventory for the ANTA run.
Expand Down Expand Up @@ -123,6 +125,7 @@ async def setup_inventory(inventory: AntaInventory, tags: set[str] | None, devic
return selected_inventory


@deprecated("This function is deprecated and will be removed in ANTA v2.0.0. Use AntaRunner class instead.", category=DeprecationWarning)
def prepare_tests(
inventory: AntaInventory, catalog: AntaCatalog, tests: set[str] | None, tags: set[str] | None
) -> defaultdict[AntaDevice, set[AntaTestDefinition]] | None:
Expand Down Expand Up @@ -179,6 +182,7 @@ def prepare_tests(
return device_to_tests


@deprecated("This function is deprecated and will be removed in ANTA v2.0.0. Use AntaRunner class instead.", category=DeprecationWarning)
def get_coroutines(selected_tests: defaultdict[AntaDevice, set[AntaTestDefinition]], manager: ResultManager | None = None) -> list[Coroutine[Any, Any, TestResult]]:
"""Get the coroutines for the ANTA run.
Expand Down
2 changes: 2 additions & 0 deletions tests/benchmark/test_anta.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_anta_dry_run(

results = session_results[request.node.callspec.id]

# TODO: Use AntaRunner in ANTA v2.0.0
@benchmark
def _() -> None:
results.reset()
Expand Down Expand Up @@ -69,6 +70,7 @@ def test_anta(

results = session_results[request.node.callspec.id]

# TODO: Use AntaRunner in ANTA v2.0.0
@benchmark
def _() -> None:
results.reset()
Expand Down
6 changes: 6 additions & 0 deletions tests/benchmark/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from typing import TYPE_CHECKING, Any

import pytest

from anta._runner import AntaRunner, AntaRunnerScope
from anta.result_manager import ResultManager
from anta.runner import get_coroutines, prepare_tests
Expand All @@ -23,6 +25,8 @@
from anta.result_manager.models import TestResult


# TODO: Remove this in ANTA v2.0.0
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_prepare_tests(benchmark: BenchmarkFixture, catalog: AntaCatalog, inventory: AntaInventory) -> None:
"""Benchmark `anta.runner.prepare_tests`."""

Expand All @@ -37,6 +41,8 @@ def _() -> defaultdict[AntaDevice, set[AntaTestDefinition]] | None:
assert sum(len(tests) for tests in selected_tests.values()) == len(inventory) * len(catalog.tests)


# TODO: Remove this in ANTA v2.0.0
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_get_coroutines(benchmark: BenchmarkFixture, catalog: AntaCatalog, inventory: AntaInventory) -> None:
"""Benchmark `anta.runner.get_coroutines`."""
selected_tests = prepare_tests(inventory=inventory, catalog=catalog, tests=None, tags=None)
Expand Down
17 changes: 10 additions & 7 deletions tests/units/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
FAKE_CATALOG: AntaCatalog = AntaCatalog.from_list([(FakeTest, None)])


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
async def test_empty_tests(caplog: pytest.LogCaptureFixture, inventory: AntaInventory) -> None:
"""Test that when the list of tests is empty, a log is raised."""
caplog.set_level(logging.INFO)
Expand All @@ -50,7 +50,7 @@ async def test_empty_tests(caplog: pytest.LogCaptureFixture, inventory: AntaInve
assert "The list of tests is empty, exiting" in caplog.records[record_index].message


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
async def test_empty_inventory(caplog: pytest.LogCaptureFixture) -> None:
"""Test that when the Inventory is empty, a log is raised."""
caplog.set_level(logging.INFO)
Expand All @@ -70,7 +70,7 @@ async def test_empty_inventory(caplog: pytest.LogCaptureFixture) -> None:
assert "The inventory is empty, exiting" in caplog.records[record_index].message


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
@pytest.mark.parametrize(
("inventory", "tags", "devices"),
[
Expand All @@ -94,6 +94,7 @@ async def test_no_selected_device(caplog: pytest.LogCaptureFixture, inventory: A


# TODO: Remove this in ANTA v2.0.0
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.skipif(os.name != "posix", reason="Cannot run this test on Windows")
def test_adjust_rlimit_nofile_valid_env(caplog: pytest.LogCaptureFixture) -> None:
"""Test adjust_rlimit_nofile with valid environment variables."""
Expand Down Expand Up @@ -128,6 +129,7 @@ def side_effect_setrlimit(resource_id: int, limits: tuple[int, int]) -> None:


# TODO: Remove this in ANTA v2.0.0
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.skipif(os.name != "posix", reason="Cannot run this test on Windows")
def test_adjust_rlimit_nofile_invalid_env(caplog: pytest.LogCaptureFixture) -> None:
"""Test adjust_rlimit_nofile with valid environment variables."""
Expand Down Expand Up @@ -162,7 +164,7 @@ def side_effect_setrlimit(resource_id: int, limits: tuple[int, int]) -> None:
setrlimit_mock.assert_called_once_with(resource.RLIMIT_NOFILE, (16384, 1048576))


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
@pytest.mark.skipif(os.name == "posix", reason="Run this test on Windows only")
async def test_check_runner_log_for_windows(caplog: pytest.LogCaptureFixture, inventory: AntaInventory) -> None:
"""Test log output for Windows host regarding rlimit."""
Expand All @@ -173,7 +175,7 @@ async def test_check_runner_log_for_windows(caplog: pytest.LogCaptureFixture, in
assert "Running on a non-POSIX system, cannot adjust the maximum number of file descriptors." in caplog.records[0].message


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
# We could instead merge multiple coverage report together but that requires more work than just this.
@pytest.mark.skipif(os.name != "posix", reason="Fake non-posix for coverage")
async def test_check_runner_log_for_windows_fake(caplog: pytest.LogCaptureFixture, inventory: AntaInventory) -> None:
Expand All @@ -189,6 +191,7 @@ async def test_check_runner_log_for_windows_fake(caplog: pytest.LogCaptureFixtur
assert "Running on a non-POSIX system, cannot adjust the maximum number of file descriptors." in caplog.records[0].message


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
@pytest.mark.parametrize(
("inventory", "tags", "tests", "devices_count", "tests_count"),
[
Expand Down Expand Up @@ -218,7 +221,7 @@ async def test_prepare_tests(
assert sum(len(tests) for tests in selected_tests.values()) == tests_count


# TODO: Remove this in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
async def test_dry_run(caplog: pytest.LogCaptureFixture, inventory: AntaInventory) -> None:
"""Test that when dry_run is True, no tests are run."""
caplog.set_level(logging.INFO)
Expand All @@ -227,7 +230,7 @@ async def test_dry_run(caplog: pytest.LogCaptureFixture, inventory: AntaInventor
assert "Dry-run mode, exiting before running the tests." in caplog.records[-1].message


# TODO: Move this to TestAntaRunner in ANTA v2.0.0
# TODO: Move this to AntaRunner tests in ANTA v2.0.0
async def test_cannot_create_test(caplog: pytest.LogCaptureFixture, inventory: AntaInventory) -> None:
"""Test that when an Exception is raised during test instantiation, it is caught and a log is raised."""
caplog.set_level(logging.CRITICAL)
Expand Down

0 comments on commit 4737b1f

Please sign in to comment.