Spaces:
Sleeping
Sleeping
File size: 10,565 Bytes
a2079ba | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | #!/usr/bin/env python3
"""
Test schema validation and policy engine
"""
import logging
from agent.schema_validation import (
validate_network_model,
get_validation_errors,
VLANModel,
SubnetModel,
DeviceModel
)
from agent.policy_engine import NetworkPolicy
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_valid_network():
"""Test validation with a valid network model"""
print("\n=== Test 1: Valid Network Model ===")
model = {
"name": "test-network",
"version": "1.0.0",
"description": "Test network",
"business_requirements": ["High availability"],
"constraints": ["Budget friendly"],
"vlans": [
{"id": 10, "name": "Management", "purpose": "Network mgmt", "subnet": "10.0.10.0/24"},
{"id": 20, "name": "Users", "purpose": "Employee workstations", "subnet": "10.0.20.0/24"}
],
"subnets": [
{
"network": "10.0.10.0/24",
"gateway": "10.0.10.1",
"vlan": 10,
"purpose": "Management",
"dhcp_enabled": True,
"dhcp_range_start": "10.0.10.100",
"dhcp_range_end": "10.0.10.200"
},
{
"network": "10.0.20.0/24",
"gateway": "10.0.20.1",
"vlan": 20,
"purpose": "Users"
}
],
"devices": [
{
"name": "core-sw-01",
"role": "core",
"model": "Catalyst 9300",
"vendor": "cisco",
"mgmt_ip": "10.0.10.10",
"location": "Main IDF",
"interfaces": []
}
],
"services": ["DHCP", "DNS", "NTP"]
}
errors = get_validation_errors(model)
if errors:
print(f"✗ Validation failed:")
for err in errors:
print(f" - {err}")
else:
validated = validate_network_model(model)
print(f"✓ Network model validated successfully")
print(f" - Name: {validated.name}")
print(f" - VLANs: {len(validated.vlans)}")
print(f" - Subnets: {len(validated.subnets)}")
print(f" - Devices: {len(validated.devices)}")
def test_invalid_vlan():
"""Test VLAN validation"""
print("\n=== Test 2: Invalid VLAN ID ===")
model = {
"name": "test",
"version": "1.0.0",
"description": "Test",
"vlans": [
{"id": 5000, "name": "Invalid"} # VLAN ID out of range
],
"devices": [],
"subnets": [],
"services": []
}
errors = get_validation_errors(model)
if errors:
print(f"✓ Correctly caught validation error:")
for err in errors:
print(f" - {err}")
else:
print("✗ Should have failed validation")
def test_invalid_subnet():
"""Test subnet validation"""
print("\n=== Test 3: Invalid Subnet ===")
model = {
"name": "test",
"version": "1.0.0",
"description": "Test",
"vlans": [{"id": 10, "name": "Test"}],
"subnets": [
{
"network": "10.0.10.0/24",
"gateway": "192.168.1.1", # Gateway not in subnet
"vlan": 10
}
],
"devices": [],
"services": []
}
errors = get_validation_errors(model)
if errors:
print(f"✓ Correctly caught validation error:")
for err in errors:
print(f" - {err}")
else:
print("✗ Should have failed validation")
def test_duplicate_vlan_ids():
"""Test duplicate VLAN detection"""
print("\n=== Test 4: Duplicate VLAN IDs ===")
model = {
"name": "test",
"version": "1.0.0",
"description": "Test",
"vlans": [
{"id": 10, "name": "VLAN10"},
{"id": 10, "name": "AlsoVLAN10"} # Duplicate!
],
"devices": [],
"subnets": [],
"services": []
}
errors = get_validation_errors(model)
if errors:
print(f"✓ Correctly caught duplicate VLAN IDs:")
for err in errors:
print(f" - {err}")
else:
print("✗ Should have failed validation")
def test_policy_engine():
"""Test policy engine checks"""
print("\n=== Test 5: Policy Engine ===")
model = {
"name": "test-network",
"version": "1.0.0",
"description": "Test network",
"vlans": [
{"id": 1, "name": "Default"}, # VLAN 1 - should warn
{"id": 10, "name": "Mgmt"},
{"id": 20, "name": "Guest WiFi"} # Space in name - should warn
],
"subnets": [
{"network": "10.0.10.0/24", "gateway": "10.0.10.1", "vlan": 10},
{"network": "10.0.20.0/24", "gateway": "10.0.20.1", "vlan": 20}
],
"devices": [
{
"name": "switch1", # No role in name, no 2-digit suffix
"role": "core",
"model": "Test",
"vendor": "cisco",
"mgmt_ip": "10.0.10.10",
"location": "Office"
}
],
"services": ["DHCP"] # Missing DNS, NTP
}
policy = NetworkPolicy()
violations = policy.check_network_model(model)
print(f"Found {len(violations)} policy violations:")
print(policy.format_violations())
by_severity = policy.get_violations_by_severity()
print(f"\n✓ Policy check complete:")
print(f" - Errors: {len(by_severity['ERROR'])}")
print(f" - Warnings: {len(by_severity['WARNING'])}")
print(f" - Info: {len(by_severity['INFO'])}")
def test_overlapping_subnets():
"""Test overlapping subnet detection"""
print("\n=== Test 6: Overlapping Subnets ===")
model = {
"name": "test",
"version": "1.0.0",
"description": "Test",
"vlans": [
{"id": 10, "name": "Network1"},
{"id": 20, "name": "Network2"}
],
"subnets": [
{"network": "10.0.0.0/16", "gateway": "10.0.0.1", "vlan": 10},
{"network": "10.0.10.0/24", "gateway": "10.0.10.1", "vlan": 20} # Overlaps!
],
"devices": [],
"services": []
}
policy = NetworkPolicy()
violations = policy.check_network_model(model)
overlap_errors = [v for v in violations if 'overlapping' in v.message.lower()]
if overlap_errors:
print(f"✓ Correctly detected overlapping subnets:")
for err in overlap_errors:
print(f" - {err}")
else:
print("✗ Should have detected overlapping subnets")
def test_complete_validation():
"""Test complete validation flow"""
print("\n=== Test 7: Complete Validation Flow ===")
model = {
"name": "production-network",
"version": "1.0.0",
"description": "Production corporate network",
"business_requirements": ["HA", "Secure guest access", "QoS for VoIP"],
"constraints": ["Cisco only", "Budget $25K"],
"vlans": [
{"id": 10, "name": "Management", "purpose": "Network management"},
{"id": 20, "name": "Users", "purpose": "Employee workstations"},
{"id": 30, "name": "Guest", "purpose": "Guest WiFi"},
{"id": 40, "name": "Voice", "purpose": "VoIP phones"}
],
"subnets": [
{"network": "10.0.10.0/24", "gateway": "10.0.10.1", "vlan": 10, "purpose": "Management"},
{"network": "10.0.20.0/22", "gateway": "10.0.20.1", "vlan": 20, "purpose": "Users"},
{"network": "10.0.30.0/24", "gateway": "10.0.30.1", "vlan": 30, "purpose": "Guest"},
{"network": "10.0.40.0/24", "gateway": "10.0.40.1", "vlan": 40, "purpose": "Voice"}
],
"devices": [
{
"name": "core-sw-01",
"role": "core",
"model": "Catalyst 9300",
"vendor": "cisco",
"mgmt_ip": "10.0.10.10",
"location": "Main IDF"
},
{
"name": "core-sw-02",
"role": "core",
"model": "Catalyst 9300",
"vendor": "cisco",
"mgmt_ip": "10.0.10.11",
"location": "Main IDF"
},
{
"name": "access-sw-01",
"role": "access",
"model": "Catalyst 2960X",
"vendor": "cisco",
"mgmt_ip": "10.0.10.20",
"location": "Floor 1"
}
],
"routing": {
"protocol": "ospf",
"process_id": 1,
"router_id": "10.0.0.1",
"areas": ["0"]
},
"services": ["DHCP", "DNS", "NTP", "Syslog"]
}
# Schema validation
errors = get_validation_errors(model)
if errors:
print(f"✗ Schema validation failed:")
for err in errors:
print(f" - {err}")
return
print("✓ Schema validation passed")
validated = validate_network_model(model)
print(f" - {len(validated.vlans)} VLANs")
print(f" - {len(validated.subnets)} subnets")
print(f" - {len(validated.devices)} devices")
# Policy validation
policy = NetworkPolicy()
violations = policy.check_network_model(model)
print(f"\n✓ Policy validation complete:")
by_severity = policy.get_violations_by_severity()
print(f" - Errors: {len(by_severity['ERROR'])}")
print(f" - Warnings: {len(by_severity['WARNING'])}")
print(f" - Info: {len(by_severity['INFO'])}")
if policy.has_errors():
print("\n✗ Cannot proceed - fix errors first")
else:
print("\n✓ Ready for deployment")
if __name__ == "__main__":
print("\n" + "="*60)
print("Schema Validation & Policy Engine Test Suite")
print("="*60)
try:
test_valid_network()
test_invalid_vlan()
test_invalid_subnet()
test_duplicate_vlan_ids()
test_policy_engine()
test_overlapping_subnets()
test_complete_validation()
print("\n" + "="*60)
print("✓ All validation tests completed!")
print("="*60 + "\n")
except Exception as e:
print(f"\n✗ Test failed: {e}")
import traceback
traceback.print_exc()
exit(1)
|