Skip to content

Commit

Permalink
refactor: test another flow for performances
Browse files Browse the repository at this point in the history
  • Loading branch information
mtache committed Jan 7, 2025
1 parent 303ae12 commit 49e6f9a
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions anta/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 49e6f9a

Please sign in to comment.