Debian Codegraph @

tests/units/anta_tests/test_configuration.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.tests.configuration.py."""
5
6 from __future__ import annotations
7
8 from typing import TYPE_CHECKING
9
10 from anta.result_manager.models import AntaTestStatus
11 from anta.tests.configuration import VerifyRunningConfig, VerifyRunningConfigDiffs, VerifyRunningConfigLines, VerifyZeroTouch
12 from tests.units.anta_tests import test
13
14 if TYPE_CHECKING:
15 from tests.units.anta_tests import AntaUnitTestData
16
17 DATA: AntaUnitTestData = {
18 (VerifyZeroTouch, "success"): {"eos_data": [{"mode": "disabled"}], "expected": {"result": AntaTestStatus.SUCCESS}},
19 (VerifyZeroTouch, "failure"): {
20 "eos_data": [{"mode": "enabled"}],
21 "expected": {"result": AntaTestStatus.FAILURE, "messages": ["ZTP is NOT disabled"]},
22 },
23 (VerifyRunningConfigDiffs, "success"): {"eos_data": [""], "expected": {"result": AntaTestStatus.SUCCESS}},
24 (VerifyRunningConfigDiffs, "failure"): {"eos_data": ["blah blah"], "expected": {"result": AntaTestStatus.FAILURE, "messages": ["blah blah"]}},
25 (VerifyRunningConfigLines, "success"): {"eos_data": ["blah blah"], "inputs": {"regex_patterns": ["blah"]}, "expected": {"result": AntaTestStatus.SUCCESS}},
26 (VerifyRunningConfigLines, "success-patterns"): {
27 "eos_data": ["enable password something\nsome other line"],
28 "inputs": {"regex_patterns": ["^enable password .*$", "^.*other line$"]},
29 "expected": {"result": AntaTestStatus.SUCCESS},
30 },
31 (VerifyRunningConfigLines, "failure"): {
32 "eos_data": ["enable password something\nsome other line"],
33 "inputs": {"regex_patterns": ["bla", "bleh"]},
34 "expected": {"result": AntaTestStatus.FAILURE, "messages": ["Following patterns were not found: 'bla', 'bleh"]},
35 },
36 (VerifyRunningConfig, "success"): {
37 "eos_data": [
38 {
39 "cmds": {
40 "no enable password": None,
41 "interface Ethernet1": {"cmds": {"no switchport": None}},
42 "interface Ethernet1.100": {"cmds": {"encapsulation dot1q vlan 100": None, "ip address 10.0.0.1/30": None}},
43 "router bgp 65101": {
44 "cmds": {
45 "router-id 10.111.254.1": None,
46 "maximum-paths 4 ecmp 4": None,
47 }
48 },
49 }
50 }
51 ],
52 "inputs": {
53 "rules": [
54 # Scenario: single section — two entries: regex+threshold (eq) and contains
55 {
56 "section": ["router bgp 65101"],
57 "entries": [
58 {
59 "match": "maximum-paths (\\d+)",
60 "mode": "regex",
61 "description": "BGP maximum ECMP paths",
62 "threshold": {"value": 4, "operator": "eq"},
63 },
64 {"match": "router-id", "mode": "contains", "description": "BGP router-id"},
65 ],
66 },
67 # Scenario: top-level exact+absent:true — "no enable password" ≠ "enable password", absent check passes
68 {
69 "entries": [
70 {
71 "match": "enable password",
72 "mode": "exact",
73 "absent": True,
74 "description": "No cleartext enable password",
75 }
76 ],
77 },
78 # Scenario: section without entry description — auto-generated label
79 {
80 "section": ["interface Ethernet1"],
81 "entries": [{"match": "no switchport"}],
82 },
83 # Scenario: sub-interface section — two entries verify encapsulation and IP address presence
84 {
85 "section": ["interface Ethernet1.100"],
86 "entries": [
87 {"match": "encapsulation dot1q vlan 100", "description": "802.1Q encapsulation"},
88 {"match": "ip address", "mode": "contains", "description": "IP address assignment"},
89 ],
90 },
91 ]
92 },
93 "expected": {
94 "result": AntaTestStatus.SUCCESS,
95 "atomic_results": [
96 {
97 "description": "BGP maximum ECMP paths - Captured value of 'maximum-paths (\\d+)' == 4 in 'router bgp 65101'",
98 "result": AntaTestStatus.SUCCESS,
99 "messages": [],
100 },
101 {
102 "description": "BGP router-id - Substring 'router-id' in 'router bgp 65101'",
103 "result": AntaTestStatus.SUCCESS,
104 "messages": [],
105 },
106 {
107 "description": "No cleartext enable password - Command 'enable password' in the running-config",
108 "result": AntaTestStatus.SUCCESS,
109 "messages": [],
110 },
111 {
112 "description": "Command 'no switchport' in 'interface Ethernet1'",
113 "result": AntaTestStatus.SUCCESS,
114 "messages": [],
115 },
116 {
117 "description": "802.1Q encapsulation - Command 'encapsulation dot1q vlan 100' in 'interface Ethernet1.100'",
118 "result": AntaTestStatus.SUCCESS,
119 "messages": [],
120 },
121 {
122 "description": "IP address assignment - Substring 'ip address' in 'interface Ethernet1.100'",
123 "result": AntaTestStatus.SUCCESS,
124 "messages": [],
125 },
126 ],
127 },
128 },
129 (VerifyRunningConfig, "success-wildcard-section"): {
130 "eos_data": [
131 {
132 "cmds": {
133 "interface Ethernet1": {"cmds": {"description WAN": None, "no switchport": None, "mtu 9214": None}},
134 "interface Ethernet2": {"cmds": {"description LAN": None, "no switchport": None, "mtu 9214": None}},
135 "interface Management0": {"cmds": {"description MGMT": None}},
136 }
137 }
138 ],
139 "inputs": {
140 "rules": [
141 # Scenario: regex wildcard + contains — Ethernet1 and Ethernet2 matched, Management0 excluded
142 {
143 "section": ["interface Ethernet\\d+"],
144 "entries": [{"match": "description", "mode": "contains", "description": "Interface description"}],
145 },
146 # Scenario: same wildcard section + regex threshold — each matched section validated independently
147 {
148 "section": ["interface Ethernet\\d+"],
149 "entries": [{"match": "mtu (\\d+)", "mode": "regex", "description": "Interface MTU", "threshold": {"value": 1500, "operator": "ge"}}],
150 },
151 # Scenario: $ anchor — re.fullmatch makes it redundant but harmless; Ethernet1 only, still wildcard
152 {
153 "section": ["interface Ethernet1$"],
154 "entries": [{"match": "no switchport", "description": "Routed port mode"}],
155 },
156 ]
157 },
158 "expected": {
159 "result": AntaTestStatus.SUCCESS,
160 "atomic_results": [
161 {
162 "description": "Interface description - Substring 'description' in 'interface Ethernet1'",
163 "result": AntaTestStatus.SUCCESS,
164 "messages": [],
165 },
166 {
167 "description": "Interface description - Substring 'description' in 'interface Ethernet2'",
168 "result": AntaTestStatus.SUCCESS,
169 "messages": [],
170 },
171 {
172 "description": "Interface MTU - Captured value of 'mtu (\\d+)' >= 1500 in 'interface Ethernet1'",
173 "result": AntaTestStatus.SUCCESS,
174 "messages": [],
175 },
176 {
177 "description": "Interface MTU - Captured value of 'mtu (\\d+)' >= 1500 in 'interface Ethernet2'",
178 "result": AntaTestStatus.SUCCESS,
179 "messages": [],
180 },
181 {
182 "description": "Routed port mode - Command 'no switchport' in 'interface Ethernet1'",
183 "result": AntaTestStatus.SUCCESS,
184 "messages": [],
185 },
186 ],
187 },
188 },
189 (VerifyRunningConfig, "success-nested-section"): {
190 "eos_data": [
191 {
192 "cmds": {
193 "router bgp 65101": {
194 "cmds": {
195 "router-id 10.111.254.1": None,
196 "vrf DEV": {"cmds": {"router-id 192.168.1.1": None}},
197 }
198 }
199 }
200 }
201 ],
202 "inputs": {
203 "rules": [
204 # Scenario: two-level exact section — navigates bgp > vrf DEV; validates router-id
205 {
206 "section": ["router bgp 65101", "vrf DEV"],
207 "entries": [{"match": "router-id", "mode": "contains", "description": "BGP VRF DEV router-id"}],
208 },
209 # Scenario: entries=[] — section existence check only; no atomics emitted on success
210 {
211 "section": ["router bgp 65101"],
212 "entries": [],
213 },
214 ]
215 },
216 "expected": {
217 "result": AntaTestStatus.SUCCESS,
218 "atomic_results": [
219 {
220 "description": "BGP VRF DEV router-id - Substring 'router-id' in 'router bgp 65101 > vrf DEV'",
221 "result": AntaTestStatus.SUCCESS,
222 "messages": [],
223 },
224 ],
225 },
226 },
227 (VerifyRunningConfig, "success-threshold-operators"): {
228 # all threshold operators passing — le, ge, eq
229 "eos_data": [
230 {
231 "cmds": {
232 "router bgp 65120": {
233 "cmds": {
234 "graceful-restart restart-time 300": None,
235 "maximum-paths 4": None,
236 }
237 },
238 "interface Ethernet1": {"cmds": {"mtu 9214": None}},
239 }
240 }
241 ],
242 "inputs": {
243 "rules": [
244 # Scenario: eq — captured restart-time 300 equals expected 300
245 {
246 "section": ["router bgp 65120"],
247 "entries": [{"match": "graceful-restart restart-time (\\d+)", "mode": "regex", "threshold": {"value": 300, "operator": "eq"}}],
248 },
249 # Scenario: le — captured maximum-paths 4 is within limit 8
250 {
251 "section": ["router bgp 65120"],
252 "entries": [{"match": "maximum-paths (\\d+)", "mode": "regex", "threshold": {"value": 8, "operator": "le"}}],
253 },
254 # Scenario: ge — captured MTU 9214 meets minimum 1500
255 {
256 "section": ["interface Ethernet1"],
257 "entries": [
258 {"match": "mtu (\\d+)", "mode": "regex", "description": "Uplink MTU", "threshold": {"value": 1500, "operator": "ge"}},
259 ],
260 },
261 ]
262 },
263 "expected": {
264 "result": AntaTestStatus.SUCCESS,
265 "atomic_results": [
266 {
267 "description": "Captured value of 'graceful-restart restart-time (\\d+)' == 300 in 'router bgp 65120'",
268 "result": AntaTestStatus.SUCCESS,
269 "messages": [],
270 },
271 {
272 "description": "Captured value of 'maximum-paths (\\d+)' <= 8 in 'router bgp 65120'",
273 "result": AntaTestStatus.SUCCESS,
274 "messages": [],
275 },
276 {
277 "description": "Uplink MTU - Captured value of 'mtu (\\d+)' >= 1500 in 'interface Ethernet1'",
278 "result": AntaTestStatus.SUCCESS,
279 "messages": [],
280 },
281 ],
282 },
283 },
284 (VerifyRunningConfig, "failure-section-not-found-empty-entries"): {
285 # entries=[] with section not found — failure atomic emitted even without entries to validate
286 "eos_data": [{"cmds": {"ip routing": None}}],
287 "inputs": {
288 "rules": [
289 {
290 "section": ["router ospf 1"],
291 "entries": [],
292 },
293 ]
294 },
295 "expected": {
296 "result": AntaTestStatus.FAILURE,
297 "messages": ["Section 'router ospf 1' in the running-config - Not found"],
298 "atomic_results": [
299 {"description": "Section 'router ospf 1' in the running-config", "result": AntaTestStatus.FAILURE, "messages": ["Not found"]},
300 ],
301 },
302 },
303 (VerifyRunningConfig, "failure-multiline-match"): {
304 # exact mode on a multiline key — a single character difference anywhere in the string fails the match
305 "eos_data": [
306 {
307 "cmds": {
308 "banner login\n Welcome to this Arista switch.\n Unauthorized access is prohibited.\n Contact NOC at [email protected].\nEOF": None,
309 "banner motd\n MOTD: Maintenance window scheduled Saturday 22:00-02:00 UTC.\nEOF": None,
310 }
311 }
312 ],
313 "inputs": {
314 "rules": [
315 {
316 "entries": [
317 {
318 "match": "banner login\n Welcome to this Arista switch.\n Unauthorized access is prohibited.\n "
319 "Contact NOC at [email protected].\nEOF",
320 }
321 ]
322 }
323 ]
324 },
325 "expected": {
326 "result": AntaTestStatus.FAILURE,
327 "messages": [
328 "Command 'banner login\n Welcome to this Arista switch.\n Unauthorized access is prohibited.\n"
329 " Contact NOC at [email protected].\nEOF' in the running-config - Not found"
330 ],
331 "atomic_results": [
332 {
333 "description": "Command 'banner login\n Welcome to this Arista switch.\n Unauthorized access is prohibited.\n"
334 " Contact NOC at [email protected].\nEOF' in the running-config",
335 "result": AntaTestStatus.FAILURE,
336 "messages": ["Not found"],
337 }
338 ],
339 },
340 },
341 (VerifyRunningConfig, "failure-mode-exact"): {
342 # exact fails (cipher keywords in wrong order; MAC string is prefix of actual command) and exact+absent:true inside a section
343 "eos_data": [
344 {
345 "cmds": {
346 "management ssh": {
347 "cmds": {
348 "cipher aes128-ctr arcfour aes256-cbc aes256-ctr": None,
349 "mac hmac-sha2-256 hmac-sha2-512 [email protected]": None,
350 "fips restrictions": None,
351 }
352 }
353 }
354 }
355 ],
356 "inputs": {
357 "rules": [
358 {
359 "section": ["management ssh"],
360 "entries": [
361 # Cipher keywords are in a different order than the EOS canonical output — exact equality fails
362 {"match": "cipher aes128-ctr aes256-ctr aes256-cbc"},
363 # Match is missing the trailing [email protected] that EOS appends — exact fails
364 {"match": "mac hmac-sha2-256 hmac-sha2-512"},
365 # fips restrictions in the section but the rule requires it to be absent
366 {"match": "fips restrictions", "mode": "exact", "absent": True},
367 ],
368 }
369 ]
370 },
371 "expected": {
372 "result": AntaTestStatus.FAILURE,
373 "messages": [
374 "Command 'cipher aes128-ctr aes256-ctr aes256-cbc' in 'management ssh' - Not found",
375 "Command 'mac hmac-sha2-256 hmac-sha2-512' in 'management ssh' - Not found",
376 "Command 'fips restrictions' in 'management ssh' - Expected to be absent",
377 ],
378 "atomic_results": [
379 {
380 "description": "Command 'cipher aes128-ctr aes256-ctr aes256-cbc' in 'management ssh'",
381 "result": AntaTestStatus.FAILURE,
382 "messages": ["Not found"],
383 },
384 {
385 "description": "Command 'mac hmac-sha2-256 hmac-sha2-512' in 'management ssh'",
386 "result": AntaTestStatus.FAILURE,
387 "messages": ["Not found"],
388 },
389 {
390 "description": "Command 'fips restrictions' in 'management ssh'",
391 "result": AntaTestStatus.FAILURE,
392 "messages": ["Expected to be absent"],
393 },
394 ],
395 },
396 },
397 (VerifyRunningConfig, "failure-top-level-entries"): {
398 "eos_data": [
399 {
400 "cmds": {
401 "aaa authorization exec default local": None,
402 "logging buffered 3000000 debugging": None,
403 }
404 }
405 ],
406 "inputs": {
407 "rules": [
408 # Scenario: two entries in one rule — exact not found; exact+absent found
409 {
410 "entries": [
411 # Must be present — not found
412 {"match": "aaa authentication login default local"},
413 # Must be absent — found
414 {"match": "aaa authorization exec default local", "mode": "exact", "absent": True},
415 ]
416 },
417 # Scenario: no description — auto-generated label
418 {
419 "entries": [{"match": "ntp server", "mode": "contains"}],
420 },
421 # Scenario: entry with description
422 {
423 "entries": [
424 {"match": "logging host", "mode": "contains", "description": "Syslog reachability"},
425 ],
426 },
427 ]
428 },
429 "expected": {
430 "result": AntaTestStatus.FAILURE,
431 "messages": [
432 "Command 'aaa authentication login default local' in the running-config - Not found",
433 "Command 'aaa authorization exec default local' in the running-config - Expected to be absent",
434 "Substring 'ntp server' in the running-config - Not found",
435 "Syslog reachability - Substring 'logging host' in the running-config - Not found",
436 ],
437 "atomic_results": [
438 {
439 "description": "Command 'aaa authentication login default local' in the running-config",
440 "result": AntaTestStatus.FAILURE,
441 "messages": ["Not found"],
442 },
443 {
444 "description": "Command 'aaa authorization exec default local' in the running-config",
445 "result": AntaTestStatus.FAILURE,
446 "messages": ["Expected to be absent"],
447 },
448 {
449 "description": "Substring 'ntp server' in the running-config",
450 "result": AntaTestStatus.FAILURE,
451 "messages": ["Not found"],
452 },
453 {
454 "description": "Syslog reachability - Substring 'logging host' in the running-config",
455 "result": AntaTestStatus.FAILURE,
456 "messages": ["Not found"],
457 },
458 ],
459 },
460 },
461 (VerifyRunningConfig, "failure-top-level-no-recurse"): {
462 # top-level rule (no section) must NOT match commands that exist only inside nested sections
463 "eos_data": [
464 {
465 "cmds": {
466 "ip routing": None,
467 "router bgp 65101": {"cmds": {"router-id 10.111.254.1": None}},
468 }
469 }
470 ],
471 "inputs": {
472 "rules": [
473 {
474 "entries": [{"match": "router-id", "mode": "contains", "description": "Router-id at top level"}],
475 }
476 ]
477 },
478 "expected": {
479 "result": AntaTestStatus.FAILURE,
480 "messages": ["Router-id at top level - Substring 'router-id' in the running-config - Not found"],
481 "atomic_results": [
482 {
483 "description": "Router-id at top level - Substring 'router-id' in the running-config",
484 "result": AntaTestStatus.FAILURE,
485 "messages": ["Not found"],
486 },
487 ],
488 },
489 },
490 (VerifyRunningConfig, "failure-regex-threshold-operators"): {
491 "eos_data": [
492 {
493 "cmds": {
494 "router bgp 65101": {
495 "cmds": {
496 "maximum-paths 8 ecmp 8": None,
497 "graceful-restart restart-time 200": None,
498 }
499 },
500 "interface Ethernet1": {"cmds": {"mtu 800": None}},
501 }
502 }
503 ],
504 "inputs": {
505 "rules": [
506 # Scenario: le operator — captured ecmp 8 exceeds limit 4; check fails
507 {
508 "section": ["router bgp 65101"],
509 "entries": [{"match": "ecmp (\\d+)", "mode": "regex", "threshold": {"value": 4, "operator": "le"}}],
510 },
511 # Scenario: eq operator — captured restart-time 200 ≠ expected 300; check fails
512 {
513 "section": ["router bgp 65101"],
514 "entries": [{"match": "graceful-restart restart-time (\\d+)", "mode": "regex", "threshold": {"value": 300, "operator": "eq"}}],
515 },
516 # Scenario: ge operator with description — captured MTU 800 below minimum 9000
517 {
518 "section": ["interface Ethernet1"],
519 "entries": [
520 {
521 "match": "mtu (\\d+)",
522 "mode": "regex",
523 "threshold": {"value": 9000, "operator": "ge"},
524 "description": "Uplink must have jumbo MTU",
525 }
526 ],
527 },
528 ]
529 },
530 "expected": {
531 "result": AntaTestStatus.FAILURE,
532 "messages": [
533 "Captured value of 'ecmp (\\d+)' <= 4 in 'router bgp 65101' - Actual: 8",
534 "Captured value of 'graceful-restart restart-time (\\d+)' == 300 in 'router bgp 65101' - Actual: 200",
535 "Uplink must have jumbo MTU - Captured value of 'mtu (\\d+)' >= 9000 in 'interface Ethernet1' - Actual: 800",
536 ],
537 "atomic_results": [
538 {
539 "description": "Captured value of 'ecmp (\\d+)' <= 4 in 'router bgp 65101'",
540 "result": AntaTestStatus.FAILURE,
541 "messages": ["Actual: 8"],
542 },
543 {
544 "description": "Captured value of 'graceful-restart restart-time (\\d+)' == 300 in 'router bgp 65101'",
545 "result": AntaTestStatus.FAILURE,
546 "messages": ["Actual: 200"],
547 },
548 {
549 "description": "Uplink must have jumbo MTU - Captured value of 'mtu (\\d+)' >= 9000 in 'interface Ethernet1'",
550 "result": AntaTestStatus.FAILURE,
551 "messages": ["Actual: 800"],
552 },
553 ],
554 },
555 },
556 (VerifyRunningConfig, "failure-regex-threshold-no-section"): {
557 # regex+threshold:le at top level — no section
558 "eos_data": [{"cmds": {"logging buffered 3000000 debugging": None}}],
559 "inputs": {
560 "rules": [
561 {
562 "entries": [{"match": "logging buffered (\\d+)", "mode": "regex", "threshold": {"value": 2000000, "operator": "le"}}],
563 }
564 ]
565 },
566 "expected": {
567 "result": AntaTestStatus.FAILURE,
568 "messages": ["Captured value of 'logging buffered (\\d+)' <= 2000000 in the running-config - Actual: 3000000"],
569 "atomic_results": [
570 {
571 "description": "Captured value of 'logging buffered (\\d+)' <= 2000000 in the running-config",
572 "result": AntaTestStatus.FAILURE,
573 "messages": ["Actual: 3000000"],
574 }
575 ],
576 },
577 },
578 (VerifyRunningConfig, "failure-section-regex-no-partial-match"): {
579 "eos_data": [{"cmds": {"interface Ethernet11": {"cmds": {"no switchport": None}}}}],
580 "inputs": {
581 "rules": [
582 # Scenario: no $ anchor — re.fullmatch still prevents partial match; Ethernet1 does not match Ethernet11
583 {
584 "section": ["interface Ethernet1"],
585 "entries": [{"match": "no switchport"}],
586 },
587 # Scenario: $ anchor — redundant with re.fullmatch but harmless; same rejection result
588 {
589 "section": ["interface Ethernet1$"],
590 "entries": [{"match": "no switchport"}],
591 },
592 ]
593 },
594 "expected": {
595 "result": AntaTestStatus.FAILURE,
596 "messages": [
597 "Section 'interface Ethernet1' in the running-config - Not found",
598 "Section 'interface Ethernet1$' in the running-config - Not found",
599 ],
600 "atomic_results": [
601 {"description": "Section 'interface Ethernet1' in the running-config", "result": AntaTestStatus.FAILURE, "messages": ["Not found"]},
602 {"description": "Section 'interface Ethernet1$' in the running-config", "result": AntaTestStatus.FAILURE, "messages": ["Not found"]},
603 ],
604 },
605 },
606 (VerifyRunningConfig, "failure-section-not-found"): {
607 "eos_data": [{"cmds": {"interface Ethernet1": {"cmds": {"no switchport": None}}}}],
608 "inputs": {
609 "rules": [
610 # Scenario: section not found — label is "Section '<pattern>'"
611 {
612 "section": ["interface Ethernet1.100"],
613 "entries": [{"match": "encapsulation dot1q vlan 100"}],
614 },
615 # Scenario: another section not found
616 {
617 "section": ["interface Ethernet99"],
618 "entries": [{"match": "no switchport"}],
619 },
620 ]
621 },
622 "expected": {
623 "result": AntaTestStatus.FAILURE,
624 "messages": [
625 "Section 'interface Ethernet1.100' in the running-config - Not found",
626 "Section 'interface Ethernet99' in the running-config - Not found",
627 ],
628 "atomic_results": [
629 {"description": "Section 'interface Ethernet1.100' in the running-config", "result": AntaTestStatus.FAILURE, "messages": ["Not found"]},
630 {"description": "Section 'interface Ethernet99' in the running-config", "result": AntaTestStatus.FAILURE, "messages": ["Not found"]},
631 ],
632 },
633 },
634 (VerifyRunningConfig, "failure-wildcard-section"): {
635 # wildcard section; only Ethernet2 fails — Ethernet1 passes, proving per-section atomicity
636 "eos_data": [
637 {
638 "cmds": {
639 "interface Ethernet1": {"cmds": {"description WAN": None, "no switchport": None}},
640 "interface Ethernet2": {"cmds": {"no switchport": None}},
641 }
642 }
643 ],
644 "inputs": {
645 "rules": [
646 {
647 "section": ["interface Ethernet\\d+"],
648 "entries": [{"match": "description", "mode": "contains", "description": "Interface description"}],
649 }
650 ]
651 },
652 "expected": {
653 "result": AntaTestStatus.FAILURE,
654 "messages": [
655 "Interface description - Substring 'description' in 'interface Ethernet2' - Not found",
656 ],
657 "atomic_results": [
658 {
659 "description": "Interface description - Substring 'description' in 'interface Ethernet1'",
660 "result": AntaTestStatus.SUCCESS,
661 "messages": [],
662 },
663 {
664 "description": "Interface description - Substring 'description' in 'interface Ethernet2'",
665 "result": AntaTestStatus.FAILURE,
666 "messages": ["Not found"],
667 },
668 ],
669 },
670 },
671 (VerifyRunningConfig, "success-absent-modes"): {
672 "eos_data": [
673 {
674 "cmds": {
675 "ip routing": None,
676 "username admin privilege 15": None,
677 "logging host 192.0.2.30 vrf MGMT": None,
678 "ntp server vrf MGMT 192.0.2.10 prefer": None,
679 }
680 }
681 ],
682 "inputs": {
683 "rules": [
684 # Scenario: contains+absent:true — prohibited prefix not present in config; check passes
685 {
686 "entries": [{"match": "snmp-server community", "mode": "contains", "absent": True, "description": "No SNMP community strings"}],
687 },
688 # Scenario: regex+absent:true — plaintext secret pattern not matched; check passes
689 {
690 "entries": [{"match": "username .+ secret 0 ", "mode": "regex", "absent": True, "description": "No plaintext secrets"}],
691 },
692 # Scenario: regex+absent:false — at least one command matches the logging host pattern
693 {
694 "entries": [{"match": "logging host \\S+", "mode": "regex", "description": "Remote syslog configured"}],
695 },
696 # Scenario: contains+absent:false — ntp server substring found in a command
697 {
698 "entries": [{"match": "ntp server", "mode": "contains", "description": "NTP server configured"}],
699 },
700 ]
701 },
702 "expected": {
703 "result": AntaTestStatus.SUCCESS,
704 "atomic_results": [
705 {
706 "description": "No SNMP community strings - Substring 'snmp-server community' in the running-config",
707 "result": AntaTestStatus.SUCCESS,
708 "messages": [],
709 },
710 {
711 "description": "No plaintext secrets - Pattern 'username .+ secret 0 ' in the running-config",
712 "result": AntaTestStatus.SUCCESS,
713 "messages": [],
714 },
715 {
716 "description": "Remote syslog configured - Pattern 'logging host \\S+' in the running-config",
717 "result": AntaTestStatus.SUCCESS,
718 "messages": [],
719 },
720 {
721 "description": "NTP server configured - Substring 'ntp server' in the running-config",
722 "result": AntaTestStatus.SUCCESS,
723 "messages": [],
724 },
725 ],
726 },
727 },
728 (VerifyRunningConfig, "failure-absent-prohibited"): {
729 "eos_data": [
730 {
731 "cmds": {
732 "snmp-server community public ro": None,
733 "aaa authorization exec default local": None,
734 "username admin secret 0 plaintext123": None,
735 "ip routing": None,
736 }
737 }
738 ],
739 "inputs": {
740 "rules": [
741 # Scenario: contains+absent:true — "snmp-server community" prefix found; check fails
742 {
743 "entries": [{"match": "snmp-server community", "mode": "contains", "absent": True, "description": "No SNMP community strings"}],
744 },
745 # Scenario: contains mid-command — "authorization exec" is found inside the full aaa line; check fails
746 {
747 "entries": [{"match": "authorization exec", "mode": "contains", "absent": True, "description": "No AAA exec authorization"}],
748 },
749 # Scenario: regex+absent:true — plaintext secret 0 pattern matched; check fails
750 {
751 "entries": [{"match": "username .+ secret 0 ", "mode": "regex", "absent": True, "description": "No plaintext secrets"}],
752 },
753 ]
754 },
755 "expected": {
756 "result": AntaTestStatus.FAILURE,
757 "messages": [
758 "No SNMP community strings - Substring 'snmp-server community' in the running-config - Expected to be absent",
759 "No AAA exec authorization - Substring 'authorization exec' in the running-config - Expected to be absent",
760 "No plaintext secrets - Pattern 'username .+ secret 0 ' in the running-config - Expected to not match",
761 ],
762 "atomic_results": [
763 {
764 "description": "No SNMP community strings - Substring 'snmp-server community' in the running-config",
765 "result": AntaTestStatus.FAILURE,
766 "messages": ["Expected to be absent"],
767 },
768 {
769 "description": "No AAA exec authorization - Substring 'authorization exec' in the running-config",
770 "result": AntaTestStatus.FAILURE,
771 "messages": ["Expected to be absent"],
772 },
773 {
774 "description": "No plaintext secrets - Pattern 'username .+ secret 0 ' in the running-config",
775 "result": AntaTestStatus.FAILURE,
776 "messages": ["Expected to not match"],
777 },
778 ],
779 },
780 },
781 (VerifyRunningConfig, "failure-regex-threshold-non-numeric-capture"): {
782 # regex matches but capture group is non-numeric — clean failure instead of ValueError crash
783 "eos_data": [{"cmds": {"description Uplink": None}}],
784 "inputs": {
785 "rules": [
786 {
787 "entries": [{"match": "description (\\S+)", "mode": "regex", "threshold": {"value": 5, "operator": "le"}}],
788 }
789 ]
790 },
791 "expected": {
792 "result": AntaTestStatus.FAILURE,
793 "messages": ["Captured value of 'description (\\S+)' <= 5 in the running-config - Captured value 'Uplink' is not an integer"],
794 "atomic_results": [
795 {
796 "description": "Captured value of 'description (\\S+)' <= 5 in the running-config",
797 "result": AntaTestStatus.FAILURE,
798 "messages": ["Captured value 'Uplink' is not an integer"],
799 }
800 ],
801 },
802 },
803 (VerifyRunningConfig, "success-nested-wildcard-section"): {
804 "eos_data": [
805 {
806 "cmds": {
807 "router bgp 65101": {
808 "cmds": {
809 "vrf PROD": {"cmds": {"router-id 10.0.1.1": None}},
810 "vrf DEV": {"cmds": {"router-id 10.0.2.1": None}},
811 }
812 }
813 }
814 }
815 ],
816 "inputs": {
817 "rules": [
818 # Scenario: regex+regex — both VRFs matched; PROD and DEV each get their own atomic
819 {
820 "section": ["router bgp \\d+", "vrf .*"],
821 "entries": [{"match": "router-id", "mode": "contains", "description": "BGP VRF router-id"}],
822 },
823 # Scenario: regex+exact — only DEV matched at second level; one atomic result
824 {
825 "section": ["router bgp \\d+", "vrf DEV"],
826 "entries": [{"match": "router-id", "mode": "contains", "description": "BGP DEV VRF router-id"}],
827 },
828 ]
829 },
830 "expected": {
831 "result": AntaTestStatus.SUCCESS,
832 "atomic_results": [
833 {
834 "description": "BGP VRF router-id - Substring 'router-id' in 'router bgp 65101 > vrf PROD'",
835 "result": AntaTestStatus.SUCCESS,
836 "messages": [],
837 },
838 {
839 "description": "BGP VRF router-id - Substring 'router-id' in 'router bgp 65101 > vrf DEV'",
840 "result": AntaTestStatus.SUCCESS,
841 "messages": [],
842 },
843 {
844 "description": "BGP DEV VRF router-id - Substring 'router-id' in 'router bgp 65101 > vrf DEV'",
845 "result": AntaTestStatus.SUCCESS,
846 "messages": [],
847 },
848 ],
849 },
850 },
851 (VerifyRunningConfig, "failure-nested-section-child-not-found"): {
852 # first level matches but second level doesn't exist
853 "eos_data": [
854 {
855 "cmds": {
856 "router bgp 65101": {
857 "cmds": {
858 "router-id 10.111.254.1": None,
859 "vrf DEV": {"cmds": {"router-id 10.0.2.1": None}},
860 }
861 }
862 }
863 }
864 ],
865 "inputs": {
866 "rules": [
867 {
868 "section": ["router bgp 65101", "vrf NONEXISTENT"],
869 "entries": [{"match": "router-id", "mode": "contains"}],
870 },
871 ]
872 },
873 "expected": {
874 "result": AntaTestStatus.FAILURE,
875 "messages": ["Section 'router bgp 65101 > vrf NONEXISTENT' in the running-config - Not found"],
876 "atomic_results": [
877 {
878 "description": "Section 'router bgp 65101 > vrf NONEXISTENT' in the running-config",
879 "result": AntaTestStatus.FAILURE,
880 "messages": ["Not found"],
881 },
882 ],
883 },
884 },
885 (VerifyRunningConfig, "success-absent-in-section"): {
886 # absent entries scoped to a section — prohibited commands not present in the section
887 "eos_data": [
888 {
889 "cmds": {
890 "management ssh": {
891 "cmds": {
892 "cipher aes256-ctr aes256-cbc": None,
893 }
894 }
895 }
896 }
897 ],
898 "inputs": {
899 "rules": [
900 {
901 "section": ["management ssh"],
902 "entries": [
903 {"match": "fips restrictions", "absent": True, "description": "No FIPS mode"},
904 {"match": "telnet", "mode": "contains", "absent": True, "description": "No telnet reference"},
905 {"match": "password .+ 0 ", "mode": "regex", "absent": True, "description": "No plaintext passwords"},
906 ],
907 },
908 ]
909 },
910 "expected": {
911 "result": AntaTestStatus.SUCCESS,
912 "atomic_results": [
913 {
914 "description": "No FIPS mode - Command 'fips restrictions' in 'management ssh'",
915 "result": AntaTestStatus.SUCCESS,
916 "messages": [],
917 },
918 {
919 "description": "No telnet reference - Substring 'telnet' in 'management ssh'",
920 "result": AntaTestStatus.SUCCESS,
921 "messages": [],
922 },
923 {
924 "description": "No plaintext passwords - Pattern 'password .+ 0 ' in 'management ssh'",
925 "result": AntaTestStatus.SUCCESS,
926 "messages": [],
927 },
928 ],
929 },
930 },
931 (VerifyRunningConfig, "success-threshold-default-operator"): {
932 # threshold without explicit operator — defaults to eq
933 "eos_data": [
934 {
935 "cmds": {
936 "interface Ethernet1": {"cmds": {"mtu 9214": None}},
937 }
938 }
939 ],
940 "inputs": {
941 "rules": [
942 {
943 "section": ["interface Ethernet1"],
944 "entries": [{"match": "mtu (\\d+)", "mode": "regex", "description": "MTU check", "threshold": {"value": 9214}}],
945 },
946 ]
947 },
948 "expected": {
949 "result": AntaTestStatus.SUCCESS,
950 "atomic_results": [
951 {
952 "description": "MTU check - Captured value of 'mtu (\\d+)' == 9214 in 'interface Ethernet1'",
953 "result": AntaTestStatus.SUCCESS,
954 "messages": [],
955 },
956 ],
957 },
958 },
959 (VerifyRunningConfig, "failure-wildcard-section-no-match"): {
960 # wildcard regex pattern matches zero sections — reported as section not found
961 "eos_data": [
962 {
963 "cmds": {
964 "interface Ethernet1": {"cmds": {"no switchport": None}},
965 }
966 }
967 ],
968 "inputs": {
969 "rules": [
970 {
971 "section": ["interface Loopback\\d+"],
972 "entries": [{"match": "description", "mode": "contains", "description": "Loopback description"}],
973 },
974 ]
975 },
976 "expected": {
977 "result": AntaTestStatus.FAILURE,
978 "messages": ["Section 'interface Loopback\\d+' in the running-config - Not found"],
979 "atomic_results": [
980 {
981 "description": "Section 'interface Loopback\\d+' in the running-config",
982 "result": AntaTestStatus.FAILURE,
983 "messages": ["Not found"],
984 },
985 ],
986 },
987 },
988 (VerifyRunningConfig, "failure-wildcard-threshold-mixed"): {
989 # wildcard section + threshold — Ethernet1 passes (9214 >= 9000) but Ethernet2 fails (1500 < 9000)
990 "eos_data": [
991 {
992 "cmds": {
993 "interface Ethernet1": {"cmds": {"mtu 9214": None}},
994 "interface Ethernet2": {"cmds": {"mtu 1500": None}},
995 }
996 }
997 ],
998 "inputs": {
999 "rules": [
1000 {
1001 "section": ["interface Ethernet\\d+"],
1002 "entries": [
1003 {
1004 "match": "mtu (\\d+)",
1005 "mode": "regex",
1006 "description": "Jumbo MTU",
1007 "threshold": {"value": 9000, "operator": "ge"},
1008 }
1009 ],
1010 },
1011 ]
1012 },
1013 "expected": {
1014 "result": AntaTestStatus.FAILURE,
1015 "messages": ["Jumbo MTU - Captured value of 'mtu (\\d+)' >= 9000 in 'interface Ethernet2' - Actual: 1500"],
1016 "atomic_results": [
1017 {
1018 "description": "Jumbo MTU - Captured value of 'mtu (\\d+)' >= 9000 in 'interface Ethernet1'",
1019 "result": AntaTestStatus.SUCCESS,
1020 "messages": [],
1021 },
1022 {
1023 "description": "Jumbo MTU - Captured value of 'mtu (\\d+)' >= 9000 in 'interface Ethernet2'",
1024 "result": AntaTestStatus.FAILURE,
1025 "messages": ["Actual: 1500"],
1026 },
1027 ],
1028 },
1029 },
1030 }