Debian Codegraph @

tests/units/inventory/test_models.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 """ANTA Inventory models unit tests."""
5
6 from __future__ import annotations
7
8 import json
9 from pathlib import Path
10 from typing import TYPE_CHECKING, Any
11
12 import pytest
13 from pydantic import ValidationError
14 from yaml import safe_load
15
16 from anta.inventory.models import AntaInventoryHost, AntaInventoryInput, AntaInventoryNetwork, AntaInventoryRange
17
18 if TYPE_CHECKING:
19 from _pytest.mark.structures import ParameterSet
20
21 FILE_DIR: Path = Path(__file__).parents[2] / "data"
22
23 INVENTORY_HOST_VALID_PARAMS: list[ParameterSet] = [
24 pytest.param(None, "1.1.1.1", None, None, None, None, id="IPv4"),
25 pytest.param(None, "fe80::cc62:a9ff:feef:932a", None, None, None, None, id="IPv6"),
26 pytest.param(None, "1.1.1.1", 666, None, None, None, id="IPv4_with_port"),
27 pytest.param(None, "1.1.1.1", None, None, True, None, id="cache_enabled"),
28 pytest.param(None, "1.1.1.1", None, None, False, None, id="cache_disabled"),
29 pytest.param(None, "1.1.1.1", None, None, False, True, id="use_session_true"),
30 pytest.param(None, "1.1.1.1", None, None, False, False, id="use_session_false"),
31 ]
32
33 INVENTORY_HOST_INVALID_PARAMS: list[ParameterSet] = [
34 pytest.param(None, "1.1.1.1/32", None, None, False, None, id="IPv4_with_netmask"),
35 pytest.param(None, "1.1.1.1", 66666, None, False, None, id="IPv4_with_wrong_port"),
36 pytest.param(None, "fe80::cc62:a9ff:feef:932a/128", None, None, False, None, id="IPv6_with_netmask"),
37 pytest.param(None, "fe80::cc62:a9ff:feef:", None, None, False, None, id="invalid_IPv6"),
38 pytest.param(None, "@", None, None, False, None, id="special_char"),
39 pytest.param(None, "1.1.1.1", None, None, None, None, id="cache_is_None"),
40 ]
41
42 INVENTORY_NETWORK_VALID_PARAMS: list[ParameterSet] = [
43 pytest.param("1.1.1.0/24", None, None, None, id="IPv4_subnet"),
44 pytest.param("2001:db8::/32", None, None, None, id="IPv6_subnet"),
45 pytest.param("1.1.1.0/24", None, False, None, id="cache_enabled"),
46 pytest.param("1.1.1.0/24", None, True, None, id="cache_disabled"),
47 pytest.param("1.1.1.0/24", None, True, True, id="use_session_true"),
48 pytest.param("1.1.1.0/24", None, True, False, id="use_session_false"),
49 ]
50
51 INVENTORY_NETWORK_INVALID_PARAMS: list[ParameterSet] = [
52 pytest.param("1.1.1.0/17", None, False, None, id="IPv4_subnet"),
53 pytest.param("2001:db8::/16", None, False, None, id="IPv6_subnet"),
54 pytest.param("1.1.1.0/24", None, None, None, id="cache_is_None"),
55 ]
56
57 INVENTORY_RANGE_VALID_PARAMS: list[ParameterSet] = [
58 pytest.param("10.1.0.1", "10.1.0.10", None, None, None, id="IPv4_range"),
59 pytest.param("10.1.0.1", "10.1.0.10", None, True, None, id="cache_enabled"),
60 pytest.param("10.1.0.1", "10.1.0.10", None, False, None, id="cache_disabled"),
61 pytest.param("10.1.0.1", "10.1.0.10", None, False, True, id="use_session_true"),
62 pytest.param("10.1.0.1", "10.1.0.10", None, False, False, id="use_session_false"),
63 ]
64
65 INVENTORY_RANGE_INVALID_PARAMS: list[ParameterSet] = [
66 pytest.param("toto", "10.1.0.10", None, False, None, id="IPv4_range"),
67 pytest.param("10.1.0.1", "10.1.0.10", None, None, None, id="cache_is_None"),
68 ]
69
70 INVENTORY_MODEL_VALID = [
71 {
72 "name": "Valid_Host_Only",
73 "input": {"hosts": [{"host": "192.168.0.17"}, {"host": "192.168.0.2"}]},
74 "expected_result": "valid",
75 },
76 {
77 "name": "Valid_Networks_Only",
78 "input": {"networks": [{"network": "192.168.0.0/16"}, {"network": "192.168.1.0/24"}]},
79 "expected_result": "valid",
80 },
81 {
82 "name": "Valid_Ranges_Only",
83 "input": {
84 "ranges": [
85 {"start": "10.1.0.1", "end": "10.1.0.10"},
86 {"start": "10.2.0.1", "end": "10.2.1.10"},
87 ],
88 },
89 "expected_result": "valid",
90 },
91 ]
92
93 INVENTORY_MODEL_INVALID = [
94 {
95 "name": "Host_with_Invalid_entry",
96 "input": {"hosts": [{"host": "192.168.0.17"}, {"host": "192.168.0.2/32"}]},
97 "expected_result": "invalid",
98 },
99 ]
100
101
102 class TestAntaInventoryHost:
103 """Test anta.inventory.models.AntaInventoryHost."""
104
105 @pytest.mark.parametrize(("name", "host", "port", "tags", "disable_cache", "use_session_auth"), INVENTORY_HOST_VALID_PARAMS)
106 def test_valid(self, name: str, host: str, port: int, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
107 """Valid model parameters."""
108 params: dict[str, Any] = {"name": name, "host": host, "port": port, "tags": tags}
109 if disable_cache is not None:
110 params = params | {"disable_cache": disable_cache}
111
112 if use_session_auth is not None:
113 params = params | {"use_session_auth": use_session_auth}
114
115 inventory_host = AntaInventoryHost.model_validate(params)
116 assert host == str(inventory_host.host)
117 assert port == inventory_host.port
118 assert name == inventory_host.name
119 assert tags == inventory_host.tags
120 if disable_cache is None:
121 # Check cache default value
122 assert inventory_host.disable_cache is False
123 else:
124 assert inventory_host.disable_cache == disable_cache
125
126 if use_session_auth is None:
127 # Check use_session_auth default value
128 assert inventory_host.use_session_auth is False
129 else:
130 assert inventory_host.use_session_auth == use_session_auth
131
132 @pytest.mark.parametrize(("name", "host", "port", "tags", "disable_cache", "use_session_auth"), INVENTORY_HOST_INVALID_PARAMS)
133 def test_invalid(self, name: str, host: str, port: int, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
134 """Invalid model parameters."""
135 with pytest.raises(ValidationError):
136 AntaInventoryHost.model_validate(
137 {"name": name, "host": host, "port": port, "tags": tags, "disable_cache": disable_cache, "use_session_auth": use_session_auth}
138 )
139
140
141 class TestAntaInventoryNetwork:
142 """Test anta.inventory.models.AntaInventoryNetwork."""
143
144 @pytest.mark.parametrize(("network", "tags", "disable_cache", "use_session_auth"), INVENTORY_NETWORK_VALID_PARAMS)
145 def test_valid(self, network: str, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
146 """Valid model parameters."""
147 params: dict[str, Any] = {"network": network, "tags": tags}
148 if disable_cache is not None:
149 params = params | {"disable_cache": disable_cache}
150
151 if use_session_auth is not None:
152 params = params | {"use_session_auth": use_session_auth}
153
154 inventory_network = AntaInventoryNetwork.model_validate(params)
155 assert network == str(inventory_network.network)
156 assert tags == inventory_network.tags
157 if disable_cache is None:
158 # Check cache default value
159 assert inventory_network.disable_cache is False
160 else:
161 assert inventory_network.disable_cache == disable_cache
162
163 if use_session_auth is None:
164 # Check use_session_auth default value
165 assert inventory_network.use_session_auth is False
166 else:
167 assert inventory_network.use_session_auth == use_session_auth
168
169 @pytest.mark.parametrize(("network", "tags", "disable_cache", "use_session_auth"), INVENTORY_NETWORK_INVALID_PARAMS)
170 def test_invalid(self, network: str, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
171 """Invalid model parameters."""
172 with pytest.raises(ValidationError):
173 AntaInventoryNetwork.model_validate({"network": network, "tags": tags, "disable_cache": disable_cache, "use_session_auth": use_session_auth})
174
175
176 class TestAntaInventoryRange:
177 """Test anta.inventory.models.AntaInventoryRange."""
178
179 @pytest.mark.parametrize(("start", "end", "tags", "disable_cache", "use_session_auth"), INVENTORY_RANGE_VALID_PARAMS)
180 def test_valid(self, start: str, end: str, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
181 """Valid model parameters."""
182 params: dict[str, Any] = {"start": start, "end": end, "tags": tags}
183 if disable_cache is not None:
184 params = params | {"disable_cache": disable_cache}
185
186 if use_session_auth is not None:
187 params = params | {"use_session_auth": use_session_auth}
188
189 inventory_range = AntaInventoryRange.model_validate(params)
190 assert start == str(inventory_range.start)
191 assert end == str(inventory_range.end)
192 assert tags == inventory_range.tags
193 if disable_cache is None:
194 # Check cache default value
195 assert inventory_range.disable_cache is False
196 else:
197 assert inventory_range.disable_cache == disable_cache
198
199 if use_session_auth is None:
200 # Check use_session_auth default value
201 assert inventory_range.use_session_auth is False
202 else:
203 assert inventory_range.use_session_auth == use_session_auth
204
205 @pytest.mark.parametrize(("start", "end", "tags", "disable_cache", "use_session_auth"), INVENTORY_RANGE_INVALID_PARAMS)
206 def test_invalid(self, start: str, end: str, tags: set[str], disable_cache: bool | None, use_session_auth: bool | None) -> None:
207 """Invalid model parameters."""
208 with pytest.raises(ValidationError):
209 AntaInventoryRange.model_validate({"start": start, "end": end, "tags": tags, "disable_cache": disable_cache, "use_session_auth": use_session_auth})
210
211
212 class TestAntaInventoryInputs:
213 """Test anta.inventory.models.AntaInventoryInputs."""
214
215 def test_dump_to_json(self) -> None:
216 """Load a YAML file, dump it to JSON and verify it works."""
217 input_yml_path = FILE_DIR / "test_inventory_with_tags.yml"
218 expected_json_path = FILE_DIR / "test_inventory_with_tags.json"
219 with input_yml_path.open("r") as f:
220 data = safe_load(f)
221 anta_inventory_input = AntaInventoryInput(**data["anta_inventory"])
222
223 with expected_json_path.open("r") as f:
224 expected_data = json.load(f)
225
226 assert anta_inventory_input.to_json() == json.dumps(expected_data["anta_inventory"], indent=2)
227
228 def test_dump_to_yaml(self) -> None:
229 """Load a JSON file, dump it to YAML and verify it works."""
230 input_json_path = FILE_DIR / "test_inventory_medium.json"
231 expected_yml_path = FILE_DIR / "test_inventory_medium.yml"
232 with input_json_path.open("r") as f:
233 data = json.load(f)
234 anta_inventory_input = AntaInventoryInput(**data["anta_inventory"])
235
236 with expected_yml_path.open("r") as f:
237 expected_data = safe_load(f)
238
239 assert safe_load(anta_inventory_input.yaml()) == expected_data["anta_inventory"]