2025-02-05 11:32:35 +01:00
|
|
|
# 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.
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test anta.report.__init__.py."""
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
from typing import TYPE_CHECKING, Callable
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from rich.table import Table
|
|
|
|
|
|
|
|
from anta import RICH_COLOR_PALETTE
|
|
|
|
from anta.reporter import ReportTable
|
2025-02-05 11:39:09 +01:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from anta.custom_types import TestStatus
|
|
|
|
from anta.result_manager import ResultManager
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
class TestReportTable:
|
|
|
|
"""Test ReportTable class."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
# not testing __init__ as nothing is going on there
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("usr_list", "delimiter", "expected_output"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
|
|
|
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"),
|
2025-02-05 11:39:09 +01:00
|
|
|
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",
|
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test__split_list_to_txt_list(self, usr_list: list[str], delimiter: str | None, expected_output: str) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test _split_list_to_txt_list."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
report = ReportTable()
|
|
|
|
assert report._split_list_to_txt_list(usr_list, delimiter) == expected_output
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"headers",
|
|
|
|
[
|
|
|
|
pytest.param([], id="empty list"),
|
|
|
|
pytest.param(["elem1"], id="one elem list"),
|
|
|
|
pytest.param(["elem1", "elem2"], id="two elemst"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test__build_headers(self, headers: list[str]) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test _build_headers."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
report = ReportTable()
|
|
|
|
table = Table()
|
|
|
|
table_column_before = len(table.columns)
|
|
|
|
report._build_headers(headers, table)
|
|
|
|
assert len(table.columns) == table_column_before + len(headers)
|
|
|
|
if len(table.columns) > 0:
|
|
|
|
assert table.columns[table_column_before].style == RICH_COLOR_PALETTE.HEADER
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("status", "expected_status"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
|
|
|
pytest.param("unknown", "unknown", id="unknown status"),
|
|
|
|
pytest.param("unset", "[grey74]unset", id="unset status"),
|
|
|
|
pytest.param("skipped", "[bold orange4]skipped", id="skipped status"),
|
|
|
|
pytest.param("failure", "[bold red]failure", id="failure status"),
|
|
|
|
pytest.param("error", "[indian_red]error", id="error status"),
|
|
|
|
pytest.param("success", "[green4]success", id="success status"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test__color_result(self, status: TestStatus, expected_status: str) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test _build_headers."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=protected-access
|
|
|
|
report = ReportTable()
|
|
|
|
assert report._color_result(status) == expected_status
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("title", "number_of_tests", "expected_length"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
2025-02-05 11:39:09 +01:00
|
|
|
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"),
|
2025-02-05 11:32:35 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_report_all(
|
|
|
|
self,
|
|
|
|
result_manager_factory: Callable[[int], ResultManager],
|
|
|
|
title: str | None,
|
|
|
|
number_of_tests: int,
|
|
|
|
expected_length: int,
|
|
|
|
) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test report_all."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2025-02-05 11:39:09 +01:00
|
|
|
manager = result_manager_factory(number_of_tests)
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
report = ReportTable()
|
2025-02-05 11:39:09 +01:00
|
|
|
kwargs = {"title": title}
|
2025-02-05 11:32:35 +01:00
|
|
|
kwargs = {k: v for k, v in kwargs.items() if v is not None}
|
2025-02-05 11:39:09 +01:00
|
|
|
res = report.report_all(manager, **kwargs) # type: ignore[arg-type]
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
assert isinstance(res, Table)
|
|
|
|
assert res.title == (title or "All tests results")
|
|
|
|
assert res.row_count == expected_length
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("test", "title", "number_of_tests", "expected_length"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
|
|
|
pytest.param(None, None, 5, 5, id="all results"),
|
|
|
|
pytest.param("VerifyTest3", None, 5, 1, id="result for test VerifyTest3"),
|
|
|
|
pytest.param(None, "Custom title", 5, 5, id="Change table title"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_report_summary_tests(
|
|
|
|
self,
|
|
|
|
result_manager_factory: Callable[[int], ResultManager],
|
2025-02-05 11:39:09 +01:00
|
|
|
test: str | None,
|
2025-02-05 11:32:35 +01:00
|
|
|
title: str | None,
|
|
|
|
number_of_tests: int,
|
|
|
|
expected_length: int,
|
|
|
|
) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test report_summary_tests."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2025-02-05 11:39:09 +01:00
|
|
|
# TODO: refactor this later... this is injecting double test results by modyfing the device name
|
2025-02-05 11:32:35 +01:00
|
|
|
# should be a fixture
|
2025-02-05 11:39:09 +01:00
|
|
|
manager = result_manager_factory(number_of_tests)
|
|
|
|
new_results = [result.model_copy() for result in manager.results]
|
2025-02-05 11:32:35 +01:00
|
|
|
for result in new_results:
|
|
|
|
result.name = "test_device"
|
|
|
|
result.result = "failure"
|
|
|
|
|
|
|
|
report = ReportTable()
|
2025-02-05 11:39:09 +01:00
|
|
|
kwargs = {"tests": [test] if test is not None else None, "title": title}
|
2025-02-05 11:32:35 +01:00
|
|
|
kwargs = {k: v for k, v in kwargs.items() if v is not None}
|
2025-02-05 11:39:09 +01:00
|
|
|
res = report.report_summary_tests(manager, **kwargs) # type: ignore[arg-type]
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
assert isinstance(res, Table)
|
2025-02-05 11:39:09 +01:00
|
|
|
assert res.title == (title or "Summary per test")
|
2025-02-05 11:32:35 +01:00
|
|
|
assert res.row_count == expected_length
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("dev", "title", "number_of_tests", "expected_length"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
2025-02-05 11:39:09 +01:00
|
|
|
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"),
|
2025-02-05 11:32:35 +01:00
|
|
|
],
|
|
|
|
)
|
2025-02-05 11:39:09 +01:00
|
|
|
def test_report_summary_devices(
|
2025-02-05 11:32:35 +01:00
|
|
|
self,
|
|
|
|
result_manager_factory: Callable[[int], ResultManager],
|
2025-02-05 11:39:09 +01:00
|
|
|
dev: str | None,
|
2025-02-05 11:32:35 +01:00
|
|
|
title: str | None,
|
|
|
|
number_of_tests: int,
|
|
|
|
expected_length: int,
|
|
|
|
) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test report_summary_devices."""
|
2025-02-05 11:32:35 +01:00
|
|
|
# pylint: disable=too-many-arguments
|
2025-02-05 11:39:09 +01:00
|
|
|
# TODO: refactor this later... this is injecting double test results by modyfing the device name
|
2025-02-05 11:32:35 +01:00
|
|
|
# should be a fixture
|
2025-02-05 11:39:09 +01:00
|
|
|
manager = result_manager_factory(number_of_tests)
|
|
|
|
new_results = [result.model_copy() for result in manager.results]
|
2025-02-05 11:32:35 +01:00
|
|
|
for result in new_results:
|
2025-02-05 11:39:09 +01:00
|
|
|
result.name = dev or "test_device"
|
2025-02-05 11:32:35 +01:00
|
|
|
result.result = "failure"
|
2025-02-05 11:39:09 +01:00
|
|
|
manager.results = new_results
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
report = ReportTable()
|
2025-02-05 11:39:09 +01:00
|
|
|
kwargs = {"devices": [dev] if dev is not None else None, "title": title}
|
2025-02-05 11:32:35 +01:00
|
|
|
kwargs = {k: v for k, v in kwargs.items() if v is not None}
|
2025-02-05 11:39:09 +01:00
|
|
|
res = report.report_summary_devices(manager, **kwargs) # type: ignore[arg-type]
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
assert isinstance(res, Table)
|
2025-02-05 11:39:09 +01:00
|
|
|
assert res.title == (title or "Summary per device")
|
2025-02-05 11:32:35 +01:00
|
|
|
assert res.row_count == expected_length
|