-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_advanced_features.py
More file actions
197 lines (141 loc) · 5.19 KB
/
Copy pathtest_advanced_features.py
File metadata and controls
197 lines (141 loc) · 5.19 KB
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
#!/usr/bin/env python3
"""
Test script for advanced features
"""
import sys
from pathlib import Path
# Add current directory to path
sys.path.insert(0, str(Path(__file__).parent))
from trend_analyzer import TrendAnalyzer
from reports_generator import ReportGenerator
from performance_metrics import PerformanceMetrics
from ml_analyzer import MLAnalyzer
def test_trend_analyzer():
"""Test TrendAnalyzer"""
print("Testing TrendAnalyzer...")
analyzer = TrendAnalyzer("test_history.json")
# Add sample data
sample_findings = [
{'severity': 'CRITICAL', 'category': 'Disk', 'message': 'Disk full'},
{'severity': 'HIGH', 'category': 'Ceph-OSD', 'message': 'OSD down'},
{'severity': 'MEDIUM', 'category': 'LVM', 'message': 'Thin pool usage high'}
]
analyzer.add_analysis(sample_findings, metadata={'hostname': 'test-host'})
# Get summary
summary = analyzer.get_summary()
print(f" ✅ Total analyses: {summary['total_analyses']}")
# Clean up
import os
if os.path.exists("test_history.json"):
os.remove("test_history.json")
return True
def test_reports_generator():
"""Test ReportGenerator"""
print("Testing ReportGenerator...")
generator = ReportGenerator()
sample_findings = [
{
'severity': 'CRITICAL',
'category': 'Disk',
'message': 'Disk /dev/sda is full',
'details': 'Usage: 98%'
},
{
'severity': 'HIGH',
'category': 'Ceph-OSD',
'message': 'OSD.5 is down',
'details': 'Check OSD status'
}
]
# Generate HTML report
html = generator.generate_html_report(sample_findings, title="Test Report")
print(f" ✅ HTML report generated ({len(html)} bytes)")
# Generate text report
text = generator.generate_text_report(sample_findings, title="Test Report")
print(f" ✅ Text report generated ({len(text)} bytes)")
return True
def test_performance_metrics():
"""Test PerformanceMetrics"""
print("Testing PerformanceMetrics...")
metrics = PerformanceMetrics("test_metrics.json")
sample_findings = [
{'severity': 'CRITICAL', 'category': 'Disk', 'message': 'Disk full'},
{'severity': 'HIGH', 'category': 'Ceph-OSD', 'message': 'OSD down'},
{'severity': 'MEDIUM', 'category': 'LVM', 'message': 'Thin pool usage high'}
]
# Add metrics
metrics.add_metrics(sample_findings)
# Get health score
health_score = metrics.calculate_health_score(sample_findings)
print(f" ✅ Health score calculated: {health_score}/100")
# Get summary
summary = metrics.get_performance_summary()
if summary.get('status') != 'no_data':
print(f" ✅ Latest health score: {summary['latest_health_score']}")
print(f" ✅ Risk level: {summary['latest_risk_level']}")
# Clean up
import os
if os.path.exists("test_metrics.json"):
os.remove("test_metrics.json")
return True
def test_ml_analyzer():
"""Test MLAnalyzer"""
print("Testing MLAnalyzer...")
analyzer = MLAnalyzer("test_ml_data.json")
sample_findings = [
{'severity': 'CRITICAL', 'category': 'Disk', 'message': 'Disk full'},
{'severity': 'HIGH', 'category': 'Ceph-OSD', 'message': 'OSD down'},
{'severity': 'MEDIUM', 'category': 'LVM', 'message': 'Thin pool usage high'}
]
# Add training sample
analyzer.add_training_sample(sample_findings)
# Prioritize issues
prioritized = analyzer.prioritize_issues(sample_findings)
print(f" ✅ Prioritized {len(prioritized)} issues")
if prioritized:
print(f" ✅ Top priority: {prioritized[0].get('message', 'N/A')} "
f"(score: {prioritized[0].get('priority_score', 0)})")
# Get failure predictions
predictions = analyzer.predict_failures(sample_findings)
print(f" ✅ Risk level: {predictions.get('risk_level', 'UNKNOWN')}")
# Clean up
import os
if os.path.exists("test_ml_data.json"):
os.remove("test_ml_data.json")
return True
def main():
"""Run all tests"""
print("\n" + "=" * 60)
print("Testing Advanced Features")
print("=" * 60 + "\n")
tests = [
("Trend Analyzer", test_trend_analyzer),
("Reports Generator", test_reports_generator),
("Performance Metrics", test_performance_metrics),
("ML Analyzer", test_ml_analyzer)
]
results = []
for name, test_func in tests:
try:
success = test_func()
results.append((name, success))
print(f"✅ {name} test passed\n")
except Exception as e:
results.append((name, False))
print(f"❌ {name} test failed: {str(e)}\n")
# Summary
print("=" * 60)
print("Test Summary")
print("=" * 60)
passed = sum(1 for _, success in results if success)
total = len(results)
for name, success in results:
status = "✅ PASS" if success else "❌ FAIL"
print(f"{status:10} {name}")
print("\n" + "=" * 60)
print(f"Total: {passed}/{total} tests passed")
print("=" * 60)
return passed == total
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)