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:38:32 +01:00
|
|
|
"""test anta.device.py."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
from pathlib import Path
|
2025-02-05 11:38:32 +01:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2025-02-05 11:32:35 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
import pytest
|
|
|
|
from asyncssh import SSHClientConnection, SSHClientConnectionOptions
|
|
|
|
from rich import print as rprint
|
|
|
|
|
2025-02-05 11:39:42 +01:00
|
|
|
import asynceapi
|
2025-02-05 11:32:35 +01:00
|
|
|
from anta.device import AntaDevice, AsyncEOSDevice
|
|
|
|
from anta.models import AntaCommand
|
|
|
|
from tests.lib.fixture import COMMAND_OUTPUT
|
|
|
|
from tests.lib.utils import generate_test_ids_list
|
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from _pytest.mark.structures import ParameterSet
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
INIT_DATA: list[dict[str, Any]] = [
|
|
|
|
{
|
|
|
|
"name": "no name, no port",
|
|
|
|
"device": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
},
|
|
|
|
"expected": {"name": "42.42.42.42"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "no name, port",
|
|
|
|
"device": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
"port": 666,
|
|
|
|
},
|
|
|
|
"expected": {"name": "42.42.42.42:666"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "name",
|
|
|
|
"device": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
"name": "test.anta.ninja",
|
|
|
|
"disable_cache": True,
|
|
|
|
},
|
|
|
|
"expected": {"name": "test.anta.ninja"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "insecure",
|
|
|
|
"device": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
"name": "test.anta.ninja",
|
|
|
|
"insecure": True,
|
|
|
|
},
|
|
|
|
"expected": {"name": "test.anta.ninja"},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
EQUALITY_DATA: list[dict[str, Any]] = [
|
|
|
|
{
|
|
|
|
"name": "equal",
|
|
|
|
"device1": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
},
|
|
|
|
"device2": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "blah",
|
|
|
|
},
|
|
|
|
"expected": True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "equals-name",
|
|
|
|
"device1": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
"name": "device1",
|
|
|
|
},
|
|
|
|
"device2": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "plop",
|
|
|
|
"password": "anta",
|
|
|
|
"name": "device2",
|
|
|
|
},
|
|
|
|
"expected": True,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "not-equal-port",
|
|
|
|
"device1": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
},
|
|
|
|
"device2": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
"port": 666,
|
|
|
|
},
|
|
|
|
"expected": False,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "not-equal-host",
|
|
|
|
"device1": {
|
|
|
|
"host": "42.42.42.41",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
},
|
|
|
|
"device2": {
|
|
|
|
"host": "42.42.42.42",
|
|
|
|
"username": "anta",
|
|
|
|
"password": "anta",
|
|
|
|
},
|
|
|
|
"expected": False,
|
|
|
|
},
|
|
|
|
]
|
2025-02-05 11:39:42 +01:00
|
|
|
ASYNCEAPI_COLLECT_DATA: list[dict[str, Any]] = [
|
2025-02-05 11:32:35 +01:00
|
|
|
{
|
|
|
|
"name": "command",
|
|
|
|
"device": {},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {
|
|
|
|
"return_value": [
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
2025-02-05 11:38:32 +01:00
|
|
|
},
|
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expected": {
|
|
|
|
"output": {
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
|
|
|
"errors": [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "enable",
|
|
|
|
"device": {"enable": True},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {
|
|
|
|
"return_value": [
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
2025-02-05 11:38:32 +01:00
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expected": {
|
|
|
|
"output": {
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
|
|
|
"errors": [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "enable password",
|
|
|
|
"device": {"enable": True, "enable_password": "anta"},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {
|
|
|
|
"return_value": [
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
2025-02-05 11:38:32 +01:00
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expected": {
|
|
|
|
"output": {
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
|
|
|
"errors": [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "revision",
|
|
|
|
"device": {},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"revision": 3,
|
|
|
|
"patch_kwargs": {
|
|
|
|
"return_value": [
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
2025-02-05 11:38:32 +01:00
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expected": {
|
|
|
|
"output": {
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
},
|
|
|
|
"errors": [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2025-02-05 11:39:42 +01:00
|
|
|
"name": "asynceapi.EapiCommandError",
|
2025-02-05 11:32:35 +01:00
|
|
|
"device": {},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {
|
2025-02-05 11:39:42 +01:00
|
|
|
"side_effect": asynceapi.EapiCommandError(
|
2025-02-05 11:38:32 +01:00
|
|
|
passed=[],
|
|
|
|
failed="show version",
|
|
|
|
errors=["Authorization denied for command 'show version'"],
|
|
|
|
errmsg="Invalid command",
|
|
|
|
not_exec=[],
|
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"expected": {"output": None, "errors": ["Authorization denied for command 'show version'"]},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "httpx.HTTPError",
|
|
|
|
"device": {},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {"side_effect": httpx.HTTPError(message="404")},
|
|
|
|
},
|
2025-02-05 11:38:32 +01:00
|
|
|
"expected": {"output": None, "errors": ["HTTPError: 404"]},
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "httpx.ConnectError",
|
|
|
|
"device": {},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"patch_kwargs": {"side_effect": httpx.ConnectError(message="Cannot open port")},
|
|
|
|
},
|
2025-02-05 11:38:32 +01:00
|
|
|
"expected": {"output": None, "errors": ["ConnectError: Cannot open port"]},
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
]
|
2025-02-05 11:39:42 +01:00
|
|
|
ASYNCEAPI_COPY_DATA: list[dict[str, Any]] = [
|
2025-02-05 11:32:35 +01:00
|
|
|
{
|
|
|
|
"name": "from",
|
|
|
|
"device": {},
|
|
|
|
"copy": {
|
|
|
|
"sources": [Path("/mnt/flash"), Path("/var/log/agents")],
|
2025-02-05 11:38:32 +01:00
|
|
|
"destination": Path(),
|
2025-02-05 11:32:35 +01:00
|
|
|
"direction": "from",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "to",
|
|
|
|
"device": {},
|
|
|
|
"copy": {
|
|
|
|
"sources": [Path("/mnt/flash"), Path("/var/log/agents")],
|
2025-02-05 11:38:32 +01:00
|
|
|
"destination": Path(),
|
2025-02-05 11:32:35 +01:00
|
|
|
"direction": "to",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "wrong",
|
|
|
|
"device": {},
|
|
|
|
"copy": {
|
|
|
|
"sources": [Path("/mnt/flash"), Path("/var/log/agents")],
|
2025-02-05 11:38:32 +01:00
|
|
|
"destination": Path(),
|
2025-02-05 11:32:35 +01:00
|
|
|
"direction": "wrong",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
REFRESH_DATA: list[dict[str, Any]] = [
|
|
|
|
{
|
|
|
|
"name": "established",
|
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": True},
|
|
|
|
{
|
2025-02-05 11:38:32 +01:00
|
|
|
"return_value": [
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
}
|
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": True, "established": True, "hw_model": "DCS-7280CR3-32P4-F"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "is not online",
|
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": False},
|
|
|
|
{
|
|
|
|
"return_value": {
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"modelName": "DCS-7280CR3-32P4-F",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
2025-02-05 11:38:32 +01:00
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": False, "established": False, "hw_model": None},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "cannot parse command",
|
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": True},
|
|
|
|
{
|
2025-02-05 11:38:32 +01:00
|
|
|
"return_value": [
|
|
|
|
{
|
|
|
|
"mfgName": "Arista",
|
|
|
|
"hardwareRevision": "11.00",
|
|
|
|
"serialNumber": "JPE19500066",
|
|
|
|
"systemMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"hwMacAddress": "fc:bd:67:3d:13:c5",
|
|
|
|
"configMacAddress": "00:00:00:00:00:00",
|
|
|
|
"version": "4.31.1F-34361447.fraserrel (engineering build)",
|
|
|
|
"architecture": "x86_64",
|
|
|
|
"internalVersion": "4.31.1F-34361447.fraserrel",
|
|
|
|
"internalBuildId": "4940d112-a2fc-4970-8b5a-a16cd03fd08c",
|
|
|
|
"imageFormatVersion": "3.0",
|
|
|
|
"imageOptimization": "Default",
|
|
|
|
"bootupTimestamp": 1700729434.5892005,
|
|
|
|
"uptime": 20666.78,
|
|
|
|
"memTotal": 8099732,
|
|
|
|
"memFree": 4989568,
|
|
|
|
"isIntlVersion": False,
|
|
|
|
}
|
|
|
|
],
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": True, "established": False, "hw_model": None},
|
|
|
|
},
|
|
|
|
{
|
2025-02-05 11:39:42 +01:00
|
|
|
"name": "asynceapi.EapiCommandError",
|
2025-02-05 11:32:35 +01:00
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": True},
|
|
|
|
{
|
2025-02-05 11:39:42 +01:00
|
|
|
"side_effect": asynceapi.EapiCommandError(
|
2025-02-05 11:38:32 +01:00
|
|
|
passed=[],
|
|
|
|
failed="show version",
|
|
|
|
errors=["Authorization denied for command 'show version'"],
|
|
|
|
errmsg="Invalid command",
|
|
|
|
not_exec=[],
|
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": True, "established": False, "hw_model": None},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "httpx.HTTPError",
|
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": True},
|
|
|
|
{"side_effect": httpx.HTTPError(message="404")},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": True, "established": False, "hw_model": None},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "httpx.ConnectError",
|
|
|
|
"device": {},
|
|
|
|
"patch_kwargs": (
|
|
|
|
{"return_value": True},
|
|
|
|
{"side_effect": httpx.ConnectError(message="Cannot open port")},
|
|
|
|
),
|
|
|
|
"expected": {"is_online": True, "established": False, "hw_model": None},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
COLLECT_DATA: list[dict[str, Any]] = [
|
|
|
|
{
|
|
|
|
"name": "device cache enabled, command cache enabled, no cache hit",
|
|
|
|
"device": {"disable_cache": False},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": True,
|
|
|
|
},
|
|
|
|
"expected": {"cache_hit": False},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "device cache enabled, command cache enabled, cache hit",
|
|
|
|
"device": {"disable_cache": False},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": True,
|
|
|
|
},
|
|
|
|
"expected": {"cache_hit": True},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "device cache disabled, command cache enabled",
|
|
|
|
"device": {"disable_cache": True},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": True,
|
|
|
|
},
|
|
|
|
"expected": {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "device cache enabled, command cache disabled, cache has command",
|
|
|
|
"device": {"disable_cache": False},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": False,
|
|
|
|
},
|
|
|
|
"expected": {"cache_hit": True},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "device cache enabled, command cache disabled, cache does not have data",
|
|
|
|
"device": {
|
|
|
|
"disable_cache": False,
|
|
|
|
},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": False,
|
|
|
|
},
|
|
|
|
"expected": {"cache_hit": False},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "device cache disabled, command cache disabled",
|
|
|
|
"device": {
|
|
|
|
"disable_cache": True,
|
|
|
|
},
|
|
|
|
"command": {
|
|
|
|
"command": "show version",
|
|
|
|
"use_cache": False,
|
|
|
|
},
|
|
|
|
"expected": {},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
CACHE_STATS_DATA: list[ParameterSet] = [
|
|
|
|
pytest.param({"disable_cache": False}, {"total_commands_sent": 0, "cache_hits": 0, "cache_hit_ratio": "0.00%"}, id="with_cache"),
|
|
|
|
pytest.param({"disable_cache": True}, None, id="without_cache"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class TestAntaDevice:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test for anta.device.AntaDevice Abstract class."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
@pytest.mark.asyncio()
|
2025-02-05 11:32:35 +01:00
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:38:32 +01:00
|
|
|
("device", "command_data", "expected_data"),
|
|
|
|
((d["device"], d["command"], d["expected"]) for d in COLLECT_DATA),
|
2025-02-05 11:32:35 +01:00
|
|
|
indirect=["device"],
|
|
|
|
ids=generate_test_ids_list(COLLECT_DATA),
|
|
|
|
)
|
|
|
|
async def test_collect(self, device: AntaDevice, command_data: dict[str, Any], expected_data: dict[str, Any]) -> None:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test AntaDevice.collect behavior."""
|
2025-02-05 11:32:35 +01:00
|
|
|
command = AntaCommand(command=command_data["command"], use_cache=command_data["use_cache"])
|
|
|
|
|
|
|
|
# Dummy output for cache hit
|
|
|
|
cached_output = "cached_value"
|
|
|
|
|
|
|
|
if device.cache is not None and expected_data["cache_hit"] is True:
|
|
|
|
await device.cache.set(command.uid, cached_output)
|
|
|
|
|
|
|
|
await device.collect(command)
|
|
|
|
|
|
|
|
if device.cache is not None: # device_cache is enabled
|
|
|
|
current_cached_data = await device.cache.get(command.uid)
|
|
|
|
if command.use_cache is True: # command is allowed to use cache
|
|
|
|
if expected_data["cache_hit"] is True:
|
|
|
|
assert command.output == cached_output
|
|
|
|
assert current_cached_data == cached_output
|
|
|
|
assert device.cache.hit_miss_ratio["hits"] == 2
|
|
|
|
else:
|
|
|
|
assert command.output == COMMAND_OUTPUT
|
|
|
|
assert current_cached_data == COMMAND_OUTPUT
|
|
|
|
assert device.cache.hit_miss_ratio["hits"] == 1
|
|
|
|
else: # command is not allowed to use cache
|
2025-02-05 11:39:42 +01:00
|
|
|
device._collect.assert_called_once_with(command=command, collection_id=None) # type: ignore[attr-defined] # pylint: disable=protected-access
|
2025-02-05 11:32:35 +01:00
|
|
|
assert command.output == COMMAND_OUTPUT
|
|
|
|
if expected_data["cache_hit"] is True:
|
|
|
|
assert current_cached_data == cached_output
|
|
|
|
else:
|
|
|
|
assert current_cached_data is None
|
|
|
|
else: # device is disabled
|
|
|
|
assert device.cache is None
|
2025-02-05 11:39:42 +01:00
|
|
|
device._collect.assert_called_once_with(command=command, collection_id=None) # type: ignore[attr-defined] # pylint: disable=protected-access
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
@pytest.mark.parametrize(("device", "expected"), CACHE_STATS_DATA, indirect=["device"])
|
2025-02-05 11:32:35 +01:00
|
|
|
def test_cache_statistics(self, device: AntaDevice, expected: dict[str, Any] | None) -> None:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Verify that when cache statistics attribute does not exist.
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
TODO add a test where cache has some value.
|
2025-02-05 11:32:35 +01:00
|
|
|
"""
|
2025-02-05 11:38:32 +01:00
|
|
|
assert device.cache_statistics == expected
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestAsyncEOSDevice:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test for anta.device.AsyncEOSDevice."""
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("data", INIT_DATA, ids=generate_test_ids_list(INIT_DATA))
|
|
|
|
def test__init__(self, data: dict[str, Any]) -> None:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test the AsyncEOSDevice constructor."""
|
2025-02-05 11:32:35 +01:00
|
|
|
device = AsyncEOSDevice(**data["device"])
|
|
|
|
|
|
|
|
assert device.name == data["expected"]["name"]
|
|
|
|
if data["device"].get("disable_cache") is True:
|
|
|
|
assert device.cache is None
|
|
|
|
assert device.cache_locks is None
|
|
|
|
else: # False or None
|
|
|
|
assert device.cache is not None
|
|
|
|
assert device.cache_locks is not None
|
|
|
|
hash(device)
|
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
with patch("anta.device.__DEBUG__", new=True):
|
2025-02-05 11:32:35 +01:00
|
|
|
rprint(device)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("data", EQUALITY_DATA, ids=generate_test_ids_list(EQUALITY_DATA))
|
|
|
|
def test__eq(self, data: dict[str, Any]) -> None:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test the AsyncEOSDevice equality."""
|
2025-02-05 11:32:35 +01:00
|
|
|
device1 = AsyncEOSDevice(**data["device1"])
|
|
|
|
device2 = AsyncEOSDevice(**data["device2"])
|
|
|
|
if data["expected"]:
|
|
|
|
assert device1 == device2
|
|
|
|
else:
|
|
|
|
assert device1 != device2
|
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
@pytest.mark.asyncio()
|
2025-02-05 11:32:35 +01:00
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:38:32 +01:00
|
|
|
("async_device", "patch_kwargs", "expected"),
|
|
|
|
((d["device"], d["patch_kwargs"], d["expected"]) for d in REFRESH_DATA),
|
2025-02-05 11:32:35 +01:00
|
|
|
ids=generate_test_ids_list(REFRESH_DATA),
|
|
|
|
indirect=["async_device"],
|
|
|
|
)
|
|
|
|
async def test_refresh(self, async_device: AsyncEOSDevice, patch_kwargs: list[dict[str, Any]], expected: dict[str, Any]) -> None:
|
|
|
|
# pylint: disable=protected-access
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test AsyncEOSDevice.refresh()."""
|
|
|
|
with patch.object(async_device._session, "check_connection", **patch_kwargs[0]), patch.object(async_device._session, "cli", **patch_kwargs[1]):
|
|
|
|
await async_device.refresh()
|
2025-02-05 11:39:42 +01:00
|
|
|
async_device._session.check_connection.assert_called_once() # type: ignore[attr-defined] # asynceapi.Device.check_connection is patched
|
2025-02-05 11:38:32 +01:00
|
|
|
if expected["is_online"]:
|
2025-02-05 11:39:42 +01:00
|
|
|
async_device._session.cli.assert_called_once() # type: ignore[attr-defined] # asynceapi.Device.cli is patched
|
2025-02-05 11:38:32 +01:00
|
|
|
assert async_device.is_online == expected["is_online"]
|
|
|
|
assert async_device.established == expected["established"]
|
|
|
|
assert async_device.hw_model == expected["hw_model"]
|
2025-02-05 11:32:35 +01:00
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
@pytest.mark.asyncio()
|
2025-02-05 11:32:35 +01:00
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:38:32 +01:00
|
|
|
("async_device", "command", "expected"),
|
2025-02-05 11:39:42 +01:00
|
|
|
((d["device"], d["command"], d["expected"]) for d in ASYNCEAPI_COLLECT_DATA),
|
|
|
|
ids=generate_test_ids_list(ASYNCEAPI_COLLECT_DATA),
|
2025-02-05 11:32:35 +01:00
|
|
|
indirect=["async_device"],
|
|
|
|
)
|
|
|
|
async def test__collect(self, async_device: AsyncEOSDevice, command: dict[str, Any], expected: dict[str, Any]) -> None:
|
|
|
|
# pylint: disable=protected-access
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test AsyncEOSDevice._collect()."""
|
|
|
|
cmd = AntaCommand(command=command["command"], revision=command["revision"]) if "revision" in command else AntaCommand(command=command["command"])
|
2025-02-05 11:32:35 +01:00
|
|
|
with patch.object(async_device._session, "cli", **command["patch_kwargs"]):
|
2025-02-05 11:39:42 +01:00
|
|
|
collection_id = "pytest"
|
|
|
|
await async_device.collect(cmd, collection_id=collection_id)
|
2025-02-05 11:38:32 +01:00
|
|
|
commands: list[dict[str, Any]] = []
|
2025-02-05 11:32:35 +01:00
|
|
|
if async_device.enable and async_device._enable_password is not None:
|
|
|
|
commands.append(
|
|
|
|
{
|
|
|
|
"cmd": "enable",
|
|
|
|
"input": str(async_device._enable_password),
|
2025-02-05 11:38:32 +01:00
|
|
|
},
|
2025-02-05 11:32:35 +01:00
|
|
|
)
|
|
|
|
elif async_device.enable:
|
|
|
|
# No password
|
|
|
|
commands.append({"cmd": "enable"})
|
|
|
|
if cmd.revision:
|
|
|
|
commands.append({"cmd": cmd.command, "revision": cmd.revision})
|
|
|
|
else:
|
|
|
|
commands.append({"cmd": cmd.command})
|
2025-02-05 11:39:42 +01:00
|
|
|
async_device._session.cli.assert_called_once_with(commands=commands, ofmt=cmd.ofmt, version=cmd.version, req_id=f"ANTA-{collection_id}-{id(cmd)}") # type: ignore[attr-defined] # asynceapi.Device.cli is patched # pylint: disable=line-too-long
|
2025-02-05 11:32:35 +01:00
|
|
|
assert cmd.output == expected["output"]
|
|
|
|
assert cmd.errors == expected["errors"]
|
|
|
|
|
2025-02-05 11:38:32 +01:00
|
|
|
@pytest.mark.asyncio()
|
2025-02-05 11:32:35 +01:00
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:38:32 +01:00
|
|
|
("async_device", "copy"),
|
2025-02-05 11:39:42 +01:00
|
|
|
((d["device"], d["copy"]) for d in ASYNCEAPI_COPY_DATA),
|
|
|
|
ids=generate_test_ids_list(ASYNCEAPI_COPY_DATA),
|
2025-02-05 11:32:35 +01:00
|
|
|
indirect=["async_device"],
|
|
|
|
)
|
|
|
|
async def test_copy(self, async_device: AsyncEOSDevice, copy: dict[str, Any]) -> None:
|
2025-02-05 11:38:32 +01:00
|
|
|
"""Test AsyncEOSDevice.copy()."""
|
2025-02-05 11:32:35 +01:00
|
|
|
conn = SSHClientConnection(asyncio.get_event_loop(), SSHClientConnectionOptions())
|
|
|
|
with patch("asyncssh.connect") as connect_mock:
|
|
|
|
connect_mock.return_value.__aenter__.return_value = conn
|
|
|
|
with patch("asyncssh.scp") as scp_mock:
|
|
|
|
await async_device.copy(copy["sources"], copy["destination"], copy["direction"])
|
|
|
|
if copy["direction"] == "from":
|
|
|
|
src = [(conn, file) for file in copy["sources"]]
|
|
|
|
dst = copy["destination"]
|
|
|
|
elif copy["direction"] == "to":
|
|
|
|
src = copy["sources"]
|
|
|
|
dst = conn, copy["destination"]
|
|
|
|
else:
|
|
|
|
scp_mock.assert_not_awaited()
|
|
|
|
return
|
|
|
|
scp_mock.assert_awaited_once_with(src, dst)
|