Skip to content

Commit 44e23d7

Browse files
authored
Fixed unpatched object storage url bug in unit test (#340)
2 parents edca7d8 + c174d72 commit 44e23d7

File tree

9 files changed

+176
-22
lines changed

9 files changed

+176
-22
lines changed

ads/model/deployment/model_deployment_infrastructure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def _load_default_properties(self) -> Dict:
223223
defaults[self.CONST_REPLICA] = DEFAULT_REPLICA
224224

225225
if NB_SESSION_OCID:
226+
nb_session = None
226227
try:
227228
nb_session = DSCNotebookSession.from_ocid(NB_SESSION_OCID)
228229
except Exception as e:

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ dependencies = [
5757
"asteval>=0.9.25",
5858
"cerberus>=1.3.4",
5959
"cloudpickle>=1.6.0",
60-
"fsspec>=0.8.7",
60+
"fsspec>=0.8.7,<2023.9.1", # v2.9.1 introduced issues, releved by unit tests
6161
"gitpython>=3.1.2",
6262
"jinja2>=2.11.2",
6363
"matplotlib>=3.1.3",
@@ -109,7 +109,7 @@ onnx = [
109109
"lightgbm==3.3.1",
110110
"onnx>=1.12.0",
111111
"onnxmltools>=1.10.0",
112-
"onnxruntime>=1.10.0",
112+
"onnxruntime>=1.10.0,<1.16", # v1.16 introduced issues https://github.com/microsoft/onnxruntime/issues/17631, releved by unit tests
113113
"oracle_ads[viz]",
114114
"protobuf<=3.20",
115115
"skl2onnx>=1.10.4",

tests/unitary/with_extras/model/test_artifact.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,23 @@ def test_prepare_runtime_yaml_inference_training(
138138
(TrainingEnvInfo, mlcpu_path_cust, None, None, training_info_cust),
139139
],
140140
)
141+
@patch("ads.model.runtime.env_info.get_service_packs")
141142
def test__populate_env_info_inference(
142-
self, env_info_class, conda_pack, bucketname, namespace, expected_env_info
143+
self, mock_get_service_packs, env_info_class, conda_pack, bucketname, namespace, expected_env_info
143144
):
144145
"""test _populate_env_info."""
146+
env_path = (
147+
expected_env_info.inference_env_path if isinstance(expected_env_info, InferenceEnvInfo)
148+
else expected_env_info.training_env_path
149+
)
150+
mock_get_service_packs.return_value = (
151+
{
152+
env_path : ("mlcpuv1", "3.6")
153+
},
154+
{
155+
"mlcpuv1" : (env_path, "3.6")
156+
}
157+
)
145158
env_info = self.artifact._populate_env_info(
146159
env_info_class,
147160
conda_pack=conda_pack,

tests/unitary/with_extras/model/test_env_info.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,16 @@ def test_from_slug_prod_sp(self):
177177
info.training_env_type = "service_pack"
178178
info.training_python_version = "3.6"
179179

180-
def test_from_slug_not_exist(self):
180+
@patch("ads.model.runtime.env_info.get_service_packs")
181+
def test_from_slug_not_exist(self, mock_get_service_packs):
182+
mock_get_service_packs.return_value = (
183+
{
184+
"test_path" : ("mlcpuv1", "3.6"),
185+
},
186+
{
187+
"mlcpuv1" : ("test_path", "3.6"),
188+
}
189+
)
181190
with pytest.warns(UserWarning, match="not a service pack"):
182191
TrainingEnvInfo.from_slug(
183192
"not_exist", namespace="ociodscdev", bucketname="service-conda-packs"
@@ -256,7 +265,16 @@ def test_from_slug_prod_sp(self):
256265
info.inference_env_type = "service_pack"
257266
info.inference_python_version = "3.6"
258267

259-
def test_from_slug_not_exist(self):
268+
@patch("ads.model.runtime.env_info.get_service_packs")
269+
def test_from_slug_not_exist(self, mock_get_service_packs):
270+
mock_get_service_packs.return_value = (
271+
{
272+
"test_path" : ("mlcpuv1", "3.6"),
273+
},
274+
{
275+
"mlcpuv1" : ("test_path", "3.6"),
276+
}
277+
)
260278
with pytest.warns(UserWarning, match="not a service pack"):
261279
InferenceEnvInfo.from_slug(
262280
"not_exist", namespace="ociodscdev", bucketname="service-conda-packs"

tests/unitary/with_extras/model/test_generic_model.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,29 @@ def test_prepare_fail(self, mock_handle_model_file_name):
279279
"oci://service-conda-packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
280280
)
281281

282+
@patch("ads.model.runtime.env_info.get_service_packs")
282283
@patch("ads.common.auth.default_signer")
283-
def test_prepare_both_conda_env(self, mock_signer):
284+
def test_prepare_both_conda_env(self, mock_signer, mock_get_service_packs):
284285
"""prepare a model by only providing inference conda env."""
286+
inference_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
287+
inference_python_version="3.6"
288+
training_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Oracle_Database_for_CPU_Python_3.7/1.0/database_p37_cpu_v1"
289+
training_python_version="3.7"
290+
mock_get_service_packs.return_value = (
291+
{
292+
inference_conda_env : ("mlcpuv1", inference_python_version),
293+
training_conda_env : ("database_p37_cpu_v1", training_python_version)
294+
},
295+
{
296+
"mlcpuv1" : (inference_conda_env, inference_python_version),
297+
"database_p37_cpu_v1" : (training_conda_env, training_python_version)
298+
}
299+
)
285300
self.generic_model.prepare(
286-
inference_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1",
287-
inference_python_version="3.6",
288-
training_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Oracle_Database_for_CPU_Python_3.7/1.0/database_p37_cpu_v1",
289-
training_python_version="3.7",
301+
inference_conda_env=inference_conda_env,
302+
inference_python_version=inference_python_version,
303+
training_conda_env=training_conda_env,
304+
training_python_version=training_python_version,
290305
model_file_name="fake_model_name",
291306
force_overwrite=True,
292307
)
@@ -349,8 +364,19 @@ def test_reload(self):
349364

350365
@patch.object(GenericModel, "_random_display_name", return_value="test_name")
351366
@patch.object(DataScienceModel, "create")
352-
def test_save(self, mock_dsc_model_create, mock__random_display_name):
367+
@patch("ads.model.runtime.env_info.get_service_packs")
368+
def test_save(self, mock_get_service_packs, mock_dsc_model_create, mock__random_display_name):
353369
"""test saving a model to artifact."""
370+
inference_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
371+
inference_python_version="3.7"
372+
mock_get_service_packs.return_value = (
373+
{
374+
inference_conda_env : ("dataexpl_p37_cpu_v3", inference_python_version),
375+
},
376+
{
377+
"dataexpl_p37_cpu_v3" : (inference_conda_env, inference_python_version),
378+
}
379+
)
354380
mock_dsc_model_create.return_value = MagicMock(id="fake_id")
355381
self.generic_model.prepare(
356382
inference_conda_env="dataexpl_p37_cpu_v3",
@@ -360,7 +386,7 @@ def test_save(self, mock_dsc_model_create, mock__random_display_name):
360386
force_overwrite=True,
361387
training_id=None,
362388
)
363-
self.generic_model.save()
389+
self.generic_model.save(ignore_introspection=True)
364390
assert self.generic_model.model_id is not None and isinstance(
365391
self.generic_model.model_id, str
366392
)
@@ -371,8 +397,19 @@ def test_save(self, mock_dsc_model_create, mock__random_display_name):
371397
parallel_process_count=utils.DEFAULT_PARALLEL_PROCESS_COUNT,
372398
)
373399

374-
def test_save_not_implemented_error(self):
400+
@patch("ads.model.runtime.env_info.get_service_packs")
401+
def test_save_not_implemented_error(self, mock_get_service_packs):
375402
"""test saving a model to artifact."""
403+
inference_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
404+
inference_python_version="3.7"
405+
mock_get_service_packs.return_value = (
406+
{
407+
inference_conda_env : ("dataexpl_p37_cpu_v3", inference_python_version),
408+
},
409+
{
410+
"dataexpl_p37_cpu_v3" : (inference_conda_env, inference_python_version),
411+
}
412+
)
376413
self.generic_model._serialize = False
377414
self.generic_model.prepare(
378415
inference_conda_env="dataexpl_p37_cpu_v3",

tests/unitary/with_extras/model/test_model_deployment_details.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import os
77
from unittest import TestCase
8+
from unittest.mock import patch
89

910
import yaml
1011
from ads.model.runtime.model_deployment_details import ModelDeploymentDetails
@@ -27,7 +28,18 @@ def setUpClass(cls):
2728
with open(os.path.join(curr_dir, "runtime_fail.yaml"), encoding="utf-8") as rt:
2829
cls.runtime_dict_fail = yaml.load(rt, loader)
2930

30-
def test_from_dict(self):
31+
@patch("ads.model.runtime.env_info.get_service_packs")
32+
def test_from_dict(self, mock_get_service_packs):
33+
inference_conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
34+
inference_python_version="3.6"
35+
mock_get_service_packs.return_value = (
36+
{
37+
inference_conda_env : ("mlcpuv1", inference_python_version),
38+
},
39+
{
40+
"mlcpuv1" : (inference_conda_env, inference_python_version),
41+
}
42+
)
3143
model_deployment = ModelDeploymentDetails.from_dict(
3244
self.runtime_dict["MODEL_DEPLOYMENT"]
3345
)

tests/unitary/with_extras/model/test_model_metadata_mixin.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Copyright (c) 2022, 2023 Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

6+
from unittest.mock import patch
67
import numpy as np
78
import pytest
89
from sklearn import datasets, linear_model
@@ -104,7 +105,19 @@ def test_metadata_generic_model_container_runtime(self):
104105
)
105106
assert model.metadata_provenance.training_id is None
106107

107-
def test_metadata_sklearn_model(self):
108+
@patch("ads.model.runtime.env_info.get_service_packs")
109+
def test_metadata_sklearn_model(self, mock_get_service_packs):
110+
conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
111+
python_version="3.7"
112+
mock_get_service_packs.return_value = (
113+
{
114+
conda_env : ("dataexpl_p37_cpu_v3", python_version),
115+
},
116+
{
117+
"dataexpl_p37_cpu_v3" : (conda_env, python_version),
118+
119+
}
120+
)
108121
model = SklearnModel(self.rgr, artifact_dir="./test_sklearn")
109122
model.prepare(
110123
inference_conda_env="dataexpl_p37_cpu_v3",
@@ -146,7 +159,19 @@ def test_metadata_sklearn_model(self):
146159
)
147160
assert model.metadata_provenance.training_id is None
148161

149-
def test_metadata_xgboost_model(self):
162+
@patch("ads.model.runtime.env_info.get_service_packs")
163+
def test_metadata_xgboost_model(self, mock_get_service_packs):
164+
conda_env="oci://service-conda-packs@ociodscdev/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
165+
python_version="3.7"
166+
mock_get_service_packs.return_value = (
167+
{
168+
conda_env : ("dataexpl_p37_cpu_v3", python_version),
169+
},
170+
{
171+
"dataexpl_p37_cpu_v3" : (conda_env, python_version),
172+
173+
}
174+
)
150175
model = XGBoostModel(self.xgb_rgr, artifact_dir="./test_xgboost")
151176
model.prepare(
152177
inference_conda_env="dataexpl_p37_cpu_v3",
@@ -192,7 +217,7 @@ def test_metadata_xgboost_model(self):
192217
assert model.metadata_provenance.training_id is None
193218
assert (
194219
model.runtime_info.model_deployment.inference_conda_env.inference_env_path
195-
== "oci://service-conda-packs@id19sfcrra6z/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
220+
== "oci://service-conda-packs@ociodscdev/service_pack/cpu/Data_Exploration_and_Manipulation_for_CPU_Python_3.7/3.0/dataexpl_p37_cpu_v3"
196221
)
197222

198223
def teardown_method(self):

tests/unitary/with_extras/model/test_model_provenance_details.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

66
import os
7+
from unittest.mock import patch
78

89
import yaml
910
from ads.model.runtime.model_provenance_details import (
@@ -57,8 +58,19 @@ def setup_class(cls):
5758
with open(os.path.join(curr_dir, "runtime_fail.yaml"), encoding="utf-8") as rt:
5859
cls.runtime_dict_fail = yaml.load(rt, loader)
5960

60-
def test_from_dict(self):
61-
61+
@patch("ads.model.runtime.env_info.get_service_packs")
62+
def test_from_dict(self, mock_get_service_packs):
63+
conda_env="oci://service_conda_packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
64+
python_version="3.6"
65+
mock_get_service_packs.return_value = (
66+
{
67+
conda_env : ("mlcpuv1", python_version),
68+
},
69+
{
70+
"mlcpuv1" : (conda_env, python_version),
71+
72+
}
73+
)
6274
model_provenance = ModelProvenanceDetails.from_dict(
6375
self.runtime_dict["MODEL_PROVENANCE"]
6476
)

tests/unitary/with_extras/model/test_runtime_info.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,19 @@ def test__validate_dict_fail(self):
3636
with pytest.raises(AssertionError):
3737
RuntimeInfo._validate_dict(self.runtime_dict_fail)
3838

39-
def test_from_yaml(self):
39+
@patch("ads.model.runtime.env_info.get_service_packs")
40+
def test_from_yaml(self, mock_get_service_packs):
41+
conda_env="oci://service_conda_packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
42+
python_version="3.6"
43+
mock_get_service_packs.return_value = (
44+
{
45+
conda_env : ("mlcpuv1", python_version),
46+
},
47+
{
48+
"mlcpuv1" : (conda_env, python_version),
49+
50+
}
51+
)
4052
runtime_info = RuntimeInfo.from_yaml(
4153
uri=os.path.join(self.curr_dir, "runtime.yaml")
4254
)
@@ -95,7 +107,19 @@ def test_from_yaml(self):
95107

96108
@patch.object(InferenceEnvInfo, "_validate", side_effect=DocumentError)
97109
@patch.object(TrainingEnvInfo, "_validate", side_effect=DocumentError)
98-
def test_from_yaml_fail(self, mock_inference, mock_training):
110+
@patch("ads.model.runtime.env_info.get_service_packs")
111+
def test_from_yaml_fail(self, mock_get_service_packs, mock_inference, mock_training):
112+
conda_env="oci://service_conda_packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
113+
python_version="3.6"
114+
mock_get_service_packs.return_value = (
115+
{
116+
conda_env : ("mlcpuv1", python_version),
117+
},
118+
{
119+
"mlcpuv1" : (conda_env, python_version),
120+
121+
}
122+
)
99123
with pytest.raises(DocumentError):
100124
RuntimeInfo.from_yaml(uri=os.path.join(self.curr_dir, "runtime_fail.yaml"))
101125

@@ -119,7 +143,19 @@ def test_from_yaml_wrong_format(self, mock_provenance, mock_deployment):
119143
with pytest.raises(FileNotFoundError):
120144
RuntimeInfo.from_yaml(uri=os.path.join(self.curr_dir, "fake.yaml"))
121145

122-
def test_from_and_to_yaml_file(self):
146+
@patch("ads.model.runtime.env_info.get_service_packs")
147+
def test_from_and_to_yaml_file(self, mock_get_service_packs):
148+
conda_env="oci://service_conda_packs@ociodscdev/service_pack/cpu/General_Machine_Learning_for_CPUs/1.0/mlcpuv1"
149+
python_version="3.6"
150+
mock_get_service_packs.return_value = (
151+
{
152+
conda_env : ("mlcpuv1", python_version),
153+
},
154+
{
155+
"mlcpuv1" : (conda_env, python_version),
156+
157+
}
158+
)
123159
runtime = RuntimeInfo.from_yaml(uri=os.path.join(self.curr_dir, "runtime.yaml"))
124160
runtime.to_yaml(uri=os.path.join(self.curr_dir, "runtime_copy.yaml"))
125161
runtime_copy = RuntimeInfo.from_yaml(

0 commit comments

Comments
 (0)