Skip to content

Commit

Permalink
Added custom_test_specs
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-baillargeon committed Aug 6, 2024
1 parent 63df07d commit 8853a02
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
6 changes: 4 additions & 2 deletions python-avd/pyavd/_anta/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class FabricData:
Attributes:
----------
structured_configs : dict[str, dict]
The structured configurations of the devices in the fabric. The key is the device name and the value is the structured config.
The structured configurations of the devices in the fabric.
The key is the device name and the value is the structured config.
loopback0_mapping : dict[str, IPv4Address]
The mapping of the Loopback0 IP addresses for each device.
vtep_mapping : dict[str, IPv4Address]
Expand Down Expand Up @@ -128,7 +129,8 @@ def check_inputs(self) -> Self:
def is_ready(self) -> bool:
"""Check if the TestSpec has all necessary components to create an AntaTestDefinition.
A TestSpec is ready if the test class is defined and either an input factory or an input dict is provided if the test requires input.
A TestSpec is ready if the test class is defined and either an input factory or
an input dict is provided if the test requires input.
"""
is_ready = False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

from pyavd._utils import load_classes

from .anta_test_specs import ANTA_TEST_SPECS
from .test_specs import PYAVD_TEST_SPECS

if TYPE_CHECKING:
from anta.models import AntaTest

from .anta_test_specs import TestSpec
from .test_specs import TestSpec

LOGGER = logging.getLogger("pyavd")

Expand Down Expand Up @@ -44,9 +44,9 @@ def update_test_spec(test_spec: TestSpec, anta_available_tests: dict[str, type[A
return True


ANTA_TEST_SPECS = list(
PYAVD_TEST_SPECS = list(
filter(
partial(update_test_spec, anta_available_tests=ANTA_AVAILABLE_TESTS),
ANTA_TEST_SPECS,
)
PYAVD_TEST_SPECS,
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .constants import StructuredConfigKey
from .models import TestSpec

ANTA_TEST_SPECS: list[TestSpec] = [
PYAVD_TEST_SPECS: list[TestSpec] = [
TestSpec(
name="VerifyAPIHttpsSSL",
conditional_keys=[StructuredConfigKey.HTTPS_SSL_PROFILE],
Expand Down
16 changes: 10 additions & 6 deletions python-avd/pyavd/get_device_anta_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
if TYPE_CHECKING:
from anta.catalog import AntaCatalog

from ._anta.utils import FabricData
from ._anta.utils import FabricData, TestSpec


def get_device_anta_catalog(hostname: str, fabric_data: FabricData) -> AntaCatalog:
def get_device_anta_catalog(hostname: str, fabric_data: FabricData, custom_test_specs: list[TestSpec] | None = None) -> AntaCatalog:
"""Generate an ANTA catalog for a single device.
Parameters
Expand All @@ -22,21 +22,25 @@ def get_device_anta_catalog(hostname: str, fabric_data: FabricData) -> AntaCatal
Contains relevant data (e.g. structured configurations, loopback mappings, etc.)
of all devices in the fabric to generate the catalog.
The instance must be created using the `get_fabric_data` function of this module.
logger : logging.Logger
Optional custom logger to use. If not provided, the `pyavd` logger will be used.
custom_test_specs : list[TestSpec]
Optional user-defined list of TestSpec to be added to the default PyAVD test specs.
Returns:
-------
AntaCatalog
The generated ANTA catalog for the device.
"""
from ._anta.utils import ConfigManager, create_catalog
from ._anta.utils.anta_test_loader import ANTA_TEST_SPECS
from ._anta.utils.test_loader import PYAVD_TEST_SPECS

custom_test_specs = custom_test_specs or []

# Create the device-specific ConfigManager used to generate the inputs for the tests
config_manager = ConfigManager(hostname, fabric_data)

return create_catalog(config_manager, ANTA_TEST_SPECS)
PYAVD_TEST_SPECS.extend([test for test in custom_test_specs if test not in PYAVD_TEST_SPECS])

return create_catalog(config_manager, PYAVD_TEST_SPECS)


def get_fabric_data(structured_configs: dict[str, dict]) -> FabricData:
Expand Down

0 comments on commit 8853a02

Please sign in to comment.