1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-02-07 17:58:24 +00:00

fix: Improve error message for community name validation and clean up test formatting

This commit is contained in:
Wilhelm Schonfeldt 2025-09-26 11:49:27 +02:00
parent eabd98b606
commit b2a05994aa
3 changed files with 33 additions and 31 deletions

View file

@ -24,7 +24,9 @@ class StructuredCommunities(HyperglassModel):
def validate_names(cls, value: t.Dict[str, str], info: ValidationInfo) -> t.Dict[str, str]:
"""Validate that names are provided when mode is 'name'."""
if info.data and info.data.get("mode") == "name" and not value:
raise ValueError("When using mode 'name', at least one community mapping must be provided in 'names'")
raise ValueError(
"When using mode 'name', at least one community mapping must be provided in 'names'"
)
return value

View file

@ -250,6 +250,7 @@ def _parse_route_block(block: t.List[str]) -> t.Optional[MikrotikRouteEntry]:
class MikrotikBGPRouteTable(BGPRouteTable):
"""Canonical MikroTik BGP Route Table."""
# No custom __init__ needed; inherit from BGPRouteTable (which should be a Pydantic model)

View file

@ -14,10 +14,10 @@ from hyperglass.models.config.structured import StructuredCommunities
def test_community_validation_name_mode():
"""Test that name mode correctly appends friendly names to communities."""
# Mock the state to return our test configuration
from hyperglass import state
# Create a mock structured config with name mode
mock_structured = Mock()
mock_structured.communities = StructuredCommunities(
@ -25,18 +25,18 @@ def test_community_validation_name_mode():
names={
"65000:1000:0": "Upstream Any",
"65000:1001:0": "Upstream A (all locations)",
"65000:1": "Test Community"
}
"65000:1": "Test Community",
},
)
# Mock the params with our structured config
mock_params = Mock()
mock_params.structured = mock_structured
# Mock the use_state function to return our mock params
original_use_state = getattr(state, 'use_state', None)
original_use_state = getattr(state, "use_state", None)
state.use_state = Mock(return_value=mock_params)
try:
# Test data for BGP route
test_data = {
@ -49,30 +49,30 @@ def test_community_validation_name_mode():
"as_path": [65000, 65001],
"communities": [
"65000:1000:0", # Should get friendly name
"65000:1001:0", # Should get friendly name
"65000:1001:0", # Should get friendly name
"65000:9999:0", # Should remain unchanged (no mapping)
"65000:1", # Should get friendly name
"65000:1", # Should get friendly name
],
"next_hop": "192.0.2.1",
"source_as": 65001,
"source_rid": "192.0.2.1",
"peer_rid": "192.0.2.2",
"rpki_state": 1
"rpki_state": 1,
}
# Create BGPRoute instance
route = BGPRoute(**test_data)
# Check that communities have been transformed correctly
expected_communities = [
"65000:1000:0,Upstream Any",
"65000:1001:0,Upstream A (all locations)",
"65000:9999:0", # No friendly name, stays unchanged
"65000:1,Test Community"
"65000:1,Test Community",
]
assert route.communities == expected_communities
finally:
# Restore original use_state function
if original_use_state:
@ -81,22 +81,21 @@ def test_community_validation_name_mode():
def test_community_validation_permit_mode_unchanged():
"""Test that permit mode still works as before."""
from hyperglass import state
# Create a mock structured config with permit mode
mock_structured = Mock()
mock_structured.communities = StructuredCommunities(
mode="permit",
items=["^65000:.*$", "1234:1"]
mode="permit", items=["^65000:.*$", "1234:1"]
)
mock_params = Mock()
mock_params.structured = mock_structured
original_use_state = getattr(state, 'use_state', None)
original_use_state = getattr(state, "use_state", None)
state.use_state = Mock(return_value=mock_params)
try:
test_data = {
"prefix": "192.0.2.0/24",
@ -109,21 +108,21 @@ def test_community_validation_permit_mode_unchanged():
"communities": [
"65000:100", # Should be permitted (matches ^65000:.*$)
"65001:200", # Should be denied (doesn't match patterns)
"1234:1", # Should be permitted (exact match)
"1234:1", # Should be permitted (exact match)
],
"next_hop": "192.0.2.1",
"source_as": 65001,
"source_rid": "192.0.2.1",
"peer_rid": "192.0.2.2",
"rpki_state": 1
"rpki_state": 1,
}
route = BGPRoute(**test_data)
# Should only include permitted communities
expected_communities = ["65000:100", "1234:1"]
assert route.communities == expected_communities
finally:
if original_use_state:
state.use_state = original_use_state
@ -132,4 +131,4 @@ def test_community_validation_permit_mode_unchanged():
if __name__ == "__main__":
test_community_validation_name_mode()
test_community_validation_permit_mode_unchanged()
print("All tests passed!")
print("All tests passed!")