| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.cli.nrfu.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from pathlib import Path |
| 9 |
from typing import TYPE_CHECKING |
| 10 |
from unittest.mock import AsyncMock, patch |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from anta.cli import anta |
| 15 |
from anta.cli.utils import ExitCode |
| 16 |
from anta.inventory import AntaInventory |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from click.testing import CliRunner |
| 20 |
|
| 21 |
DATA_DIR: Path = Path(__file__).parents[3].resolve() / "data" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
def test_anta_nrfu_help(click_runner: CliRunner) -> None: |
| 27 |
"""Test anta nrfu --help.""" |
| 28 |
result = click_runner.invoke(anta, ["nrfu", "--help"]) |
| 29 |
assert result.exit_code == ExitCode.OK |
| 30 |
assert "Usage: anta nrfu" in result.output |
| 31 |
|
| 32 |
|
| 33 |
def test_anta_nrfu_wrong_subcommand(click_runner: CliRunner) -> None: |
| 34 |
"""Test anta nrfu toast.""" |
| 35 |
result = click_runner.invoke(anta, ["nrfu", "oook"]) |
| 36 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 37 |
assert "Usage: anta nrfu" in result.output |
| 38 |
assert "No such command 'oook'." in result.output |
| 39 |
|
| 40 |
|
| 41 |
def test_anta_nrfu(click_runner: CliRunner) -> None: |
| 42 |
"""Test anta nrfu, catalog is given via env.""" |
| 43 |
result = click_runner.invoke(anta, ["nrfu"]) |
| 44 |
assert result.exit_code == ExitCode.OK |
| 45 |
assert "ANTA Inventory contains 3 devices" in result.output |
| 46 |
assert "Tests catalog contains 1 tests" in result.output |
| 47 |
|
| 48 |
|
| 49 |
def test_anta_nrfu_dry_run(click_runner: CliRunner) -> None: |
| 50 |
"""Test anta nrfu --dry-run, catalog is given via env.""" |
| 51 |
result = click_runner.invoke(anta, ["nrfu", "--dry-run"]) |
| 52 |
assert result.exit_code == ExitCode.OK |
| 53 |
assert "ANTA Inventory contains 3 devices" in result.output |
| 54 |
assert "Tests catalog contains 1 tests" in result.output |
| 55 |
assert "Dry-run" in result.output |
| 56 |
|
| 57 |
|
| 58 |
@pytest.mark.parametrize( |
| 59 |
("args", "env", "expected"), |
| 60 |
[ |
| 61 |
pytest.param(["nrfu"], {}, True, id="default"), |
| 62 |
pytest.param(["nrfu", "--disconnect"], {}, True, id="option-enable"), |
| 63 |
pytest.param(["nrfu", "--no-disconnect"], {}, False, id="option-disable"), |
| 64 |
pytest.param(["nrfu"], {"ANTA_DISCONNECT_INVENTORY": "true"}, True, id="env-var-enable"), |
| 65 |
pytest.param(["nrfu"], {"ANTA_DISCONNECT_INVENTORY": "false"}, False, id="env-var-disable"), |
| 66 |
], |
| 67 |
) |
| 68 |
def test_anta_nrfu_disconnect(click_runner: CliRunner, args: list[str], env: dict[str, str], expected: bool) -> None: |
| 69 |
"""Test anta nrfu disconnect inputs are passed to the runner.""" |
| 70 |
with patch("anta.cli.nrfu.utils.AntaRunner.run", new=AsyncMock()) as run_mock: |
| 71 |
result = click_runner.invoke(anta, args, env=env) |
| 72 |
|
| 73 |
assert result.exit_code == ExitCode.OK |
| 74 |
run_mock.assert_awaited_once() |
| 75 |
assert run_mock.await_args is not None |
| 76 |
assert run_mock.await_args.kwargs["disconnect"] is expected |
| 77 |
|
| 78 |
|
| 79 |
def test_anta_nrfu_wrong_catalog_format(click_runner: CliRunner) -> None: |
| 80 |
"""Test anta nrfu --dry-run, catalog is given via env.""" |
| 81 |
result = click_runner.invoke(anta, ["nrfu", "--dry-run", "--catalog-format", "toto"]) |
| 82 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 83 |
assert "Invalid value for '--catalog-format' (env var: 'ANTA_CATALOG_FORMAT'): 'toto' is not one of 'yaml', 'json'." in result.output |
| 84 |
|
| 85 |
|
| 86 |
def test_anta_password_required(click_runner: CliRunner) -> None: |
| 87 |
"""Test that password is provided.""" |
| 88 |
env = {"ANTA_PASSWORD": None} |
| 89 |
result = click_runner.invoke(anta, ["nrfu"], env=env) |
| 90 |
|
| 91 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 92 |
assert "EOS password needs to be provided by using either the '--password' option or the '--prompt' option." in result.output |
| 93 |
|
| 94 |
|
| 95 |
def test_anta_password(click_runner: CliRunner) -> None: |
| 96 |
"""Test that password can be provided either via --password or --prompt.""" |
| 97 |
env = {"ANTA_PASSWORD": None} |
| 98 |
result = click_runner.invoke(anta, ["nrfu", "--password", "secret"], env=env) |
| 99 |
assert result.exit_code == ExitCode.OK |
| 100 |
result = click_runner.invoke(anta, ["nrfu", "--prompt"], input="password\n", env=env) |
| 101 |
assert result.exit_code == ExitCode.OK |
| 102 |
|
| 103 |
|
| 104 |
def test_anta_enable_password(click_runner: CliRunner) -> None: |
| 105 |
"""Test that enable password can be provided either via --enable-password or --prompt.""" |
| 106 |
|
| 107 |
result = click_runner.invoke(anta, ["nrfu", "--enable", "--enable-password", "secret"]) |
| 108 |
assert result.exit_code == ExitCode.OK |
| 109 |
|
| 110 |
|
| 111 |
result = click_runner.invoke(anta, ["nrfu", "--enable", "--prompt"], input="y\npassword\n") |
| 112 |
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" in result.output |
| 113 |
assert "Please enter a password to enter EOS privileged EXEC mode" in result.output |
| 114 |
assert result.exit_code == ExitCode.OK |
| 115 |
|
| 116 |
|
| 117 |
result = click_runner.invoke(anta, ["nrfu", "--enable", "--prompt"], input="N\n") |
| 118 |
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" in result.output |
| 119 |
assert "Please enter a password to enter EOS privileged EXEC mode" not in result.output |
| 120 |
assert result.exit_code == ExitCode.OK |
| 121 |
|
| 122 |
result = click_runner.invoke(anta, ["nrfu", "--enable", "--enable-password", "blah", "--prompt"], input="y\npassword\n") |
| 123 |
assert "Is a password required to enter EOS privileged EXEC mode? [y/N]:" not in result.output |
| 124 |
assert "Please enter a password to enter EOS privileged EXEC mode" not in result.output |
| 125 |
assert result.exit_code == ExitCode.OK |
| 126 |
|
| 127 |
|
| 128 |
result = click_runner.invoke(anta, ["nrfu", "--enable-password", "blah"]) |
| 129 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 130 |
assert "Providing a password to access EOS Privileged EXEC mode requires '--enable' option." in result.output |
| 131 |
|
| 132 |
|
| 133 |
def test_anta_enable_alone(click_runner: CliRunner) -> None: |
| 134 |
"""Test that enable can be provided either without enable-password.""" |
| 135 |
result = click_runner.invoke(anta, ["nrfu", "--enable"]) |
| 136 |
assert result.exit_code == ExitCode.OK |
| 137 |
|
| 138 |
|
| 139 |
def test_disable_cache(click_runner: CliRunner) -> None: |
| 140 |
"""Test that disable_cache is working on inventory.""" |
| 141 |
result = click_runner.invoke(anta, ["nrfu", "--disable-cache"]) |
| 142 |
stdout_lines = result.stdout.split("\n") |
| 143 |
|
| 144 |
for line in stdout_lines: |
| 145 |
if "disable_cache" in line: |
| 146 |
assert "True" in line |
| 147 |
assert result.exit_code == ExitCode.OK |
| 148 |
|
| 149 |
|
| 150 |
@pytest.mark.parametrize( |
| 151 |
("args", "env", "expected"), |
| 152 |
[ |
| 153 |
pytest.param(["nrfu"], {}, None, id="default"), |
| 154 |
pytest.param(["nrfu", "--use-session-auth"], {}, True, id="option-enable"), |
| 155 |
pytest.param(["nrfu", "--no-session-auth"], {}, False, id="option-disable"), |
| 156 |
pytest.param(["nrfu"], {"ANTA_USE_SESSION_AUTH": "true"}, True, id="env-var-enable"), |
| 157 |
pytest.param(["nrfu"], {"ANTA_USE_SESSION_AUTH": "false"}, False, id="env-var-disable"), |
| 158 |
], |
| 159 |
) |
| 160 |
def test_anta_nrfu_use_session(click_runner: CliRunner, args: list[str], env: dict[str, str], expected: bool | None) -> None: |
| 161 |
"""Test anta nrfu use_session_auth inputs are forwarded to AntaInventory.parse.""" |
| 162 |
with patch("anta.cli.utils.AntaInventory.parse", wraps=AntaInventory.parse) as parse_mock: |
| 163 |
result = click_runner.invoke(anta, args, env=env) |
| 164 |
assert result.exit_code == ExitCode.OK |
| 165 |
parse_mock.assert_called_once() |
| 166 |
assert parse_mock.call_args.kwargs["use_session_auth"] is expected |
| 167 |
|
| 168 |
|
| 169 |
def test_hide(click_runner: CliRunner) -> None: |
| 170 |
"""Test the `--hide` option of the `anta nrfu` command.""" |
| 171 |
result = click_runner.invoke(anta, ["nrfu", "--hide", "success", "text"]) |
| 172 |
assert "SUCCESS" not in result.output |
| 173 |
|
| 174 |
|
| 175 |
def test_invalid_inventory(caplog: pytest.LogCaptureFixture, click_runner: CliRunner) -> None: |
| 176 |
"""Test invalid inventory.""" |
| 177 |
result = click_runner.invoke(anta, ["nrfu", "--inventory", str(DATA_DIR / "invalid_inventory.yml")]) |
| 178 |
assert "CRITICAL" in caplog.text |
| 179 |
assert "Failed to parse the inventory" in caplog.text |
| 180 |
assert result.exit_code == ExitCode.USAGE_ERROR |
| 181 |
|
| 182 |
|
| 183 |
def test_invalid_catalog(caplog: pytest.LogCaptureFixture, click_runner: CliRunner) -> None: |
| 184 |
"""Test invalid catalog.""" |
| 185 |
result = click_runner.invoke(anta, ["nrfu", "--catalog", str(DATA_DIR / "test_catalog_not_a_list.yml")]) |
| 186 |
assert "CRITICAL" in caplog.text |
| 187 |
assert "Failed to parse the catalog" in caplog.text |
| 188 |
assert result.exit_code == ExitCode.USAGE_ERROR |