1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Tests for anta.cli.get.commands."""
from __future__ import annotations
import filecmp
from pathlib import Path
from typing import TYPE_CHECKING
from unittest.mock import ANY, patch
import pytest
import requests
from cvprac.cvp_client_errors import CvpApiError
from anta.cli._main import anta
from anta.cli.utils import ExitCode
if TYPE_CHECKING:
from click.testing import CliRunner
from cvprac.cvp_client import CvpClient
DATA_DIR: Path = Path(__file__).parents[3].resolve() / "data"
@pytest.mark.parametrize(
("cvp_container", "verify_cert", "cv_token_failure", "cvp_connect_failure"),
[
pytest.param(None, True, False, False, id="all devices - verify cert"),
pytest.param(None, True, True, False, id="all devices - fail SSL check"),
pytest.param(None, False, False, False, id="all devices - do not verify cert"),
pytest.param("custom_container", False, False, False, id="custom container"),
pytest.param(None, False, False, True, id="cvp connect failure"),
],
)
def test_from_cvp(
tmp_path: Path,
click_runner: CliRunner,
cvp_container: str | None,
verify_cert: bool,
cv_token_failure: bool,
cvp_connect_failure: bool,
) -> None:
# ruff: noqa: C901
"""Test `anta get from-cvp`.
This test verifies that username and password are NOT mandatory to run this command
"""
output: Path = tmp_path / "output.yml"
cli_args = [
"get",
"from-cvp",
"--output",
str(output),
"--host",
"42.42.42.42",
"--username",
"anta",
"--password",
"anta",
]
if cvp_container is not None:
cli_args.extend(["--container", cvp_container])
if not verify_cert:
cli_args.extend(["--ignore-cert"])
def mock_get_cv_token(*_args: str, **_kwargs: str) -> None:
if cv_token_failure:
raise requests.exceptions.SSLError
def mock_cvp_connect(_self: CvpClient, *_args: str, **_kwargs: str) -> None:
if cvp_connect_failure:
raise CvpApiError(msg="mocked CvpApiError")
# always get a token
with (
patch("anta.cli.get.commands.get_cv_token", autospec=True, side_effect=mock_get_cv_token),
patch(
"cvprac.cvp_client.CvpClient.connect",
autospec=True,
side_effect=mock_cvp_connect,
) as mocked_cvp_connect,
patch("cvprac.cvp_client.CvpApi.get_inventory", autospec=True, return_value=[]) as mocked_get_inventory,
patch(
"cvprac.cvp_client.CvpApi.get_devices_in_container",
autospec=True,
return_value=[],
) as mocked_get_devices_in_container,
):
result = click_runner.invoke(anta, cli_args)
if not cvp_connect_failure and not cv_token_failure:
assert output.exists()
if cv_token_failure:
assert "Authentication to CloudVison failed" in result.output
assert result.exit_code == ExitCode.USAGE_ERROR
return
mocked_cvp_connect.assert_called_once()
if cvp_connect_failure:
assert "Error connecting to CloudVision" in result.output
assert result.exit_code == ExitCode.USAGE_ERROR
return
assert "Connected to CloudVision" in result.output
if cvp_container is not None:
mocked_get_devices_in_container.assert_called_once_with(ANY, cvp_container)
else:
mocked_get_inventory.assert_called_once()
assert result.exit_code == ExitCode.OK
def test_from_cvp_os_error(tmp_path: Path, click_runner: CliRunner, caplog: pytest.LogCaptureFixture) -> None:
"""Test from_cvp when an OSError occurs."""
output: Path = tmp_path / "output.yml"
cli_args = ["get", "from-cvp", "--output", str(output), "--host", "42.42.42.42", "--username", "anta", "--password", "anta"]
with (
patch("anta.cli.get.commands.get_cv_token", autospec=True, side_effect=None),
patch("cvprac.cvp_client.CvpClient.connect", autospec=True, side_effect=None) as mocked_cvp_connect,
patch("cvprac.cvp_client.CvpApi.get_inventory", autospec=True, return_value=[]) as mocked_get_inventory,
patch("cvprac.cvp_client.CvpApi.get_devices_in_container", autospec=True, return_value=[]),
patch("anta.cli.get.utils.Path.open", side_effect=OSError("Permission denied")),
):
result = click_runner.invoke(anta, cli_args)
mocked_cvp_connect.assert_called_once()
mocked_get_inventory.assert_called_once()
assert not output.exists()
assert "Could not write inventory to path" in caplog.text
assert result.exit_code == ExitCode.USAGE_ERROR
@pytest.mark.parametrize(
("ansible_inventory", "ansible_group", "expected_exit", "expected_log"),
[
pytest.param("ansible_inventory.yml", None, ExitCode.OK, None, id="no group"),
pytest.param("ansible_inventory.yml", "ATD_LEAFS", ExitCode.OK, None, id="group found"),
pytest.param(
"ansible_inventory.yml",
"DUMMY",
ExitCode.USAGE_ERROR,
"Group DUMMY not found in Ansible inventory",
id="group not found",
),
pytest.param(
"empty_ansible_inventory.yml",
None,
ExitCode.USAGE_ERROR,
"is empty",
id="empty inventory",
),
],
)
def test_from_ansible(
tmp_path: Path,
click_runner: CliRunner,
ansible_inventory: Path,
ansible_group: str | None,
expected_exit: int,
expected_log: str | None,
) -> None:
"""Test `anta get from-ansible`.
This test verifies:
* the parsing of an ansible-inventory
* the ansible_group functionaliy
The output path is ALWAYS set to a non existing file.
"""
output: Path = tmp_path / "output.yml"
ansible_inventory_path = DATA_DIR / ansible_inventory
# Init cli_args
cli_args = [
"get",
"from-ansible",
"--output",
str(output),
"--ansible-inventory",
str(ansible_inventory_path),
]
# Set --ansible-group
if ansible_group is not None:
cli_args.extend(["--ansible-group", ansible_group])
result = click_runner.invoke(anta, cli_args)
assert result.exit_code == expected_exit
if expected_exit != ExitCode.OK:
assert expected_log
assert expected_log in result.output
else:
assert output.exists()
# TODO: check size of generated inventory to validate the group functionality!
@pytest.mark.parametrize(
("env_set", "overwrite", "is_tty", "prompt", "expected_exit", "expected_log"),
[
pytest.param(
True,
False,
True,
"y",
ExitCode.OK,
"",
id="no-overwrite-tty-init-prompt-yes",
),
pytest.param(
True,
False,
True,
"N",
ExitCode.INTERNAL_ERROR,
"Aborted",
id="no-overwrite-tty-init-prompt-no",
),
pytest.param(
True,
False,
False,
None,
ExitCode.USAGE_ERROR,
"Conversion aborted since destination file is not empty (not running in interactive TTY)",
id="no-overwrite-no-tty-init",
),
pytest.param(False, False, True, None, ExitCode.OK, "", id="no-overwrite-tty-no-init"),
pytest.param(False, False, False, None, ExitCode.OK, "", id="no-overwrite-no-tty-no-init"),
pytest.param(True, True, True, None, ExitCode.OK, "", id="overwrite-tty-init"),
pytest.param(True, True, False, None, ExitCode.OK, "", id="overwrite-no-tty-init"),
pytest.param(False, True, True, None, ExitCode.OK, "", id="overwrite-tty-no-init"),
pytest.param(False, True, False, None, ExitCode.OK, "", id="overwrite-no-tty-no-init"),
],
)
def test_from_ansible_overwrite(
tmp_path: Path,
click_runner: CliRunner,
temp_env: dict[str, str | None],
env_set: bool,
overwrite: bool,
is_tty: bool,
prompt: str | None,
expected_exit: int,
expected_log: str | None,
) -> None:
"""Test `anta get from-ansible` overwrite mechanism.
The test uses a static ansible-inventory and output as these are tested in other functions
This test verifies:
* that overwrite is working as expected with or without init data in the target file
* that when the target file is not empty and a tty is present, the user is prompt with confirmation
* Check the behavior when the prompt is filled
The initial content of the ANTA inventory is set using init_anta_inventory, if it is None, no inventory is set.
* With overwrite True, the expectation is that the from-ansible command succeeds
* With no init (init_anta_inventory == None), the expectation is also that command succeeds
"""
ansible_inventory_path = DATA_DIR / "ansible_inventory.yml"
expected_anta_inventory_path = DATA_DIR / "expected_anta_inventory.yml"
tmp_output = tmp_path / "output.yml"
cli_args = [
"get",
"from-ansible",
"--ansible-inventory",
str(ansible_inventory_path),
]
if env_set:
tmp_inv = Path(str(temp_env["ANTA_INVENTORY"]))
else:
temp_env["ANTA_INVENTORY"] = None
tmp_inv = tmp_output
cli_args.extend(["--output", str(tmp_inv)])
if overwrite:
cli_args.append("--overwrite")
# Verify initial content is different
if tmp_inv.exists():
assert not filecmp.cmp(tmp_inv, expected_anta_inventory_path)
with patch("sys.stdin.isatty", return_value=is_tty):
result = click_runner.invoke(anta, cli_args, env=temp_env, input=prompt)
assert result.exit_code == expected_exit
if expected_exit == ExitCode.OK:
assert filecmp.cmp(tmp_inv, expected_anta_inventory_path)
elif expected_exit == ExitCode.INTERNAL_ERROR:
assert expected_log
assert expected_log in result.output
@pytest.mark.parametrize(
("module", "test_name", "short", "count", "expected_output", "expected_exit_code"),
[
pytest.param(
None,
None,
False,
False,
"VerifyAcctConsoleMethods",
ExitCode.OK,
id="Get all tests",
),
pytest.param(
"anta.tests.aaa",
None,
False,
False,
"VerifyAcctConsoleMethods",
ExitCode.OK,
id="Get tests, filter on module",
),
pytest.param(
None,
"VerifyNTPAssociations",
False,
False,
"VerifyNTPAssociations",
ExitCode.OK,
id="Get tests, filter on exact test name",
),
pytest.param(
None,
"VerifyNTP",
False,
False,
"anta.tests.system",
ExitCode.OK,
id="Get tests, filter on included test name",
),
pytest.param(
None,
"VerifyNTP",
True,
False,
"VerifyNTPAssociations",
ExitCode.OK,
id="Get tests --short",
),
pytest.param(
"unknown_module",
None,
True,
False,
"Module `unknown_module` was not found!",
ExitCode.USAGE_ERROR,
id="Get tests wrong module",
),
pytest.param(
"unknown_module.unknown",
None,
True,
False,
"Module `unknown_module.unknown` was not found!",
ExitCode.USAGE_ERROR,
id="Get tests wrong submodule",
),
pytest.param(
".unknown_module",
None,
True,
False,
"`--module <module>` option does not support relative imports",
ExitCode.USAGE_ERROR,
id="Use relative module name",
),
pytest.param(
None,
"VerifySomething",
True,
False,
"No test 'VerifySomething' found in 'anta.tests'",
ExitCode.OK,
id="Get tests wrong test name",
),
pytest.param(
"anta.tests.aaa",
"VerifyNTP",
True,
False,
"No test 'VerifyNTP' found in 'anta.tests.aaa'",
ExitCode.OK,
id="Get tests test exists but not in module",
),
pytest.param(
"anta.tests.system",
"VerifyNTPAssociations",
False,
True,
"There is 1 test available in 'anta.tests.system'.",
ExitCode.OK,
id="Get single test count",
),
pytest.param(
"anta.tests.stun",
None,
False,
True,
"There are 3 tests available in 'anta.tests.stun'",
ExitCode.OK,
id="Get multiple test count",
),
],
)
def test_get_tests(
click_runner: CliRunner, module: str | None, test_name: str | None, *, short: bool, count: bool, expected_output: str, expected_exit_code: str
) -> None:
"""Test `anta get tests`."""
cli_args = [
"get",
"tests",
]
if module is not None:
cli_args.extend(["--module", module])
if test_name is not None:
cli_args.extend(["--test", test_name])
if short:
cli_args.append("--short")
if count:
cli_args.append("--count")
result = click_runner.invoke(anta, cli_args)
assert result.exit_code == expected_exit_code
assert expected_output in result.output
def test_get_tests_local_module(click_runner: CliRunner) -> None:
"""Test injecting CWD in sys.
The test overwrite CWD to return this file parents and local_module is located there.
"""
cli_args = ["get", "tests", "--module", "local_module"]
cwd = Path.cwd()
local_module_parent_path = Path(__file__).parent
with patch("anta.cli.get.utils.Path.cwd", return_value=local_module_parent_path):
result = click_runner.invoke(anta, cli_args)
assert result.exit_code == ExitCode.OK
# In the rare case where people would be running `pytest .` in this directory
if cwd != local_module_parent_path:
assert "injecting CWD in PYTHONPATH and retrying..." in result.output
assert "No test found in 'local_module'" in result.output
@pytest.mark.parametrize(
("module", "test_name", "catalog", "expected_output", "expected_exit_code"),
[
pytest.param(
None,
None,
None,
"VerifyAcctConsoleMethods",
ExitCode.OK,
id="Get all commands",
),
pytest.param(
"anta.tests.aaa",
None,
None,
"VerifyAcctConsoleMethods",
ExitCode.OK,
id="Get commands, filter on module",
),
pytest.param(
None,
"VerifyNTPAssociations",
None,
"VerifyNTPAssociations",
ExitCode.OK,
id="Get commands, filter on exact test name",
),
pytest.param(
None,
"VerifyNTP",
None,
"anta.tests.system",
ExitCode.OK,
id="Get commands, filter on included test name",
),
pytest.param(
"unknown_module",
None,
None,
"Module `unknown_module` was not found!",
ExitCode.USAGE_ERROR,
id="Get commands wrong module",
),
pytest.param(
"unknown_module.unknown",
None,
None,
"Module `unknown_module.unknown` was not found!",
ExitCode.USAGE_ERROR,
id="Get commands wrong submodule",
),
pytest.param(
".unknown_module",
None,
None,
"`--module <module>` option does not support relative imports",
ExitCode.USAGE_ERROR,
id="Use relative module name",
),
pytest.param(
None,
"VerifySomething",
None,
"No test 'VerifySomething' found in 'anta.tests'",
ExitCode.OK,
id="Get commands wrong test name",
),
pytest.param(
"anta.tests.aaa",
"VerifyNTP",
None,
"No test 'VerifyNTP' found in 'anta.tests.aaa'",
ExitCode.OK,
id="Get commands test exists but not in module",
),
pytest.param(
None,
None,
DATA_DIR / "test_catalog.yml",
"VerifyEOSVersion",
ExitCode.OK,
id="Get all commands from catalog",
),
pytest.param(
"anta.tests.aaa",
None,
DATA_DIR / "test_catalog.yml",
"No test found in 'anta.tests.aaa' for catalog '", # partial match as no test from aaa in the catalog
ExitCode.OK,
id="Get all commands from module in catalog",
),
pytest.param(
None,
None,
DATA_DIR / "non_existing_catalog.yml",
"Invalid value for '--catalog'",
ExitCode.USAGE_ERROR,
id="Catalog does not exist",
),
# TODO: catalog format JSON
],
)
def test_get_commands(
click_runner: CliRunner, module: str | None, test_name: str | None, catalog: str | None, expected_output: str, expected_exit_code: str
) -> None:
"""Test `anta get commands`."""
cli_args = [
"get",
"commands",
]
if module is not None:
cli_args.extend(["--module", module])
if test_name is not None:
cli_args.extend(["--test", test_name])
if catalog is not None:
cli_args.extend(["--catalog", catalog])
# Make sure to disable any ANTA_CATALOG env that could be set and pollute the test
result = click_runner.invoke(anta, cli_args, env={"ANTA_CATALOG": None})
assert result.exit_code == expected_exit_code
assert expected_output in result.output
@pytest.mark.parametrize(
("module", "test_name", "catalog", "expected_count"),
[
pytest.param(
"anta.tests.aaa",
None,
None,
4,
id="Get unique commands, filter on module",
),
pytest.param(
None,
"VerifyNTPAssociations",
None,
1,
id="Get unique commands, filter on exact test name",
),
pytest.param(
None,
"VerifyNTP",
None,
2,
id="Get unique commands, filter on included test name",
),
pytest.param(
None,
None,
DATA_DIR / "test_catalog.yml",
1,
id="Get all unique commands from catalog",
),
],
)
def test_get_commands_unique(click_runner: CliRunner, module: str | None, test_name: str | None, catalog: str | None, expected_count: int) -> None:
"""Test `anta get commands`."""
cli_args = [
"get",
"commands",
]
if module is not None:
cli_args.extend(["--module", module])
if test_name is not None:
cli_args.extend(["--test", test_name])
if catalog is not None:
cli_args.extend(["--catalog", catalog])
cli_args.extend(["--unique"])
# Make sure to disable any ANTA_CATALOG env that could be set and pollute the test
result = click_runner.invoke(anta, cli_args, env={"ANTA_CATALOG": None})
assert expected_count == len(result.output.splitlines())
|