Adding upstream version 0.14.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-05 11:38:32 +01:00
parent ecf5ca3300
commit 6721599912
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
211 changed files with 12174 additions and 6401 deletions

View file

@ -1,44 +1,51 @@
# Copyright (c) 2023-2024 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""
Test anta.report.__init__.py
"""
"""Test anta.report.__init__.py."""
from __future__ import annotations
from typing import Callable
from typing import TYPE_CHECKING, Callable
import pytest
from rich.table import Table
from anta import RICH_COLOR_PALETTE
from anta.custom_types import TestStatus
from anta.reporter import ReportTable
from anta.result_manager import ResultManager
if TYPE_CHECKING:
from anta.custom_types import TestStatus
from anta.result_manager import ResultManager
class Test_ReportTable:
"""
Test ReportTable class
"""
class TestReportTable:
"""Test ReportTable class."""
# not testing __init__ as nothing is going on there
@pytest.mark.parametrize(
"usr_list, delimiter, expected_output",
("usr_list", "delimiter", "expected_output"),
[
pytest.param([], None, "", id="empty list no delimiter"),
pytest.param([], "*", "", id="empty list with delimiter"),
pytest.param(["elem1"], None, "elem1", id="one elem list no delimiter"),
pytest.param(["elem1"], "*", "* elem1", id="one elem list with delimiter"),
pytest.param(["elem1", "elem2"], None, "elem1\nelem2", id="two elems list no delimiter"),
pytest.param(["elem1", "elem2"], "&", "& elem1\n& elem2", id="two elems list with delimiter"),
pytest.param(
["elem1", "elem2"],
None,
"elem1\nelem2",
id="two elems list no delimiter",
),
pytest.param(
["elem1", "elem2"],
"&",
"& elem1\n& elem2",
id="two elems list with delimiter",
),
],
)
def test__split_list_to_txt_list(self, usr_list: list[str], delimiter: str | None, expected_output: str) -> None:
"""
test _split_list_to_txt_list
"""
"""Test _split_list_to_txt_list."""
# pylint: disable=protected-access
report = ReportTable()
assert report._split_list_to_txt_list(usr_list, delimiter) == expected_output
@ -52,9 +59,7 @@ class Test_ReportTable:
],
)
def test__build_headers(self, headers: list[str]) -> None:
"""
test _build_headers
"""
"""Test _build_headers."""
# pylint: disable=protected-access
report = ReportTable()
table = Table()
@ -65,7 +70,7 @@ class Test_ReportTable:
assert table.columns[table_column_before].style == RICH_COLOR_PALETTE.HEADER
@pytest.mark.parametrize(
"status, expected_status",
("status", "expected_status"),
[
pytest.param("unknown", "unknown", id="unknown status"),
pytest.param("unset", "[grey74]unset", id="unset status"),
@ -76,48 +81,42 @@ class Test_ReportTable:
],
)
def test__color_result(self, status: TestStatus, expected_status: str) -> None:
"""
test _build_headers
"""
"""Test _build_headers."""
# pylint: disable=protected-access
report = ReportTable()
assert report._color_result(status) == expected_status
@pytest.mark.parametrize(
"host, testcase, title, number_of_tests, expected_length",
("title", "number_of_tests", "expected_length"),
[
pytest.param(None, None, None, 5, 5, id="all results"),
pytest.param("host1", None, None, 5, 0, id="result for host1 when no host1 test"),
pytest.param(None, "VerifyTest3", None, 5, 1, id="result for test VerifyTest3"),
pytest.param(None, None, "Custom title", 5, 5, id="Change table title"),
pytest.param(None, 5, 5, id="all results"),
pytest.param(None, 0, 0, id="result for host1 when no host1 test"),
pytest.param(None, 5, 5, id="result for test VerifyTest3"),
pytest.param("Custom title", 5, 5, id="Change table title"),
],
)
def test_report_all(
self,
result_manager_factory: Callable[[int], ResultManager],
host: str | None,
testcase: str | None,
title: str | None,
number_of_tests: int,
expected_length: int,
) -> None:
"""
test report_all
"""
"""Test report_all."""
# pylint: disable=too-many-arguments
rm = result_manager_factory(number_of_tests)
manager = result_manager_factory(number_of_tests)
report = ReportTable()
kwargs = {"host": host, "testcase": testcase, "title": title}
kwargs = {"title": title}
kwargs = {k: v for k, v in kwargs.items() if v is not None}
res = report.report_all(rm, **kwargs) # type: ignore[arg-type]
res = report.report_all(manager, **kwargs) # type: ignore[arg-type]
assert isinstance(res, Table)
assert res.title == (title or "All tests results")
assert res.row_count == expected_length
@pytest.mark.parametrize(
"testcase, title, number_of_tests, expected_length",
("test", "title", "number_of_tests", "expected_length"),
[
pytest.param(None, None, 5, 5, id="all results"),
pytest.param("VerifyTest3", None, 5, 1, id="result for test VerifyTest3"),
@ -127,67 +126,62 @@ class Test_ReportTable:
def test_report_summary_tests(
self,
result_manager_factory: Callable[[int], ResultManager],
testcase: str | None,
test: str | None,
title: str | None,
number_of_tests: int,
expected_length: int,
) -> None:
"""
test report_summary_tests
"""
"""Test report_summary_tests."""
# pylint: disable=too-many-arguments
# TODO refactor this later... this is injecting double test results by modyfing the device name
# TODO: refactor this later... this is injecting double test results by modyfing the device name
# should be a fixture
rm = result_manager_factory(number_of_tests)
new_results = [result.model_copy() for result in rm.get_results()]
manager = result_manager_factory(number_of_tests)
new_results = [result.model_copy() for result in manager.results]
for result in new_results:
result.name = "test_device"
result.result = "failure"
rm.add_test_results(new_results)
report = ReportTable()
kwargs = {"testcase": testcase, "title": title}
kwargs = {"tests": [test] if test is not None else None, "title": title}
kwargs = {k: v for k, v in kwargs.items() if v is not None}
res = report.report_summary_tests(rm, **kwargs) # type: ignore[arg-type]
res = report.report_summary_tests(manager, **kwargs) # type: ignore[arg-type]
assert isinstance(res, Table)
assert res.title == (title or "Summary per test case")
assert res.title == (title or "Summary per test")
assert res.row_count == expected_length
@pytest.mark.parametrize(
"host, title, number_of_tests, expected_length",
("dev", "title", "number_of_tests", "expected_length"),
[
pytest.param(None, None, 5, 2, id="all results"),
pytest.param("host1", None, 5, 1, id="result for host host1"),
pytest.param(None, "Custom title", 5, 2, id="Change table title"),
pytest.param(None, None, 5, 1, id="all results"),
pytest.param("device1", None, 5, 1, id="result for host host1"),
pytest.param(None, "Custom title", 5, 1, id="Change table title"),
],
)
def test_report_summary_hosts(
def test_report_summary_devices(
self,
result_manager_factory: Callable[[int], ResultManager],
host: str | None,
dev: str | None,
title: str | None,
number_of_tests: int,
expected_length: int,
) -> None:
"""
test report_summary_hosts
"""
"""Test report_summary_devices."""
# pylint: disable=too-many-arguments
# TODO refactor this later... this is injecting double test results by modyfing the device name
# TODO: refactor this later... this is injecting double test results by modyfing the device name
# should be a fixture
rm = result_manager_factory(number_of_tests)
new_results = [result.model_copy() for result in rm.get_results()]
manager = result_manager_factory(number_of_tests)
new_results = [result.model_copy() for result in manager.results]
for result in new_results:
result.name = host or "test_device"
result.name = dev or "test_device"
result.result = "failure"
rm.add_test_results(new_results)
manager.results = new_results
report = ReportTable()
kwargs = {"host": host, "title": title}
kwargs = {"devices": [dev] if dev is not None else None, "title": title}
kwargs = {k: v for k, v in kwargs.items() if v is not None}
res = report.report_summary_hosts(rm, **kwargs) # type: ignore[arg-type]
res = report.report_summary_devices(manager, **kwargs) # type: ignore[arg-type]
assert isinstance(res, Table)
assert res.title == (title or "Summary per host")
assert res.title == (title or "Summary per device")
assert res.row_count == expected_length