| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.cli.exec.commands.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from pathlib import Path |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from anta.cli import anta |
| 14 |
from anta.cli.exec.commands import clear_counters, collect_tech_support, snapshot |
| 15 |
from anta.cli.utils import ExitCode |
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
from click.testing import CliRunner |
| 19 |
|
| 20 |
|
| 21 |
def test_clear_counters_help(click_runner: CliRunner) -> None: |
| 22 |
"""Test `anta exec clear-counters --help`.""" |
| 23 |
result = click_runner.invoke(clear_counters, ["--help"]) |
| 24 |
assert result.exit_code == 0 |
| 25 |
assert "Usage" in result.output |
| 26 |
|
| 27 |
|
| 28 |
def test_snapshot_help(click_runner: CliRunner) -> None: |
| 29 |
"""Test `anta exec snapshot --help`.""" |
| 30 |
result = click_runner.invoke(snapshot, ["--help"]) |
| 31 |
assert result.exit_code == 0 |
| 32 |
assert "Usage" in result.output |
| 33 |
|
| 34 |
|
| 35 |
def test_collect_tech_support_help(click_runner: CliRunner) -> None: |
| 36 |
"""Test `anta exec collect-tech-support --help`.""" |
| 37 |
result = click_runner.invoke(collect_tech_support, ["--help"]) |
| 38 |
assert result.exit_code == 0 |
| 39 |
assert "Usage" in result.output |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.parametrize( |
| 43 |
"tags", |
| 44 |
[ |
| 45 |
pytest.param(None, id="no tags"), |
| 46 |
pytest.param("leaf,spine", id="with tags"), |
| 47 |
], |
| 48 |
) |
| 49 |
def test_clear_counters(click_runner: CliRunner, tags: str | None) -> None: |
| 50 |
"""Test `anta exec clear-counters`.""" |
| 51 |
cli_args = ["exec", "clear-counters"] |
| 52 |
if tags is not None: |
| 53 |
cli_args.extend(["--tags", tags]) |
| 54 |
result = click_runner.invoke(anta, cli_args) |
| 55 |
assert result.exit_code == ExitCode.OK |
| 56 |
|
| 57 |
|
| 58 |
COMMAND_LIST_PATH_FILE = Path(__file__).parent.parent.parent.parent / "data" / "test_snapshot_commands.yml" |
| 59 |
|
| 60 |
|
| 61 |
@pytest.mark.parametrize( |
| 62 |
("commands_path", "tags"), |
| 63 |
[ |
| 64 |
pytest.param(None, None, id="missing command list"), |
| 65 |
pytest.param(Path("/I/do/not/exist"), None, id="wrong path for command_list"), |
| 66 |
pytest.param(COMMAND_LIST_PATH_FILE, None, id="command-list only"), |
| 67 |
pytest.param(COMMAND_LIST_PATH_FILE, "leaf,spine", id="with tags"), |
| 68 |
], |
| 69 |
) |
| 70 |
def test_snapshot(tmp_path: Path, click_runner: CliRunner, commands_path: Path | None, tags: str | None) -> None: |
| 71 |
"""Test `anta exec snapshot`.""" |
| 72 |
cli_args = ["exec", "snapshot", "--output", str(tmp_path)] |
| 73 |
|
| 74 |
if commands_path is not None: |
| 75 |
cli_args.extend(["--commands-list", str(commands_path)]) |
| 76 |
if tags is not None: |
| 77 |
cli_args.extend(["--tags", tags]) |
| 78 |
result = click_runner.invoke(anta, cli_args) |
| 79 |
|
| 80 |
if commands_path is None: |
| 81 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 82 |
return |
| 83 |
if not Path.exists(Path(commands_path)): |
| 84 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 85 |
return |
| 86 |
assert result.exit_code == ExitCode.OK |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize( |
| 90 |
("output", "latest", "configure", "tags"), |
| 91 |
[ |
| 92 |
pytest.param(None, None, False, None, id="no params"), |
| 93 |
pytest.param("/tmp/dummy", None, False, None, id="with output"), |
| 94 |
pytest.param(None, 1, False, None, id="only last show tech"), |
| 95 |
pytest.param(None, None, True, None, id="configure"), |
| 96 |
pytest.param(None, None, False, "leaf,spine", id="with tags"), |
| 97 |
], |
| 98 |
) |
| 99 |
def test_collect_tech_support(click_runner: CliRunner, output: str | None, latest: str | None, configure: bool | None, tags: str | None) -> None: |
| 100 |
"""Test `anta exec collect-tech-support`.""" |
| 101 |
cli_args = ["exec", "collect-tech-support"] |
| 102 |
if output is not None: |
| 103 |
cli_args.extend(["--output", output]) |
| 104 |
if latest is not None: |
| 105 |
cli_args.extend(["--latest", latest]) |
| 106 |
if configure is True: |
| 107 |
cli_args.extend(["--configure"]) |
| 108 |
if tags is not None: |
| 109 |
cli_args.extend(["--tags", tags]) |
| 110 |
result = click_runner.invoke(anta, cli_args) |
| 111 |
assert result.exit_code == ExitCode.OK |