Debian Codegraph @

tests/units/input_models/test_configuration.py

raw symbols SCIP
1 # Copyright (c) 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.configuration."""
5
6 from __future__ import annotations
7
8 from typing import Any
9
10 import pytest
11 from pydantic import ValidationError
12
13 from anta.input_models.configuration import ConfigEntry, ConfigRule
14
15
16 class TestConfigEntry:
17 """Tests for anta.input_models.configuration.ConfigEntry."""
18
19 @pytest.mark.parametrize(
20 "model_params",
21 [
22 # threshold is valid when mode=regex and match contains a capture group
23 pytest.param(
24 {
25 "match": "mtu (\\d+)",
26 "mode": "regex",
27 "threshold": {"value": 1500, "operator": "ge"},
28 },
29 id="threshold-with-regex-mode",
30 ),
31 # mode defaults to exact; no threshold — always valid
32 pytest.param(
33 {"match": "no shutdown", "mode": "exact"},
34 id="no-threshold-exact",
35 ),
36 # contains mode without threshold — always valid
37 pytest.param(
38 {"match": "router-id", "mode": "contains"},
39 id="no-threshold-contains",
40 ),
41 # regex mode without threshold — valid even without a capture group
42 pytest.param(
43 {"match": "logging host \\S+", "mode": "regex"},
44 id="no-threshold-regex",
45 ),
46 ],
47 )
48 def test_valid(self, model_params: dict[str, Any]) -> None:
49 """Test ConfigEntry valid inputs."""
50 ConfigEntry.model_validate(model_params)
51
52 @pytest.mark.parametrize(
53 "model_params",
54 [
55 # threshold requires mode=regex; exact is rejected
56 pytest.param(
57 {"match": "mtu", "mode": "exact", "threshold": {"value": 1500, "operator": "ge"}},
58 id="threshold-with-exact-mode",
59 ),
60 # threshold requires mode=regex; contains is rejected
61 pytest.param(
62 {
63 "match": "mtu",
64 "mode": "contains",
65 "threshold": {"value": 1500, "operator": "ge"},
66 },
67 id="threshold-with-contains-mode",
68 ),
69 # threshold requires a capture group in the pattern; bare \\d+ has none
70 pytest.param(
71 {
72 "match": "mtu \\d+",
73 "mode": "regex",
74 "threshold": {"value": 1500, "operator": "ge"},
75 },
76 id="threshold-regex-no-capture-group",
77 ),
78 # threshold requires absent=False; combining threshold with absent=True is rejected
79 pytest.param(
80 {
81 "match": "mtu (\\d+)",
82 "mode": "regex",
83 "absent": True,
84 "threshold": {"value": 1500, "operator": "ge"},
85 },
86 id="threshold-with-absent-true",
87 ),
88 # mode=regex with invalid regex syntax is rejected at model validation time, not at runtime
89 pytest.param(
90 {"match": "mtu [invalid", "mode": "regex"},
91 id="invalid-regex-syntax",
92 ),
93 ],
94 )
95 def test_invalid(self, model_params: dict[str, Any]) -> None:
96 """Test ConfigEntry invalid inputs."""
97 with pytest.raises(ValidationError):
98 ConfigEntry.model_validate(model_params)
99
100
101 class TestConfigRule:
102 """Tests for anta.input_models.configuration.ConfigRule."""
103
104 @pytest.mark.parametrize(
105 "model_params",
106 [
107 # section=None means top-level commands — valid default
108 pytest.param(
109 {"section": None, "entries": [{"match": "ip routing"}]},
110 id="no-section",
111 ),
112 # single exact section path — valid
113 pytest.param(
114 {"section": ["router bgp 65101"], "entries": [{"match": "router-id"}]},
115 id="exact-section",
116 ),
117 # single regex section pattern — valid; regex fallback is applied at runtime
118 pytest.param(
119 {"section": ["interface Ethernet\\d+"], "entries": [{"match": "description"}]},
120 id="regex-section",
121 ),
122 # entries=[] is valid — section existence check: passes if section resolves, fails if not found
123 pytest.param(
124 {"section": ["router bgp 65101"], "entries": []},
125 id="section-existence-only",
126 ),
127 ],
128 )
129 def test_valid(self, model_params: dict[str, Any]) -> None:
130 """Test ConfigRule valid inputs."""
131 ConfigRule.model_validate(model_params)
132
133 @pytest.mark.parametrize(
134 "model_params",
135 [
136 # section must have at least one pattern — empty list is rejected
137 pytest.param(
138 {"section": [], "entries": [{"match": "ip routing"}]},
139 id="empty-section-list",
140 ),
141 # each section pattern must be non-empty — empty string is rejected
142 pytest.param(
143 {"section": [""], "entries": [{"match": "ip routing"}]},
144 id="empty-string-in-section",
145 ),
146 # section patterns used as regex fallback — invalid syntax is rejected at model validation time
147 pytest.param(
148 {"section": ["interface Ethernet[invalid"], "entries": [{"match": "ip routing"}]},
149 id="invalid-section-regex-syntax",
150 ),
151 ],
152 )
153 def test_invalid(self, model_params: dict[str, Any]) -> None:
154 """Test ConfigRule invalid inputs."""
155 with pytest.raises(ValidationError):
156 ConfigRule.model_validate(model_params)