|
18 | 18 | - e2e-test-pkgs must be present at samples/python/e2e-test-pkgs |
19 | 19 | - The nemotron model must be available in e2e-test-pkgs/models/ |
20 | 20 | - Native DLLs (Core.dll, onnxruntime.dll, onnxruntime-genai.dll) must be present |
21 | | -
|
22 | | -DLL preloading and the ``e2e_manager`` fixture are provided by ``test/conftest.py``. |
23 | 21 | """ |
24 | 22 |
|
25 | 23 | from __future__ import annotations |
26 | 24 |
|
27 | 25 | import math |
| 26 | +import os |
28 | 27 | import struct |
29 | 28 | import sys |
30 | 29 | import threading |
| 30 | +from pathlib import Path |
31 | 31 |
|
32 | 32 | import pytest |
33 | 33 |
|
34 | 34 | from foundry_local_sdk.openai.live_audio_transcription_types import ( |
35 | 35 | LiveAudioTranscriptionResponse, |
36 | 36 | ) |
37 | 37 |
|
38 | | -# Import shared helper from conftest |
39 | | -from ..conftest import _get_e2e_test_pkgs_path |
| 38 | + |
| 39 | +def _get_e2e_test_pkgs_path(): |
| 40 | + """Locate the e2e-test-pkgs directory by walking up from this file.""" |
| 41 | + current = Path(__file__).resolve().parent |
| 42 | + while True: |
| 43 | + candidate = current / "samples" / "python" / "e2e-test-pkgs" |
| 44 | + if candidate.exists(): |
| 45 | + return candidate |
| 46 | + parent = current.parent |
| 47 | + if parent == current: |
| 48 | + return None |
| 49 | + current = parent |
40 | 50 |
|
41 | 51 |
|
42 | 52 | def _has_e2e_assets() -> bool: |
@@ -84,6 +94,115 @@ def _generate_sine_wave_pcm( |
84 | 94 | ] |
85 | 95 |
|
86 | 96 |
|
| 97 | +def _preload_and_init_e2e(): |
| 98 | + """Load DLLs from e2e-test-pkgs and initialize the SDK for E2E tests.""" |
| 99 | + import ctypes |
| 100 | + from foundry_local_sdk.configuration import Configuration |
| 101 | + from foundry_local_sdk.detail.core_interop import ( |
| 102 | + CoreInterop, |
| 103 | + RequestBuffer, |
| 104 | + ResponseBuffer, |
| 105 | + StreamingRequestBuffer, |
| 106 | + ) |
| 107 | + from foundry_local_sdk.detail.utils import NativeBinaryPaths, _get_ext |
| 108 | + from foundry_local_sdk.foundry_local_manager import FoundryLocalManager |
| 109 | + |
| 110 | + pkgs = _get_e2e_test_pkgs_path() |
| 111 | + if pkgs is None: |
| 112 | + return None, "e2e-test-pkgs directory not found" |
| 113 | + |
| 114 | + paths = NativeBinaryPaths( |
| 115 | + core=pkgs / "Microsoft.AI.Foundry.Local.Core.dll", |
| 116 | + ort=pkgs / "onnxruntime.dll", |
| 117 | + genai=pkgs / "onnxruntime-genai.dll", |
| 118 | + ) |
| 119 | + |
| 120 | + if not (paths.core.exists() and paths.ort.exists() and paths.genai.exists()): |
| 121 | + return None, "E2E DLLs not found" |
| 122 | + |
| 123 | + # Register directory so Core.dll can find ORT/GenAI via P/Invoke |
| 124 | + os.add_dll_directory(str(pkgs)) |
| 125 | + os.environ["ORT_LIB_PATH"] = str(paths.ort) |
| 126 | + |
| 127 | + # Pre-load ORT and GenAI so they are available when Core does P/Invoke |
| 128 | + try: |
| 129 | + ctypes.CDLL(str(paths.ort)) |
| 130 | + ctypes.CDLL(str(paths.genai)) |
| 131 | + except OSError as e: |
| 132 | + return None, f"Failed to load ORT/GenAI DLLs: {e}" |
| 133 | + |
| 134 | + # Load Core.dll and set up function signatures |
| 135 | + CoreInterop._flcore_library = ctypes.CDLL(str(paths.core)) |
| 136 | + lib = CoreInterop._flcore_library |
| 137 | + lib.execute_command.argtypes = [ctypes.POINTER(RequestBuffer), ctypes.POINTER(ResponseBuffer)] |
| 138 | + lib.execute_command.restype = None |
| 139 | + lib.free_response.argtypes = [ctypes.POINTER(ResponseBuffer)] |
| 140 | + lib.free_response.restype = None |
| 141 | + lib.execute_command_with_callback.argtypes = [ |
| 142 | + ctypes.POINTER(RequestBuffer), ctypes.POINTER(ResponseBuffer), |
| 143 | + ctypes.c_void_p, ctypes.c_void_p, |
| 144 | + ] |
| 145 | + lib.execute_command_with_callback.restype = None |
| 146 | + lib.execute_command_with_binary.argtypes = [ |
| 147 | + ctypes.POINTER(StreamingRequestBuffer), ctypes.POINTER(ResponseBuffer), |
| 148 | + ] |
| 149 | + lib.execute_command_with_binary.restype = None |
| 150 | + CoreInterop._initialized = True |
| 151 | + |
| 152 | + # Initialize FoundryLocalManager |
| 153 | + config = Configuration( |
| 154 | + app_name="FoundryLocalE2ETest", |
| 155 | + model_cache_dir=str(pkgs / "models"), |
| 156 | + additional_settings={"Bootstrap": "false"}, |
| 157 | + ) |
| 158 | + flcore_lib_name = f"Microsoft.AI.Foundry.Local.Core{_get_ext()}" |
| 159 | + config.foundry_local_core_path = str(paths.core_dir / flcore_lib_name) |
| 160 | + config.additional_settings["OrtLibraryPath"] = str(paths.ort) |
| 161 | + config.additional_settings["OrtGenAILibraryPath"] = str(paths.genai) |
| 162 | + |
| 163 | + FoundryLocalManager.instance = None |
| 164 | + FoundryLocalManager.initialize(config) |
| 165 | + |
| 166 | + return FoundryLocalManager.instance, None |
| 167 | + |
| 168 | + |
| 169 | +@pytest.fixture(scope="module") |
| 170 | +def e2e_manager(): |
| 171 | + """Initialize FoundryLocalManager with e2e-test-pkgs DLLs.""" |
| 172 | + from foundry_local_sdk.detail.core_interop import CoreInterop |
| 173 | + from foundry_local_sdk.foundry_local_manager import FoundryLocalManager |
| 174 | + |
| 175 | + CoreInterop._initialized = False |
| 176 | + CoreInterop._flcore_library = None |
| 177 | + CoreInterop._ort_library = None |
| 178 | + CoreInterop._genai_library = None |
| 179 | + FoundryLocalManager.instance = None |
| 180 | + |
| 181 | + try: |
| 182 | + mgr, error = _preload_and_init_e2e() |
| 183 | + except Exception as e: |
| 184 | + pytest.skip(f"Could not initialize: {e}") |
| 185 | + return |
| 186 | + |
| 187 | + if error: |
| 188 | + pytest.skip(error) |
| 189 | + return |
| 190 | + |
| 191 | + yield mgr |
| 192 | + |
| 193 | + # Teardown |
| 194 | + try: |
| 195 | + catalog = mgr.catalog |
| 196 | + for mv in catalog.get_loaded_models(): |
| 197 | + try: |
| 198 | + mv.unload() |
| 199 | + except Exception: |
| 200 | + pass |
| 201 | + except Exception: |
| 202 | + pass |
| 203 | + FoundryLocalManager.instance = None |
| 204 | + |
| 205 | + |
87 | 206 | class TestLiveAudioTranscriptionE2E: |
88 | 207 | """E2E test for live audio transcription with synthetic PCM audio.""" |
89 | 208 |
|
|
0 commit comments