| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Tests for anta.cli.exec.utils.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import logging |
| 9 |
from pathlib import Path |
| 10 |
from typing import TYPE_CHECKING, Any |
| 11 |
from unittest.mock import AsyncMock, call, patch |
| 12 |
|
| 13 |
import pytest |
| 14 |
import respx |
| 15 |
from asyncssh import ChannelOpenError, ConnectionLost, HostKeyNotVerifiable, KeyExchangeFailed, SFTPFailure |
| 16 |
from asyncssh.constants import OPEN_CONNECT_FAILED |
| 17 |
|
| 18 |
from anta.cli.exec.utils import clear_counters, collect_commands, collect_show_tech |
| 19 |
from anta.models import AntaCommand |
| 20 |
from anta.tools import safe_command |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from anta.device import AntaDevice |
| 24 |
from anta.inventory import AntaInventory |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
@pytest.mark.parametrize( |
| 29 |
("inventory", "inventory_state", "per_device_command_output", "tags"), |
| 30 |
[ |
| 31 |
pytest.param( |
| 32 |
{"count": 3}, |
| 33 |
{ |
| 34 |
"device-0": {"is_online": False}, |
| 35 |
"device-1": {"is_online": False}, |
| 36 |
"device-2": {"is_online": False}, |
| 37 |
}, |
| 38 |
{}, |
| 39 |
None, |
| 40 |
id="no_connected_device", |
| 41 |
), |
| 42 |
pytest.param( |
| 43 |
{"count": 3}, |
| 44 |
{ |
| 45 |
"device-0": {"is_online": True, "hw_model": "cEOSLab"}, |
| 46 |
"device-1": {"is_online": True, "hw_model": "vEOS-lab"}, |
| 47 |
"device-2": {"is_online": False}, |
| 48 |
}, |
| 49 |
{}, |
| 50 |
None, |
| 51 |
id="cEOSLab and vEOS-lab devices", |
| 52 |
), |
| 53 |
pytest.param( |
| 54 |
{"count": 3}, |
| 55 |
{ |
| 56 |
"device-0": {"is_online": True}, |
| 57 |
"device-1": {"is_online": True}, |
| 58 |
"device-2": {"is_online": False}, |
| 59 |
}, |
| 60 |
{"device-0": None}, |
| 61 |
None, |
| 62 |
id="device with error", |
| 63 |
), |
| 64 |
pytest.param( |
| 65 |
{"count": 3}, |
| 66 |
{ |
| 67 |
"device-0": {"is_online": True}, |
| 68 |
"device-1": {"is_online": True}, |
| 69 |
"device-2": {"is_online": True}, |
| 70 |
}, |
| 71 |
{}, |
| 72 |
["spine"], |
| 73 |
id="tags", |
| 74 |
), |
| 75 |
], |
| 76 |
indirect=["inventory"], |
| 77 |
) |
| 78 |
async def test_clear_counters( |
| 79 |
caplog: pytest.LogCaptureFixture, |
| 80 |
inventory: AntaInventory, |
| 81 |
inventory_state: dict[str, Any], |
| 82 |
per_device_command_output: dict[str, Any], |
| 83 |
tags: set[str] | None, |
| 84 |
) -> None: |
| 85 |
"""Test anta.cli.exec.utils.clear_counters.""" |
| 86 |
|
| 87 |
async def mock_connect_inventory() -> None: |
| 88 |
"""Mock connect_inventory coroutine.""" |
| 89 |
for name, device in inventory.items(): |
| 90 |
device.is_online = inventory_state[name].get("is_online", True) |
| 91 |
device.established = inventory_state[name].get("established", device.is_online) |
| 92 |
device.hw_model = inventory_state[name].get("hw_model", "dummy") |
| 93 |
|
| 94 |
async def collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 95 |
"""Mock collect coroutine.""" |
| 96 |
command.output = per_device_command_output.get(self.name, "") |
| 97 |
|
| 98 |
|
| 99 |
with ( |
| 100 |
patch("anta.device.AsyncEOSDevice.collect", side_effect=collect, autospec=True) as mocked_collect, |
| 101 |
patch( |
| 102 |
"anta.inventory.AntaInventory.connect_inventory", |
| 103 |
side_effect=mock_connect_inventory, |
| 104 |
) as mocked_connect_inventory, |
| 105 |
): |
| 106 |
await clear_counters(inventory, tags=tags) |
| 107 |
|
| 108 |
mocked_connect_inventory.assert_awaited_once() |
| 109 |
devices_established = inventory.get_inventory(established_only=True, tags=tags).devices |
| 110 |
if devices_established: |
| 111 |
|
| 112 |
calls = [] |
| 113 |
for device in devices_established: |
| 114 |
calls.append( |
| 115 |
call( |
| 116 |
device, |
| 117 |
command=AntaCommand( |
| 118 |
command="clear counters", |
| 119 |
version="latest", |
| 120 |
revision=None, |
| 121 |
ofmt="json", |
| 122 |
output=per_device_command_output.get(device.name, ""), |
| 123 |
errors=[], |
| 124 |
), |
| 125 |
collection_id=None, |
| 126 |
), |
| 127 |
) |
| 128 |
if device.hw_model not in ["cEOSLab", "vEOS-lab"]: |
| 129 |
calls.append( |
| 130 |
call( |
| 131 |
device, |
| 132 |
command=AntaCommand( |
| 133 |
command="clear hardware counter drop", |
| 134 |
version="latest", |
| 135 |
revision=None, |
| 136 |
ofmt="json", |
| 137 |
output=per_device_command_output.get(device.name, ""), |
| 138 |
), |
| 139 |
collection_id=None, |
| 140 |
), |
| 141 |
) |
| 142 |
mocked_collect.assert_has_awaits(calls) |
| 143 |
|
| 144 |
for key, value in per_device_command_output.items(): |
| 145 |
if value is None: |
| 146 |
|
| 147 |
assert "ERROR" in caplog.text |
| 148 |
assert f"Could not clear counters on device {key}: []" in caplog.text |
| 149 |
else: |
| 150 |
mocked_collect.assert_not_awaited() |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize( |
| 155 |
("inventory", "inventory_state", "commands", "tags"), |
| 156 |
[ |
| 157 |
pytest.param( |
| 158 |
{"count": 1}, |
| 159 |
{ |
| 160 |
"device-0": {"is_online": False}, |
| 161 |
}, |
| 162 |
{"json_format": ["show version"]}, |
| 163 |
None, |
| 164 |
id="no_connected_device", |
| 165 |
), |
| 166 |
pytest.param( |
| 167 |
{"count": 3}, |
| 168 |
{ |
| 169 |
"device-0": {"is_online": True}, |
| 170 |
"device-1": {"is_online": True}, |
| 171 |
"device-2": {"is_online": False}, |
| 172 |
}, |
| 173 |
{"json_format": ["show version", "show ip interface brief"]}, |
| 174 |
None, |
| 175 |
id="JSON commands", |
| 176 |
), |
| 177 |
pytest.param( |
| 178 |
{"count": 3}, |
| 179 |
{ |
| 180 |
"device-0": {"is_online": True}, |
| 181 |
"device-1": {"is_online": True}, |
| 182 |
"device-2": {"is_online": False}, |
| 183 |
}, |
| 184 |
{"json_format": ["show version"], "text_format": ["show running-config", "show ip interface"]}, |
| 185 |
None, |
| 186 |
id="Text commands", |
| 187 |
), |
| 188 |
pytest.param( |
| 189 |
{"count": 2}, |
| 190 |
{ |
| 191 |
"device-0": {"is_online": True, "tags": {"spine"}}, |
| 192 |
"device-1": {"is_online": True}, |
| 193 |
}, |
| 194 |
{"json_format": ["show version"]}, |
| 195 |
{"spine"}, |
| 196 |
id="tags", |
| 197 |
), |
| 198 |
pytest.param( |
| 199 |
{"count": 1}, |
| 200 |
{ |
| 201 |
"device-0": {"is_online": True}, |
| 202 |
}, |
| 203 |
{"blah_format": ["42"]}, |
| 204 |
None, |
| 205 |
id="bad-input", |
| 206 |
), |
| 207 |
pytest.param( |
| 208 |
{"count": 1}, |
| 209 |
{ |
| 210 |
"device-0": {"is_online": True}, |
| 211 |
}, |
| 212 |
{"json_format": ["undefined command", "show version"]}, |
| 213 |
None, |
| 214 |
id="command-failed-to-be-collected", |
| 215 |
), |
| 216 |
pytest.param( |
| 217 |
{"count": 1}, |
| 218 |
{ |
| 219 |
"device-0": {"is_online": True}, |
| 220 |
}, |
| 221 |
{"json_format": ["uncaught exception"]}, |
| 222 |
None, |
| 223 |
id="uncaught-exception", |
| 224 |
), |
| 225 |
], |
| 226 |
indirect=["inventory"], |
| 227 |
) |
| 228 |
async def test_collect_commands( |
| 229 |
caplog: pytest.LogCaptureFixture, |
| 230 |
tmp_path: Path, |
| 231 |
inventory: AntaInventory, |
| 232 |
inventory_state: dict[str, Any], |
| 233 |
commands: dict[str, list[str]], |
| 234 |
tags: set[str] | None, |
| 235 |
) -> None: |
| 236 |
"""Test anta.cli.exec.utils.collect_commands.""" |
| 237 |
caplog.set_level(logging.INFO) |
| 238 |
root_dir = tmp_path |
| 239 |
|
| 240 |
async def mock_connect_inventory() -> None: |
| 241 |
"""Mock connect_inventory coroutine.""" |
| 242 |
for name, device in inventory.items(): |
| 243 |
device.is_online = inventory_state[name].get("is_online", True) |
| 244 |
device.established = inventory_state[name].get("established", device.is_online) |
| 245 |
device.hw_model = inventory_state[name].get("hw_model", "dummy") |
| 246 |
device.tags = inventory_state[name].get("tags", set()) |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
with ( |
| 251 |
respx.mock, |
| 252 |
patch( |
| 253 |
"anta.inventory.AntaInventory.connect_inventory", |
| 254 |
side_effect=mock_connect_inventory, |
| 255 |
) as mocked_connect_inventory, |
| 256 |
): |
| 257 |
|
| 258 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show version").respond( |
| 259 |
json={"result": [{"toto": 42}]} |
| 260 |
) |
| 261 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show ip interface brief").respond( |
| 262 |
json={"result": [{"toto": 42}]} |
| 263 |
) |
| 264 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show running-config").respond( |
| 265 |
json={"result": [{"output": "blah"}]} |
| 266 |
) |
| 267 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show ip interface").respond( |
| 268 |
json={"result": [{"output": "blah"}]} |
| 269 |
) |
| 270 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="undefined command").respond( |
| 271 |
json={ |
| 272 |
"error": { |
| 273 |
"code": 1002, |
| 274 |
"message": "CLI command 1 of 1 'undefined command' failed: invalid command", |
| 275 |
"data": [{"errors": ["Invalid input (at token 0: 'undefined')"]}], |
| 276 |
} |
| 277 |
} |
| 278 |
) |
| 279 |
await collect_commands(inventory, commands, root_dir, tags=tags) |
| 280 |
|
| 281 |
mocked_connect_inventory.assert_awaited_once() |
| 282 |
devices_established = inventory.get_inventory(established_only=True, tags=tags or None).devices |
| 283 |
if not devices_established: |
| 284 |
assert "INFO" in caplog.text |
| 285 |
assert "No online device found. Exiting" in caplog.text |
| 286 |
return |
| 287 |
|
| 288 |
for device in devices_established: |
| 289 |
|
| 290 |
assert device.tags.intersection(tags) != {} if tags else True |
| 291 |
json_path = root_dir / device.name / "json" |
| 292 |
text_path = root_dir / device.name / "text" |
| 293 |
if "json_format" in commands: |
| 294 |
|
| 295 |
if "undefined command" in commands["json_format"]: |
| 296 |
assert "ERROR" in caplog.text |
| 297 |
assert "Command 'undefined command' on device device-0 failed: Invalid input (at token 0: 'undefined')" in caplog.text |
| 298 |
|
| 299 |
assert f"Collected command 'undefined command' from device {device.name}" not in caplog.text |
| 300 |
commands["json_format"].remove("undefined command") |
| 301 |
|
| 302 |
elif "uncaught exception" in commands["json_format"]: |
| 303 |
assert "ERROR" in caplog.text |
| 304 |
assert "Error when collecting commands: " in caplog.text |
| 305 |
|
| 306 |
assert f"Collected command 'uncaught exception' from device {device.name}" not in caplog.text |
| 307 |
commands["json_format"].remove("uncaught exception") |
| 308 |
|
| 309 |
assert json_path.is_dir() |
| 310 |
assert len(list(Path.iterdir(json_path))) == len(commands["json_format"]) |
| 311 |
for command in commands["json_format"]: |
| 312 |
assert Path.is_file(json_path / f"{safe_command(command)}.json") |
| 313 |
assert f"Collected command '{command}' from device {device.name}" in caplog.text |
| 314 |
if "text_format" in commands: |
| 315 |
assert text_path.is_dir() |
| 316 |
assert len(list(text_path.iterdir())) == len(commands["text_format"]) |
| 317 |
for command in commands["text_format"]: |
| 318 |
assert Path.is_file(text_path / f"{safe_command(command)}.log") |
| 319 |
assert f"Collected command '{command}' from device {device.name}" in caplog.text |
| 320 |
|
| 321 |
|
| 322 |
@pytest.mark.parametrize( |
| 323 |
("ssh_error", "expected_message"), |
| 324 |
[ |
| 325 |
pytest.param( |
| 326 |
ConnectionLost("Connection lost"), |
| 327 |
"ConnectionLost: Connection lost", |
| 328 |
id="connection-lost", |
| 329 |
), |
| 330 |
pytest.param( |
| 331 |
KeyExchangeFailed("Key exchange failed"), |
| 332 |
"KeyExchangeFailed: Key exchange failed", |
| 333 |
id="key-exchange-failed", |
| 334 |
), |
| 335 |
pytest.param( |
| 336 |
ConnectionLost(""), |
| 337 |
"ConnectionLost", |
| 338 |
id="connection-lost-empty-reason", |
| 339 |
), |
| 340 |
pytest.param( |
| 341 |
HostKeyNotVerifiable("Host key not verifiable"), |
| 342 |
"The host SSH key could not be verified", |
| 343 |
id="host-key-not-verifiable", |
| 344 |
), |
| 345 |
pytest.param( |
| 346 |
SFTPFailure("Permission denied"), |
| 347 |
"SFTPFailure: Permission denied", |
| 348 |
id="sftp-failure", |
| 349 |
), |
| 350 |
pytest.param( |
| 351 |
ChannelOpenError(OPEN_CONNECT_FAILED, "channel failed"), |
| 352 |
"ChannelOpenError: channel failed", |
| 353 |
id="channel-open-error", |
| 354 |
), |
| 355 |
pytest.param( |
| 356 |
OSError("Network is unreachable"), |
| 357 |
"OSError: Network is unreachable", |
| 358 |
id="os-error", |
| 359 |
), |
| 360 |
], |
| 361 |
) |
| 362 |
async def test_collect_show_tech_ssh_errors( |
| 363 |
caplog: pytest.LogCaptureFixture, |
| 364 |
tmp_path: Path, |
| 365 |
inventory: AntaInventory, |
| 366 |
ssh_error: Exception, |
| 367 |
expected_message: str, |
| 368 |
) -> None: |
| 369 |
"""Test collect_show_tech handles asyncssh errors gracefully.""" |
| 370 |
caplog.set_level(logging.ERROR) |
| 371 |
|
| 372 |
async def mock_connect_inventory() -> None: |
| 373 |
"""Mock connect_inventory coroutine.""" |
| 374 |
for device in inventory.values(): |
| 375 |
device.is_online = True |
| 376 |
device.established = True |
| 377 |
device.hw_model = "dummy" |
| 378 |
|
| 379 |
async def mock_collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 380 |
"""Mock collect coroutine — simulate successful command collection.""" |
| 381 |
if "ls -1t" in command.command: |
| 382 |
command.output = "dummy_tech-support_2023-12-01.1115.log.gz" |
| 383 |
elif "include aaa authorization" in command.command: |
| 384 |
command.output = "aaa authorization exec default local" |
| 385 |
|
| 386 |
async def mock_copy(self: AntaDevice, *args: Any, **kwargs: Any) -> None: |
| 387 |
"""Mock copy that raises SSH errors.""" |
| 388 |
raise ssh_error |
| 389 |
|
| 390 |
with ( |
| 391 |
patch("anta.device.AsyncEOSDevice.collect", side_effect=mock_collect, autospec=True), |
| 392 |
patch("anta.device.AsyncEOSDevice.copy", side_effect=mock_copy, autospec=True), |
| 393 |
patch("anta.inventory.AntaInventory.connect_inventory", side_effect=mock_connect_inventory), |
| 394 |
): |
| 395 |
await collect_show_tech(inventory, root_dir=tmp_path, configure=False) |
| 396 |
|
| 397 |
assert expected_message in caplog.text |
| 398 |
assert "Unable to collect tech-support on" in caplog.text |
| 399 |
|
| 400 |
|
| 401 |
async def test_collect_show_tech_usage_error_does_not_abort_batch( |
| 402 |
caplog: pytest.LogCaptureFixture, |
| 403 |
tmp_path: Path, |
| 404 |
inventory: AntaInventory, |
| 405 |
device: AntaDevice, |
| 406 |
) -> None: |
| 407 |
"""Test collect_show_tech handles unsupported device types without aborting other devices.""" |
| 408 |
caplog.set_level(logging.ERROR) |
| 409 |
device.name = "unsupported-device" |
| 410 |
inventory.add_device(device) |
| 411 |
copied_devices: list[str] = [] |
| 412 |
|
| 413 |
async def mock_connect_inventory() -> None: |
| 414 |
for inventory_device in inventory.values(): |
| 415 |
inventory_device.is_online = True |
| 416 |
inventory_device.established = True |
| 417 |
inventory_device.hw_model = "dummy" |
| 418 |
|
| 419 |
async def mock_collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 420 |
if "ls -1t" in command.command: |
| 421 |
command.output = "dummy_tech-support_2023-12-01.1115.log.gz" |
| 422 |
elif "include aaa authorization" in command.command: |
| 423 |
command.output = "aaa authorization exec default local" |
| 424 |
|
| 425 |
async def mock_unsupported_collect(command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 426 |
if "ls -1t" in command.command: |
| 427 |
command.output = "unsupported_tech-support_2023-12-01.1115.log.gz" |
| 428 |
elif "include aaa authorization" in command.command: |
| 429 |
command.output = "" |
| 430 |
|
| 431 |
async def mock_copy(self: AntaDevice, *args: Any, **kwargs: Any) -> None: |
| 432 |
copied_devices.append(self.name) |
| 433 |
|
| 434 |
with ( |
| 435 |
patch("anta.device.AsyncEOSDevice.collect", side_effect=mock_collect, autospec=True), |
| 436 |
patch("anta.device.AsyncEOSDevice.copy", side_effect=mock_copy, autospec=True), |
| 437 |
patch.object(device, "collect", side_effect=mock_unsupported_collect), |
| 438 |
patch("anta.inventory.AntaInventory.connect_inventory", side_effect=mock_connect_inventory), |
| 439 |
): |
| 440 |
await collect_show_tech(inventory, root_dir=tmp_path, configure=True) |
| 441 |
|
| 442 |
assert copied_devices == ["device-0"] |
| 443 |
assert "Unable to collect tech-support on unsupported-device" in caplog.text |
| 444 |
assert "anta exec collect-tech-support is only supported with AsyncEOSDevice for now." in caplog.text |
| 445 |
|
| 446 |
|
| 447 |
@pytest.mark.parametrize( |
| 448 |
("tech_support_output", "aaa_output", "expected_message"), |
| 449 |
[ |
| 450 |
pytest.param("", "aaa authorization exec default local", "Unable to get tech-support filenames", id="no-tech-support-files"), |
| 451 |
pytest.param( |
| 452 |
"dummy_tech-support_2023-12-01.1115.log.gz", |
| 453 |
"", |
| 454 |
"configuration 'aaa authorization exec default local' is not present", |
| 455 |
id="missing-aaa-without-configure", |
| 456 |
), |
| 457 |
], |
| 458 |
) |
| 459 |
async def test_collect_show_tech_pre_copy_failures( |
| 460 |
caplog: pytest.LogCaptureFixture, |
| 461 |
tmp_path: Path, |
| 462 |
inventory: AntaInventory, |
| 463 |
tech_support_output: str, |
| 464 |
aaa_output: str, |
| 465 |
expected_message: str, |
| 466 |
) -> None: |
| 467 |
"""Test collect_show_tech handles failures before copying files.""" |
| 468 |
caplog.set_level(logging.ERROR) |
| 469 |
|
| 470 |
async def mock_connect_inventory() -> None: |
| 471 |
for device in inventory.values(): |
| 472 |
device.is_online = True |
| 473 |
device.established = True |
| 474 |
device.hw_model = "dummy" |
| 475 |
|
| 476 |
async def mock_collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 477 |
if "ls -1t" in command.command: |
| 478 |
command.output = tech_support_output |
| 479 |
elif "include aaa authorization" in command.command: |
| 480 |
command.output = aaa_output |
| 481 |
|
| 482 |
with ( |
| 483 |
patch("anta.device.AsyncEOSDevice.collect", side_effect=mock_collect, autospec=True), |
| 484 |
patch("anta.device.AsyncEOSDevice.copy", new_callable=AsyncMock) as mocked_copy, |
| 485 |
patch("anta.inventory.AntaInventory.connect_inventory", side_effect=mock_connect_inventory), |
| 486 |
): |
| 487 |
await collect_show_tech(inventory, root_dir=tmp_path, configure=False) |
| 488 |
|
| 489 |
mocked_copy.assert_not_awaited() |
| 490 |
assert expected_message in caplog.text |
| 491 |
|
| 492 |
|
| 493 |
@pytest.mark.parametrize( |
| 494 |
("enable", "enable_password", "expected_commands"), |
| 495 |
[ |
| 496 |
pytest.param( |
| 497 |
True, |
| 498 |
"enable-password", |
| 499 |
[ |
| 500 |
{"cmd": "enable", "input": "enable-password"}, |
| 501 |
{"cmd": "configure terminal"}, |
| 502 |
{"cmd": "aaa authorization exec default local"}, |
| 503 |
], |
| 504 |
id="enable-with-password", |
| 505 |
), |
| 506 |
pytest.param( |
| 507 |
True, |
| 508 |
None, |
| 509 |
[ |
| 510 |
{"cmd": "enable"}, |
| 511 |
{"cmd": "configure terminal"}, |
| 512 |
{"cmd": "aaa authorization exec default local"}, |
| 513 |
], |
| 514 |
id="enable-without-password", |
| 515 |
), |
| 516 |
pytest.param( |
| 517 |
False, |
| 518 |
None, |
| 519 |
[ |
| 520 |
{"cmd": "configure terminal"}, |
| 521 |
{"cmd": "aaa authorization exec default local"}, |
| 522 |
], |
| 523 |
id="without-enable", |
| 524 |
), |
| 525 |
], |
| 526 |
) |
| 527 |
async def test_collect_show_tech_configures_aaa_and_honors_latest( |
| 528 |
tmp_path: Path, |
| 529 |
inventory: AntaInventory, |
| 530 |
*, |
| 531 |
enable: bool, |
| 532 |
enable_password: str | None, |
| 533 |
expected_commands: list[dict[str, str]], |
| 534 |
) -> None: |
| 535 |
"""Test collect_show_tech configures AAA authorization and limits collected show-tech files.""" |
| 536 |
collected_commands: list[str] = [] |
| 537 |
copied_devices: list[str] = [] |
| 538 |
device = next(iter(inventory.values())) |
| 539 |
device.enable = enable |
| 540 |
device._enable_password = enable_password |
| 541 |
|
| 542 |
async def mock_connect_inventory() -> None: |
| 543 |
for inventory_device in inventory.values(): |
| 544 |
inventory_device.is_online = True |
| 545 |
inventory_device.established = True |
| 546 |
inventory_device.hw_model = "dummy" |
| 547 |
|
| 548 |
async def mock_collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: |
| 549 |
collected_commands.append(command.command) |
| 550 |
if "ls -1t" in command.command: |
| 551 |
command.output = "dummy_tech-support_2023-12-01.1115.log.gz" |
| 552 |
elif "include aaa authorization" in command.command: |
| 553 |
command.output = "" |
| 554 |
|
| 555 |
async def mock_copy(self: AntaDevice, *args: Any, **kwargs: Any) -> None: |
| 556 |
copied_devices.append(self.name) |
| 557 |
|
| 558 |
with ( |
| 559 |
patch("anta.device.AsyncEOSDevice.collect", side_effect=mock_collect, autospec=True), |
| 560 |
patch("anta.device.AsyncEOSDevice.copy", side_effect=mock_copy, autospec=True), |
| 561 |
patch("anta.inventory.AntaInventory.connect_inventory", side_effect=mock_connect_inventory), |
| 562 |
patch.object(device._client, "cli", new_callable=AsyncMock) as mocked_cli, |
| 563 |
): |
| 564 |
await collect_show_tech(inventory, root_dir=tmp_path, configure=True, latest=1) |
| 565 |
|
| 566 |
assert collected_commands[0].endswith("| head -1") |
| 567 |
mocked_cli.assert_awaited_once_with(commands=expected_commands) |
| 568 |
assert copied_devices == ["device-0"] |