Debian Codegraph @

tests/units/anta_tests/__init__.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """Tests for anta.tests module."""
5
6 from __future__ import annotations
7
8 import asyncio
9 from typing import TYPE_CHECKING, Any, Literal, TypedDict
10
11 from anta.models import AntaTest
12
13 if TYPE_CHECKING:
14 import sys
15
16 from anta.device import AntaDevice
17 from anta.result_manager.models import AntaTestStatus
18
19 if sys.version_info >= (3, 11):
20 from typing import NotRequired
21 else:
22 from typing_extensions import NotRequired
23
24 from typing import TypeAlias
25
26
27 class AtomicResult(TypedDict):
28 """Expected atomic result of a unit test of an AntaTest subclass."""
29
30 description: str
31 result: Literal[AntaTestStatus.SUCCESS, AntaTestStatus.FAILURE, AntaTestStatus.SKIPPED]
32 messages: NotRequired[list[str]]
33 inputs: NotRequired[dict[str, Any]]
34
35
36 class UnitTestResult(TypedDict):
37 """Expected result of a unit test of an AntaTest subclass.
38
39 For our AntaTest unit tests we expect only success, failure or skipped.
40 Never unset nor error.
41 """
42
43 result: Literal[AntaTestStatus.SUCCESS, AntaTestStatus.FAILURE, AntaTestStatus.SKIPPED]
44 messages: NotRequired[list[str]]
45 atomic_results: NotRequired[list[AtomicResult]]
46
47
48 class AntaUnitTest(TypedDict):
49 """The parameters required for a unit test of an AntaTest subclass."""
50
51 inputs: NotRequired[dict[str, Any]]
52 eos_data: list[dict[str, Any] | str]
53 expected: UnitTestResult
54
55
56 AntaUnitTestData: TypeAlias = dict[tuple[type[AntaTest], str], AntaUnitTest]
57
58
59 def test(device: AntaDevice, anta_test: type[AntaTest], unit_test_data: AntaUnitTest) -> None:
60 """Generic test function for AntaTest subclass.
61
62 Generate unit tests for each AntaTest subclass.
63
64 See `tests/units/anta_tests/README.md` for more information on how to use it.
65 """
66 # Instantiate the AntaTest subclass
67 test_instance = anta_test(device, inputs=unit_test_data.get("inputs"), eos_data=unit_test_data["eos_data"])
68 # Run the test() method
69 asyncio.run(test_instance.test())
70
71 # Assert expected result
72 assert test_instance.result.result == unit_test_data["expected"]["result"], (
73 f"Expected '{unit_test_data['expected']['result']}' result, got '{test_instance.result.result}'. Messages: {test_instance.result.messages}"
74 )
75 # Assert test messages
76 if "messages" in unit_test_data["expected"]:
77 # Assert number of messages
78 assert len(test_instance.result.messages) == len(unit_test_data["expected"]["messages"]), (
79 f"Expected {len(unit_test_data['expected']['messages'])} messages, got {len(test_instance.result.messages)}"
80 )
81 # Test will pass if the expected message is included in the test result message
82 for message, expected in zip(test_instance.result.messages, unit_test_data["expected"]["messages"], strict=True):
83 assert expected in message, f"Expected message '{expected}' not found in '{message}'"
84 else:
85 # Test result should not have messages
86 assert test_instance.result.messages == [], "There are untested messages, see diffs with '-vv' option"
87
88 # Assert atomic results
89 if "atomic_results" in unit_test_data["expected"]:
90 # Assert number of atomic results
91 assert len(test_instance.result.atomic_results) == len(unit_test_data["expected"]["atomic_results"]), (
92 f"Expected {len(unit_test_data['expected']['atomic_results'])} atomic results, got {len(test_instance.result.atomic_results)}"
93 )
94 # Assert each atomic result
95 for atomic_result_model, expected_atomic_result in zip(test_instance.result.atomic_results, unit_test_data["expected"]["atomic_results"], strict=True):
96 atomic_result = atomic_result_model.model_dump(mode="json", exclude_none=True)
97 messages = atomic_result.pop("messages")
98 expected_messages = expected_atomic_result.pop("messages", [])
99
100 # First assert the rest of the atomic result
101 assert atomic_result == expected_atomic_result, "Expected atomic result did not match, see diffs with '-vv' option"
102
103 # Then assert the messages if any
104 if expected_messages:
105 # We expect messages in atomic test result
106 assert len(messages) == len(expected_messages), (
107 f"Expected {len(expected_messages)} messages, got {len(messages)} in atomic result "
108 f"with description '{atomic_result['description']}', see diffs with '-vv' option"
109 )
110 # Test will pass if the expected message is included in the atomic test result message
111 for message, expected in zip(messages, expected_messages, strict=True):
112 assert expected in message, f"Expected message '{expected}' not found in '{message}'"
113 else:
114 # Atomic test result should not have messages
115 assert messages == [], f"There are untested messages in atomic result with description '{atomic_result['description']}', see diffs with '-vv' option"
116 else:
117 # Test result should not have atomic results
118 assert test_instance.result.atomic_results == [], "There are untested atomic results, see diffs with '-vv' option"