1
0
Fork 0

Merging upstream version 0.14.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-05 11:39:09 +01:00
parent 082ce481df
commit 2265bd9c67
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
211 changed files with 12174 additions and 6401 deletions
anta/result_manager

View file

@ -2,10 +2,8 @@
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Models related to anta.result_manager module."""
from __future__ import annotations
# Need to keep List for pydantic in 3.8
from typing import List, Optional
from __future__ import annotations
from pydantic import BaseModel
@ -13,10 +11,10 @@ from anta.custom_types import TestStatus
class TestResult(BaseModel):
"""
Describe the result of a test from a single device.
"""Describe the result of a test from a single device.
Attributes:
Attributes
----------
name: Device name where the test has run.
test: Test name runs on the device.
categories: List of categories the TestResult belongs to, by default the AntaTest categories.
@ -24,63 +22,70 @@ class TestResult(BaseModel):
result: Result of the test. Can be one of "unset", "success", "failure", "error" or "skipped".
messages: Message to report after the test if any.
custom_field: Custom field to store a string for flexibility in integrating with ANTA
"""
name: str
test: str
categories: List[str]
categories: list[str]
description: str
result: TestStatus = "unset"
messages: List[str] = []
custom_field: Optional[str] = None
messages: list[str] = []
custom_field: str | None = None
def is_success(self, message: str | None = None) -> None:
"""
Helper to set status to success
"""Set status to success.
Args:
----
message: Optional message related to the test
"""
self._set_status("success", message)
def is_failure(self, message: str | None = None) -> None:
"""
Helper to set status to failure
"""Set status to failure.
Args:
----
message: Optional message related to the test
"""
self._set_status("failure", message)
def is_skipped(self, message: str | None = None) -> None:
"""
Helper to set status to skipped
"""Set status to skipped.
Args:
----
message: Optional message related to the test
"""
self._set_status("skipped", message)
def is_error(self, message: str | None = None) -> None:
"""
Helper to set status to error
"""Set status to error.
Args:
----
message: Optional message related to the test
"""
self._set_status("error", message)
def _set_status(self, status: TestStatus, message: str | None = None) -> None:
"""
Set status and insert optional message
"""Set status and insert optional message.
Args:
----
status: status of the test
message: optional message
"""
self.result = status
if message is not None:
self.messages.append(message)
def __str__(self) -> str:
"""
Returns a human readable string of this TestResult
"""
"""Return a human readable string of this TestResult."""
return f"Test '{self.test}' (on '{self.name}'): Result '{self.result}'\nMessages: {self.messages}"