Skip to content

Commit

Permalink
refactor: Cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuloc committed Oct 3, 2024
1 parent b986427 commit 7e56c37
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
18 changes: 14 additions & 4 deletions anta/cli/get/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,19 @@ def tests() -> None:
import importlib
import inspect
import pkgutil
import warnings

from numpydoc.docscrape import NumpyDocString

from anta.models import AntaTest

# TODO: with this method need to disable warning for unknown section in numpydoc
# Would be better to not use numpydoc
def explore_package(module_name, level=2):
def explore_package(module_name: str, level: int = 2) -> None:
loader = pkgutil.get_loader(module_name)
if loader is None:
# TODO: log something
return
path = Path(loader.get_filename()).parent
for sub_module in pkgutil.walk_packages([str(path)]):
_, sub_module_name, _ = sub_module
Expand All @@ -159,9 +163,15 @@ def explore_package(module_name, level=2):
else:
console.print(f"{qname}:")
qname_module = importlib.import_module(qname)
for name, obj in inspect.getmembers(qname_module):
for _name, obj in inspect.getmembers(qname_module):
if inspect.isclass(obj) and issubclass(obj, AntaTest) and obj != AntaTest:
doc = NumpyDocString(obj.__doc__)
print("\n".join(l[2 * level :] for l in doc["Examples"][level:-1]))
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="Unknown section Expected Results")
doc = NumpyDocString(obj.__doc__)
console.print(f" - {obj.name}:")
console.print(f" # {obj.description}", soft_wrap=False)
# TODO: add info about when the test was added
if len(doc["Examples"]) > 2 + level:
console.print("\n".join(line[2 * level :] for line in doc["Examples"][level + 1 : -1]))

explore_package("anta.tests")
10 changes: 1 addition & 9 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,19 +506,11 @@ def save_commands_data(self, eos_data: list[dict[str, Any] | str]) -> None:

def __init_subclass__(cls) -> None:
"""Verify that the mandatory class attributes are defined."""
mandatory_attributes = ["categories", "commands"]
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)
# default_attributes = ["name", "description"]
if not hasattr(cls, "name"):
cls.name = cls.__name__
if not hasattr(cls, "description"):
if not cls.test.__doc__:
# No doctsring - raise
raise Exception("TODO")
cls.description = cls.test.__doc__.split(sep="\n")[0]

@property
def module(self) -> str:
Expand Down
4 changes: 4 additions & 0 deletions anta/tests/aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ 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 @@ -79,6 +81,8 @@ 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
1 change: 0 additions & 1 deletion anta/tests/routing/isis.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ class VerifyISISSegmentRoutingAdjacencySegments(AntaTest):
- interface: Ethernet2
address: 10.0.1.3
sid_origin: dynamic
```
"""

Expand Down
16 changes: 8 additions & 8 deletions anta/tests/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,10 @@ class VerifyBannerLogin(AntaTest):
```yaml
anta.tests.security:
- VerifyBannerLogin:
login_banner: |
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
login_banner: |
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
```
"""

Expand Down Expand Up @@ -542,10 +542,10 @@ class VerifyBannerMotd(AntaTest):
```yaml
anta.tests.security:
- VerifyBannerMotd:
motd_banner: |
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
motd_banner: |
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
```
"""

Expand Down

0 comments on commit 7e56c37

Please sign in to comment.