Skip to content

Commit

Permalink
feat: Make name and description optional for AntaTests (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc authored Oct 31, 2024
1 parent 3649f54 commit 27a1699
Show file tree
Hide file tree
Showing 41 changed files with 97 additions and 420 deletions.
31 changes: 21 additions & 10 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,7 @@ class AntaTest(ABC):
The following is an example of an AntaTest subclass implementation:
```python
class VerifyReachability(AntaTest):
name = "VerifyReachability"
description = "Test the network reachability to one or many destination IP(s)."
'''Test the network reachability to one or many destination IP(s).'''
categories = ["connectivity"]
commands = [AntaTemplate(template="ping vrf {vrf} {dst} source {src} repeat 2")]
Expand Down Expand Up @@ -326,12 +325,17 @@ def test(self) -> None:
Python logger for this test instance.
"""

# Mandatory class attributes
# TODO: find a way to tell mypy these are mandatory for child classes - maybe Protocol
# Optional class attributes
name: ClassVar[str]
description: ClassVar[str]

# Mandatory class attributes
# TODO: find a way to tell mypy these are mandatory for child classes
# follow this https://discuss.python.org/t/provide-a-canonical-way-to-declare-an-abstract-class-variable/69416
# for now only enforced at runtime with __init_subclass__
categories: ClassVar[list[str]]
commands: ClassVar[list[AntaTemplate | AntaCommand]]

# Class attributes to handle the progress bar of ANTA CLI
progress: Progress | None = None
nrfu_task: TaskID | None = None
Expand Down Expand Up @@ -505,12 +509,19 @@ def save_commands_data(self, eos_data: list[dict[str, Any] | str]) -> None:
self.instance_commands[index].output = data

def __init_subclass__(cls) -> None:
"""Verify that the mandatory class attributes are defined."""
mandatory_attributes = ["name", "description", "categories", "commands"]
for attr in mandatory_attributes:
if not hasattr(cls, attr):
msg = f"Class {cls.__module__}.{cls.__name__} is missing required class attribute {attr}"
raise NotImplementedError(msg)
"""Verify that the mandatory class attributes are defined and set name and description if not set."""
mandatory_attributes = ["categories", "commands"]
if missing_attrs := [attr for attr in mandatory_attributes if not hasattr(cls, attr)]:
msg = f"Class {cls.__module__}.{cls.__name__} is missing required class attribute(s): {', '.join(missing_attrs)}"
raise AttributeError(msg)

cls.name = getattr(cls, "name", cls.__name__)
if not hasattr(cls, "description"):
if not cls.__doc__ or cls.__doc__.strip() == "":
# No doctsring or empty doctsring - raise
msg = f"Cannot set the description for class {cls.name}, either set it in the class definition or add a docstring to the class."
raise AttributeError(msg)
cls.description = cls.__doc__.split(sep="\n", maxsplit=1)[0]

@property
def module(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions anta/reporter/csv_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def split_list_to_txt_list(cls, usr_list: list[str], delimiter: str = " - ") ->

@classmethod
def convert_to_list(cls, result: TestResult) -> list[str]:
"""
Convert a TestResult into a list of string for creating file content.
"""Convert a TestResult into a list of string for creating file content.
Parameters
----------
Expand Down
14 changes: 0 additions & 14 deletions anta/tests/aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class VerifyTacacsSourceIntf(AntaTest):
```
"""

name = "VerifyTacacsSourceIntf"
description = "Verifies TACACS source-interface for a specified VRF."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show tacacs", revision=1)]

Expand Down Expand Up @@ -81,8 +79,6 @@ class VerifyTacacsServers(AntaTest):
```
"""

name = "VerifyTacacsServers"
description = "Verifies TACACS servers are configured for a specified VRF."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show tacacs", revision=1)]

Expand Down Expand Up @@ -134,8 +130,6 @@ class VerifyTacacsServerGroups(AntaTest):
```
"""

name = "VerifyTacacsServerGroups"
description = "Verifies if the provided TACACS server group(s) are configured."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show tacacs", revision=1)]

Expand Down Expand Up @@ -184,8 +178,6 @@ class VerifyAuthenMethods(AntaTest):
```
"""

name = "VerifyAuthenMethods"
description = "Verifies the AAA authentication method lists for different authentication types (login, enable, dot1x)."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show aaa methods authentication", revision=1)]

Expand Down Expand Up @@ -245,8 +237,6 @@ class VerifyAuthzMethods(AntaTest):
```
"""

name = "VerifyAuthzMethods"
description = "Verifies the AAA authorization method lists for different authorization types (commands, exec)."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show aaa methods authorization", revision=1)]

Expand Down Expand Up @@ -301,8 +291,6 @@ class VerifyAcctDefaultMethods(AntaTest):
```
"""

name = "VerifyAcctDefaultMethods"
description = "Verifies the AAA accounting default method lists for different accounting types (system, exec, commands, dot1x)."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show aaa methods accounting", revision=1)]

Expand Down Expand Up @@ -364,8 +352,6 @@ class VerifyAcctConsoleMethods(AntaTest):
```
"""

name = "VerifyAcctConsoleMethods"
description = "Verifies the AAA accounting console method lists for different accounting types (system, exec, commands, dot1x)."
categories: ClassVar[list[str]] = ["aaa"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show aaa methods accounting", revision=1)]

Expand Down
12 changes: 3 additions & 9 deletions anta/tests/avt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@


class VerifyAVTPathHealth(AntaTest):
"""
Verifies the status of all Adaptive Virtual Topology (AVT) paths for all VRFs.
"""Verifies the status of all Adaptive Virtual Topology (AVT) paths for all VRFs.
Expected Results
----------------
Expand All @@ -34,7 +33,6 @@ class VerifyAVTPathHealth(AntaTest):
```
"""

name = "VerifyAVTPathHealth"
description = "Verifies the status of all AVT paths for all VRFs."
categories: ClassVar[list[str]] = ["avt"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show adaptive-virtual-topology path")]
Expand Down Expand Up @@ -73,8 +71,7 @@ def test(self) -> None:


class VerifyAVTSpecificPath(AntaTest):
"""
Verifies the status and type of an Adaptive Virtual Topology (AVT) path for a specified VRF.
"""Verifies the status and type of an Adaptive Virtual Topology (AVT) path for a specified VRF.
Expected Results
----------------
Expand All @@ -97,7 +94,6 @@ class VerifyAVTSpecificPath(AntaTest):
```
"""

name = "VerifyAVTSpecificPath"
description = "Verifies the status and type of an AVT path for a specified VRF."
categories: ClassVar[list[str]] = ["avt"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [
Expand Down Expand Up @@ -191,8 +187,7 @@ def test(self) -> None:


class VerifyAVTRole(AntaTest):
"""
Verifies the Adaptive Virtual Topology (AVT) role of a device.
"""Verifies the Adaptive Virtual Topology (AVT) role of a device.
Expected Results
----------------
Expand All @@ -208,7 +203,6 @@ class VerifyAVTRole(AntaTest):
```
"""

name = "VerifyAVTRole"
description = "Verifies the AVT role of a device."
categories: ClassVar[list[str]] = ["avt"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show adaptive-virtual-topology path")]
Expand Down
7 changes: 0 additions & 7 deletions anta/tests/bfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class VerifyBFDSpecificPeers(AntaTest):
```
"""

name = "VerifyBFDSpecificPeers"
description = "Verifies the IPv4 BFD peer's sessions and remote disc in the specified VRF."
categories: ClassVar[list[str]] = ["bfd"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bfd peers", revision=1)]
Expand Down Expand Up @@ -123,8 +122,6 @@ class VerifyBFDPeersIntervals(AntaTest):
```
"""

name = "VerifyBFDPeersIntervals"
description = "Verifies the timers of the IPv4 BFD peers in the specified VRF."
categories: ClassVar[list[str]] = ["bfd"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bfd peers detail", revision=1)]

Expand Down Expand Up @@ -218,8 +215,6 @@ class VerifyBFDPeersHealth(AntaTest):
```
"""

name = "VerifyBFDPeersHealth"
description = "Verifies the health of all IPv4 BFD peers."
categories: ClassVar[list[str]] = ["bfd"]
# revision 1 as later revision introduces additional nesting for type
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [
Expand Down Expand Up @@ -308,8 +303,6 @@ class VerifyBFDPeersRegProtocols(AntaTest):
```
"""

name = "VerifyBFDPeersRegProtocols"
description = "Verifies that IPv4 BFD peer(s) have the specified protocol(s) registered."
categories: ClassVar[list[str]] = ["bfd"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show bfd peers detail", revision=1)]

Expand Down
5 changes: 0 additions & 5 deletions anta/tests/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class VerifyZeroTouch(AntaTest):
```
"""

name = "VerifyZeroTouch"
description = "Verifies ZeroTouch is disabled"
categories: ClassVar[list[str]] = ["configuration"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show zerotouch", revision=1)]

Expand Down Expand Up @@ -64,8 +62,6 @@ class VerifyRunningConfigDiffs(AntaTest):
```
"""

name = "VerifyRunningConfigDiffs"
description = "Verifies there is no difference between the running-config and the startup-config"
categories: ClassVar[list[str]] = ["configuration"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show running-config diffs", ofmt="text")]

Expand Down Expand Up @@ -104,7 +100,6 @@ class VerifyRunningConfigLines(AntaTest):
```
"""

name = "VerifyRunningConfigLines"
description = "Search the Running-Config for the given RegEx patterns."
categories: ClassVar[list[str]] = ["configuration"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show running-config", ofmt="text")]
Expand Down
3 changes: 0 additions & 3 deletions anta/tests/connectivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class VerifyReachability(AntaTest):
```
"""

name = "VerifyReachability"
description = "Test the network reachability to one or many destination IP(s)."
categories: ClassVar[list[str]] = ["connectivity"]
# Removing the <space> between '{size}' and '{df_bit}' to compensate the df-bit set default value
# i.e if df-bit kept disable then it will add redundant space in between the command
Expand Down Expand Up @@ -129,7 +127,6 @@ class VerifyLLDPNeighbors(AntaTest):
```
"""

name = "VerifyLLDPNeighbors"
description = "Verifies that the provided LLDP neighbors are connected properly."
categories: ClassVar[list[str]] = ["connectivity"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show lldp neighbors detail", revision=1)]
Expand Down
2 changes: 0 additions & 2 deletions anta/tests/field_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class VerifyFieldNotice44Resolution(AntaTest):
```
"""

name = "VerifyFieldNotice44Resolution"
description = "Verifies that the device is using the correct Aboot version per FN0044."
categories: ClassVar[list[str]] = ["field notices"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show version detail", revision=1)]
Expand Down Expand Up @@ -143,7 +142,6 @@ class VerifyFieldNotice72Resolution(AntaTest):
```
"""

name = "VerifyFieldNotice72Resolution"
description = "Verifies if the device is exposed to FN0072, and if the issue has been mitigated."
categories: ClassVar[list[str]] = ["field notices"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show version detail", revision=1)]
Expand Down
10 changes: 3 additions & 7 deletions anta/tests/flow_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@


def validate_record_export(record_export: dict[str, str], tracker_info: dict[str, str]) -> str:
"""
Validate the record export configuration against the tracker info.
"""Validate the record export configuration against the tracker info.
Parameters
----------
Expand All @@ -41,8 +40,7 @@ def validate_record_export(record_export: dict[str, str], tracker_info: dict[str


def validate_exporters(exporters: list[dict[str, str]], tracker_info: dict[str, str]) -> str:
"""
Validate the exporter configurations against the tracker info.
"""Validate the exporter configurations against the tracker info.
Parameters
----------
Expand Down Expand Up @@ -74,8 +72,7 @@ def validate_exporters(exporters: list[dict[str, str]], tracker_info: dict[str,


class VerifyHardwareFlowTrackerStatus(AntaTest):
"""
Verifies if hardware flow tracking is running and an input tracker is active.
"""Verifies if hardware flow tracking is running and an input tracker is active.
This test optionally verifies the tracker interval/timeout and exporter configuration.
Expand All @@ -102,7 +99,6 @@ class VerifyHardwareFlowTrackerStatus(AntaTest):
```
"""

name = "VerifyHardwareFlowTrackerStatus"
description = (
"Verifies if hardware flow tracking is running and an input tracker is active. Optionally verifies the tracker interval/timeout and exporter configuration."
)
Expand Down
4 changes: 1 addition & 3 deletions anta/tests/greent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class VerifyGreenTCounters(AntaTest):
```
"""

name = "VerifyGreenTCounters"
description = "Verifies if the GreenT counters are incremented."
categories: ClassVar[list[str]] = ["greent"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show monitor telemetry postcard counters", revision=1)]
Expand Down Expand Up @@ -61,8 +60,7 @@ class VerifyGreenT(AntaTest):
```
"""

name = "VerifyGreenT"
description = "Verifies if a GreenT policy is created."
description = "Verifies if a GreenT policy other than the default is created."
categories: ClassVar[list[str]] = ["greent"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show monitor telemetry postcard policy profile", revision=1)]

Expand Down
11 changes: 0 additions & 11 deletions anta/tests/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class VerifyTransceiversManufacturers(AntaTest):
```
"""

name = "VerifyTransceiversManufacturers"
description = "Verifies if all transceivers come from approved manufacturers."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show inventory", revision=2)]

Expand Down Expand Up @@ -77,8 +75,6 @@ class VerifyTemperature(AntaTest):
```
"""

name = "VerifyTemperature"
description = "Verifies the device temperature."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show system environment temperature", revision=1)]

Expand Down Expand Up @@ -110,8 +106,6 @@ class VerifyTransceiversTemperature(AntaTest):
```
"""

name = "VerifyTransceiversTemperature"
description = "Verifies the transceivers temperature."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show system environment temperature transceiver", revision=1)]

Expand Down Expand Up @@ -151,8 +145,6 @@ class VerifyEnvironmentSystemCooling(AntaTest):
```
"""

name = "VerifyEnvironmentSystemCooling"
description = "Verifies the system cooling status."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show system environment cooling", revision=1)]

Expand Down Expand Up @@ -232,8 +224,6 @@ class VerifyEnvironmentPower(AntaTest):
```
"""

name = "VerifyEnvironmentPower"
description = "Verifies the power supplies status."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show system environment power", revision=1)]

Expand Down Expand Up @@ -274,7 +264,6 @@ class VerifyAdverseDrops(AntaTest):
```
"""

name = "VerifyAdverseDrops"
description = "Verifies there are no adverse drops on DCS-7280 and DCS-7500 family switches."
categories: ClassVar[list[str]] = ["hardware"]
commands: ClassVar[list[AntaCommand | AntaTemplate]] = [AntaCommand(command="show hardware counter drop", revision=1)]
Expand Down
Loading

0 comments on commit 27a1699

Please sign in to comment.