Adding upstream version 1.1.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-05 11:54:23 +01:00
parent f13b7abbd8
commit 77504588ab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
196 changed files with 10121 additions and 3780 deletions

View file

@ -9,7 +9,6 @@ from typing import TYPE_CHECKING
from anta.cli import anta
from anta.cli.utils import ExitCode
from tests.lib.utils import default_anta_env
if TYPE_CHECKING:
from click.testing import CliRunner
@ -49,10 +48,16 @@ def test_anta_nrfu_dry_run(click_runner: CliRunner) -> None:
assert "Dry-run" in result.output
def test_anta_nrfu_wrong_catalog_format(click_runner: CliRunner) -> None:
"""Test anta nrfu --dry-run, catalog is given via env."""
result = click_runner.invoke(anta, ["nrfu", "--dry-run", "--catalog-format", "toto"])
assert result.exit_code == ExitCode.USAGE_ERROR
assert "Invalid value for '--catalog-format': 'toto' is not one of 'yaml', 'json'." in result.output
def test_anta_password_required(click_runner: CliRunner) -> None:
"""Test that password is provided."""
env = default_anta_env()
env["ANTA_PASSWORD"] = None
env = {"ANTA_PASSWORD": None}
result = click_runner.invoke(anta, ["nrfu"], env=env)
assert result.exit_code == ExitCode.USAGE_ERROR
@ -61,8 +66,7 @@ def test_anta_password_required(click_runner: CliRunner) -> None:
def test_anta_password(click_runner: CliRunner) -> None:
"""Test that password can be provided either via --password or --prompt."""
env = default_anta_env()
env["ANTA_PASSWORD"] = None
env = {"ANTA_PASSWORD": None}
result = click_runner.invoke(anta, ["nrfu", "--password", "secret"], env=env)
assert result.exit_code == ExitCode.OK
result = click_runner.invoke(anta, ["nrfu", "--prompt"], input="password\npassword\n", env=env)
@ -113,3 +117,9 @@ def test_disable_cache(click_runner: CliRunner) -> None:
if "disable_cache" in line:
assert "True" in line
assert result.exit_code == ExitCode.OK
def test_hide(click_runner: CliRunner) -> None:
"""Test the `--hide` option of the `anta nrfu` command."""
result = click_runner.invoke(anta, ["nrfu", "--hide", "success", "text"])
assert "SUCCESS" not in result.output

View file

@ -8,7 +8,8 @@ from __future__ import annotations
import json
import re
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any
from unittest.mock import patch
from anta.cli import anta
from anta.cli.utils import ExitCode
@ -51,7 +52,7 @@ def test_anta_nrfu_table(click_runner: CliRunner) -> None:
"""Test anta nrfu, catalog is given via env."""
result = click_runner.invoke(anta, ["nrfu", "table"])
assert result.exit_code == ExitCode.OK
assert "dummy │ VerifyEOSVersion │ success" in result.output
assert "leaf1 │ VerifyEOSVersion │ success" in result.output
def test_anta_nrfu_table_group_by_device(click_runner: CliRunner) -> None:
@ -72,7 +73,7 @@ def test_anta_nrfu_text(click_runner: CliRunner) -> None:
"""Test anta nrfu, catalog is given via env."""
result = click_runner.invoke(anta, ["nrfu", "text"])
assert result.exit_code == ExitCode.OK
assert "dummy :: VerifyEOSVersion :: SUCCESS" in result.output
assert "leaf1 :: VerifyEOSVersion :: SUCCESS" in result.output
def test_anta_nrfu_json(click_runner: CliRunner) -> None:
@ -84,13 +85,113 @@ def test_anta_nrfu_json(click_runner: CliRunner) -> None:
assert match is not None
result_list = json.loads(match.group())
for res in result_list:
if res["name"] == "dummy":
if res["name"] == "leaf1":
assert res["test"] == "VerifyEOSVersion"
assert res["result"] == "success"
def test_anta_nrfu_json_output(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu json with output file."""
json_output = tmp_path / "test.json"
result = click_runner.invoke(anta, ["nrfu", "json", "--output", str(json_output)])
# Making sure the output is not printed to stdout
match = re.search(r"\[\n {2}{[\s\S]+ {2}}\n\]", result.output)
assert match is None
assert result.exit_code == ExitCode.OK
assert "JSON results saved to" in result.output
assert json_output.exists()
def test_anta_nrfu_json_output_failure(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu json with output file."""
json_output = tmp_path / "test.json"
original_open = Path.open
def mock_path_open(*args: Any, **kwargs: Any) -> Path: # noqa: ANN401
"""Mock Path.open only for the json_output file of this test."""
if args[0] == json_output:
msg = "Simulated OSError"
raise OSError(msg)
# If not the json_output file, call the original Path.open
return original_open(*args, **kwargs)
with patch("pathlib.Path.open", mock_path_open):
result = click_runner.invoke(anta, ["nrfu", "json", "--output", str(json_output)])
assert result.exit_code == ExitCode.USAGE_ERROR
assert "Failed to save JSON results to" in result.output
assert not json_output.exists()
def test_anta_nrfu_template(click_runner: CliRunner) -> None:
"""Test anta nrfu, catalog is given via env."""
result = click_runner.invoke(anta, ["nrfu", "tpl-report", "--template", str(DATA_DIR / "template.j2")])
assert result.exit_code == ExitCode.OK
assert "* VerifyEOSVersion is SUCCESS for dummy" in result.output
assert "* VerifyEOSVersion is SUCCESS for leaf1" in result.output
def test_anta_nrfu_csv(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu csv."""
csv_output = tmp_path / "test.csv"
result = click_runner.invoke(anta, ["nrfu", "csv", "--csv-output", str(csv_output)])
assert result.exit_code == ExitCode.OK
assert "CSV report saved to" in result.output
assert csv_output.exists()
def test_anta_nrfu_csv_failure(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu csv."""
csv_output = tmp_path / "test.csv"
with patch("anta.reporter.csv_reporter.ReportCsv.generate", side_effect=OSError()):
result = click_runner.invoke(anta, ["nrfu", "csv", "--csv-output", str(csv_output)])
assert result.exit_code == ExitCode.USAGE_ERROR
assert "Failed to save CSV report to" in result.output
assert not csv_output.exists()
def test_anta_nrfu_md_report(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu md-report."""
md_output = tmp_path / "test.md"
result = click_runner.invoke(anta, ["nrfu", "md-report", "--md-output", str(md_output)])
assert result.exit_code == ExitCode.OK
assert "Markdown report saved to" in result.output
assert md_output.exists()
def test_anta_nrfu_md_report_failure(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu md-report failure."""
md_output = tmp_path / "test.md"
with patch("anta.reporter.md_reporter.MDReportGenerator.generate", side_effect=OSError()):
result = click_runner.invoke(anta, ["nrfu", "md-report", "--md-output", str(md_output)])
assert result.exit_code == ExitCode.USAGE_ERROR
assert "Failed to save Markdown report to" in result.output
assert not md_output.exists()
def test_anta_nrfu_md_report_with_hide(click_runner: CliRunner, tmp_path: Path) -> None:
"""Test anta nrfu md-report with the `--hide` option."""
md_output = tmp_path / "test.md"
result = click_runner.invoke(anta, ["nrfu", "--hide", "success", "md-report", "--md-output", str(md_output)])
assert result.exit_code == ExitCode.OK
assert "Markdown report saved to" in result.output
assert md_output.exists()
with md_output.open("r", encoding="utf-8") as f:
content = f.read()
# Use regex to find the "Total Tests Success" value
match = re.search(r"\| (\d+) \| (\d+) \| \d+ \| \d+ \| \d+ \|", content)
assert match is not None
total_tests = int(match.group(1))
total_tests_success = int(match.group(2))
assert total_tests == 0
assert total_tests_success == 0