File: test_decorators.py

package info (click to toggle)
anta 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,048 kB
  • sloc: python: 48,164; sh: 28; javascript: 9; makefile: 4
file content (77 lines) | stat: -rw-r--r-- 2,677 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""test anta.decorators.py."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING, ClassVar

import pytest

from anta.decorators import deprecated_test_class, skip_on_platforms
from anta.models import AntaCommand, AntaTemplate, AntaTest

if TYPE_CHECKING:
    from anta.device import AntaDevice


class ExampleTest(AntaTest):
    """ANTA test that always succeed."""

    categories: ClassVar[list[str]] = []
    commands: ClassVar[list[AntaCommand | AntaTemplate]] = []

    @AntaTest.anta_test
    def test(self) -> None:
        """Test function."""
        self.result.is_success()


@pytest.mark.parametrize(
    "new_tests",
    [
        pytest.param(None, id="No new_tests"),
        pytest.param(["NewExampleTest"], id="one new_tests"),
        pytest.param(["NewExampleTest1", "NewExampleTest2"], id="multiple new_tests"),
    ],
)
def test_deprecated_test_class(caplog: pytest.LogCaptureFixture, device: AntaDevice, new_tests: list[str] | None) -> None:
    """Test deprecated_test_class decorator."""
    caplog.set_level(logging.INFO)

    decorated_test_class = deprecated_test_class(new_tests=new_tests)(ExampleTest)

    # Initialize the decorated test
    decorated_test_class(device)

    if new_tests is None:
        assert "ExampleTest test is deprecated." in caplog.messages
    else:
        assert f"ExampleTest test is deprecated. Consider using the following new tests: {', '.join(new_tests)}." in caplog.messages


@pytest.mark.parametrize(
    ("platforms", "device_platform", "expected_result"),
    [
        pytest.param([], "cEOS-lab", "success", id="empty platforms"),
        pytest.param(["cEOS-lab"], "cEOS-lab", "skipped", id="skip on one platform - match"),
        pytest.param(["cEOS-lab"], "vEOS", "success", id="skip on one platform - no match"),
        pytest.param(["cEOS-lab", "vEOS"], "cEOS-lab", "skipped", id="skip on multiple platforms - match"),
    ],
)
async def test_skip_on_platforms(device: AntaDevice, platforms: list[str], device_platform: str, expected_result: str) -> None:
    """Test skip_on_platforms decorator.

    Leverage the ExampleTest defined at the top of the module.
    """
    # Apply the decorator - ignoring mypy warning - this is for testing
    ExampleTest.test = skip_on_platforms(platforms)(ExampleTest.test)  # type: ignore[method-assign]

    device.hw_model = device_platform

    test_instance = ExampleTest(device)
    await test_instance.test()

    assert test_instance.result.result == expected_result