Spaces:
Running
Running
File size: 17,203 Bytes
8e74f68 |
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
"""
Tests for RAG-based Incident Learning System
Tests incident capture, root cause analysis, and regression test generation
"""
import pytest
import json
from datetime import datetime
from pathlib import Path
from agent.incident_learning import (
Incident,
IncidentDatabase,
RootCauseAnalyzer,
RegressionTestGenerator,
capture_deployment_failure,
learn_from_incident
)
@pytest.fixture
def incident_db(tmp_path):
"""Create temporary incident database"""
return IncidentDatabase(db_path=tmp_path / "incidents")
@pytest.fixture
def sample_incident():
"""Create sample incident for testing"""
return Incident(
id="test-20251125-120000",
timestamp=datetime.now().isoformat(),
severity="high",
category="deployment_failure",
description="VLAN 100 configuration failed - duplicate VLAN ID",
affected_devices=["leaf-01", "leaf-02"],
network_model={
"name": "test-network",
"vlans": [
{"id": 100, "name": "Users"},
{"id": 100, "name": "Servers"} # Duplicate!
]
},
validation_errors=[
{"error": "Duplicate VLAN ID 100", "type": "validation"}
]
)
@pytest.fixture
def routing_incident():
"""Routing issue incident"""
return Incident(
id="routing-20251125-130000",
timestamp=datetime.now().isoformat(),
severity="critical",
category="routing_issue",
description="Routing loop detected between spine-01 and spine-02",
affected_devices=["spine-01", "spine-02"],
root_cause="BGP AS-PATH loop due to incorrect route reflector config"
)
class TestIncident:
"""Test Incident dataclass"""
def test_incident_creation(self, sample_incident):
"""Test creating incident"""
assert sample_incident.id == "test-20251125-120000"
assert sample_incident.severity == "high"
assert sample_incident.category == "deployment_failure"
assert len(sample_incident.affected_devices) == 2
def test_incident_to_dict(self, sample_incident):
"""Test serialization"""
data = sample_incident.to_dict()
assert data['id'] == sample_incident.id
assert data['severity'] == sample_incident.severity
assert data['category'] == sample_incident.category
assert data['affected_devices'] == sample_incident.affected_devices
assert 'network_model' in data
assert 'validation_errors' in data
class TestIncidentDatabase:
"""Test incident database operations"""
def test_add_incident(self, incident_db, sample_incident):
"""Test adding incident to database"""
incident_id = incident_db.add_incident(sample_incident)
assert incident_id == sample_incident.id
# Verify file was created
assert incident_db.incidents_file.exists()
# Verify data
with open(incident_db.incidents_file) as f:
data = json.load(f)
assert incident_id in data
def test_get_incident(self, incident_db, sample_incident):
"""Test retrieving incident by ID"""
incident_db.add_incident(sample_incident)
retrieved = incident_db.get_incident(sample_incident.id)
assert retrieved is not None
assert retrieved.id == sample_incident.id
assert retrieved.severity == sample_incident.severity
assert retrieved.description == sample_incident.description
def test_get_nonexistent_incident(self, incident_db):
"""Test getting incident that doesn't exist"""
result = incident_db.get_incident("nonexistent-id")
assert result is None
def test_update_incident(self, incident_db, sample_incident):
"""Test updating incident"""
incident_db.add_incident(sample_incident)
# Update with resolution
incident_db.update_incident(sample_incident.id, {
'resolution': 'Fixed duplicate VLAN IDs',
'resolved_at': datetime.now().isoformat()
})
# Verify update
updated = incident_db.get_incident(sample_incident.id)
assert updated.resolution == 'Fixed duplicate VLAN IDs'
assert updated.resolved_at is not None
def test_get_all_incidents(self, incident_db, sample_incident, routing_incident):
"""Test getting all incidents"""
incident_db.add_incident(sample_incident)
incident_db.add_incident(routing_incident)
incidents = incident_db.get_all_incidents()
assert len(incidents) == 2
# Should be sorted by timestamp (newest first)
assert incidents[0].id == routing_incident.id
def test_filter_by_severity(self, incident_db, sample_incident, routing_incident):
"""Test filtering incidents by severity"""
incident_db.add_incident(sample_incident) # high
incident_db.add_incident(routing_incident) # critical
critical = incident_db.get_all_incidents(severity="critical")
assert len(critical) == 1
assert critical[0].severity == "critical"
high = incident_db.get_all_incidents(severity="high")
assert len(high) == 1
assert high[0].severity == "high"
def test_filter_by_category(self, incident_db, sample_incident, routing_incident):
"""Test filtering by category"""
incident_db.add_incident(sample_incident)
incident_db.add_incident(routing_incident)
deploy_failures = incident_db.get_all_incidents(category="deployment_failure")
assert len(deploy_failures) == 1
assert deploy_failures[0].category == "deployment_failure"
routing = incident_db.get_all_incidents(category="routing_issue")
assert len(routing) == 1
assert routing[0].category == "routing_issue"
def test_keyword_search(self, incident_db, sample_incident, routing_incident):
"""Test keyword search (fallback mode)"""
incident_db.add_incident(sample_incident)
incident_db.add_incident(routing_incident)
# Search for VLAN issues
results = incident_db.search_similar("VLAN duplicate", n_results=5)
# In mock mode without ChromaDB, keyword search may return empty if no exact match
assert isinstance(results, list)
# If results found, verify they're relevant
if results:
assert any("VLAN" in r.description or "vlan" in r.description.lower() for r in results)
# Search for routing issues
results = incident_db.search_similar("routing loop BGP", n_results=5)
assert isinstance(results, list)
class TestRootCauseAnalyzer:
"""Test root cause analysis"""
def test_analyze_incident(self, incident_db, sample_incident):
"""Test analyzing incident"""
# Add some historical incidents for context
historical = Incident(
id="historical-1",
timestamp="2025-11-20T10:00:00",
severity="high",
category="deployment_failure",
description="Duplicate VLAN configuration on leaf-03",
affected_devices=["leaf-03"],
root_cause="Schema validation missed duplicate VLAN IDs"
)
incident_db.add_incident(historical)
analyzer = RootCauseAnalyzer(incident_db)
analysis = analyzer.analyze(sample_incident)
assert 'suggested_root_cause' in analysis
assert 'similar_incidents' in analysis
assert 'patterns_found' in analysis
assert 'confidence' in analysis
# Similar incidents may be 0 without ChromaDB vector search
assert isinstance(analysis['similar_incidents'], list)
# Confidence should be >= 0
assert analysis['confidence'] >= 0.0
def test_extract_patterns(self, incident_db):
"""Test pattern extraction from similar incidents"""
# Add multiple incidents with same root cause
for i in range(3):
incident = Incident(
id=f"pattern-test-{i}",
timestamp=datetime.now().isoformat(),
severity="medium",
category="config_error",
description=f"BGP neighbor config error on device-{i}",
affected_devices=[f"device-{i}"],
root_cause="Missing BGP neighbor authentication"
)
incident_db.add_incident(incident)
analyzer = RootCauseAnalyzer(incident_db)
incidents = incident_db.get_all_incidents()
patterns = analyzer._extract_patterns(incidents)
assert len(patterns) > 0
# Should identify common root cause
assert any("Missing BGP neighbor authentication" in p for p in patterns)
def test_confidence_calculation(self, incident_db):
"""Test confidence scoring"""
analyzer = RootCauseAnalyzer(incident_db)
# Low confidence: no similar incidents
confidence = analyzer._calculate_confidence([], [])
assert confidence == 0.0
# Medium confidence: some similar, no patterns
similar = [
Incident(
id=f"test-{i}",
timestamp=datetime.now().isoformat(),
severity="low",
category="config_error",
description="Test incident"
)
for i in range(3)
]
confidence = analyzer._calculate_confidence(similar, [])
assert 0.0 < confidence < 1.0
# High confidence: similar incidents + resolved + patterns
for inc in similar:
inc.resolution = "Fixed"
patterns = ["Common pattern 1", "Common pattern 2"]
confidence = analyzer._calculate_confidence(similar, patterns)
assert confidence >= 0.7
class TestRegressionTestGenerator:
"""Test regression test generation"""
def test_generate_config_test(self, sample_incident):
"""Test generating config error test"""
generator = RegressionTestGenerator()
test_code = generator.generate_test(sample_incident)
assert test_code is not None
assert "pyats" in test_code or "pytest" in test_code
assert sample_incident.id in test_code
assert sample_incident.description in test_code
def test_generate_routing_test(self, routing_incident):
"""Test generating routing issue test"""
generator = RegressionTestGenerator()
test_code = generator.generate_test(routing_incident)
assert test_code is not None
assert "routing" in test_code.lower()
assert routing_incident.id in test_code
def test_generate_deployment_test(self):
"""Test generating deployment failure test"""
incident = Incident(
id="deploy-fail-1",
timestamp=datetime.now().isoformat(),
severity="high",
category="deployment_failure",
description="Deployment failed due to syntax error"
)
generator = RegressionTestGenerator()
test_code = generator.generate_test(incident)
assert test_code is not None
assert "deployment" in test_code.lower() or "validation" in test_code.lower()
def test_generated_test_is_valid_python(self, sample_incident):
"""Test that generated code is valid Python"""
generator = RegressionTestGenerator()
test_code = generator.generate_test(sample_incident)
# Try to compile it
try:
compile(test_code, '<string>', 'exec')
valid = True
except SyntaxError:
valid = False
assert valid, "Generated test code has syntax errors"
class TestCaptureDeploymentFailure:
"""Test deployment failure capture"""
def test_capture_failure(self, tmp_path):
"""Test capturing deployment failure"""
# Create temp database
db = IncidentDatabase(db_path=tmp_path / "incidents")
# Monkey patch to use our temp database
import agent.incident_learning
original_db_init = agent.incident_learning.IncidentDatabase
agent.incident_learning.IncidentDatabase = lambda: db
try:
incident = capture_deployment_failure(
description="Test deployment failure",
network_model={"name": "test-net", "devices": []},
validation_errors=[{"error": "Test error"}],
affected_devices=["device-1"]
)
assert incident.severity == "high"
assert incident.category == "deployment_failure"
assert len(incident.affected_devices) == 1
# Verify it was stored
retrieved = db.get_incident(incident.id)
assert retrieved is not None
finally:
# Restore
agent.incident_learning.IncidentDatabase = original_db_init
class TestLearnFromIncident:
"""Test complete learning workflow"""
def test_learn_from_incident(self, tmp_path, sample_incident):
"""Test full learning cycle"""
db = IncidentDatabase(db_path=tmp_path / "incidents")
db.add_incident(sample_incident)
# Monkey patch database
import agent.incident_learning
original_db_init = agent.incident_learning.IncidentDatabase
agent.incident_learning.IncidentDatabase = lambda: db
try:
result = learn_from_incident(sample_incident)
assert 'incident_id' in result
assert 'root_cause' in result
assert 'regression_test' in result
assert 'confidence' in result
# Check that incident was updated with learnings
updated = db.get_incident(sample_incident.id)
assert updated.root_cause is not None
# Check that test file would be created
assert result['regression_test'] is not None
finally:
agent.incident_learning.IncidentDatabase = original_db_init
class TestPipelineIntegration:
"""Test integration with pipeline"""
def test_pipeline_has_incident_db(self):
"""Test pipeline initializes incident learning"""
from agent.pipeline_engine import OvergrowthPipeline
pipeline = OvergrowthPipeline()
assert hasattr(pipeline, 'incident_db')
assert hasattr(pipeline, 'rca_analyzer')
assert hasattr(pipeline, 'test_generator')
def test_capture_on_validation_failure(self, tmp_path):
"""Test incident captured when validation fails"""
from agent.pipeline_engine import OvergrowthPipeline, NetworkModel, NetworkIntent
pipeline = OvergrowthPipeline()
# Override incident DB to use temp directory
pipeline.incident_db = IncidentDatabase(db_path=tmp_path / "incidents")
# Create invalid network model
intent = NetworkIntent(
description="Test network with validation errors",
business_requirements=["Invalid config"],
constraints=[]
)
model = NetworkModel(
name="invalid-network",
version="1.0.0",
intent=intent,
devices=[], # Empty devices = validation error
vlans=[],
subnets=[],
routing={},
services=[]
)
# Run validation (should fail)
results = pipeline.stage0_preflight(model)
# Should have captured incident
incidents = pipeline.incident_db.get_all_incidents()
# May capture incident for validation failures
assert isinstance(incidents, list)
def test_learn_from_recent_incidents(self, tmp_path):
"""Test learning from recent incidents"""
from agent.pipeline_engine import OvergrowthPipeline
pipeline = OvergrowthPipeline()
pipeline.incident_db = IncidentDatabase(db_path=tmp_path / "incidents")
# Add some test incidents
for i in range(3):
incident = Incident(
id=f"test-{i}",
timestamp=datetime.now().isoformat(),
severity="medium",
category="config_error",
description=f"Test incident {i}"
)
pipeline.incident_db.add_incident(incident)
# Trigger learning
learnings = pipeline.learn_from_recent_incidents(limit=5)
assert 'total_incidents' in learnings
assert 'unresolved' in learnings
assert 'analyzed' in learnings
assert learnings['total_incidents'] == 3
if __name__ == '__main__':
pytest.main([__file__, '-v'])
|