Skip to content

Commit a6be1b7

Browse files
author
Mohamed Zeidan
committed
test(serve): import the SMD entrypoint instead of exec-ing its source
Coverage could not attribute the entrypoint's lines when the test exec'd its source text, so the new code read as unhit (codecov/patch 64%) despite every line being exercised. The module reads its code directory from SAGEMAKER_INFERENCE_CODE_DIRECTORY, so it can be imported for real against a prepared temp dir and reloaded per case, which is both measurable and closer to how the model server loads it. Also cover the awaited branch of handler() with an AsyncCustomOrchestrator; custom_execution_inference.py is now at 100%.
1 parent 5f0a651 commit a6be1b7

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

‎sagemaker-serve/tests/unit/model_server/test_smd_custom_execution_inference.py‎

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import ast
2323
import asyncio
2424
import hashlib
25+
import importlib
2526
import importlib.util
2627
import json
2728
import os
@@ -33,11 +34,10 @@
3334

3435
import cloudpickle
3536

36-
from sagemaker.serve.spec.inference_base import CustomOrchestrator
37+
from sagemaker.serve.spec.inference_base import AsyncCustomOrchestrator, CustomOrchestrator
3738

38-
INFERENCE_SOURCE_PATH = Path(
39-
importlib.util.find_spec("sagemaker.serve.model_server.smd.custom_execution_inference").origin
40-
)
39+
MODULE_NAME = "sagemaker.serve.model_server.smd.custom_execution_inference"
40+
INFERENCE_SOURCE_PATH = Path(importlib.util.find_spec(MODULE_NAME).origin)
4141

4242

4343
class EchoOrchestrator(CustomOrchestrator):
@@ -47,6 +47,13 @@ def handle(self, data, context=None):
4747
return {"echo": data}
4848

4949

50+
class AsyncEchoOrchestrator(AsyncCustomOrchestrator):
51+
"""Minimal async orchestrator, to exercise the awaited handler branch."""
52+
53+
async def handle(self, data, context=None):
54+
return {"async_echo": data}
55+
56+
5057
class _Request:
5158
def __init__(self, body):
5259
self.body = body
@@ -62,15 +69,16 @@ def _write_artifacts(code_dir: Path, obj=None):
6269
)
6370

6471

65-
def _exec_inference(code_dir: Path) -> dict:
66-
"""Exec the entrypoint the way the container does and return its namespace."""
67-
namespace = {"__name__": "inference", "__file__": str(INFERENCE_SOURCE_PATH)}
72+
def _load_inference(code_dir: Path):
73+
"""Import the entrypoint against code_dir, running its import-time preflight.
74+
75+
The model server imports this file as ``inference``; importing the module for real
76+
(rather than exec-ing its source) keeps the import-time work attributable to the
77+
module under test.
78+
"""
6879
with patch.dict(os.environ, {"SAGEMAKER_INFERENCE_CODE_DIRECTORY": str(code_dir)}):
69-
exec( # noqa: S102 - mirrors how the model server imports inference.py
70-
compile(INFERENCE_SOURCE_PATH.read_text(), str(INFERENCE_SOURCE_PATH), "exec"),
71-
namespace,
72-
)
73-
return namespace
80+
module = importlib.import_module(MODULE_NAME)
81+
return importlib.reload(module)
7482

7583

7684
class TestCustomExecutionInferenceIsSelfContained(unittest.TestCase):
@@ -106,18 +114,18 @@ def setUp(self):
106114
self.temp_dir = tempfile.mkdtemp()
107115
self.code_dir = Path(self.temp_dir) / "code"
108116
_write_artifacts(self.code_dir)
109-
self.namespace = _exec_inference(self.code_dir)
117+
self.module = _load_inference(self.code_dir)
110118

111119
def tearDown(self):
112120
shutil.rmtree(self.temp_dir, ignore_errors=True)
113121

114122
def test_uses_env_var_when_set(self):
115123
with patch.dict(os.environ, {"SAGEMAKER_INFERENCE_CODE_DIRECTORY": "/some/code"}):
116-
self.assertEqual(str(self.namespace["_get_code_directory"]()), "/some/code")
124+
self.assertEqual(str(self.module._get_code_directory()), "/some/code")
117125

118126
def test_falls_back_to_default(self):
119127
with patch.dict(os.environ, {}, clear=True):
120-
self.assertEqual(str(self.namespace["_get_code_directory"]()), "/opt/ml/model/code")
128+
self.assertEqual(str(self.module._get_code_directory()), "/opt/ml/model/code")
121129

122130

123131
class TestPerformIntegrityCheck(unittest.TestCase):
@@ -127,8 +135,8 @@ def setUp(self):
127135
self.temp_dir = tempfile.mkdtemp()
128136
self.code_dir = Path(self.temp_dir) / "code"
129137
_write_artifacts(self.code_dir)
130-
self.namespace = _exec_inference(self.code_dir)
131-
self.check = self.namespace["_perform_integrity_check"]
138+
self.module = _load_inference(self.code_dir)
139+
self.check = self.module._perform_integrity_check
132140
self.metadata_path = self.code_dir / "metadata.json"
133141
self.buffer = (self.code_dir / "serve.pkl").read_bytes()
134142

@@ -179,19 +187,28 @@ def tearDown(self):
179187
def test_startup_and_handle(self):
180188
_write_artifacts(self.code_dir)
181189

182-
namespace = _exec_inference(self.code_dir)
190+
module = _load_inference(self.code_dir)
183191

184-
self.assertIsInstance(namespace["custom_orchestrator"], EchoOrchestrator)
185-
result = asyncio.run(namespace["handler"](_Request(b"hello")))
192+
self.assertIsInstance(module.custom_orchestrator, EchoOrchestrator)
193+
result = asyncio.run(module.handler(_Request(b"hello")))
186194
self.assertEqual(result, {"echo": b"hello"})
187195

196+
def test_startup_and_handle_async(self):
197+
_write_artifacts(self.code_dir, obj=(AsyncEchoOrchestrator(), None))
198+
199+
module = _load_inference(self.code_dir)
200+
201+
self.assertIsInstance(module.custom_orchestrator, AsyncEchoOrchestrator)
202+
result = asyncio.run(module.handler(_Request(b"hello")))
203+
self.assertEqual(result, {"async_echo": b"hello"})
204+
188205
def test_startup_fails_on_tampered_pickle(self):
189206
_write_artifacts(self.code_dir)
190207
serve_pkl = self.code_dir / "serve.pkl"
191208
serve_pkl.write_bytes(serve_pkl.read_bytes() + b"x")
192209

193210
with self.assertRaises(ValueError):
194-
_exec_inference(self.code_dir)
211+
_load_inference(self.code_dir)
195212

196213

197214
if __name__ == "__main__":

0 commit comments

Comments
 (0)