| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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 |
id |
| 32 |
pytest.param( |
| 33 |
{"match": "no shutdown", "mode": "exact"}, |
| 34 |
id="no-threshold-exact", |
| 35 |
), |
| 36 |
id |
| 37 |
pytest.param( |
| 38 |
{"match": "router-id", "mode": "contains"}, |
| 39 |
id="no-threshold-contains", |
| 40 |
), |
| 41 |
up |
| 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 |
|
| 56 |
pytest.param( |
| 57 |
{"match": "mtu", "mode": "exact", "threshold": {"value": 1500, "operator": "ge"}}, |
| 58 |
id="threshold-with-exact-mode", |
| 59 |
), |
| 60 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
lt |
| 108 |
pytest.param( |
| 109 |
{"section": None, "entries": [{"match": "ip routing"}]}, |
| 110 |
id="no-section", |
| 111 |
), |
| 112 |
id |
| 113 |
pytest.param( |
| 114 |
{"section": ["router bgp 65101"], "entries": [{"match": "router-id"}]}, |
| 115 |
id="exact-section", |
| 116 |
), |
| 117 |
me |
| 118 |
pytest.param( |
| 119 |
{"section": ["interface Ethernet\\d+"], "entries": [{"match": "description"}]}, |
| 120 |
id="regex-section", |
| 121 |
), |
| 122 |
nd |
| 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 |
ed |
| 137 |
pytest.param( |
| 138 |
{"section": [], "entries": [{"match": "ip routing"}]}, |
| 139 |
id="empty-section-list", |
| 140 |
), |
| 141 |
ed |
| 142 |
pytest.param( |
| 143 |
{"section": [""], "entries": [{"match": "ip routing"}]}, |
| 144 |
id="empty-string-in-section", |
| 145 |
), |
| 146 |
me |
| 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) |