| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""See https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files.""" |
| 5 |
|
| 6 |
from collections.abc import Iterator |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
import respx |
| 11 |
|
| 12 |
from anta.device import AsyncEOSDevice |
| 13 |
from anta.inventory import AntaInventory |
| 14 |
|
| 15 |
DATA_DIR: Path = Path(__file__).parent.resolve() / "data" |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def inventory(request: pytest.FixtureRequest) -> Iterator[AntaInventory]: |
| 20 |
"""Generate an ANTA inventory.""" |
| 21 |
user = "admin" |
| 22 |
password = "password" |
| 23 |
params = request.param if hasattr(request, "param") else {} |
| 24 |
count = params.get("count", 1) |
| 25 |
disable_cache = params.get("disable_cache", True) |
| 26 |
reachable = params.get("reachable", True) |
| 27 |
if "filename" in params: |
| 28 |
inv = AntaInventory.parse(DATA_DIR / params["filename"], username=user, password=password, disable_cache=disable_cache) |
| 29 |
else: |
| 30 |
inv = AntaInventory() |
| 31 |
for i in range(count): |
| 32 |
inv.add_device( |
| 33 |
AsyncEOSDevice( |
| 34 |
host=f"device-{i}.anta.arista.com", |
| 35 |
username=user, |
| 36 |
password=password, |
| 37 |
name=f"device-{i}", |
| 38 |
disable_cache=disable_cache, |
| 39 |
) |
| 40 |
) |
| 41 |
if reachable: |
| 42 |
|
| 43 |
with respx.mock: |
| 44 |
respx.head(path="/command-api") |
| 45 |
respx.post(path="/command-api", headers={"Content-Type": "application/json-rpc"}, json__params__cmds__0__cmd="show version").respond( |
| 46 |
json={ |
| 47 |
"result": [ |
| 48 |
{ |
| 49 |
"modelName": "pytest", |
| 50 |
} |
| 51 |
], |
| 52 |
} |
| 53 |
) |
| 54 |
yield inv |
| 55 |
else: |
| 56 |
with respx.mock: |
| 57 |
respx.head(path="/command-api").respond(status_code=401) |
| 58 |
yield inv |