2025-02-05 11:32:35 +01:00
|
|
|
# Copyright (c) 2023-2024 Arista Networks, Inc.
|
|
|
|
# Use of this source code is governed by the Apache License 2.0
|
|
|
|
# that can be found in the LICENSE file.
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Tests for anta.cli.exec.utils."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
from unittest.mock import call, patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
from anta.cli.exec.utils import (
|
2025-02-05 11:39:50 +01:00
|
|
|
clear_counters,
|
2025-02-05 11:39:09 +01:00
|
|
|
)
|
2025-02-05 11:32:35 +01:00
|
|
|
from anta.models import AntaCommand
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
# , collect_commands, collect_scheduled_show_tech
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
if TYPE_CHECKING:
|
2025-02-05 11:39:09 +01:00
|
|
|
from anta.device import AntaDevice
|
|
|
|
from anta.inventory import AntaInventory
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
# TODO: complete test cases
|
|
|
|
@pytest.mark.asyncio()
|
2025-02-05 11:32:35 +01:00
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("inventory_state", "per_device_command_output", "tags"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
|
|
|
pytest.param(
|
2025-02-05 11:39:09 +01:00
|
|
|
{
|
|
|
|
"dummy": {"is_online": False},
|
|
|
|
"dummy2": {"is_online": False},
|
|
|
|
"dummy3": {"is_online": False},
|
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
{},
|
|
|
|
None,
|
|
|
|
id="no_connected_device",
|
|
|
|
),
|
|
|
|
pytest.param(
|
2025-02-05 11:39:09 +01:00
|
|
|
{
|
|
|
|
"dummy": {"is_online": True, "hw_model": "cEOSLab"},
|
|
|
|
"dummy2": {"is_online": True, "hw_model": "vEOS-lab"},
|
|
|
|
"dummy3": {"is_online": False},
|
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
{},
|
|
|
|
None,
|
|
|
|
id="cEOSLab and vEOS-lab devices",
|
|
|
|
),
|
|
|
|
pytest.param(
|
2025-02-05 11:39:09 +01:00
|
|
|
{
|
|
|
|
"dummy": {"is_online": True},
|
|
|
|
"dummy2": {"is_online": True},
|
|
|
|
"dummy3": {"is_online": False},
|
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
{"dummy": None}, # None means the command failed to collect
|
|
|
|
None,
|
|
|
|
id="device with error",
|
|
|
|
),
|
|
|
|
pytest.param(
|
2025-02-05 11:39:09 +01:00
|
|
|
{
|
|
|
|
"dummy": {"is_online": True},
|
|
|
|
"dummy2": {"is_online": True},
|
|
|
|
"dummy3": {"is_online": True},
|
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
{},
|
|
|
|
["spine"],
|
|
|
|
id="tags",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2025-02-05 11:39:50 +01:00
|
|
|
async def test_clear_counters(
|
2025-02-05 11:39:09 +01:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
2025-02-05 11:32:35 +01:00
|
|
|
test_inventory: AntaInventory,
|
|
|
|
inventory_state: dict[str, Any],
|
|
|
|
per_device_command_output: dict[str, Any],
|
2025-02-05 11:39:09 +01:00
|
|
|
tags: set[str] | None,
|
2025-02-05 11:32:35 +01:00
|
|
|
) -> None:
|
2025-02-05 11:39:50 +01:00
|
|
|
"""Test anta.cli.exec.utils.clear_counters."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
async def mock_connect_inventory() -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Mock connect_inventory coroutine."""
|
2025-02-05 11:32:35 +01:00
|
|
|
for name, device in test_inventory.items():
|
|
|
|
device.is_online = inventory_state[name].get("is_online", True)
|
|
|
|
device.established = inventory_state[name].get("established", device.is_online)
|
|
|
|
device.hw_model = inventory_state[name].get("hw_model", "dummy")
|
|
|
|
|
2025-02-05 11:39:50 +01:00
|
|
|
async def collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: # noqa: ARG001, ANN401 #pylint: disable=unused-argument
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Mock collect coroutine."""
|
2025-02-05 11:32:35 +01:00
|
|
|
command.output = per_device_command_output.get(self.name, "")
|
|
|
|
|
|
|
|
# Need to patch the child device class
|
2025-02-05 11:39:09 +01:00
|
|
|
with (
|
2025-02-05 11:39:50 +01:00
|
|
|
patch("anta.device.AsyncEOSDevice.collect", side_effect=collect, autospec=True) as mocked_collect,
|
2025-02-05 11:39:09 +01:00
|
|
|
patch(
|
|
|
|
"anta.inventory.AntaInventory.connect_inventory",
|
|
|
|
side_effect=mock_connect_inventory,
|
|
|
|
) as mocked_connect_inventory,
|
|
|
|
):
|
2025-02-05 11:39:50 +01:00
|
|
|
await clear_counters(test_inventory, tags=tags)
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
mocked_connect_inventory.assert_awaited_once()
|
2025-02-05 11:39:09 +01:00
|
|
|
devices_established = test_inventory.get_inventory(established_only=True, tags=tags).devices
|
2025-02-05 11:32:35 +01:00
|
|
|
if devices_established:
|
|
|
|
# Building the list of calls
|
|
|
|
calls = []
|
|
|
|
for device in devices_established:
|
|
|
|
calls.append(
|
|
|
|
call(
|
|
|
|
device,
|
2025-02-05 11:39:09 +01:00
|
|
|
command=AntaCommand(
|
|
|
|
command="clear counters",
|
|
|
|
version="latest",
|
|
|
|
revision=None,
|
|
|
|
ofmt="json",
|
|
|
|
output=per_device_command_output.get(device.name, ""),
|
|
|
|
errors=[],
|
|
|
|
),
|
2025-02-05 11:39:50 +01:00
|
|
|
collection_id=None,
|
2025-02-05 11:39:09 +01:00
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
)
|
|
|
|
if device.hw_model not in ["cEOSLab", "vEOS-lab"]:
|
|
|
|
calls.append(
|
|
|
|
call(
|
|
|
|
device,
|
2025-02-05 11:39:09 +01:00
|
|
|
command=AntaCommand(
|
|
|
|
command="clear hardware counter drop",
|
|
|
|
version="latest",
|
|
|
|
revision=None,
|
|
|
|
ofmt="json",
|
|
|
|
output=per_device_command_output.get(device.name, ""),
|
|
|
|
),
|
2025-02-05 11:39:50 +01:00
|
|
|
collection_id=None,
|
2025-02-05 11:39:09 +01:00
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
)
|
|
|
|
mocked_collect.assert_has_awaits(calls)
|
|
|
|
# Check error
|
|
|
|
for key, value in per_device_command_output.items():
|
|
|
|
if value is None:
|
|
|
|
# means some command failed to collect
|
|
|
|
assert "ERROR" in caplog.text
|
|
|
|
assert f"Could not clear counters on device {key}: []" in caplog.text
|
|
|
|
else:
|
|
|
|
mocked_collect.assert_not_awaited()
|