From 34864a6011d2af4a9d94e37c41f7eb41c075dad2 Mon Sep 17 00:00:00 2001 From: gmuloc Date: Wed, 27 Sep 2023 11:46:47 +0200 Subject: [PATCH] Feat: Check catalog funcionality --- anta/catalog.py | 32 ++++++++++++++++++++++++++++++++ anta/cli/check/commands.py | 12 ++---------- 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 anta/catalog.py diff --git a/anta/catalog.py b/anta/catalog.py new file mode 100644 index 000000000..ea5926c18 --- /dev/null +++ b/anta/catalog.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the LICENSE file. +""" +Catalog related functions +""" +from __future__ import annotations + +import logging + +from anta.device import AsyncEOSDevice +from anta.models import AntaTest +from anta.result_manager import ResultManager + +logger = logging.getLogger(__name__) + + +def is_catalog_valid(catalog: list[tuple[AntaTest, AntaTest.Input]]) -> ResultManager: + """ + TODO - for now a test requires a device but this may be revisited in the future + """ + # Mock device + mock_device = AsyncEOSDevice(name="mock", host="127.0.0.1", username="mock", password="mock") + + manager = ResultManager() + # Instantiate each test to verify the Inputs are correct + for test_class, test_inputs in catalog: + # TODO - this is the same code with typing as in runner.py but somehow mypy complains that test_class + # ot type AntaTest is not callable + test_instance = test_class(device=mock_device, inputs=test_inputs) # type: ignore[operator] + manager.add_test_result(test_instance.result) + return manager diff --git a/anta/cli/check/commands.py b/anta/cli/check/commands.py index eba1a763e..070699f00 100644 --- a/anta/cli/check/commands.py +++ b/anta/cli/check/commands.py @@ -12,11 +12,10 @@ import click +from anta.catalog import is_catalog_valid from anta.cli.console import console from anta.cli.utils import parse_catalog -from anta.device import AsyncEOSDevice from anta.models import AntaTest -from anta.result_manager import ResultManager logger = logging.getLogger(__name__) @@ -37,14 +36,7 @@ def catalog(ctx: click.Context, catalog: list[tuple[AntaTest, AntaTest.Input]]) Check that the catalog is valid """ logger.info(f"Checking syntax of catalog {ctx.obj['catalog_path']}") - mock_device = AsyncEOSDevice(name="mock", host="127.0.0.1", username="mock", password="mock") - manager = ResultManager() - # Instantiate each test to verify the Inputs are correct - for test_class, test_inputs in catalog: - # TODO - this is the same code with typing as in runner.py but somehow mypy complains that test_class - # ot type AntaTest is not callable - test_instance = test_class(device=mock_device, inputs=test_inputs) # type: ignore[operator] - manager.add_test_result(test_instance.result) + manager = is_catalog_valid(catalog) if manager.error_status: console.print(f"[bold][red]Catalog {ctx.obj['catalog_path']} is invalid") # TODO print nice report