Skip to content

Commit de6bf18

Browse files
ruiren_microsoftCopilot
andcommitted
Move E2E infrastructure out of conftest.py into E2E test file
Per kunal-vaishnavi feedback: conftest.py should not contain E2E-specific helpers. Moved _get_e2e_test_pkgs_path, _preload_and_init_e2e, and e2e_manager fixture into test_live_audio_transcription_e2e.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 478e047 commit de6bf18

2 files changed

Lines changed: 123 additions & 136 deletions

File tree

sdk/python/test/conftest.py

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import os
1616
import logging
17-
import sys
1817

1918
import pytest
2019

@@ -25,20 +24,6 @@
2524

2625
logger = logging.getLogger(__name__)
2726

28-
29-
def _get_e2e_test_pkgs_path():
30-
"""Locate the e2e-test-pkgs directory by walking up from this file."""
31-
from pathlib import Path as _Path
32-
current = _Path(__file__).resolve().parent
33-
while True:
34-
candidate = current / "samples" / "python" / "e2e-test-pkgs"
35-
if candidate.exists():
36-
return candidate
37-
parent = current.parent
38-
if parent == current:
39-
return None
40-
current = parent
41-
4227
TEST_MODEL_ALIAS = "qwen2.5-0.5b"
4328
AUDIO_MODEL_ALIAS = "whisper-tiny"
4429
EMBEDDING_MODEL_ALIAS = "qwen3-0.6b-embedding-generic-cpu"
@@ -166,120 +151,3 @@ def core_interop(manager):
166151
def model_load_manager(manager):
167152
"""Return the ModelLoadManager from the session-scoped manager (internal, for component tests)."""
168153
return manager._model_load_manager
169-
170-
171-
# ---------------------------------------------------------------------------
172-
# E2E fixtures for live audio transcription tests
173-
# ---------------------------------------------------------------------------
174-
175-
def _preload_and_init_e2e():
176-
"""Load DLLs from e2e-test-pkgs and initialize the SDK for E2E tests.
177-
178-
Loads Core.dll, sets up FFI function signatures, and initializes
179-
FoundryLocalManager pointing at the e2e-test-pkgs models directory.
180-
"""
181-
import ctypes
182-
from foundry_local_sdk.detail.core_interop import (
183-
CoreInterop,
184-
RequestBuffer,
185-
ResponseBuffer,
186-
StreamingRequestBuffer,
187-
)
188-
from foundry_local_sdk.detail.utils import NativeBinaryPaths, _get_ext
189-
190-
pkgs = _get_e2e_test_pkgs_path()
191-
if pkgs is None:
192-
return None, "e2e-test-pkgs directory not found"
193-
194-
paths = NativeBinaryPaths(
195-
core=pkgs / "Microsoft.AI.Foundry.Local.Core.dll",
196-
ort=pkgs / "onnxruntime.dll",
197-
genai=pkgs / "onnxruntime-genai.dll",
198-
)
199-
200-
if not (paths.core.exists() and paths.ort.exists() and paths.genai.exists()):
201-
return None, "E2E DLLs not found"
202-
203-
# Register directory so Core.dll can find ORT/GenAI via P/Invoke
204-
os.add_dll_directory(str(pkgs))
205-
os.environ["ORT_LIB_PATH"] = str(paths.ort)
206-
207-
# Pre-load ORT and GenAI so they are available when Core does P/Invoke
208-
try:
209-
ctypes.CDLL(str(paths.ort))
210-
ctypes.CDLL(str(paths.genai))
211-
except OSError as e:
212-
return None, f"Failed to load ORT/GenAI DLLs: {e}"
213-
214-
# Load Core.dll and set up function signatures
215-
CoreInterop._flcore_library = ctypes.CDLL(str(paths.core))
216-
lib = CoreInterop._flcore_library
217-
lib.execute_command.argtypes = [ctypes.POINTER(RequestBuffer), ctypes.POINTER(ResponseBuffer)]
218-
lib.execute_command.restype = None
219-
lib.free_response.argtypes = [ctypes.POINTER(ResponseBuffer)]
220-
lib.free_response.restype = None
221-
lib.execute_command_with_callback.argtypes = [
222-
ctypes.POINTER(RequestBuffer), ctypes.POINTER(ResponseBuffer),
223-
ctypes.c_void_p, ctypes.c_void_p,
224-
]
225-
lib.execute_command_with_callback.restype = None
226-
lib.execute_command_with_binary.argtypes = [
227-
ctypes.POINTER(StreamingRequestBuffer), ctypes.POINTER(ResponseBuffer),
228-
]
229-
lib.execute_command_with_binary.restype = None
230-
CoreInterop._initialized = True
231-
232-
# Initialize FoundryLocalManager
233-
config = Configuration(
234-
app_name="FoundryLocalE2ETest",
235-
model_cache_dir=str(pkgs / "models"),
236-
additional_settings={"Bootstrap": "false"},
237-
)
238-
flcore_lib_name = f"Microsoft.AI.Foundry.Local.Core{_get_ext()}"
239-
config.foundry_local_core_path = str(paths.core_dir / flcore_lib_name)
240-
config.additional_settings["OrtLibraryPath"] = str(paths.ort)
241-
config.additional_settings["OrtGenAILibraryPath"] = str(paths.genai)
242-
243-
FoundryLocalManager.instance = None
244-
FoundryLocalManager.initialize(config)
245-
246-
return FoundryLocalManager.instance, None
247-
248-
249-
@pytest.fixture(scope="module")
250-
def e2e_manager():
251-
"""Initialize FoundryLocalManager with e2e-test-pkgs DLLs for E2E tests."""
252-
if not sys.platform.startswith("win"):
253-
pytest.skip("E2E test requires Windows")
254-
255-
from foundry_local_sdk.detail.core_interop import CoreInterop
256-
257-
CoreInterop._initialized = False
258-
CoreInterop._flcore_library = None
259-
CoreInterop._ort_library = None
260-
CoreInterop._genai_library = None
261-
FoundryLocalManager.instance = None
262-
263-
try:
264-
mgr, error = _preload_and_init_e2e()
265-
except Exception as e:
266-
pytest.skip(f"Could not initialize: {e}")
267-
return
268-
269-
if error:
270-
pytest.skip(error)
271-
return
272-
273-
yield mgr
274-
275-
# Teardown
276-
try:
277-
catalog = mgr.catalog
278-
for mv in catalog.get_loaded_models():
279-
try:
280-
mv.unload()
281-
except Exception:
282-
pass
283-
except Exception:
284-
pass
285-
FoundryLocalManager.instance = None

sdk/python/test/openai/test_live_audio_transcription_e2e.py

Lines changed: 123 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,35 @@
1818
- e2e-test-pkgs must be present at samples/python/e2e-test-pkgs
1919
- The nemotron model must be available in e2e-test-pkgs/models/
2020
- 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``.
2321
"""
2422

2523
from __future__ import annotations
2624

2725
import math
26+
import os
2827
import struct
2928
import sys
3029
import threading
30+
from pathlib import Path
3131

3232
import pytest
3333

3434
from foundry_local_sdk.openai.live_audio_transcription_types import (
3535
LiveAudioTranscriptionResponse,
3636
)
3737

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
4050

4151

4252
def _has_e2e_assets() -> bool:
@@ -84,6 +94,115 @@ def _generate_sine_wave_pcm(
8494
]
8595

8696

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+
87206
class TestLiveAudioTranscriptionE2E:
88207
"""E2E test for live audio transcription with synthetic PCM audio."""
89208

0 commit comments

Comments
 (0)