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