File size: 12,983 Bytes
d59ae58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Debug and validation test runner for Multi-Agent NLP System.
Orchestrates infrastructure validation, agent testing, and end-to-end workflow tests.
"""

import asyncio
import subprocess
import sys
import os
import time
from pathlib import Path
from typing import Dict, List, Optional


class DebugTestRunner:
    """Orchestrates debugging and validation tests for the multi-agent system."""
    
    def __init__(self, project_root: str = None):
        self.project_root = Path(project_root or "/Users/cuonghuynh/CascadeProjects/multi-agent-nlp-system")
        self.test_results = {}
        
    async def run_all_debug_tests(self):
        """Run all debugging and validation tests in sequence."""
        print("πŸš€ Starting Multi-Agent NLP System Debug & Validation")
        print("=" * 60)
        
        test_phases = [
            ("Infrastructure Validation", self.run_infrastructure_validation),
            ("Docker Services Check", self.check_docker_services),
            ("Agent Health Check", self.run_agent_health_check),
            ("Kafka Communication Test", self.test_kafka_communication),
            ("End-to-End Workflow Test", self.run_e2e_workflow_test),
            ("Performance Monitoring", self.run_performance_monitoring),
        ]
        
        overall_success = True
        
        for phase_name, phase_func in test_phases:
            print(f"\nπŸ” Phase: {phase_name}")
            print("-" * 40)
            
            try:
                success = await phase_func()
                self.test_results[phase_name] = success
                
                if success:
                    print(f"βœ… {phase_name} completed successfully")
                else:
                    print(f"❌ {phase_name} failed")
                    overall_success = False
                    
            except Exception as e:
                print(f"πŸ’₯ {phase_name} crashed: {str(e)}")
                self.test_results[phase_name] = False
                overall_success = False
        
        self.print_final_summary(overall_success)
        return overall_success
    
    async def run_infrastructure_validation(self) -> bool:
        """Run the infrastructure validation script."""
        try:
            # Check if validation script exists
            validation_script = self.project_root / "check_infrastructure.py"
            if not validation_script.exists():
                print("❌ Infrastructure validation script not found")
                return False
            
            # Install required packages
            print("πŸ“¦ Installing validation dependencies...")
            result = subprocess.run([
                sys.executable, "-m", "pip", "install", "-r", 
                str(self.project_root / "requirements-validation.txt")
            ], capture_output=True, text=True)
            
            if result.returncode != 0:
                print(f"⚠️  Package installation warnings: {result.stderr}")
            
            # Run infrastructure validation
            print("πŸ”§ Running infrastructure validation...")
            result = subprocess.run([
                sys.executable, str(validation_script)
            ], cwd=str(self.project_root), capture_output=True, text=True)
            
            print(result.stdout)
            if result.stderr:
                print(f"Errors: {result.stderr}")
            
            return result.returncode == 0
            
        except Exception as e:
            print(f"Infrastructure validation error: {str(e)}")
            return False
    
    async def check_docker_services(self) -> bool:
        """Check if Docker services are running properly."""
        try:
            import docker
            client = docker.from_env()
            
            # Check if containers are running
            containers = client.containers.list()
            print(f"πŸ“¦ Found {len(containers)} running containers:")
            
            for container in containers:
                print(f"   - {container.name}: {container.status}")
            
            # Check for expected services
            expected_services = ['kafka', 'redis', 'minio', 'zookeeper']
            running_services = [c.name for c in containers]
            
            missing_services = []
            for service in expected_services:
                if not any(service in name for name in running_services):
                    missing_services.append(service)
            
            if missing_services:
                print(f"⚠️  Missing services: {missing_services}")
                print("πŸ’‘ Try running: docker-compose -f docker-compose-minimal.yml up -d")
                return False
            
            return True
            
        except Exception as e:
            print(f"Docker services check error: {str(e)}")
            return False
    
    async def run_agent_health_check(self) -> bool:
        """Check health of individual agents."""
        try:
            # Look for agent directories
            agent_dirs = []
            for item in self.project_root.iterdir():
                if item.is_dir() and 'agent' in item.name.lower():
                    agent_dirs.append(item)
            
            if not agent_dirs:
                print("⚠️  No agent directories found")
                print("πŸ’‘ This is expected if you haven't created agent services yet")
            else:
                print(f"πŸ€– Found {len(agent_dirs)} agent directories:")
                for agent_dir in agent_dirs:
                    print(f"   - {agent_dir.name}")
            
            # Test Kafka client functionality
            print("πŸ”— Testing shared Kafka client...")
            kafka_test_result = await self.test_kafka_client()
            
            return kafka_test_result
            
        except Exception as e:
            print(f"Agent health check error: {str(e)}")
            return False
    
    async def test_kafka_client(self) -> bool:
        """Test the shared Kafka client functionality."""
        try:
            # Add the shared directory to Python path
            sys.path.insert(0, str(self.project_root / "shared"))
            
            from kafka_client import create_kafka_config, KafkaManager
            
            # Create Kafka configuration
            config = create_kafka_config(bootstrap_servers="localhost:9092")
            
            # Test Kafka manager
            manager = KafkaManager(config)
            
            print("   Testing Kafka producer...")
            await manager.start_producer()
            
            # Send test message
            success = await manager.send_message(
                "test-topic", 
                {"test": "message", "timestamp": time.time()}
            )
            
            if success:
                print("   βœ… Kafka producer test passed")
            else:
                print("   ❌ Kafka producer test failed")
                return False
            
            # Test consumer
            print("   Testing Kafka consumer...")
            consumer = await manager.create_consumer("test-group", ["test-topic"])
            
            # Register a test handler
            def test_handler(message):
                print(f"   πŸ“¨ Received test message: {message['value']}")
            
            consumer.register_handler("test-topic", test_handler)
            
            # Cleanup
            await manager.shutdown()
            print("   βœ… Kafka client tests passed")
            
            return True
            
        except Exception as e:
            print(f"   ❌ Kafka client test error: {str(e)}")
            return False
    
    async def test_kafka_communication(self) -> bool:
        """Test inter-service communication via Kafka."""
        try:
            print("πŸ“‘ Testing Kafka communication between services...")
            
            # This would test actual message passing between agents
            # For now, we'll do a basic connectivity test
            
            from kafka import KafkaProducer, KafkaConsumer
            import json
            
            # Test producer
            producer = KafkaProducer(
                bootstrap_servers=['localhost:9092'],
                value_serializer=lambda v: json.dumps(v).encode('utf-8')
            )
            
            # Send test messages to different topics
            test_topics = ['nlp-requests', 'image-generation', 'video-processing']
            
            for topic in test_topics:
                future = producer.send(topic, {'test': f'message for {topic}'})
                result = future.get(timeout=5)
                print(f"   βœ… Message sent to {topic}")
            
            producer.close()
            return True
            
        except Exception as e:
            print(f"Kafka communication test error: {str(e)}")
            return False
    
    async def run_e2e_workflow_test(self) -> bool:
        """Run end-to-end workflow tests."""
        try:
            print("πŸ”„ Running end-to-end workflow tests...")
            
            # This would test complete workflows through the system
            # For now, we'll simulate a basic workflow
            
            workflow_steps = [
                "API Gateway receives request",
                "Request routed to appropriate agent",
                "Agent processes request",
                "Results stored in MinIO",
                "Response sent back through Kafka",
                "API Gateway returns response"
            ]
            
            for i, step in enumerate(workflow_steps, 1):
                print(f"   {i}. {step}")
                await asyncio.sleep(0.1)  # Simulate processing time
            
            print("   βœ… E2E workflow simulation completed")
            return True
            
        except Exception as e:
            print(f"E2E workflow test error: {str(e)}")
            return False
    
    async def run_performance_monitoring(self) -> bool:
        """Run performance monitoring checks."""
        try:
            print("πŸ“Š Running performance monitoring...")
            
            import psutil
            
            # Check system resources
            cpu_percent = psutil.cpu_percent(interval=1)
            memory = psutil.virtual_memory()
            disk = psutil.disk_usage('/')
            
            print(f"   CPU Usage: {cpu_percent}%")
            print(f"   Memory Usage: {memory.percent}%")
            print(f"   Disk Usage: {disk.percent}%")
            
            # Check if resources are within acceptable limits
            if cpu_percent > 90:
                print("   ⚠️  High CPU usage detected")
                return False
            
            if memory.percent > 90:
                print("   ⚠️  High memory usage detected")
                return False
            
            print("   βœ… System performance within acceptable limits")
            return True
            
        except Exception as e:
            print(f"Performance monitoring error: {str(e)}")
            return False
    
    def print_final_summary(self, overall_success: bool):
        """Print final summary of all test results."""
        print("\n" + "=" * 60)
        print("πŸ“‹ FINAL DEBUG & VALIDATION SUMMARY")
        print("=" * 60)
        
        for test_name, result in self.test_results.items():
            status = "βœ… PASS" if result else "❌ FAIL"
            print(f"{test_name:<30} {status}")
        
        passed = sum(1 for result in self.test_results.values() if result)
        total = len(self.test_results)
        
        print(f"\nOverall Result: {passed}/{total} tests passed")
        
        if overall_success:
            print("πŸŽ‰ All debugging and validation tests passed!")
            print("πŸ’‘ Your multi-agent NLP system is ready for production!")
        else:
            print("⚠️  Some issues detected. Please review and fix before proceeding.")
            print("\nπŸ”§ Troubleshooting tips:")
            print("   1. Ensure Docker is running: docker --version")
            print("   2. Start services: docker-compose -f docker-compose-minimal.yml up -d")
            print("   3. Check logs: docker-compose -f docker-compose-minimal.yml logs")
            print("   4. Verify network connectivity")


async def main():
    """Main function to run all debug tests."""
    runner = DebugTestRunner()
    
    try:
        success = await runner.run_all_debug_tests()
        sys.exit(0 if success else 1)
        
    except KeyboardInterrupt:
        print("\n⏹️  Debug tests interrupted by user")
        sys.exit(1)
    except Exception as e:
        print(f"\nπŸ’₯ Unexpected error: {str(e)}")
        sys.exit(1)


if __name__ == "__main__":
    asyncio.run(main())