From 700269aa15e4f492af2842eb98cc1f2554b2c3a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20T=C3=A2che?= Date: Fri, 17 May 2024 17:21:55 +0200 Subject: [PATCH] chore: import aioeapi module (#676) --- .pre-commit-config.yaml | 2 +- NOTICE | 28 +++ anta/aioeapi.py | 106 ----------- anta/cli/exec/commands.py | 8 +- anta/cli/exec/utils.py | 6 +- anta/device.py | 43 +++-- anta/models.py | 2 +- asynceapi/__init__.py | 12 ++ asynceapi/aio_portcheck.py | 58 ++++++ asynceapi/config_session.py | 289 ++++++++++++++++++++++++++++ asynceapi/device.py | 291 +++++++++++++++++++++++++++++ asynceapi/errors.py | 42 +++++ docs/README.md | 2 + docs/troubleshooting.md | 11 ++ pyproject.toml | 9 +- tests/lib/fixture.py | 18 +- tests/units/cli/exec/test_utils.py | 15 +- tests/units/test_device.py | 35 ++-- 18 files changed, 802 insertions(+), 175 deletions(-) create mode 100644 NOTICE delete mode 100644 anta/aioeapi.py create mode 100644 asynceapi/__init__.py create mode 100644 asynceapi/aio_portcheck.py create mode 100644 asynceapi/config_session.py create mode 100644 asynceapi/device.py create mode 100644 asynceapi/errors.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1c86d39f6..bdfb5abf1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ --- # See https://pre-commit.com for more information # See https://pre-commit.com/hooks.html for more hooks -files: ^(anta|docs|scripts|tests)/ +files: ^(anta|docs|scripts|tests|asynceapi)/ repos: - repo: https://github.com/pre-commit/pre-commit-hooks diff --git a/NOTICE b/NOTICE new file mode 100644 index 000000000..fbbee8d43 --- /dev/null +++ b/NOTICE @@ -0,0 +1,28 @@ +ANTA Project + +Copyright 2024 Arista Networks + +This product includes software developed at Arista Networks. + +------------------------------------------------------------------------ + +This product includes software developed by contributors from the +following projects, which are also licensed under the Apache License, Version 2.0: + +1. aio-eapi + - Copyright 2024 Jeremy Schulman + - URL: https://github.com/jeremyschulman/aio-eapi + +------------------------------------------------------------------------ + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/anta/aioeapi.py b/anta/aioeapi.py deleted file mode 100644 index f99ea1851..000000000 --- a/anta/aioeapi.py +++ /dev/null @@ -1,106 +0,0 @@ -# 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. -"""Patch for aioeapi waiting for https://github.com/jeremyschulman/aio-eapi/pull/13.""" -from __future__ import annotations - -from typing import Any, AnyStr - -import aioeapi - -Device = aioeapi.Device - - -class EapiCommandError(RuntimeError): - """Exception class for EAPI command errors. - - Attributes - ---------- - failed: str - the failed command - errmsg: str - a description of the failure reason - errors: list[str] - the command failure details - passed: list[dict] - a list of command results of the commands that passed - not_exec: list[str] - a list of commands that were not executed - """ - - # pylint: disable=too-many-arguments - def __init__(self, failed: str, errors: list[str], errmsg: str, passed: list[str | dict[str, Any]], not_exec: list[dict[str, Any]]) -> None: - """Initializer for the EapiCommandError exception.""" - self.failed = failed - self.errmsg = errmsg - self.errors = errors - self.passed = passed - self.not_exec = not_exec - super().__init__() - - def __str__(self) -> str: - """Returns the error message associated with the exception.""" - return self.errmsg - - -aioeapi.EapiCommandError = EapiCommandError - - -async def jsonrpc_exec(self, jsonrpc: dict) -> list[dict | AnyStr]: # type: ignore - """Execute the JSON-RPC dictionary object. - - Parameters - ---------- - jsonrpc: dict - The JSON-RPC as created by the `meth`:jsonrpc_command(). - - Raises - ------ - EapiCommandError - In the event that a command resulted in an error response. - - Returns - ------- - The list of command results; either dict or text depending on the - JSON-RPC format pameter. - """ - res = await self.post("/command-api", json=jsonrpc) - res.raise_for_status() - body = res.json() - - commands = jsonrpc["params"]["cmds"] - ofmt = jsonrpc["params"]["format"] - - get_output = (lambda _r: _r["output"]) if ofmt == "text" else (lambda _r: _r) - - # if there are no errors then return the list of command results. - if (err_data := body.get("error")) is None: - return [get_output(cmd_res) for cmd_res in body["result"]] - - # --------------------------------------------------------------------- - # if we are here, then there were some command errors. Raise a - # EapiCommandError exception with args (commands that failed, passed, - # not-executed). - # --------------------------------------------------------------------- - - # -------------------------- eAPI specification ---------------------- - # On an error, no result object is present, only an error object, which - # is guaranteed to have the following attributes: code, messages, and - # data. Similar to the result object in the successful response, the - # data object is a list of objects corresponding to the results of all - # commands up to, and including, the failed command. If there was a an - # error before any commands were executed (e.g. bad credentials), data - # will be empty. The last object in the data array will always - # correspond to the failed command. The command failure details are - # always stored in the errors array. - - cmd_data = err_data["data"] - len_data = len(cmd_data) - err_at = len_data - 1 - err_msg = err_data["message"] - - raise EapiCommandError( - passed=[get_output(cmd_data[cmd_i]) for cmd_i, cmd in enumerate(commands[:err_at])], - failed=commands[err_at]["cmd"], - errors=cmd_data[err_at]["errors"], - errmsg=err_msg, - not_exec=commands[err_at + 1 :], - ) - - -aioeapi.Device.jsonrpc_exec = jsonrpc_exec diff --git a/anta/cli/exec/commands.py b/anta/cli/exec/commands.py index 9842cc882..531614abd 100644 --- a/anta/cli/exec/commands.py +++ b/anta/cli/exec/commands.py @@ -16,7 +16,7 @@ from yaml import safe_load from anta.cli.console import console -from anta.cli.exec.utils import clear_counters_utils, collect_commands, collect_scheduled_show_tech +from anta.cli.exec import utils from anta.cli.utils import inventory_options if TYPE_CHECKING: @@ -29,7 +29,7 @@ @inventory_options def clear_counters(inventory: AntaInventory, tags: set[str] | None) -> None: """Clear counter statistics on EOS devices.""" - asyncio.run(clear_counters_utils(inventory, tags=tags)) + asyncio.run(utils.clear_counters(inventory, tags=tags)) @click.command() @@ -62,7 +62,7 @@ def snapshot(inventory: AntaInventory, tags: set[str] | None, commands_list: Pat except FileNotFoundError: logger.error("Error reading %s", commands_list) sys.exit(1) - asyncio.run(collect_commands(inventory, eos_commands, output, tags=tags)) + asyncio.run(utils.collect_commands(inventory, eos_commands, output, tags=tags)) @click.command() @@ -98,4 +98,4 @@ def collect_tech_support( configure: bool, ) -> None: """Collect scheduled tech-support from EOS devices.""" - asyncio.run(collect_scheduled_show_tech(inventory, output, configure=configure, tags=tags, latest=latest)) + asyncio.run(utils.collect_show_tech(inventory, output, configure=configure, tags=tags, latest=latest)) diff --git a/anta/cli/exec/utils.py b/anta/cli/exec/utils.py index 3e3ab511f..a5f7da2b1 100644 --- a/anta/cli/exec/utils.py +++ b/anta/cli/exec/utils.py @@ -14,13 +14,13 @@ from pathlib import Path from typing import TYPE_CHECKING, Literal -from aioeapi import EapiCommandError from click.exceptions import UsageError from httpx import ConnectError, HTTPError from anta.custom_types import REGEXP_PATH_MARKERS from anta.device import AntaDevice, AsyncEOSDevice from anta.models import AntaCommand +from asynceapi import EapiCommandError if TYPE_CHECKING: from anta.inventory import AntaInventory @@ -30,7 +30,7 @@ logger = logging.getLogger(__name__) -async def clear_counters_utils(anta_inventory: AntaInventory, tags: set[str] | None = None) -> None: +async def clear_counters(anta_inventory: AntaInventory, tags: set[str] | None = None) -> None: """Clear counters.""" async def clear(dev: AntaDevice) -> None: @@ -95,7 +95,7 @@ async def collect(dev: AntaDevice, command: str, outformat: Literal["json", "tex logger.error("Error when collecting commands: %s", str(r)) -async def collect_scheduled_show_tech(inv: AntaInventory, root_dir: Path, *, configure: bool, tags: set[str] | None = None, latest: int | None = None) -> None: +async def collect_show_tech(inv: AntaInventory, root_dir: Path, *, configure: bool, tags: set[str] | None = None, latest: int | None = None) -> None: """Collect scheduled show-tech on devices.""" async def collect(device: AntaDevice) -> None: diff --git a/anta/device.py b/anta/device.py index d517b8fb0..f0ec6a00c 100644 --- a/anta/device.py +++ b/anta/device.py @@ -18,7 +18,8 @@ from asyncssh import SSHClientConnection, SSHClientConnectionOptions from httpx import ConnectError, HTTPError, TimeoutException -from anta import __DEBUG__, aioeapi +import asynceapi +from anta import __DEBUG__ from anta.logger import anta_log_exception, exc_to_str from anta.models import AntaCommand @@ -116,7 +117,7 @@ def __rich_repr__(self) -> Iterator[tuple[str, Any]]: yield "disable_cache", self.cache is None @abstractmethod - async def _collect(self, command: AntaCommand) -> None: + async def _collect(self, command: AntaCommand, *, collection_id: str | None = None) -> None: """Collect device command output. This abstract coroutine can be used to implement any command collection method @@ -131,11 +132,11 @@ async def _collect(self, command: AntaCommand) -> None: Args: ---- - command: the command to collect - + command: The command to collect. + collection_id: An identifier used to build the eAPI request ID. """ - async def collect(self, command: AntaCommand) -> None: + async def collect(self, command: AntaCommand, *, collection_id: str | None = None) -> None: """Collect the output for a specified command. When caching is activated on both the device and the command, @@ -148,8 +149,8 @@ async def collect(self, command: AntaCommand) -> None: Args: ---- - command (AntaCommand): The command to process. - + command: The command to collect. + collection_id: An identifier used to build the eAPI request ID. """ # Need to ignore pylint no-member as Cache is a proxy class and pylint is not smart enough # https://github.com/pylint-dev/pylint/issues/7258 @@ -161,20 +162,20 @@ async def collect(self, command: AntaCommand) -> None: logger.debug("Cache hit for %s on %s", command.command, self.name) command.output = cached_output else: - await self._collect(command=command) + await self._collect(command=command, collection_id=collection_id) await self.cache.set(command.uid, command.output) # pylint: disable=no-member else: - await self._collect(command=command) + await self._collect(command=command, collection_id=collection_id) - async def collect_commands(self, commands: list[AntaCommand]) -> None: + async def collect_commands(self, commands: list[AntaCommand], *, collection_id: str | None = None) -> None: """Collect multiple commands. Args: ---- - commands: the commands to collect - + commands: The commands to collect. + collection_id: An identifier used to build the eAPI request ID. """ - await asyncio.gather(*(self.collect(command=command) for command in commands)) + await asyncio.gather(*(self.collect(command=command, collection_id=collection_id) for command in commands)) @abstractmethod async def refresh(self) -> None: @@ -270,7 +271,7 @@ def __init__( raise ValueError(message) self.enable = enable self._enable_password = enable_password - self._session: aioeapi.Device = aioeapi.Device(host=host, port=port, username=username, password=password, proto=proto, timeout=timeout) + self._session: asynceapi.Device = asynceapi.Device(host=host, port=port, username=username, password=password, proto=proto, timeout=timeout) ssh_params: dict[str, Any] = {} if insecure: ssh_params["known_hosts"] = None @@ -305,7 +306,7 @@ def _keys(self) -> tuple[Any, ...]: """ return (self._session.host, self._session.port) - async def _collect(self, command: AntaCommand) -> None: # noqa: C901 function is too complex - because of many required except blocks + async def _collect(self, command: AntaCommand, *, collection_id: str | None = None) -> None: # noqa: C901 function is too complex - because of many required except blocks #pylint: disable=line-too-long """Collect device command output from EOS using aio-eapi. Supports outformat `json` and `text` as output structure. @@ -314,9 +315,10 @@ async def _collect(self, command: AntaCommand) -> None: # noqa: C901 function Args: ---- - command: the AntaCommand to collect. + command: The command to collect. + collection_id: An identifier used to build the eAPI request ID. """ - commands: list[dict[str, Any]] = [] + commands: list[dict[str, str | int]] = [] if self.enable and self._enable_password is not None: commands.append( { @@ -329,14 +331,15 @@ async def _collect(self, command: AntaCommand) -> None: # noqa: C901 function commands.append({"cmd": "enable"}) commands += [{"cmd": command.command, "revision": command.revision}] if command.revision else [{"cmd": command.command}] try: - response: list[dict[str, Any]] = await self._session.cli( + response: list[dict[str, Any] | str] = await self._session.cli( commands=commands, ofmt=command.ofmt, version=command.version, - ) + req_id=f"ANTA-{collection_id}-{id(command)}" if collection_id else f"ANTA-{id(command)}", + ) # type: ignore[assignment] # multiple commands returns a list # Do not keep response of 'enable' command command.output = response[-1] - except aioeapi.EapiCommandError as e: + except asynceapi.EapiCommandError as e: # This block catches exceptions related to EOS issuing an error. command.errors = e.errors if command.requires_privileges: diff --git a/anta/models.py b/anta/models.py index b1a473439..c44f7e8b4 100644 --- a/anta/models.py +++ b/anta/models.py @@ -527,7 +527,7 @@ async def collect(self) -> None: """Collect outputs of all commands of this test class from the device of this test instance.""" try: if self.blocked is False: - await self.device.collect_commands(self.instance_commands) + await self.device.collect_commands(self.instance_commands, collection_id=self.name) except Exception as e: # pylint: disable=broad-exception-caught # device._collect() is user-defined code. # We need to catch everything if we want the AntaTest object diff --git a/asynceapi/__init__.py b/asynceapi/__init__.py new file mode 100644 index 000000000..d6586cf9b --- /dev/null +++ b/asynceapi/__init__.py @@ -0,0 +1,12 @@ +# Copyright (c) 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. +# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi + +"""Arista EOS eAPI asyncio client.""" + +from .config_session import SessionConfig +from .device import Device +from .errors import EapiCommandError + +__all__ = ["Device", "SessionConfig", "EapiCommandError"] diff --git a/asynceapi/aio_portcheck.py b/asynceapi/aio_portcheck.py new file mode 100644 index 000000000..79f4562fa --- /dev/null +++ b/asynceapi/aio_portcheck.py @@ -0,0 +1,58 @@ +# Copyright (c) 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. +# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi +"""Utility function to check if a port is open.""" +# ----------------------------------------------------------------------------- +# System Imports +# ----------------------------------------------------------------------------- + +from __future__ import annotations + +import asyncio +import socket +from typing import TYPE_CHECKING + +# ----------------------------------------------------------------------------- +# Public Imports +# ----------------------------------------------------------------------------- + +if TYPE_CHECKING: + from httpx import URL + +# ----------------------------------------------------------------------------- +# Exports +# ----------------------------------------------------------------------------- + +__all__ = ["port_check_url"] + +# ----------------------------------------------------------------------------- +# +# CODE BEGINS +# +# ----------------------------------------------------------------------------- + + +async def port_check_url(url: URL, timeout: int = 5) -> bool: + """ + Open the port designated by the URL given the timeout in seconds. + + If the port is available then return True; False otherwise. + + Parameters + ---------- + url: The URL that provides the target system + timeout: Time to await for the port to open in seconds + """ + port = url.port or socket.getservbyname(url.scheme) + + try: + wr: asyncio.StreamWriter + _, wr = await asyncio.wait_for(asyncio.open_connection(host=url.host, port=port), timeout=timeout) + + # MUST close if opened! + wr.close() + + except TimeoutError: + return False + return True diff --git a/asynceapi/config_session.py b/asynceapi/config_session.py new file mode 100644 index 000000000..4054f14bf --- /dev/null +++ b/asynceapi/config_session.py @@ -0,0 +1,289 @@ +# Copyright (c) 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. +# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi +"""asynceapi.SessionConfig definition.""" + +# ----------------------------------------------------------------------------- +# System Imports +# ----------------------------------------------------------------------------- +from __future__ import annotations + +import re +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from .device import Device + +# ----------------------------------------------------------------------------- +# Exports +# ----------------------------------------------------------------------------- + +__all__ = ["SessionConfig"] + +# ----------------------------------------------------------------------------- +# +# CODE BEGINS +# +# ----------------------------------------------------------------------------- + + +class SessionConfig: + """ + Send configuration to a device using the EOS session mechanism. + + This is the preferred way of managing configuration changes. + + Notes + ----- + This class definition is used by the parent Device class definition as + defined by `config_session`. A Caller can use the SessionConfig directly + as well, but it is not required. + """ + + CLI_CFG_FACTORY_RESET = "rollback clean-config" + + def __init__(self, device: Device, name: str) -> None: + """ + Create a new instance of SessionConfig. + + The session config instance bound + to the given device instance, and using the session `name`. + + Parameters + ---------- + device: The associated device instance + name: The name of the config session + """ + self._device = device + self._cli = device.cli + self._name = name + self._cli_config_session = f"configure session {self.name}" + + # ------------------------------------------------------------------------- + # properties for read-only attributes + # ------------------------------------------------------------------------- + + @property + def name(self) -> str: + """Return read-only session name attribute.""" + return self._name + + @property + def device(self) -> Device: + """Return read-only device instance attribute.""" + return self._device + + # ------------------------------------------------------------------------- + # Public Methods + # ------------------------------------------------------------------------- + + async def status_all(self) -> dict[str, Any]: + """ + Get the status of all the session config on the device. + + Run the following command on the device: + # show configuration sessions detail + + Returns + ------- + Dict object of native EOS eAPI response; see `status` method for + details. + + Examples + -------- + { + "maxSavedSessions": 1, + "maxOpenSessions": 5, + "sessions": { + "jeremy1": { + "instances": {}, + "state": "pending", + "commitUser": "", + "description": "" + }, + "ansible_167510439362": { + "instances": {}, + "state": "completed", + "commitUser": "joe.bob", + "description": "", + "completedTime": 1675104396.4500246 + } + } + } + """ + return await self._cli("show configuration sessions detail") # type: ignore[return-value] # json outformat returns dict[str, Any] + + async def status(self) -> dict[str, Any] | None: + """ + Get the status of a session config on the device. + + Run the following command on the device: + # show configuration sessions detail + + And return only the status dictionary for this session. If you want + all sessions, then use the `status_all` method. + + Returns + ------- + Dict instance of the session status. If the session does not exist, + then this method will return None. + + The native eAPI results from JSON output, see example: + + Examples + -------- + all results: + { + "maxSavedSessions": 1, + "maxOpenSessions": 5, + "sessions": { + "jeremy1": { + "instances": {}, + "state": "pending", + "commitUser": "", + "description": "" + }, + "ansible_167510439362": { + "instances": {}, + "state": "completed", + "commitUser": "joe.bob", + "description": "", + "completedTime": 1675104396.4500246 + } + } + } + + if the session name was 'jeremy1', then this method would return + { + "instances": {}, + "state": "pending", + "commitUser": "", + "description": "" + } + """ + res = await self.status_all() + return res["sessions"].get(self.name) + + async def push(self, content: list[str] | str, *, replace: bool = False) -> None: + """ + Send the configuration content to the device. + + If `replace` is true, then the command "rollback clean-config" is issued + before sending the configuration content. + + Parameters + ---------- + content: + The text configuration CLI commands, as a list of strings, that + will be sent to the device. If the parameter is a string, and not + a list, then split the string across linebreaks. In either case + any empty lines will be discarded before they are send to the + device. + replace: + When True, the content will replace the existing configuration + on the device. + """ + # if given s string, we need to break it up into individual command + # lines. + + if isinstance(content, str): + content = content.splitlines() + + # prepare the initial set of command to enter the config session and + # rollback clean if the `replace` argument is True. + + commands: list[str | dict[str, Any]] = [self._cli_config_session] + if replace: + commands.append(self.CLI_CFG_FACTORY_RESET) + + # add the Caller's commands, filtering out any blank lines. any command + # lines (!) are still included. + + commands.extend(filter(None, content)) + + await self._cli(commands=commands) + + async def commit(self, timer: str | None = None) -> None: + """ + Commit the session config. + + Run the following command on the device: + # configure session + # commit + + If the timer is specified, format is "hh:mm:ss", then a commit timer is + started. A second commit action must be made to confirm the config + session before the timer expires; otherwise the config-session is + automatically aborted. + """ + command = f"{self._cli_config_session} commit" + + if timer: + command += f" timer {timer}" + + await self._cli(command) + + async def abort(self) -> None: + """ + Abort the configuration session. + + Run the following command on the device: + # configure session abort + """ + await self._cli(f"{self._cli_config_session} abort") + + async def diff(self) -> str: + """ + Return the "diff" of the session config relative to the running config. + + Run the following command on the device: + # show session-config named diffs + + Returns + ------- + Return a string in diff-patch format. + + References + ---------- + * https://www.gnu.org/software/diffutils/manual/diffutils.txt + """ + return await self._cli(f"show session-config named {self.name} diffs", ofmt="text") # type: ignore[return-value] # text outformat returns str + + async def load_file(self, filename: str, *, replace: bool = False) -> None: + """ + Load the configuration from into the session configuration. + + If the replace parameter is True then the file contents will replace the existing session config (load-replace). + + Parameters + ---------- + filename: + The name of the configuration file. The caller is required to + specify the filesystem, for example, the + filename="flash:thisfile.cfg" + + replace: + When True, the contents of the file will completely replace the + session config for a load-replace behavior. + + Raises + ------ + If there are any issues with loading the configuration file then a + RuntimeError is raised with the error messages content. + """ + commands: list[str | dict[str, Any]] = [self._cli_config_session] + if replace: + commands.append(self.CLI_CFG_FACTORY_RESET) + + commands.append(f"copy {filename} session-config") + res: list[dict[str, Any]] = await self._cli(commands=commands) # type: ignore[assignment] # JSON outformat of multiple commands returns list[dict[str, Any]] + checks_re = re.compile(r"error|abort|invalid", flags=re.I) + messages = res[-1]["messages"] + + if any(map(checks_re.search, messages)): + raise RuntimeError("".join(messages)) + + async def write(self) -> None: + """Save the running config to the startup config by issuing the command "write" to the device.""" + await self._cli("write") diff --git a/asynceapi/device.py b/asynceapi/device.py new file mode 100644 index 000000000..04ec3ab7c --- /dev/null +++ b/asynceapi/device.py @@ -0,0 +1,291 @@ +# Copyright (c) 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. +# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi +"""asynceapi.Device definition.""" +# ----------------------------------------------------------------------------- +# System Imports +# ----------------------------------------------------------------------------- + +from __future__ import annotations + +from socket import getservbyname +from typing import TYPE_CHECKING, Any + +# ----------------------------------------------------------------------------- +# Public Imports +# ----------------------------------------------------------------------------- +import httpx + +# ----------------------------------------------------------------------------- +# Private Imports +# ----------------------------------------------------------------------------- +from .aio_portcheck import port_check_url +from .config_session import SessionConfig +from .errors import EapiCommandError + +if TYPE_CHECKING: + from collections.abc import Sequence + +# ----------------------------------------------------------------------------- +# Exports +# ----------------------------------------------------------------------------- + + +__all__ = ["Device"] + + +# ----------------------------------------------------------------------------- +# +# CODE BEGINS +# +# ----------------------------------------------------------------------------- + + +class Device(httpx.AsyncClient): + """ + Represent the async JSON-RPC client that communicates with an Arista EOS device. + + This class inherits directly from the + httpx.AsyncClient, so any initialization options can be passed directly. + """ + + auth = None + EAPI_OFMT_OPTIONS = ("json", "text") + EAPI_DEFAULT_OFMT = "json" + + def __init__( # noqa: PLR0913 # pylint: disable=too-many-arguments + self, + host: str | None = None, + username: str | None = None, + password: str | None = None, + proto: str = "https", + port: str | int | None = None, + **kwargs: Any, # noqa: ANN401 + ) -> None: + """ + Initialize the Device class. + + As a subclass to httpx.AsyncClient, the caller can provide any of those initializers. + Specific parameters for Device class are all optional and described below. + + Parameters + ---------- + host: The EOS target device, either hostname (DNS) or ipaddress. + username: The login user-name; requires the password parameter. + password: The login password; requires the username parameter. + proto: The protocol, http or https, to communicate eAPI with the device. + port: If not provided, the proto value is used to look up the associated + port (http=80, https=443). If provided, overrides the port used to + communite with the device. + + Other Parameters + ---------------- + base_url: str + If provided, the complete URL to the device eAPI endpoint. + + auth: + If provided, used as the httpx authorization initializer value. If + not provided, then username+password is assumed by the Caller and + used to create a BasicAuth instance. + """ + self.port = port or getservbyname(proto) + self.host = host + kwargs.setdefault("base_url", httpx.URL(f"{proto}://{self.host}:{self.port}")) + kwargs.setdefault("verify", False) + + if username and password: + self.auth = httpx.BasicAuth(username, password) + + kwargs.setdefault("auth", self.auth) + + super().__init__(**kwargs) + self.headers["Content-Type"] = "application/json-rpc" + + async def check_connection(self) -> bool: + """ + Check the target device to ensure that the eAPI port is open and accepting connections. + + It is recommended that a Caller checks the connection before involving cli commands, + but this step is not required. + + Returns + ------- + True when the device eAPI is accessible, False otherwise. + """ + return await port_check_url(self.base_url) + + async def cli( # noqa: PLR0913 # pylint: disable=too-many-arguments + self, + command: str | dict[str, Any] | None = None, + commands: Sequence[str | dict[str, Any]] | None = None, + ofmt: str | None = None, + version: int | str | None = "latest", + *, + suppress_error: bool = False, + auto_complete: bool = False, + expand_aliases: bool = False, + req_id: int | str | None = None, + ) -> list[dict[str, Any] | str] | dict[str, Any] | str | None: + """ + Execute one or more CLI commands. + + Parameters + ---------- + command: + A single command to execute; results in a single output response + commands: + A list of commands to execute; results in a list of output responses + ofmt: + Either 'json' or 'text'; indicates the output format for the CLI commands. + version: + By default the eAPI will use "version 1" for all API object models. + This driver will, by default, always set version to "latest" so + that the behavior matches the CLI of the device. The caller can + override the "latest" behavior by explicitly setting the version. + suppress_error: + When not False, then if the execution of the command would-have + raised an EapiCommandError, rather than raising this exception this + routine will return the value None. + + For example, if the following command had raised + EapiCommandError, now response would be set to None instead. + + response = dev.cli(..., suppress_error=True) + auto_complete: + Enabled/disables the command auto-compelete feature of the EAPI. Per the + documentation: + Allows users to use shorthand commands in eAPI calls. With this + parameter included a user can send 'sh ver' via eAPI to get the + output of 'show version'. + expand_aliases: + Enables/disables the command use of User defined alias. Per the + documentation: + Allowed users to provide the expandAliases parameter to eAPI + calls. This allows users to use aliased commands via the API. + For example if an alias is configured as 'sv' for 'show version' + then an API call with sv and the expandAliases parameter will + return the output of show version. + req_id: + A unique identifier that will be echoed back by the switch. May be a string or number. + + Returns + ------- + One or List of output responses, per the description above. + """ + if not any((command, commands)): + msg = "Required 'command' or 'commands'" + raise RuntimeError(msg) + + jsonrpc = self._jsonrpc_command( + commands=[command] if command else commands, ofmt=ofmt, version=version, auto_complete=auto_complete, expand_aliases=expand_aliases, req_id=req_id + ) + + try: + res = await self.jsonrpc_exec(jsonrpc) + return res[0] if command else res + except EapiCommandError: + if suppress_error: + return None + raise + + def _jsonrpc_command( # noqa: PLR0913 # pylint: disable=too-many-arguments + self, + commands: Sequence[str | dict[str, Any]] | None = None, + ofmt: str | None = None, + version: int | str | None = "latest", + *, + auto_complete: bool = False, + expand_aliases: bool = False, + req_id: int | str | None = None, + ) -> dict[str, Any]: + """Create the JSON-RPC command dictionary object.""" + cmd: dict[str, Any] = { + "jsonrpc": "2.0", + "method": "runCmds", + "params": { + "version": version, + "cmds": commands, + "format": ofmt or self.EAPI_DEFAULT_OFMT, + }, + "id": req_id or id(self), + } + if auto_complete is not None: + cmd["params"].update({"autoComplete": auto_complete}) + + if expand_aliases is not None: + cmd["params"].update({"expandAliases": expand_aliases}) + + return cmd + + async def jsonrpc_exec(self, jsonrpc: dict[str, Any]) -> list[dict[str, Any] | str]: + """ + Execute the JSON-RPC dictionary object. + + Parameters + ---------- + jsonrpc: + The JSON-RPC as created by the `meth`:_jsonrpc_command(). + + Raises + ------ + EapiCommandError + In the event that a command resulted in an error response. + + Returns + ------- + The list of command results; either dict or text depending on the + JSON-RPC format parameter. + """ + res = await self.post("/command-api", json=jsonrpc) + res.raise_for_status() + body = res.json() + + commands = jsonrpc["params"]["cmds"] + ofmt = jsonrpc["params"]["format"] + + get_output = (lambda _r: _r["output"]) if ofmt == "text" else (lambda _r: _r) + + # if there are no errors then return the list of command results. + if (err_data := body.get("error")) is None: + return [get_output(cmd_res) for cmd_res in body["result"]] + + # --------------------------------------------------------------------- + # if we are here, then there were some command errors. Raise a + # EapiCommandError exception with args (commands that failed, passed, + # not-executed). + # --------------------------------------------------------------------- + + # -------------------------- eAPI specification ---------------------- + # On an error, no result object is present, only an error object, which + # is guaranteed to have the following attributes: code, messages, and + # data. Similar to the result object in the successful response, the + # data object is a list of objects corresponding to the results of all + # commands up to, and including, the failed command. If there was a an + # error before any commands were executed (e.g. bad credentials), data + # will be empty. The last object in the data array will always + # correspond to the failed command. The command failure details are + # always stored in the errors array. + + cmd_data = err_data["data"] + len_data = len(cmd_data) + err_at = len_data - 1 + err_msg = err_data["message"] + + raise EapiCommandError( + passed=[get_output(cmd_data[cmd_i]) for cmd_i, cmd in enumerate(commands[:err_at])], + failed=commands[err_at]["cmd"], + errors=cmd_data[err_at]["errors"], + errmsg=err_msg, + not_exec=commands[err_at + 1 :], + ) + + def config_session(self, name: str) -> SessionConfig: + """ + return a SessionConfig instance bound to this device with the given session name. + + Parameters + ---------- + name: The config-session name + """ + return SessionConfig(self, name) diff --git a/asynceapi/errors.py b/asynceapi/errors.py new file mode 100644 index 000000000..614427a1a --- /dev/null +++ b/asynceapi/errors.py @@ -0,0 +1,42 @@ +# Copyright (c) 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. +# Initially written by Jeremy Schulman at https://github.com/jeremyschulman/aio-eapi +"""asynceapi module exceptions.""" + +from __future__ import annotations + +from typing import Any + +import httpx + + +class EapiCommandError(RuntimeError): + """ + Exception class for EAPI command errors. + + Attributes + ---------- + failed: the failed command + errmsg: a description of the failure reason + errors: the command failure details + passed: a list of command results of the commands that passed + not_exec: a list of commands that were not executed + """ + + def __init__(self, failed: str, errors: list[str], errmsg: str, passed: list[str | dict[str, Any]], not_exec: list[dict[str, Any]]) -> None: # noqa: PLR0913 # pylint: disable=too-many-arguments + """Initialize for the EapiCommandError exception.""" + self.failed = failed + self.errmsg = errmsg + self.errors = errors + self.passed = passed + self.not_exec = not_exec + super().__init__() + + def __str__(self) -> str: + """Return the error message associated with the exception.""" + return self.errmsg + + +# alias for exception during sending-receiving +EapiTransportError = httpx.HTTPStatusError diff --git a/docs/README.md b/docs/README.md index ce67bbeed..378867faf 100755 --- a/docs/README.md +++ b/docs/README.md @@ -85,4 +85,6 @@ Contributions are welcome. Please refer to the [contribution guide](contribution ## Credits +Thank you to [Jeremy Schulman](https://github.com/jeremyschulman) for [aio-eapi](https://github.com/jeremyschulman/aio-eapi/tree/main/aioeapi). + Thank you to [Angélique Phillipps](https://github.com/aphillipps), [Colin MacGiollaEáin](https://github.com/colinmacgiolla), [Khelil Sator](https://github.com/ksator), [Matthieu Tache](https://github.com/mtache), [Onur Gashi](https://github.com/onurgashi), [Paul Lavelle](https://github.com/paullavelle), [Guillaume Mulocher](https://github.com/gmuloc) and [Thomas Grimonet](https://github.com/titom73) for their contributions and guidances. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 596aad67c..f27de7aee 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -77,3 +77,14 @@ Example: ```bash ANTA_DEBUG=true anta -l DEBUG --log-file anta.log nrfu --enable --username username --password arista --inventory inventory.yml -c nrfu.yml text ``` + +### Troubleshooting on EOS + +ANTA is using a specific ID in eAPI requests towards EOS. This allows for easier eAPI requests debugging on the device using EOS configuration `trace CapiApp setting UwsgiRequestContext/4,CapiUwsgiServer/4` to set up CapiApp agent logs. + +Then, you can view agent logs using: +```bash +bash tail -f /var/log/agents/CapiApp-* + +2024-05-15 15:32:54.056166 1429 UwsgiRequestContext 4 request content b'{"jsonrpc": "2.0", "method": "runCmds", "params": {"version": "latest", "cmds": [{"cmd": "show ip route vrf default 10.255.0.3", "revision": 4}], "format": "json", "autoComplete": false, "expandAliases": false}, "id": "ANTA-VerifyRoutingTableEntry-132366530677328"}' +``` diff --git a/pyproject.toml b/pyproject.toml index fc05a4525..dc67db16d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,6 @@ description = "Arista Network Test Automation (ANTA) Framework" license = { file = "LICENSE" } dependencies = [ "aiocache>=0.12.2", - "aio-eapi>=0.6.3", "asyncssh>=2.13.2", "cvprac>=1.3.1", "eval-type-backport>=0.1.3", # Support newer typing features in older Python versions (required until Python 3.9 support is removed) @@ -28,6 +27,7 @@ dependencies = [ "PyYAML>=6.0", "requests>=2.31.0", "rich>=13.5.2,<14", + "httpx>=0.27.0" ] keywords = ["test", "anta", "Arista", "network", "automation", "networking", "devops", "netdevops"] classifiers = [ @@ -103,7 +103,7 @@ anta = "anta.cli:cli" # Tools ################################ [tool.setuptools.packages.find] -include = ["anta*"] +include = ["anta*", "asynceapi*"] namespaces = false ################################ @@ -177,10 +177,6 @@ filterwarnings = [ branch = true source = ["anta"] parallel = true -omit= [ - # omit aioeapi patch - "anta/aioeapi.py", -] [tool.coverage.report] # Regexes for lines to exclude from consideration @@ -312,7 +308,6 @@ exclude = [ "site-packages", "venv", ".github", - "aioeapi.py", # Remove this when https://github.com/jeremyschulman/aio-eapi/pull/13 is merged ] line-length = 165 diff --git a/tests/lib/fixture.py b/tests/lib/fixture.py index 43fb60a84..17943edc3 100644 --- a/tests/lib/fixture.py +++ b/tests/lib/fixture.py @@ -13,7 +13,7 @@ import pytest from click.testing import CliRunner, Result -from anta import aioeapi +import asynceapi from anta.cli.console import console from anta.device import AntaDevice, AsyncEOSDevice from anta.inventory import AntaInventory @@ -33,7 +33,7 @@ DEVICE_NAME = "pytest" COMMAND_OUTPUT = "retrieved" -MOCK_CLI_JSON: dict[str, aioeapi.EapiCommandError | dict[str, Any]] = { +MOCK_CLI_JSON: dict[str, asynceapi.EapiCommandError | dict[str, Any]] = { "show version": { "modelName": "DCS-7280CR3-32P4-F", "version": "4.31.1F", @@ -41,7 +41,7 @@ "enable": {}, "clear counters": {}, "clear hardware counter drop": {}, - "undefined": aioeapi.EapiCommandError( + "undefined": asynceapi.EapiCommandError( passed=[], failed="show version", errors=["Authorization denied for command 'show version'"], @@ -50,7 +50,7 @@ ), } -MOCK_CLI_TEXT: dict[str, aioeapi.EapiCommandError | str] = { +MOCK_CLI_TEXT: dict[str, asynceapi.EapiCommandError | str] = { "show version": "Arista cEOSLab", "bash timeout 10 ls -1t /mnt/flash/schedule/tech-support": "dummy_tech-support_2023-12-01.1115.log.gz\ndummy_tech-support_2023-12-01.1015.log.gz", "bash timeout 10 ls -1t /mnt/flash/schedule/tech-support | head -1": "dummy_tech-support_2023-12-01.1115.log.gz", @@ -62,7 +62,7 @@ def device(request: pytest.FixtureRequest) -> Iterator[AntaDevice]: """Return an AntaDevice instance with mocked abstract method.""" - def _collect(command: AntaCommand) -> None: + def _collect(command: AntaCommand, *args: Any, **kwargs: Any) -> None: # noqa: ARG001, ANN401 #pylint: disable=unused-argument command.output = COMMAND_OUTPUT kwargs = {"name": DEVICE_NAME, "hw_model": DEVICE_HW_MODEL} @@ -214,7 +214,7 @@ def get_output(command: str | dict[str, Any]) -> dict[str, Any]: for mock_cmd, output in mock_cli.items(): if command == mock_cmd: logger.info("Mocking command %s", mock_cmd) - if isinstance(output, aioeapi.EapiCommandError): + if isinstance(output, asynceapi.EapiCommandError): raise output return output message = f"Command '{command}' is not mocked" @@ -231,10 +231,10 @@ def get_output(command: str | dict[str, Any]) -> dict[str, Any]: logger.debug("Mock output %s", res) return res - # Patch aioeapi methods used by AsyncEOSDevice. See tests/units/test_device.py + # Patch asynceapi methods used by AsyncEOSDevice. See tests/units/test_device.py with ( - patch("aioeapi.device.Device.check_connection", return_value=True), - patch("aioeapi.device.Device.cli", side_effect=cli), + patch("asynceapi.device.Device.check_connection", return_value=True), + patch("asynceapi.device.Device.cli", side_effect=cli), patch("asyncssh.connect"), patch( "asyncssh.scp", diff --git a/tests/units/cli/exec/test_utils.py b/tests/units/cli/exec/test_utils.py index 455568bb8..ad1a78ab1 100644 --- a/tests/units/cli/exec/test_utils.py +++ b/tests/units/cli/exec/test_utils.py @@ -11,7 +11,7 @@ import pytest from anta.cli.exec.utils import ( - clear_counters_utils, + clear_counters, ) from anta.models import AntaCommand @@ -69,14 +69,14 @@ ), ], ) -async def test_clear_counters_utils( +async def test_clear_counters( caplog: pytest.LogCaptureFixture, test_inventory: AntaInventory, inventory_state: dict[str, Any], per_device_command_output: dict[str, Any], tags: set[str] | None, ) -> None: - """Test anta.cli.exec.utils.clear_counters_utils.""" + """Test anta.cli.exec.utils.clear_counters.""" async def mock_connect_inventory() -> None: """Mock connect_inventory coroutine.""" @@ -85,20 +85,19 @@ async def mock_connect_inventory() -> None: device.established = inventory_state[name].get("established", device.is_online) device.hw_model = inventory_state[name].get("hw_model", "dummy") - async def dummy_collect(self: AntaDevice, command: AntaCommand) -> None: + async def collect(self: AntaDevice, command: AntaCommand, *args: Any, **kwargs: Any) -> None: # noqa: ARG001, ANN401 #pylint: disable=unused-argument """Mock collect coroutine.""" command.output = per_device_command_output.get(self.name, "") # Need to patch the child device class with ( - patch("anta.device.AsyncEOSDevice.collect", side_effect=dummy_collect, autospec=True) as mocked_collect, + patch("anta.device.AsyncEOSDevice.collect", side_effect=collect, autospec=True) as mocked_collect, patch( "anta.inventory.AntaInventory.connect_inventory", side_effect=mock_connect_inventory, ) as mocked_connect_inventory, ): - mocked_collect.side_effect = dummy_collect - await clear_counters_utils(test_inventory, tags=tags) + await clear_counters(test_inventory, tags=tags) mocked_connect_inventory.assert_awaited_once() devices_established = test_inventory.get_inventory(established_only=True, tags=tags).devices @@ -117,6 +116,7 @@ async def dummy_collect(self: AntaDevice, command: AntaCommand) -> None: output=per_device_command_output.get(device.name, ""), errors=[], ), + collection_id=None, ), ) if device.hw_model not in ["cEOSLab", "vEOS-lab"]: @@ -130,6 +130,7 @@ async def dummy_collect(self: AntaDevice, command: AntaCommand) -> None: ofmt="json", output=per_device_command_output.get(device.name, ""), ), + collection_id=None, ), ) mocked_collect.assert_has_awaits(calls) diff --git a/tests/units/test_device.py b/tests/units/test_device.py index c901a3dd8..e8a0c5f86 100644 --- a/tests/units/test_device.py +++ b/tests/units/test_device.py @@ -15,7 +15,7 @@ from asyncssh import SSHClientConnection, SSHClientConnectionOptions from rich import print as rprint -from anta import aioeapi +import asynceapi from anta.device import AntaDevice, AsyncEOSDevice from anta.models import AntaCommand from tests.lib.fixture import COMMAND_OUTPUT @@ -128,7 +128,7 @@ "expected": False, }, ] -AIOEAPI_COLLECT_DATA: list[dict[str, Any]] = [ +ASYNCEAPI_COLLECT_DATA: list[dict[str, Any]] = [ { "name": "command", "device": {}, @@ -350,12 +350,12 @@ }, }, { - "name": "aioeapi.EapiCommandError", + "name": "asynceapi.EapiCommandError", "device": {}, "command": { "command": "show version", "patch_kwargs": { - "side_effect": aioeapi.EapiCommandError( + "side_effect": asynceapi.EapiCommandError( passed=[], failed="show version", errors=["Authorization denied for command 'show version'"], @@ -385,7 +385,7 @@ "expected": {"output": None, "errors": ["ConnectError: Cannot open port"]}, }, ] -AIOEAPI_COPY_DATA: list[dict[str, Any]] = [ +ASYNCEAPI_COPY_DATA: list[dict[str, Any]] = [ { "name": "from", "device": {}, @@ -509,12 +509,12 @@ "expected": {"is_online": True, "established": False, "hw_model": None}, }, { - "name": "aioeapi.EapiCommandError", + "name": "asynceapi.EapiCommandError", "device": {}, "patch_kwargs": ( {"return_value": True}, { - "side_effect": aioeapi.EapiCommandError( + "side_effect": asynceapi.EapiCommandError( passed=[], failed="show version", errors=["Authorization denied for command 'show version'"], @@ -644,7 +644,7 @@ async def test_collect(self, device: AntaDevice, command_data: dict[str, Any], e assert current_cached_data == COMMAND_OUTPUT assert device.cache.hit_miss_ratio["hits"] == 1 else: # command is not allowed to use cache - device._collect.assert_called_once_with(command=command) # type: ignore[attr-defined] # pylint: disable=protected-access + device._collect.assert_called_once_with(command=command, collection_id=None) # type: ignore[attr-defined] # pylint: disable=protected-access assert command.output == COMMAND_OUTPUT if expected_data["cache_hit"] is True: assert current_cached_data == cached_output @@ -652,7 +652,7 @@ async def test_collect(self, device: AntaDevice, command_data: dict[str, Any], e assert current_cached_data is None else: # device is disabled assert device.cache is None - device._collect.assert_called_once_with(command=command) # type: ignore[attr-defined] # pylint: disable=protected-access + device._collect.assert_called_once_with(command=command, collection_id=None) # type: ignore[attr-defined] # pylint: disable=protected-access @pytest.mark.parametrize(("device", "expected"), CACHE_STATS_DATA, indirect=["device"]) def test_cache_statistics(self, device: AntaDevice, expected: dict[str, Any] | None) -> None: @@ -705,9 +705,9 @@ async def test_refresh(self, async_device: AsyncEOSDevice, patch_kwargs: list[di """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() - async_device._session.check_connection.assert_called_once() + async_device._session.check_connection.assert_called_once() # type: ignore[attr-defined] # asynceapi.Device.check_connection is patched if expected["is_online"]: - async_device._session.cli.assert_called_once() + async_device._session.cli.assert_called_once() # type: ignore[attr-defined] # asynceapi.Device.cli is patched assert async_device.is_online == expected["is_online"] assert async_device.established == expected["established"] assert async_device.hw_model == expected["hw_model"] @@ -715,8 +715,8 @@ async def test_refresh(self, async_device: AsyncEOSDevice, patch_kwargs: list[di @pytest.mark.asyncio() @pytest.mark.parametrize( ("async_device", "command", "expected"), - ((d["device"], d["command"], d["expected"]) for d in AIOEAPI_COLLECT_DATA), - ids=generate_test_ids_list(AIOEAPI_COLLECT_DATA), + ((d["device"], d["command"], d["expected"]) for d in ASYNCEAPI_COLLECT_DATA), + ids=generate_test_ids_list(ASYNCEAPI_COLLECT_DATA), indirect=["async_device"], ) async def test__collect(self, async_device: AsyncEOSDevice, command: dict[str, Any], expected: dict[str, Any]) -> None: @@ -724,7 +724,8 @@ async def test__collect(self, async_device: AsyncEOSDevice, command: dict[str, A """Test AsyncEOSDevice._collect().""" cmd = AntaCommand(command=command["command"], revision=command["revision"]) if "revision" in command else AntaCommand(command=command["command"]) with patch.object(async_device._session, "cli", **command["patch_kwargs"]): - await async_device.collect(cmd) + collection_id = "pytest" + await async_device.collect(cmd, collection_id=collection_id) commands: list[dict[str, Any]] = [] if async_device.enable and async_device._enable_password is not None: commands.append( @@ -740,15 +741,15 @@ async def test__collect(self, async_device: AsyncEOSDevice, command: dict[str, A commands.append({"cmd": cmd.command, "revision": cmd.revision}) else: commands.append({"cmd": cmd.command}) - async_device._session.cli.assert_called_once_with(commands=commands, ofmt=cmd.ofmt, version=cmd.version) + 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 assert cmd.output == expected["output"] assert cmd.errors == expected["errors"] @pytest.mark.asyncio() @pytest.mark.parametrize( ("async_device", "copy"), - ((d["device"], d["copy"]) for d in AIOEAPI_COPY_DATA), - ids=generate_test_ids_list(AIOEAPI_COPY_DATA), + ((d["device"], d["copy"]) for d in ASYNCEAPI_COPY_DATA), + ids=generate_test_ids_list(ASYNCEAPI_COPY_DATA), indirect=["async_device"], ) async def test_copy(self, async_device: AsyncEOSDevice, copy: dict[str, Any]) -> None: