Debian Codegraph @

tests/units/conftest.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """See https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files."""
5
6 from __future__ import annotations
7
8 import os
9 from pathlib import Path
10 from typing import TYPE_CHECKING, Any
11 from unittest import mock
12 from unittest.mock import patch
13
14 import pytest
15 import yaml
16
17 from anta.device import AntaDevice, AsyncEOSDevice
18
19 if TYPE_CHECKING:
20 from collections.abc import Generator, Iterator
21
22 from anta.models import AntaCommand
23
24 DEVICE_HW_MODEL = "pytest"
25 DEVICE_NAME = "pytest"
26 COMMAND_OUTPUT = "retrieved"
27
28
29 @pytest.fixture(name="anta_env")
30 def anta_env_fixture() -> dict[str, str]:
31 """Return an ANTA environment for testing."""
32 return {
33 "ANTA_USERNAME": "anta",
34 "ANTA_PASSWORD": "formica",
35 "ANTA_INVENTORY": str(Path(__file__).parent.parent / "data" / "test_inventory_with_tags.yml"),
36 "ANTA_CATALOG": str(Path(__file__).parent.parent / "data" / "test_catalog.yml"),
37 }
38
39
40 @pytest.fixture
41 def device(request: pytest.FixtureRequest) -> Iterator[AntaDevice]:
42 """Return an AntaDevice instance with mocked abstract method."""
43
44 def _collect(command: AntaCommand, *args: Any, **kwargs: Any) -> None: # noqa: ARG001, ANN401
45 command.output = COMMAND_OUTPUT
46
47 kwargs = {"name": DEVICE_NAME, "hw_model": DEVICE_HW_MODEL}
48
49 if hasattr(request, "param"):
50 # Fixture is parametrized indirectly
51 kwargs.update(request.param)
52 with patch.object(AntaDevice, "__abstractmethods__", set()), patch("anta.device.AntaDevice._collect", side_effect=_collect):
53 # AntaDevice constructor does not have hw_model argument
54 hw_model = kwargs.pop("hw_model")
55 dev = AntaDevice(**kwargs) # type: ignore[abstract, arg-type]
56 dev.hw_model = hw_model
57 yield dev
58
59
60 @pytest.fixture
61 def async_device(request: pytest.FixtureRequest) -> AsyncEOSDevice:
62 """Return an AsyncEOSDevice instance."""
63 kwargs = {
64 "name": DEVICE_NAME,
65 "host": "42.42.42.42",
66 "username": "anta",
67 "password": "anta",
68 }
69
70 if hasattr(request, "param"):
71 # Fixture is parametrized indirectly
72 kwargs.update(request.param)
73 return AsyncEOSDevice(**kwargs) # type: ignore[arg-type]
74
75
76 @pytest.fixture
77 def yaml_file(request: pytest.FixtureRequest, tmp_path: Path) -> Path:
78 """Fixture to create a temporary YAML file and return the path.
79
80 Fixture is indirectly parametrized with the YAML file content.
81 """
82 assert hasattr(request, "param")
83 file = tmp_path / "test_file.yaml"
84 assert isinstance(request.param, dict)
85 content: dict[str, Any] = request.param
86 file.write_text(yaml.dump(content, allow_unicode=True))
87 return file
88
89
90 @pytest.fixture
91 def setenvvar(monkeypatch: pytest.MonkeyPatch) -> Generator[pytest.MonkeyPatch, None, None]:
92 """Fixture to set environment variables for testing."""
93 with mock.patch.dict(os.environ, clear=True):
94 yield monkeypatch