1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
# Copyright (c) 2023-2025 Arista Networks, Inc.
# Use of this source code is governed by the Apache License 2.0
# that can be found in the LICENSE file.
"""Tests for anta.cli.get.utils."""
from __future__ import annotations
from contextlib import AbstractContextManager, nullcontext
from pathlib import Path
from typing import Any, ClassVar
from unittest.mock import MagicMock, patch
import pytest
import requests
from anta.cli.get.utils import create_inventory_from_ansible, create_inventory_from_cvp, extract_examples, find_tests_in_module, get_cv_token, print_test
from anta.inventory import AntaInventory
from anta.models import AntaCommand, AntaTemplate, AntaTest
DATA_DIR: Path = Path(__file__).parents[3].resolve() / "data"
@pytest.mark.parametrize(
"verify_cert",
[
pytest.param(True, id="Verify cert enabled"),
pytest.param(False, id="Verify cert disabled"),
],
)
def test_get_cv_token(verify_cert: bool) -> None:
"""Test anta.get.utils.get_cv_token."""
ip_addr = "42.42.42.42"
username = "ant"
password = "formica"
with patch("anta.cli.get.utils.requests.request") as patched_request:
mocked_ret = MagicMock(autospec=requests.Response)
mocked_ret.json.return_value = {"sessionId": "simple"}
patched_request.return_value = mocked_ret
res = get_cv_token(ip_addr, username, password, verify_cert=verify_cert)
patched_request.assert_called_once_with(
"POST",
"https://42.42.42.42/cvpservice/login/authenticate.do",
headers={"Content-Type": "application/json", "Accept": "application/json"},
data='{"userId": "ant", "password": "formica"}',
verify=verify_cert,
timeout=10,
)
assert res == "simple"
# truncated inventories
CVP_INVENTORY = [
{
"hostname": "device1",
"containerName": "DC1",
"ipAddress": "10.20.20.97",
},
{
"hostname": "device2",
"containerName": "DC2",
"ipAddress": "10.20.20.98",
},
{
"hostname": "device3",
"containerName": "",
"ipAddress": "10.20.20.99",
},
]
@pytest.mark.parametrize(
"inventory",
[
pytest.param(CVP_INVENTORY, id="some container"),
pytest.param([], id="empty_inventory"),
],
)
def test_create_inventory_from_cvp(tmp_path: Path, inventory: list[dict[str, Any]]) -> None:
"""Test anta.get.utils.create_inventory_from_cvp."""
output = tmp_path / "output.yml"
create_inventory_from_cvp(inventory, output)
assert output.exists()
# This validate the file structure ;)
inv = AntaInventory.parse(str(output), "user", "pass")
assert len(inv) == len(inventory)
@pytest.mark.parametrize(
("inventory_filename", "ansible_group", "expected_raise", "expected_log", "expected_inv_length"),
[
pytest.param("ansible_inventory.yml", None, nullcontext(), None, 7, id="no group"),
pytest.param("ansible_inventory.yml", "ATD_LEAFS", nullcontext(), None, 4, id="group found"),
pytest.param(
"ansible_inventory.yml",
"DUMMY",
pytest.raises(ValueError, match=r"Group DUMMY not found in Ansible inventory"),
None,
0,
id="group not found",
),
pytest.param(
"empty_ansible_inventory.yml",
None,
pytest.raises(ValueError, match=r"Ansible inventory .* is empty"),
None,
0,
id="empty inventory",
),
pytest.param(
"wrong_ansible_inventory.yml",
None,
pytest.raises(ValueError, match=r"Could not parse"),
None,
0,
id="os error inventory",
),
pytest.param(
"ansible_inventory_with_vault.yml",
None,
pytest.raises(ValueError, match=r"Could not parse"),
"`anta get from-ansible` does not support inline vaulted variables",
0,
id="Vault variable in inventory",
),
pytest.param(
"ansible_inventory_unknown_yaml_tag.yml",
None,
pytest.raises(ValueError, match=r"Could not parse"),
None,
0,
id="Unknown YAML tag in inventory",
),
],
)
def test_create_inventory_from_ansible(
caplog: pytest.LogCaptureFixture,
tmp_path: Path,
inventory_filename: Path,
ansible_group: str | None,
expected_raise: AbstractContextManager[Exception],
expected_log: str | None,
expected_inv_length: int,
) -> None:
"""Test anta.get.utils.create_inventory_from_ansible."""
target_file = tmp_path / "inventory.yml"
inventory_file_path = DATA_DIR / inventory_filename
with expected_raise:
if ansible_group:
create_inventory_from_ansible(inventory_file_path, target_file, ansible_group)
else:
create_inventory_from_ansible(inventory_file_path, target_file)
assert target_file.exists()
inv = AntaInventory().parse(str(target_file), "user", "pass")
assert len(inv) == expected_inv_length
if not isinstance(expected_raise, nullcontext):
assert not target_file.exists()
if expected_log:
assert expected_log in caplog.text
class MissingExampleTest(AntaTest):
"""ANTA test that always succeed but has no Examples section."""
categories: ClassVar[list[str]] = []
commands: ClassVar[list[AntaCommand | AntaTemplate]] = []
@AntaTest.anta_test
def test(self) -> None:
"""Test function."""
self.result.is_success()
class EmptyExampleTest(AntaTest):
"""ANTA test that always succeed but has an empty Examples section.
Examples
--------
"""
# For the test purpose we want am empty section as custom tests could not be using ruff.
# ruff: noqa: D414
categories: ClassVar[list[str]] = []
commands: ClassVar[list[AntaCommand | AntaTemplate]] = []
@AntaTest.anta_test
def test(self) -> None:
"""Test function."""
self.result.is_success()
class TypoExampleTest(AntaTest):
"""ANTA test that always succeed but has a Typo in the test name in the example.
Notice capital P in TyPo below.
Examples
--------
```yaml
tests.units.cli.get.test_utils:
- TyPoExampleTest:
```
"""
# For the test purpose we want am empty section as custom tests could not be using ruff.
# ruff: noqa: D414
categories: ClassVar[list[str]] = []
commands: ClassVar[list[AntaCommand | AntaTemplate]] = []
@AntaTest.anta_test
def test(self) -> None:
"""Test function."""
self.result.is_success()
def test_find_tests_in_module() -> None:
"""Test find_tests_examples.
Only testing the failure scenarii not tested through test_commands.
TODO: expand
"""
with pytest.raises(ValueError, match=r"Error when importing"):
find_tests_in_module("blah", "UnusedTestName")
def test_print_test() -> None:
"""Test print_test."""
with pytest.raises(ValueError, match=r"Could not find the name of the test"):
print_test(TypoExampleTest)
with pytest.raises(LookupError, match=r"is missing an Example"):
print_test(MissingExampleTest)
with pytest.raises(LookupError, match=r"is missing an Example"):
print_test(EmptyExampleTest)
def test_extract_examples() -> None:
"""Test extract_examples.
Only testing the case where the 'Examples' is missing as everything else
is covered already in test_commands.py.
"""
assert MissingExampleTest.__doc__ is not None
assert EmptyExampleTest.__doc__ is not None
assert extract_examples(MissingExampleTest.__doc__) is None
assert extract_examples(EmptyExampleTest.__doc__) is None
|