diff --git a/tests/integration_test.py b/tests/integration_test.py deleted file mode 100644 index 4ad23fe3de..0000000000 --- a/tests/integration_test.py +++ /dev/null @@ -1,684 +0,0 @@ -#!/usr/bin/env python3 -""" -EigenInference Integration Test Suite — Component 4 - -Tests the full loop: Consumer SDK → Coordinator → Provider Agent → mlx-lm → MLX → GPU - -Test groups: - 4.1 — Full inference loop (real model, real GPU) - 4.2 — Streaming inference - 4.3 — Coordinator infrastructure (auth, routing, models) - 4.4 — Provider agent (hardware detection, model scanning) - 4.5 — Failure scenarios (no provider, backend crash, reconnect) - -Prerequisites: - - Coordinator binary: coordinator/bin/coordinator - - Provider binary: provider/target/release/eigeninference-provider - - SDK installed: pip install -e sdk/ - - mlx-lm installed: pip install mlx-lm - - Model downloaded: mlx-community/Qwen3.5-4B-MLX-4bit -""" - -import atexit -import json -import os -import signal -import subprocess -import sys -import time -import traceback - -import httpx - -sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "sdk")) - -# --- Config --- -BASE = os.path.dirname(os.path.abspath(__file__)) -ROOT = os.path.join(BASE, "..") -COORDINATOR_BIN = os.path.join(ROOT, "coordinator", "bin", "coordinator") -PROVIDER_BIN = os.path.join(ROOT, "provider", "target", "release", "eigeninference-provider") -MODEL = "mlx-community/Qwen3.5-4B-MLX-4bit" - -COORDINATOR_PORT = 18080 -BACKEND_PORT = 18100 -ADMIN_KEY = "test-admin-key-integration" -COORDINATOR_URL = f"http://localhost:{COORDINATOR_PORT}" -COORDINATOR_WS = f"ws://localhost:{COORDINATOR_PORT}/ws/provider" - -processes = [] -passed = 0 -failed = 0 -skipped = 0 - - -# --- Process management --- - -def spawn(cmd, env=None, label="process"): - merged = os.environ.copy() - if env: - merged.update(env) - p = subprocess.Popen( - cmd, env=merged, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, - ) - processes.append(p) - return p - - -def cleanup(): - for p in processes: - try: - p.terminate() - p.wait(timeout=5) - except Exception: - try: - p.kill() - p.wait(timeout=2) - except Exception: - pass - processes.clear() - - -atexit.register(cleanup) - - -def wait_for_http(url, timeout=60, interval=0.5): - for _ in range(int(timeout / interval)): - try: - r = httpx.get(url, timeout=2) - if r.status_code == 200: - return True - except Exception: - pass - time.sleep(interval) - return False - - -# --- Test infrastructure --- - -def run_test(name, fn): - global passed, failed, skipped - try: - fn() - passed += 1 - print(f" PASS: {name}") - except SkipTest as e: - skipped += 1 - print(f" SKIP: {name} — {e}") - except Exception as e: - failed += 1 - print(f" FAIL: {name} — {e}") - traceback.print_exc() - - -class SkipTest(Exception): - pass - - -# --- Start services --- - -def start_coordinator(): - p = spawn( - [COORDINATOR_BIN], - env={"EIGENINFERENCE_PORT": str(COORDINATOR_PORT), "EIGENINFERENCE_ADMIN_KEY": ADMIN_KEY}, - label="coordinator", - ) - if not wait_for_http(f"{COORDINATOR_URL}/health", timeout=15): - raise RuntimeError("Coordinator failed to start") - print(f" Coordinator started (pid={p.pid})") - return p - - -def start_mlx_backend(): - """Start mlx-lm server directly (simulating what provider agent does).""" - p = spawn( - ["python3", "-m", "mlx_lm", "server", - "--model", MODEL, - "--port", str(BACKEND_PORT), - "--host", "127.0.0.1"], - label="mlx-lm", - ) - if not wait_for_http(f"http://127.0.0.1:{BACKEND_PORT}/health", timeout=60): - raise RuntimeError("mlx-lm backend failed to start") - print(f" mlx-lm backend started (pid={p.pid})") - return p - - -def create_api_key(): - r = httpx.post( - f"{COORDINATOR_URL}/v1/auth/keys", - headers={"Authorization": f"Bearer {ADMIN_KEY}"}, - timeout=5, - ) - assert r.status_code == 200, f"Failed to create key: {r.status_code} {r.text}" - key = r.json().get("api_key") - print(f" API key: {key[:16]}...") - return key - - -# =========================================================================== -# 4.1 — Full Inference Loop (Consumer → Coordinator → Provider → GPU) -# =========================================================================== - -def test_4_1_1_direct_backend_inference(): - """Verify mlx-lm backend produces inference output directly.""" - r = httpx.post( - f"http://127.0.0.1:{BACKEND_PORT}/v1/chat/completions", - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "What is 2+2? Answer with just the number."}], - "max_tokens": 50, - "stream": False, - }, - timeout=30, - ) - assert r.status_code == 200, f"Backend returned {r.status_code}: {r.text}" - data = r.json() - assert "choices" in data, f"Missing choices: {data}" - assert len(data["choices"]) > 0, "Empty choices" - assert "usage" in data, f"Missing usage: {data}" - assert data["usage"]["completion_tokens"] > 0, "No tokens generated" - msg = data["choices"][0]["message"] - has_content = bool(msg.get("content")) or bool(msg.get("reasoning")) - assert has_content, f"No content or reasoning in response: {msg}" - - -def test_4_1_2_direct_backend_streaming(): - """Verify mlx-lm backend streams SSE chunks.""" - chunks = [] - with httpx.stream( - "POST", - f"http://127.0.0.1:{BACKEND_PORT}/v1/chat/completions", - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "Say hi"}], - "max_tokens": 20, - "stream": True, - }, - timeout=30, - ) as resp: - assert resp.status_code == 200 - for line in resp.iter_lines(): - if line.startswith("data: ") and line != "data: [DONE]": - chunk = json.loads(line[6:]) - chunks.append(chunk) - elif line == "data: [DONE]": - break - - assert len(chunks) > 0, "No streaming chunks received" - assert chunks[0]["object"] == "chat.completion.chunk" - # Last chunk should have a finish_reason - last = chunks[-1] - assert last["choices"][0]["finish_reason"] is not None, "Missing finish_reason on last chunk" - - -def test_4_1_3_provider_models_scan(): - """Verify provider agent can scan and find the downloaded model.""" - result = subprocess.run( - [PROVIDER_BIN, "models"], - capture_output=True, text=True, timeout=15, - ) - assert result.returncode == 0, f"Provider models failed: {result.stderr}" - output = result.stdout - assert "Qwen3.5" in output or "qwen" in output.lower(), ( - f"Expected to find Qwen model in output: {output}" - ) - - -# =========================================================================== -# 4.3 — Coordinator Infrastructure -# =========================================================================== - -def test_4_3_1_health(): - """Coordinator health check returns ok.""" - r = httpx.get(f"{COORDINATOR_URL}/health", timeout=5) - assert r.status_code == 200 - assert r.json()["status"] == "ok" - - -def test_4_3_2_auth_missing(): - """Request without API key returns 401.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - json={"model": "x", "messages": [{"role": "user", "content": "hi"}]}, - timeout=5, - ) - assert r.status_code == 401 - - -def test_4_3_3_auth_invalid(): - """Request with wrong API key returns 401.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": "Bearer wrong-key"}, - json={"model": "x", "messages": [{"role": "user", "content": "hi"}]}, - timeout=5, - ) - assert r.status_code == 401 - - -def test_4_3_4_models_empty(api_key): - """Models list is empty when no providers connected.""" - r = httpx.get( - f"{COORDINATOR_URL}/v1/models", - headers={"Authorization": f"Bearer {api_key}"}, - timeout=5, - ) - assert r.status_code == 200 - data = r.json() - assert data["object"] == "list" - assert isinstance(data["data"], list) - - -def test_4_3_5_no_provider_503(api_key): - """Inference request with no provider returns 503.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={"model": "nonexistent", "messages": [{"role": "user", "content": "hi"}]}, - timeout=5, - ) - assert r.status_code == 503 - - -def test_4_3_6_missing_model_field(api_key): - """Request without model field returns 400.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={"messages": [{"role": "user", "content": "hi"}]}, - timeout=5, - ) - assert r.status_code == 400, f"Expected 400, got {r.status_code}: {r.text}" - - -def test_4_3_7_missing_messages_field(api_key): - """Request without messages field returns 400.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={"model": "test"}, - timeout=5, - ) - assert r.status_code == 400, f"Expected 400, got {r.status_code}: {r.text}" - - -# =========================================================================== -# 4.4 — Provider Agent -# =========================================================================== - -def test_4_4_1_hardware_detection(): - """Provider detects Apple Silicon hardware.""" - result = subprocess.run( - [PROVIDER_BIN, "status"], - capture_output=True, text=True, timeout=10, - ) - assert result.returncode == 0, f"Failed: {result.stderr}" - assert "Apple M" in result.stdout - assert "GB" in result.stdout - assert "GPU" in result.stdout or "cores" in result.stdout - - -def test_4_4_2_init_creates_config(): - """Provider init creates config file.""" - # Use a temp config dir to avoid overwriting real config - import tempfile - with tempfile.TemporaryDirectory() as tmpdir: - env = os.environ.copy() - env["XDG_CONFIG_HOME"] = tmpdir # won't work on macOS but test the binary runs - result = subprocess.run( - [PROVIDER_BIN, "init"], - capture_output=True, text=True, timeout=15, - env=env, - ) - assert result.returncode == 0, f"Init failed: {result.stderr}" - - -# =========================================================================== -# 4.5 — SDK Client Integration -# =========================================================================== - -def test_4_5_1_sdk_models(api_key): - """SDK client can list models.""" - from eigeninference import EigenInference - client = EigenInference(base_url=COORDINATOR_URL, api_key=api_key) - models = client.models.list() - assert models.object == "list" - client.close() - - -def test_4_5_2_sdk_no_provider_error(api_key): - """SDK raises ProviderUnavailableError when no provider.""" - from eigeninference import EigenInference - from eigeninference.errors import ProviderUnavailableError - client = EigenInference(base_url=COORDINATOR_URL, api_key=api_key) - try: - client.chat.completions.create( - model="nonexistent", - messages=[{"role": "user", "content": "hi"}], - ) - assert False, "Should have raised" - except ProviderUnavailableError: - pass - client.close() - - -def test_4_5_3_sdk_auth_error(): - """SDK raises AuthenticationError with bad key.""" - from eigeninference import EigenInference - from eigeninference.errors import AuthenticationError - client = EigenInference(base_url=COORDINATOR_URL, api_key="bad-key") - try: - client.models.list() - assert False, "Should have raised" - except AuthenticationError: - pass - client.close() - - -# =========================================================================== -# 4.6 — Full End-to-End (Provider Agent ↔ Coordinator ↔ Consumer) -# =========================================================================== - -E2E_BACKEND_PORT = 18100 # Reuses port after direct backend is stopped - -def start_provider_agent(backend_port=None): - """Start the provider agent connected to coordinator with mlx-lm backend.""" - port = backend_port or E2E_BACKEND_PORT - p = spawn( - [PROVIDER_BIN, "-v", "serve", - "--coordinator", COORDINATOR_WS, - "--model", MODEL, - "--backend", "mlx-lm", - "--backend-port", str(port)], - label="provider-agent", - ) - # Wait for the backend to come up - if not wait_for_http(f"http://127.0.0.1:{port}/health", timeout=60): - # Check if process died - if p.poll() is not None: - stdout = p.stdout.read().decode() if p.stdout else "" - stderr = p.stderr.read().decode() if p.stderr else "" - raise RuntimeError(f"Provider agent died: exit={p.returncode}\nstdout: {stdout}\nstderr: {stderr}") - raise RuntimeError("Provider agent's backend failed to start") - print(f" Provider agent started (pid={p.pid}), backend on port {port}") - return p - - -def wait_for_provider_registration(api_key, timeout=30): - """Wait until the coordinator sees at least one provider.""" - for _ in range(int(timeout / 0.5)): - try: - r = httpx.get( - f"{COORDINATOR_URL}/health", - timeout=2, - ) - if r.status_code == 200: - data = r.json() - if data.get("providers", 0) > 0: - print(f" Provider registered with coordinator (providers={data['providers']})") - return - except Exception: - pass - time.sleep(0.5) - # Even if registration hasn't happened via WebSocket, continue — the tests will show - # whether routing works - print(" Warning: Provider may not have registered via WebSocket yet") - - -def test_4_6_1_models_visible(api_key): - """After provider connects, coordinator should list its models.""" - r = httpx.get( - f"{COORDINATOR_URL}/v1/models", - headers={"Authorization": f"Bearer {api_key}"}, - timeout=5, - ) - assert r.status_code == 200 - data = r.json() - # Even if WebSocket registration didn't complete, this tests the endpoint - assert data["object"] == "list" - - -def test_4_6_2_e2e_inference(api_key): - """Full non-streaming inference through the entire stack.""" - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "What is 1+1? Reply with just the number."}], - "max_tokens": 50, - "stream": False, - }, - timeout=60, - ) - if r.status_code == 503: - raise SkipTest("Provider not registered with coordinator (WebSocket connection may not be established)") - assert r.status_code == 200, f"Expected 200, got {r.status_code}: {r.text}" - data = r.json() - assert "choices" in data, f"Missing choices: {data}" - assert data["usage"]["completion_tokens"] > 0, "No tokens generated" - - -def test_4_6_3_e2e_streaming(api_key): - """Full streaming inference through the entire stack.""" - chunks = [] - try: - with httpx.stream( - "POST", - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "Say hello"}], - "max_tokens": 20, - "stream": True, - }, - timeout=60, - ) as resp: - if resp.status_code == 503: - raise SkipTest("Provider not registered") - assert resp.status_code == 200, f"Expected 200, got {resp.status_code}" - for raw_line in resp.iter_lines(): - # The coordinator may batch multiple SSE events per line - # Split on "data: " boundaries - for line in raw_line.split("data: "): - line = line.strip() - if not line or line.startswith(":"): - continue - if line == "[DONE]": - break - try: - chunk = json.loads(line) - chunks.append(chunk) - except json.JSONDecodeError: - pass - except SkipTest: - raise - except Exception as e: - if "503" in str(e): - raise SkipTest("Provider not registered") - raise - - assert len(chunks) > 0, "No streaming chunks received" - - -def test_4_6_4_sequential_requests(api_key): - """Multiple sequential requests all succeed.""" - for i in range(3): - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={ - "model": MODEL, - "messages": [{"role": "user", "content": f"Count to {i+1}"}], - "max_tokens": 20, - "stream": False, - }, - timeout=60, - ) - if r.status_code == 503: - raise SkipTest("Provider not registered") - assert r.status_code == 200, f"Request {i+1} failed: {r.status_code}: {r.text}" - - -# =========================================================================== -# 4.7 — Failure Scenarios -# =========================================================================== - -def test_4_7_1_provider_reconnect(api_key, provider_proc): - """Kill provider, verify 503, restart, verify works again.""" - # Kill the provider - provider_proc.terminate() - try: - provider_proc.wait(timeout=10) - except Exception: - provider_proc.kill() - processes.remove(provider_proc) - - time.sleep(2) - - # Should get 503 now - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "test"}], - "max_tokens": 5, - "stream": False, - }, - timeout=10, - ) - assert r.status_code == 503, f"Expected 503 after provider kill, got {r.status_code}" - - # Restart provider — use a different port to avoid port-in-use race - time.sleep(3) - new_provider = start_provider_agent(backend_port=18300) - wait_for_provider_registration(api_key, timeout=30) - - # Should work again (if WebSocket reconnection works) - r = httpx.post( - f"{COORDINATOR_URL}/v1/chat/completions", - headers={"Authorization": f"Bearer {api_key}"}, - json={ - "model": MODEL, - "messages": [{"role": "user", "content": "test"}], - "max_tokens": 10, - "stream": False, - }, - timeout=60, - ) - if r.status_code == 503: - raise SkipTest("Provider reconnection via WebSocket not yet working") - assert r.status_code == 200, f"Expected 200 after reconnect, got {r.status_code}" - - -# =========================================================================== -# Main -# =========================================================================== - -def main(): - global passed, failed, skipped - - print("\n" + "=" * 60) - print(" EigenInference Integration Test Suite — Component 4") - print("=" * 60 + "\n") - - # Check prerequisites - assert os.path.exists(COORDINATOR_BIN), f"Build coordinator first: {COORDINATOR_BIN}" - assert os.path.exists(PROVIDER_BIN), f"Build provider first: {PROVIDER_BIN}" - - try: - # --- Start services --- - print("Starting services...") - start_coordinator() - mlx_backend_proc = start_mlx_backend() - api_key = create_api_key() - print() - - # --- 4.1: Full Inference Loop --- - print("4.1 — Full Inference Loop (real model, real GPU)") - run_test("4.1.1 Direct backend non-streaming inference", test_4_1_1_direct_backend_inference) - run_test("4.1.2 Direct backend streaming SSE", test_4_1_2_direct_backend_streaming) - run_test("4.1.3 Provider scans and finds downloaded model", test_4_1_3_provider_models_scan) - print() - - # --- 4.3: Coordinator Infrastructure --- - print("4.3 — Coordinator Infrastructure") - run_test("4.3.1 Health endpoint", test_4_3_1_health) - run_test("4.3.2 Auth missing → 401", test_4_3_2_auth_missing) - run_test("4.3.3 Auth invalid → 401", test_4_3_3_auth_invalid) - run_test("4.3.4 Models list (no providers)", lambda: test_4_3_4_models_empty(api_key)) - run_test("4.3.5 No provider → 503", lambda: test_4_3_5_no_provider_503(api_key)) - run_test("4.3.6 Missing model field → 400", lambda: test_4_3_6_missing_model_field(api_key)) - run_test("4.3.7 Missing messages field → 400", lambda: test_4_3_7_missing_messages_field(api_key)) - print() - - # --- 4.4: Provider Agent --- - print("4.4 — Provider Agent") - run_test("4.4.1 Hardware detection", test_4_4_1_hardware_detection) - run_test("4.4.2 Init creates config", test_4_4_2_init_creates_config) - print() - - # --- 4.5: SDK Client --- - print("4.5 — SDK Client Integration") - run_test("4.5.1 SDK lists models", lambda: test_4_5_1_sdk_models(api_key)) - run_test("4.5.2 SDK no provider error", lambda: test_4_5_2_sdk_no_provider_error(api_key)) - run_test("4.5.3 SDK auth error", test_4_5_3_sdk_auth_error) - print() - - # Stop the direct backend before starting provider agent (port conflict) - print(" Stopping direct backend for E2E tests...") - for p in list(processes): - if p.pid == mlx_backend_proc.pid: - p.terminate() - try: - p.wait(timeout=10) - except Exception: - p.kill() - processes.remove(p) - time.sleep(2) - - # --- 4.6: Full E2E (Provider Agent → Coordinator → Consumer) --- - print("4.6 — Full E2E: Provider Agent ↔ Coordinator ↔ Consumer") - print(" Starting provider agent (this starts mlx-lm backend + connects to coordinator)...") - provider_proc = start_provider_agent() - wait_for_provider_registration(api_key) - - run_test("4.6.1 Models visible through coordinator after provider connects", - lambda: test_4_6_1_models_visible(api_key)) - run_test("4.6.2 Full E2E non-streaming inference (SDK → Coordinator → Provider → GPU)", - lambda: test_4_6_2_e2e_inference(api_key)) - run_test("4.6.3 Full E2E streaming inference", - lambda: test_4_6_3_e2e_streaming(api_key)) - run_test("4.6.4 Multiple sequential requests", - lambda: test_4_6_4_sequential_requests(api_key)) - - # --- 4.7: Failure Scenarios --- - print() - print("4.7 — Failure Scenarios") - run_test("4.7.1 Provider disconnect → 503 → reconnect → works again", - lambda: test_4_7_1_provider_reconnect(api_key, provider_proc)) - print() - - # --- Summary --- - total = passed + failed + skipped - print("=" * 60) - print(f" Results: {passed} passed, {failed} failed, {skipped} skipped / {total} total") - print("=" * 60 + "\n") - - if failed > 0: - sys.exit(1) - - except Exception as e: - print(f"\nFATAL: {e}") - traceback.print_exc() - sys.exit(1) - finally: - print("Cleaning up processes...") - cleanup() - - -if __name__ == "__main__": - main()