-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server_functionality.py
More file actions
285 lines (243 loc) · 9.83 KB
/
test_server_functionality.py
File metadata and controls
285 lines (243 loc) · 9.83 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
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
#!/usr/bin/env python3
"""
Test script to validate the HTTP server functionality.
This tests the server endpoints and API integration.
"""
import requests
import json
import time
import subprocess
import sys
import os
import signal
from urllib.parse import urljoin
class InfernoServerTester:
def __init__(self, base_url="http://127.0.0.1:8080"):
self.base_url = base_url
self.server_process = None
def start_server(self):
"""Start the Inferno server in the background"""
print("🚀 Starting Inferno server...")
try:
# Try to start the server
self.server_process = subprocess.Popen(
["cargo", "run", "--", "serve", "--bind", "127.0.0.1:8080"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd="/Users/ryanrobson/git/inferno"
)
# Wait a bit for the server to start
time.sleep(5)
# Check if the process is still running
if self.server_process.poll() is None:
print("✅ Server started successfully")
return True
else:
print("❌ Server failed to start")
stdout, stderr = self.server_process.communicate()
print(f"STDOUT: {stdout.decode()}")
print(f"STDERR: {stderr.decode()}")
return False
except Exception as e:
print(f"❌ Failed to start server: {e}")
return False
def stop_server(self):
"""Stop the Inferno server"""
if self.server_process:
print("🛑 Stopping server...")
self.server_process.terminate()
try:
self.server_process.wait(timeout=5)
except subprocess.TimeoutExpired:
self.server_process.kill()
self.server_process.wait()
print("✅ Server stopped")
def test_endpoint(self, endpoint, method="GET", data=None, expected_status=200):
"""Test a specific endpoint"""
url = urljoin(self.base_url, endpoint)
try:
if method == "GET":
response = requests.get(url, timeout=10)
elif method == "POST":
response = requests.post(url, json=data, timeout=10)
else:
raise ValueError(f"Unsupported method: {method}")
if response.status_code == expected_status:
print(f"✅ {method} {endpoint} - Status: {response.status_code}")
return True, response
else:
print(f"❌ {method} {endpoint} - Expected: {expected_status}, Got: {response.status_code}")
return False, response
except requests.exceptions.RequestException as e:
print(f"❌ {method} {endpoint} - Connection error: {e}")
return False, None
def test_health_check(self):
"""Test the health check endpoint"""
print("\n🏥 Testing health check endpoint...")
success, response = self.test_endpoint("/health")
if success and response:
try:
print(f" Response: {response.text}")
return True
except:
pass
return success
def test_root_endpoint(self):
"""Test the root endpoint"""
print("\n🏠 Testing root endpoint...")
success, response = self.test_endpoint("/")
if success and response:
try:
print(f" Response: {response.text[:200]}...")
return True
except:
pass
return success
def test_metrics_endpoints(self):
"""Test metrics endpoints"""
print("\n📊 Testing metrics endpoints...")
endpoints = [
"/metrics",
"/metrics/json",
"/metrics/snapshot"
]
results = []
for endpoint in endpoints:
success, response = self.test_endpoint(endpoint)
results.append(success)
if success and response:
content_type = response.headers.get('content-type', '')
print(f" Content-Type: {content_type}")
return all(results)
def test_openai_endpoints(self):
"""Test OpenAI-compatible API endpoints"""
print("\n🤖 Testing OpenAI-compatible endpoints...")
# Test models endpoint
success, response = self.test_endpoint("/v1/models")
if not success:
return False
if response:
try:
models_data = response.json()
print(f" Models available: {len(models_data.get('data', []))}")
except:
print(" Models endpoint returned non-JSON response")
# Test chat completions (this might fail without a loaded model)
chat_request = {
"model": "test-model",
"messages": [{"role": "user", "content": "Hello!"}],
"max_tokens": 10
}
print(" Testing chat completions endpoint...")
success, response = self.test_endpoint("/v1/chat/completions", "POST", chat_request, expected_status=None)
if response:
print(f" Status: {response.status_code}")
if response.status_code in [200, 400, 404, 500]: # Any reasonable response is ok for now
return True
return False
def test_websocket_endpoint(self):
"""Test WebSocket endpoint availability"""
print("\n🔗 Testing WebSocket endpoint...")
# For WebSocket, we'll just check if the endpoint exists (returns proper error)
success, response = self.test_endpoint("/ws/stream", expected_status=None)
if response:
# WebSocket endpoints often return 400 or 426 for regular HTTP requests
if response.status_code in [400, 426, 404]:
print(f"✅ WebSocket endpoint exists (Status: {response.status_code})")
return True
return False
def run_tests(self):
"""Run all server functionality tests"""
print("🔥 Testing Inferno HTTP Server Functionality")
print("=" * 50)
results = []
# Test if server is accessible
print("\n📡 Testing server connectivity...")
try:
response = requests.get(self.base_url, timeout=5)
print(f"✅ Server is accessible at {self.base_url}")
except requests.exceptions.RequestException:
print(f"❌ Server is not accessible at {self.base_url}")
print(" Make sure to start the server first with: cargo run -- serve")
return False
# Run individual tests
results.append(self.test_health_check())
results.append(self.test_root_endpoint())
results.append(self.test_metrics_endpoints())
results.append(self.test_openai_endpoints())
results.append(self.test_websocket_endpoint())
# Summary
print("\n" + "=" * 50)
print("📊 Test Results Summary:")
passed = sum(results)
total = len(results)
print(f" ✅ Passed: {passed}/{total}")
print(f" ❌ Failed: {total - passed}/{total}")
if passed == total:
print("🎉 All tests passed! Server is working correctly.")
else:
print("⚠️ Some tests failed. Check the output above for details.")
return passed == total
def test_server_configuration():
"""Test server configuration and setup"""
print("🔧 Testing server configuration...")
# Check if serve.rs has the expected endpoints
serve_file = "/Users/ryanrobson/git/inferno/src/cli/serve.rs"
if os.path.exists(serve_file):
with open(serve_file, 'r') as f:
content = f.read()
endpoints = [
"/health",
"/metrics",
"/v1/models",
"/v1/chat/completions",
"/ws/stream"
]
for endpoint in endpoints:
if endpoint in content:
print(f" ✅ {endpoint} endpoint configured")
else:
print(f" ❌ {endpoint} endpoint missing")
# Check for important middleware
middleware = ["CorsLayer", "TraceLayer"]
for mw in middleware:
if mw in content:
print(f" ✅ {mw} middleware configured")
else:
print(f" ❌ {mw} middleware missing")
print(" ✅ Server configuration looks good")
return True
else:
print(" ❌ serve.rs file not found")
return False
def main():
"""Main test function"""
print("🔥 Inferno HTTP Server Functionality Test")
print("=" * 45)
# Test configuration first
test_server_configuration()
# Check if user wants to test with a running server
print("\n" + "=" * 45)
print("🚀 To test the live server functionality:")
print("1. Start the server in another terminal:")
print(" cd /Users/ryanrobson/git/inferno")
print(" cargo run -- serve")
print("2. Then run this test script again with --live flag")
print(" python3 test_server_functionality.py --live")
if len(sys.argv) > 1 and sys.argv[1] == "--live":
print("\n📡 Testing live server...")
tester = InfernoServerTester()
return tester.run_tests()
else:
print("\n✅ Configuration test completed.")
print("📋 Server endpoints that should be available:")
print(" • GET /health - Health check")
print(" • GET / - Server information")
print(" • GET /metrics - Prometheus metrics")
print(" • GET /v1/models - List models (OpenAI compatible)")
print(" • POST /v1/chat/completions - Chat API (OpenAI compatible)")
print(" • GET /ws/stream - WebSocket streaming")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)