Debian Codegraph @

tests/units/input_models/test_interfaces.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 """Tests for anta.input_models.interfaces.py."""
5
6 # pylint: disable=C0302
7 from __future__ import annotations
8
9 from typing import TYPE_CHECKING
10
11 import pytest
12 from pydantic import ValidationError
13
14 from anta.input_models.interfaces import InterfaceState
15 from anta.tests.interfaces import (
16 VerifyInterfaceIPv4,
17 VerifyInterfacesBER,
18 VerifyInterfacesCounterDetails,
19 VerifyInterfacesEgressQueueDrops,
20 VerifyInterfacesOpticsReceivePower,
21 VerifyInterfacesOpticsTemperature,
22 VerifyInterfacesSpeed,
23 VerifyInterfacesStatus,
24 VerifyLACPInterfacesStatus,
25 )
26
27 if TYPE_CHECKING:
28 from anta.custom_types import EthernetInterface, Interface, ManagementInterface, PortChannelInterface, PositiveInteger
29
30
31 class TestInterfaceState:
32 """Test anta.input_models.interfaces.InterfaceState."""
33
34 # pylint: disable=too-few-public-methods
35
36 @pytest.mark.parametrize(
37 ("name", "portchannel", "expected"),
38 [
39 pytest.param("Ethernet1", "Port-Channel42", "Interface: Ethernet1 Port-Channel: Port-Channel42", id="with port-channel"),
40 pytest.param("Ethernet1", None, "Interface: Ethernet1", id="no port-channel"),
41 ],
42 )
43 def test_valid__str__(self, name: Interface, portchannel: PortChannelInterface | None, expected: str) -> None:
44 """Test InterfaceState __str__."""
45 assert str(InterfaceState(name=name, portchannel=portchannel)) == expected
46
47
48 class TestVerifyInterfacesStatusInput:
49 """Test anta.tests.interfaces.VerifyInterfacesStatus.Input."""
50
51 @pytest.mark.parametrize(
52 ("interfaces"),
53 [
54 pytest.param([{"name": "Ethernet1", "status": "up"}], id="valid"),
55 ],
56 )
57 def test_valid(self, interfaces: list[InterfaceState]) -> None:
58 """Test VerifyInterfacesStatus.Input valid inputs."""
59 VerifyInterfacesStatus.Input(interfaces=interfaces)
60
61 @pytest.mark.parametrize(
62 ("interfaces"),
63 [
64 pytest.param([{"name": "Ethernet1"}], id="invalid"),
65 ],
66 )
67 def test_invalid(self, interfaces: list[InterfaceState]) -> None:
68 """Test VerifyInterfacesStatus.Input invalid inputs."""
69 with pytest.raises(ValidationError):
70 VerifyInterfacesStatus.Input(interfaces=interfaces)
71
72
73 class TestVerifyLACPInterfacesStatusInput:
74 """Test anta.tests.interfaces.VerifyLACPInterfacesStatus.Input."""
75
76 @pytest.mark.parametrize(
77 ("interfaces"),
78 [
79 pytest.param([{"name": "Ethernet1", "portchannel": "Port-Channel100"}], id="valid"),
80 ],
81 )
82 def test_valid(self, interfaces: list[InterfaceState]) -> None:
83 """Test VerifyLACPInterfacesStatus.Input valid inputs."""
84 VerifyLACPInterfacesStatus.Input(interfaces=interfaces)
85
86 @pytest.mark.parametrize(
87 ("interfaces"),
88 [
89 pytest.param([{"name": "Ethernet1"}], id="invalid"),
90 ],
91 )
92 def test_invalid(self, interfaces: list[InterfaceState]) -> None:
93 """Test VerifyLACPInterfacesStatus.Input invalid inputs."""
94 with pytest.raises(ValidationError):
95 VerifyLACPInterfacesStatus.Input(interfaces=interfaces)
96
97
98 class TestVerifyInterfaceIPv4Input:
99 """Test anta.tests.interfaces.VerifyInterfaceIPv4.Input."""
100
101 @pytest.mark.parametrize(
102 ("interfaces"),
103 [
104 pytest.param([{"name": "Ethernet1", "primary_ip": "172.30.11.1/31"}], id="valid"),
105 ],
106 )
107 def test_valid(self, interfaces: list[InterfaceState]) -> None:
108 """Test VerifyInterfaceIPv4.Input valid inputs."""
109 VerifyInterfaceIPv4.Input(interfaces=interfaces)
110
111 @pytest.mark.parametrize(
112 ("interfaces"),
113 [
114 pytest.param([{"name": "Ethernet1"}], id="invalid-no-primary-ip"),
115 ],
116 )
117 def test_invalid(self, interfaces: list[InterfaceState]) -> None:
118 """Test VerifyInterfaceIPv4.Input invalid inputs."""
119 with pytest.raises(ValidationError):
120 VerifyInterfaceIPv4.Input(interfaces=interfaces)
121
122
123 class TestVerifyInterfacesSpeedInput:
124 """Test anta.tests.interfaces.VerifyInterfacesSpeed.Input."""
125
126 @pytest.mark.parametrize(
127 ("interfaces"),
128 [
129 pytest.param([{"name": "Ethernet1", "speed": 10}], id="valid-speed-is-given"),
130 ],
131 )
132 def test_valid(self, interfaces: list[InterfaceState]) -> None:
133 """Test VerifyInterfacesSpeed.Input valid inputs."""
134 VerifyInterfacesSpeed.Input(interfaces=interfaces)
135
136 @pytest.mark.parametrize(
137 ("interfaces"),
138 [
139 pytest.param([{"name": "Ethernet1"}], id="invalid-speed-is-not-given"),
140 ],
141 )
142 def test_invalid(self, interfaces: list[InterfaceState]) -> None:
143 """Test VerifyInterfacesSpeed.Input invalid inputs."""
144 with pytest.raises(ValidationError):
145 VerifyInterfacesSpeed.Input(interfaces=interfaces)
146
147
148 class TestVerifyPhysicalInterfacesCounterDetailsInput:
149 """Test anta.tests.interfaces.VerifyInterfacesCounterDetails.Input."""
150
151 @pytest.mark.parametrize(
152 ("interfaces", "ignored_interfaces", "link_status_changes_threshold"),
153 [
154 pytest.param(["Ethernet1"], ["Management1/1"], 10, id="valid-interfaces-is-given"),
155 ],
156 )
157 def test_valid(
158 self,
159 interfaces: list[EthernetInterface | ManagementInterface],
160 ignored_interfaces: list[EthernetInterface | ManagementInterface],
161 link_status_changes_threshold: PositiveInteger,
162 ) -> None:
163 """Test VerifyInterfacesCounterDetails.Input valid inputs."""
164 VerifyInterfacesCounterDetails.Input(
165 interfaces=interfaces, ignored_interfaces=ignored_interfaces, link_status_changes_threshold=link_status_changes_threshold
166 )
167
168 @pytest.mark.parametrize(
169 ("interfaces", "ignored_interfaces", "link_status_changes_threshold"),
170 [
171 pytest.param(["Ethernet1"], ["Ethernet1"], 10, id="invalid-interfaces"),
172 pytest.param(["et1"], ["Ethernet1"], 10, id="invalid-interfaces"),
173 ],
174 )
175 def test_invalid(
176 self,
177 interfaces: list[EthernetInterface | ManagementInterface],
178 ignored_interfaces: list[EthernetInterface | ManagementInterface],
179 link_status_changes_threshold: PositiveInteger,
180 ) -> None:
181 """Test VerifyInterfacesCounterDetails.Input invalid inputs."""
182 with pytest.raises(ValidationError):
183 VerifyInterfacesCounterDetails.Input(
184 interfaces=interfaces, ignored_interfaces=ignored_interfaces, link_status_changes_threshold=link_status_changes_threshold
185 )
186
187
188 class TestVerifytInterfacesBERInput:
189 """Test anta.tests.interfaces.VerifyInterfacesBER.Input."""
190
191 @pytest.mark.parametrize(
192 ("interfaces", "ignored_interfaces"),
193 [
194 pytest.param(["Ethernet1"], ["Ethernet1/1"], id="valid-interfaces-is-given"),
195 ],
196 )
197 def test_valid(self, interfaces: list[EthernetInterface], ignored_interfaces: list[EthernetInterface]) -> None:
198 """Test VerifyInterfacesBER.Input valid inputs."""
199 VerifyInterfacesBER.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
200
201 @pytest.mark.parametrize(
202 ("interfaces", "ignored_interfaces"),
203 [
204 pytest.param(["Ethernet1/1"], ["Ethernet1/1"], id="invalid-interfaces"),
205 pytest.param(["et1"], ["Ethernet1"], id="invalid-interfaces"),
206 ],
207 )
208 def test_invalid(self, interfaces: list[EthernetInterface], ignored_interfaces: list[EthernetInterface]) -> None:
209 """Test VerifyInterfacesBER.Input invalid inputs."""
210 with pytest.raises(ValidationError):
211 VerifyInterfacesBER.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
212
213
214 class TestVerifyInterfacesOpticalReceivePowerInput:
215 """Test anta.tests.interfaces.VerifyInterfacesOpticsReceivePower.Input."""
216
217 @pytest.mark.parametrize(
218 ("interfaces", "ignored_interfaces"),
219 [
220 pytest.param(["Ethernet1"], ["Ethernet1/1"], id="valid-interfaces-is-given"),
221 ],
222 )
223 def test_valid(self, interfaces: list[EthernetInterface], ignored_interfaces: list[EthernetInterface]) -> None:
224 """Test VerifyInterfacesOpticsReceivePower.Input valid inputs."""
225 VerifyInterfacesOpticsReceivePower.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
226
227 @pytest.mark.parametrize(
228 ("interfaces", "ignored_interfaces"),
229 [
230 pytest.param(["Ethernet1/1"], ["Ethernet1/1"], id="invalid-interfaces"),
231 pytest.param(["et1"], ["Ethernet1"], id="invalid-interfaces"),
232 ],
233 )
234 def test_invalid(self, interfaces: list[EthernetInterface], ignored_interfaces: list[EthernetInterface]) -> None:
235 """Test VerifyInterfacesOpticsReceivePower.Input invalid inputs."""
236 with pytest.raises(ValidationError):
237 VerifyInterfacesOpticsReceivePower.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
238
239
240 class TestVerifyInterfacesEgressQueueDropsInput:
241 """Test anta.tests.interfaces.VerifyInterfacesEgressQueueDrops.Input."""
242
243 @pytest.mark.parametrize(
244 ("interfaces", "ignored_interfaces"),
245 [
246 pytest.param(["Ethernet1"], ["Ethernet2"], id="valid-interfaces-is-given"),
247 ],
248 )
249 def test_valid(
250 self,
251 interfaces: list[EthernetInterface],
252 ignored_interfaces: list[EthernetInterface],
253 ) -> None:
254 """Test VerifyInterfacesEgressQueueDrops.Input valid inputs."""
255 VerifyInterfacesEgressQueueDrops.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
256
257 @pytest.mark.parametrize(
258 ("interfaces", "ignored_interfaces"),
259 [
260 pytest.param(["Ethernet1"], ["Ethernet1"], id="invalid-interfaces"),
261 pytest.param(["et1"], ["Ethernet1"], id="invalid-interfaces"),
262 ],
263 )
264 def test_invalid(
265 self,
266 interfaces: list[EthernetInterface],
267 ignored_interfaces: list[EthernetInterface],
268 ) -> None:
269 """Test VerifyInterfacesEgressQueueDrops.Input invalid inputs."""
270 with pytest.raises(ValidationError):
271 VerifyInterfacesEgressQueueDrops.Input(interfaces=interfaces, ignored_interfaces=ignored_interfaces)
272
273
274 class TestVerifyInterfacesOpticsTemperatureInput:
275 """Test anta.tests.interfaces.VerifyInterfacesOpticsTemperature.Input."""
276
277 @pytest.mark.parametrize(
278 ("interfaces", "ignored_interfaces", "max_transceiver_temperature"),
279 [
280 pytest.param(["Ethernet1"], ["Ethernet2"], 10.00, id="valid-interfaces-is-given"),
281 ],
282 )
283 def test_valid(
284 self,
285 interfaces: list[EthernetInterface],
286 ignored_interfaces: list[EthernetInterface],
287 max_transceiver_temperature: float,
288 ) -> None:
289 """Test VerifyInterfacesOpticsTemperature.Input valid inputs."""
290 VerifyInterfacesOpticsTemperature.Input(
291 interfaces=interfaces, ignored_interfaces=ignored_interfaces, max_transceiver_temperature=max_transceiver_temperature
292 )
293
294 @pytest.mark.parametrize(
295 ("interfaces", "ignored_interfaces", "max_transceiver_temperature"),
296 [
297 pytest.param(["Ethernet1"], ["Ethernet1"], 10.00, id="invalid-interfaces"),
298 pytest.param(["et1"], ["Ethernet1"], 10, id="invalid-interfaces"),
299 ],
300 )
301 def test_invalid(
302 self,
303 interfaces: list[EthernetInterface],
304 ignored_interfaces: list[EthernetInterface],
305 max_transceiver_temperature: float,
306 ) -> None:
307 """Test VerifyInterfacesOpticsTemperature.Input invalid inputs."""
308 with pytest.raises(ValidationError):
309 VerifyInterfacesOpticsTemperature.Input(
310 interfaces=interfaces, ignored_interfaces=ignored_interfaces, max_transceiver_temperature=max_transceiver_temperature
311 )