Skip to content

Commit d8bee3b

Browse files
committed
Add unit tests.
1 parent 4e1ecc0 commit d8bee3b

File tree

4 files changed

+184
-69
lines changed

4 files changed

+184
-69
lines changed

ads/model/deployment/model_deployment_infrastructure.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ModelDeploymentInfrastructure(Builder):
7676
Sets the access and predict log id of model deployment
7777
with_web_concurrency(web_concurrency)
7878
Sets the web concurrency of model deployment
79-
with_subnet_id(subnet_id)
79+
with_subnet_id(subnet_id)
8080
Sets the subnet id of model deployment
8181
8282
Example
@@ -211,10 +211,6 @@ def _load_default_properties(self) -> Dict:
211211
if PROJECT_OCID:
212212
defaults[self.CONST_PROJECT_ID] = PROJECT_OCID
213213

214-
import oci
215-
oci.data_science.models.NotebookSessionConfigurationDetails
216-
oci.data_science.models.NotebookSessionShapeConfigDetails
217-
218214
if NB_SESSION_OCID:
219215
try:
220216
nb_session = DSCNotebookSession.from_ocid(NB_SESSION_OCID)
@@ -599,7 +595,7 @@ def with_subnet_id(self, subnet_id: str) -> "ModelDeploymentInfrastructure":
599595
@property
600596
def subnet_id(self) -> str:
601597
"""The model deployment subnet id.
602-
598+
603599
Returns
604600
-------
605601
str

tests/unitary/default_setup/model_deployment/test_model_deployment_v2.py

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from ads.common.oci_logging import ConsolidatedLog, OCILog
1616
from ads.common.oci_mixin import OCIModelMixin
1717
from ads.model.deployment.common.utils import OCIClientManager, State
18+
from ads.common.oci_datascience import DSCNotebookSession
1819

1920
from ads.model.deployment.model_deployment import (
2021
ModelDeployment,
@@ -41,7 +42,7 @@
4142
InstanceConfiguration,
4243
ModelDeploymentInstanceShapeConfigDetails,
4344
FixedSizeScalingPolicy,
44-
#StreamConfigurationDetails,
45+
# StreamConfigurationDetails,
4546
OcirModelDeploymentEnvironmentConfigurationDetails,
4647
CategoryLogDetails,
4748
LogDetails,
@@ -50,13 +51,14 @@
5051
UpdateSingleModelDeploymentConfigurationDetails,
5152
UpdateOcirModelDeploymentEnvironmentConfigurationDetails,
5253
UpdateModelConfigurationDetails,
53-
#UpdateStreamConfigurationDetails,
54+
# UpdateStreamConfigurationDetails,
5455
)
5556
except (ImportError, AttributeError) as e:
5657
raise unittest.SkipTest(
5758
"Support for OCI ModelDeployment BYOC is not available. Skipping the ModelDeployment tests."
5859
)
5960

61+
NB_SESSION_OCID = "ocid1.datasciencenotebooksession.oc1.iad..<unique_ocid>"
6062

6163
OCI_MODEL_DEPLOYMENT_RESPONSE = oci.data_science.models.ModelDeployment(
6264
id="fakeid.datasciencemodeldeployment.oc1..xxx",
@@ -107,7 +109,7 @@
107109
),
108110
),
109111
model_deployment_url="model_deployment_url",
110-
#deployment_mode="STREAM_ONLY",
112+
# deployment_mode="STREAM_ONLY",
111113
)
112114

113115
OCI_MODEL_DEPLOYMENT_DICT = {
@@ -230,12 +232,26 @@
230232
.with_entrypoint(["python", "/opt/ds/model/deployed_model/api.py"])
231233
.with_server_port(5000)
232234
.with_health_check_port(5000)
233-
#.with_input_stream_ids(["123", "456"])
234-
#.with_output_stream_ids(["321", "654"])
235+
# .with_input_stream_ids(["123", "456"])
236+
# .with_output_stream_ids(["321", "654"])
235237
.with_model_uri("fakeid.datasciencemodel.oc1.iad.xxx")
236238
.with_deployment_mode("HTTPS_ONLY")
237239
)
238240

241+
nb_session = DSCNotebookSession(
242+
**{
243+
"notebook_session_configuration_details": {
244+
"shape": "VM.Standard.E4.Flex",
245+
"block_storage_size_in_gbs": 100,
246+
"subnet_id": "test_subnet_id",
247+
"notebook_session_shape_config_details": {
248+
"ocpus": 10.0,
249+
"memory_in_gbs": 36.0,
250+
},
251+
}
252+
}
253+
)
254+
239255

240256
class ModelDeploymentBYOCTestCase(unittest.TestCase):
241257
def initialize_model_deployment(self):
@@ -264,25 +280,30 @@ def initialize_model_deployment_from_spec(self):
264280
)
265281

266282
def initialize_model_deployment_triton_builder(self):
267-
infrastructure = ModelDeploymentInfrastructure()\
268-
.with_compartment_id("fakeid.compartment.oc1..xxx")\
269-
.with_project_id("fakeid.datascienceproject.oc1.iad.xxx")\
270-
.with_shape_name("VM.Standard.E4.Flex")\
271-
.with_replica(2)\
272-
.with_bandwidth_mbps(10)\
273-
274-
runtime = ModelDeploymentContainerRuntime()\
275-
.with_image("fake_image")\
276-
.with_server_port(5000)\
277-
.with_health_check_port(5000)\
278-
.with_model_uri("fake_model_id")\
279-
.with_env({"key":"value", "key2":"value2"})\
283+
infrastructure = (
284+
ModelDeploymentInfrastructure()
285+
.with_compartment_id("fakeid.compartment.oc1..xxx")
286+
.with_project_id("fakeid.datascienceproject.oc1.iad.xxx")
287+
.with_shape_name("VM.Standard.E4.Flex")
288+
.with_replica(2)
289+
.with_bandwidth_mbps(10)
290+
)
291+
runtime = (
292+
ModelDeploymentContainerRuntime()
293+
.with_image("fake_image")
294+
.with_server_port(5000)
295+
.with_health_check_port(5000)
296+
.with_model_uri("fake_model_id")
297+
.with_env({"key": "value", "key2": "value2"})
280298
.with_inference_server("triton")
299+
)
281300

282-
deployment = ModelDeployment()\
283-
.with_display_name("triton case")\
284-
.with_infrastructure(infrastructure)\
301+
deployment = (
302+
ModelDeployment()
303+
.with_display_name("triton case")
304+
.with_infrastructure(infrastructure)
285305
.with_runtime(runtime)
306+
)
286307
return deployment
287308

288309
def initialize_model_deployment_triton_yaml(self):
@@ -329,6 +350,37 @@ def initialize_model_deployment_from_kwargs(self):
329350
runtime=runtime,
330351
)
331352

353+
@patch(
354+
"ads.model.deployment.model_deployment_infrastructure.COMPARTMENT_OCID",
355+
infrastructure.compartment_id,
356+
)
357+
@patch(
358+
"ads.model.deployment.model_deployment_infrastructure.PROJECT_OCID",
359+
infrastructure.project_id,
360+
)
361+
@patch(
362+
"ads.model.deployment.model_deployment_infrastructure.NB_SESSION_OCID",
363+
NB_SESSION_OCID,
364+
)
365+
@patch.object(DSCNotebookSession, "from_ocid")
366+
def test__load_default_properties(self, mock_from_ocid):
367+
mock_default_properties = {
368+
ModelDeploymentInfrastructure.CONST_COMPARTMENT_ID: infrastructure.compartment_id,
369+
ModelDeploymentInfrastructure.CONST_PROJECT_ID: infrastructure.project_id,
370+
ModelDeploymentInfrastructure.CONST_SHAPE_NAME: infrastructure.shape_name,
371+
ModelDeploymentInfrastructure.CONST_BANDWIDTH_MBPS: 10,
372+
ModelDeploymentInfrastructure.CONST_SHAPE_CONFIG_DETAILS: {
373+
"ocpus": 10.0,
374+
"memory_in_gbs": 36.0,
375+
},
376+
ModelDeploymentInfrastructure.CONST_WEB_CONCURRENCY: 10,
377+
ModelDeploymentInfrastructure.CONST_REPLICA: 1,
378+
}
379+
380+
mock_from_ocid.return_value = nb_session
381+
assert infrastructure._load_default_properties() == mock_default_properties
382+
mock_from_ocid.assert_called_with(NB_SESSION_OCID)
383+
332384
def test_initialize_model_deployment(self):
333385
temp_model_deployment = self.initialize_model_deployment()
334386

@@ -406,14 +458,12 @@ def test_initialize_model_deployment_with_error(self):
406458
},
407459
)
408460

409-
410461
def test_initialize_model_deployment_with_spec_kwargs(self):
411462
model_deployment_kwargs = self.initialize_model_deployment_from_kwargs()
412463
model_deployment_builder = self.initialize_model_deployment()
413464

414465
assert model_deployment_kwargs.to_dict() == model_deployment_builder.to_dict()
415466

416-
417467
def test_initialize_model_deployment_triton_builder(self):
418468
temp_model_deployment = self.initialize_model_deployment_triton_builder()
419469
assert isinstance(
@@ -434,7 +484,6 @@ def test_initialize_model_deployment_triton_yaml(self):
434484
)
435485
assert temp_model_deployment.runtime.inference_server == "triton"
436486

437-
438487
def test_model_deployment_to_dict(self):
439488
model_deployment = self.initialize_model_deployment()
440489
assert model_deployment.to_dict() == {
@@ -796,9 +845,9 @@ def test_update_from_oci_model(self):
796845
environment_configuration_details = (
797846
model_deployment_configuration_details.environment_configuration_details
798847
)
799-
# stream_configuration_details = (
800-
# model_deployment_configuration_details.stream_configuration_details
801-
# )
848+
# stream_configuration_details = (
849+
# model_deployment_configuration_details.stream_configuration_details
850+
# )
802851
assert (
803852
runtime.environment_config_type
804853
== environment_configuration_details.environment_configuration_type
@@ -994,7 +1043,9 @@ def test_update_model_deployment_details(self, mock_prepare_artifact):
9941043
# == model_deployment.runtime.output_stream_ids
9951044
# )
9961045

997-
@patch.object(ModelDeploymentInfrastructure, "_load_default_properties", return_value={})
1046+
@patch.object(
1047+
ModelDeploymentInfrastructure, "_load_default_properties", return_value={}
1048+
)
9981049
def test_extract_from_oci_model(self, mock_load_default_properties):
9991050
infrastructure = ModelDeploymentInfrastructure()
10001051
runtime = ModelDeploymentContainerRuntime()
@@ -1050,7 +1101,7 @@ def test_extract_from_oci_model(self, mock_load_default_properties):
10501101
# "inputStreamIds": ["123", "456"],
10511102
# "outputStreamIds": ["321", "654"],
10521103
"modelUri": "fakeid.datasciencemodel.oc1.iad.xxx",
1053-
#"deploymentMode": "HTTPS_ONLY",
1104+
# "deploymentMode": "HTTPS_ONLY",
10541105
},
10551106
}
10561107

@@ -1157,9 +1208,7 @@ def test_activate(self, mock_activate):
11571208
model_deployment.dsc_model_deployment.id = "test_model_deployment_id"
11581209
model_deployment.activate(wait_for_completion=False)
11591210
mock_activate.assert_called_with(
1160-
wait_for_completion=False,
1161-
max_wait_time=1200,
1162-
poll_interval=10
1211+
wait_for_completion=False, max_wait_time=1200, poll_interval=10
11631212
)
11641213

11651214
@patch.object(
@@ -1174,9 +1223,7 @@ def test_deactivate(self, mock_deactivate):
11741223
model_deployment.dsc_model_deployment.id = "test_model_deployment_id"
11751224
model_deployment.deactivate(wait_for_completion=False)
11761225
mock_deactivate.assert_called_with(
1177-
wait_for_completion=False,
1178-
max_wait_time=1200,
1179-
poll_interval=10
1226+
wait_for_completion=False, max_wait_time=1200, poll_interval=10
11801227
)
11811228

11821229
@patch.object(
@@ -1191,9 +1238,7 @@ def test_delete(self, mock_delete):
11911238
model_deployment.dsc_model_deployment.id = "test_model_deployment_id"
11921239
model_deployment.delete(wait_for_completion=False)
11931240
mock_delete.assert_called_with(
1194-
wait_for_completion=False,
1195-
max_wait_time=1200,
1196-
poll_interval=10
1241+
wait_for_completion=False, max_wait_time=1200, poll_interval=10
11971242
)
11981243

11991244
@patch.object(OCIDataScienceMixin, "sync")

0 commit comments

Comments
 (0)