| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.cli.debug.commands.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from typing import TYPE_CHECKING, Literal |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from anta.cli import anta |
| 13 |
from anta.cli.utils import ExitCode |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from click.testing import CliRunner |
| 17 |
|
| 18 |
|
| 19 |
@pytest.mark.parametrize( |
| 20 |
("command", "ofmt", "version", "revision", "device", "failed"), |
| 21 |
[ |
| 22 |
pytest.param("show version", "json", None, None, "leaf1", False, id="json command"), |
| 23 |
pytest.param("show version", "text", None, None, "leaf1", False, id="text command"), |
| 24 |
pytest.param("show version", None, "latest", None, "leaf1", False, id="version-latest"), |
| 25 |
pytest.param("show version", None, "1", None, "leaf1", False, id="version"), |
| 26 |
pytest.param("show version", None, None, 3, "leaf1", False, id="revision"), |
| 27 |
pytest.param("undefined", None, None, None, "leaf1", True, id="command fails"), |
| 28 |
pytest.param("undefined", None, None, None, "doesnotexist", True, id="Device does not exist"), |
| 29 |
], |
| 30 |
) |
| 31 |
def test_run_cmd( |
| 32 |
click_runner: CliRunner, |
| 33 |
command: str, |
| 34 |
ofmt: Literal["json", "text"], |
| 35 |
version: Literal["1", "latest"] | None, |
| 36 |
revision: int | None, |
| 37 |
device: str, |
| 38 |
failed: bool, |
| 39 |
) -> None: |
| 40 |
"""Test `anta debug run-cmd`.""" |
| 41 |
cli_args = ["-l", "debug", "debug", "run-cmd", "--command", command, "--device", device] |
| 42 |
|
| 43 |
|
| 44 |
if ofmt is not None: |
| 45 |
cli_args.extend(["--ofmt", ofmt]) |
| 46 |
|
| 47 |
|
| 48 |
if version is not None: |
| 49 |
cli_args.extend(["--version", version]) |
| 50 |
|
| 51 |
|
| 52 |
if revision is not None: |
| 53 |
cli_args.extend(["--revision", str(revision)]) |
| 54 |
|
| 55 |
result = click_runner.invoke(anta, cli_args) |
| 56 |
if failed: |
| 57 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 58 |
else: |
| 59 |
assert result.exit_code == ExitCode.OK |
| 60 |
if revision is not None: |
| 61 |
assert f"revision={revision}" in result.output |
| 62 |
if version is not None: |
| 63 |
assert (f"version='{version}'" if version == "latest" else f"version={version}") in result.output |