From 49e6f9a31d4ee92ed64754ea4b4eaeabd212f22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20T=C3=A2che?= Date: Tue, 7 Jan 2025 10:14:57 +0100 Subject: [PATCH] refactor: test another flow for performances --- anta/models.py | 52 +++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/anta/models.py b/anta/models.py index da8e0850f..5839bee3c 100644 --- a/anta/models.py +++ b/anta/models.py @@ -19,7 +19,7 @@ from anta.constants import KNOWN_EOS_ERRORS from anta.custom_types import REGEXP_EOS_BLACKLIST_CMDS, Revision from anta.logger import anta_log_exception, exc_to_str -from anta.result_manager.models import AntaTestStatus, TestResult +from anta.result_manager.models import TestResult if TYPE_CHECKING: from collections.abc import Coroutine @@ -446,39 +446,43 @@ def __init__( """ self.logger: logging.Logger = logging.getLogger(f"{self.module}.{self.__class__.__name__}") self.device: AntaDevice = device - self.inputs: AntaTest.Input self.instance_commands: list[AntaCommand] = [] - self.result: TestResult = TestResult(name=device.name, test=self.name, categories=self.categories, description=self.description, inputs=None) - self._init_inputs(inputs) - if self.result.result == AntaTestStatus.UNSET: + self.inputs: AntaTest.Input | None + try: + self._init_inputs(inputs) + except ValidationError as e: + message = f"{self.module}.{self.name}: Inputs are not valid\n{e}" + self.logger.error(message) + self.inputs = None + self.result: TestResult = TestResult(name=device.name, test=self.name, categories=self.categories, description=self.description, inputs=self.inputs) + if self.inputs is None: + self.result.is_error(message=message) + else: self._init_commands(eos_data) + if res_ow := self.inputs.result_overwrite: + if res_ow.categories: + self.result.categories = res_ow.categories + if res_ow.description: + self.result.description = res_ow.description + self.result.custom_field = res_ow.custom_field def _init_inputs(self, inputs: dict[str, Any] | AntaTest.Input | None) -> None: """Instantiate the `inputs` instance attribute with an `AntaTest.Input` instance to validate test inputs using the model. + Raises + ------ + ValidationError + If any input validation error occurs. Overwrite result fields based on `ResultOverwrite` input definition. Any input validation error will set this test result status as 'error'. """ - try: - if inputs is None: - self.inputs = self.Input() - elif isinstance(inputs, AntaTest.Input): - self.inputs = inputs - elif isinstance(inputs, dict): - self.inputs = self.Input(**inputs) - except ValidationError as e: - message = f"{self.module}.{self.name}: Inputs are not valid\n{e}" - self.logger.error(message) - self.result.is_error(message=message) - return - if res_ow := self.inputs.result_overwrite: - if res_ow.categories: - self.result.categories = res_ow.categories - if res_ow.description: - self.result.description = res_ow.description - self.result.custom_field = res_ow.custom_field - self.result.inputs = self.inputs + if inputs is None: + self.inputs = self.Input() + elif isinstance(inputs, AntaTest.Input): + self.inputs = inputs + elif isinstance(inputs, dict): + self.inputs = self.Input(**inputs) def _init_commands(self, eos_data: list[dict[Any, Any] | str] | None) -> None: """Instantiate the `instance_commands` instance attribute from the `commands` class attribute.