22
22
23
23
MODEL_DATA = "s3://bucket/model.tar.gz"
24
24
MODEL_IMAGE = "mi"
25
- TIMESTAMP = "2017-10-10-14-14-15 "
25
+ TIMESTAMP = "2020-07-02-20-10-30-288 "
26
26
MODEL_NAME = "{}-{}" .format (MODEL_IMAGE , TIMESTAMP )
27
+ ENDPOINT_NAME = "endpoint-{}" .format (TIMESTAMP )
27
28
28
29
ACCELERATOR_TYPE = "ml.eia.medium"
29
30
INSTANCE_COUNT = 2
@@ -46,9 +47,8 @@ def sagemaker_session():
46
47
47
48
@patch ("sagemaker.production_variant" )
48
49
@patch ("sagemaker.model.Model.prepare_container_def" )
49
- @patch ("sagemaker.utils.name_from_image" )
50
- def test_deploy (name_from_image , prepare_container_def , production_variant , sagemaker_session ):
51
- name_from_image .return_value = MODEL_NAME
50
+ @patch ("sagemaker.utils.name_from_base" , return_value = MODEL_NAME )
51
+ def test_deploy (name_from_base , prepare_container_def , production_variant , sagemaker_session ):
52
52
production_variant .return_value = BASE_PRODUCTION_VARIANT
53
53
54
54
container_def = {"Image" : MODEL_IMAGE , "Environment" : {}, "ModelDataUrl" : MODEL_DATA }
@@ -57,7 +57,9 @@ def test_deploy(name_from_image, prepare_container_def, production_variant, sage
57
57
model = Model (MODEL_IMAGE , MODEL_DATA , role = ROLE , sagemaker_session = sagemaker_session )
58
58
model .deploy (instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT )
59
59
60
- name_from_image .assert_called_with (MODEL_IMAGE )
60
+ name_from_base .assert_called_with (MODEL_IMAGE )
61
+ assert 2 == name_from_base .call_count
62
+
61
63
prepare_container_def .assert_called_with (INSTANCE_TYPE , accelerator_type = None )
62
64
production_variant .assert_called_with (
63
65
MODEL_NAME , INSTANCE_TYPE , INSTANCE_COUNT , accelerator_type = None
@@ -77,9 +79,12 @@ def test_deploy(name_from_image, prepare_container_def, production_variant, sage
77
79
)
78
80
79
81
82
+ @patch ("sagemaker.utils.name_from_base" , return_value = ENDPOINT_NAME )
80
83
@patch ("sagemaker.model.Model._create_sagemaker_model" )
81
84
@patch ("sagemaker.production_variant" )
82
- def test_deploy_accelerator_type (production_variant , create_sagemaker_model , sagemaker_session ):
85
+ def test_deploy_accelerator_type (
86
+ production_variant , create_sagemaker_model , name_from_base , sagemaker_session
87
+ ):
83
88
model = Model (
84
89
MODEL_IMAGE , MODEL_DATA , role = ROLE , name = MODEL_NAME , sagemaker_session = sagemaker_session
85
90
)
@@ -100,7 +105,7 @@ def test_deploy_accelerator_type(production_variant, create_sagemaker_model, sag
100
105
)
101
106
102
107
sagemaker_session .endpoint_from_production_variants .assert_called_with (
103
- name = MODEL_NAME ,
108
+ name = ENDPOINT_NAME ,
104
109
production_variants = [production_variant_result ],
105
110
tags = None ,
106
111
kms_key = None ,
@@ -109,7 +114,6 @@ def test_deploy_accelerator_type(production_variant, create_sagemaker_model, sag
109
114
)
110
115
111
116
112
- @patch ("sagemaker.utils.name_from_image" , Mock ())
113
117
@patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
114
118
@patch ("sagemaker.production_variant" , return_value = BASE_PRODUCTION_VARIANT )
115
119
def test_deploy_endpoint_name (sagemaker_session ):
@@ -122,6 +126,7 @@ def test_deploy_endpoint_name(sagemaker_session):
122
126
initial_instance_count = INSTANCE_COUNT ,
123
127
)
124
128
129
+ assert endpoint_name == model .endpoint_name
125
130
sagemaker_session .endpoint_from_production_variants .assert_called_with (
126
131
name = endpoint_name ,
127
132
production_variants = [BASE_PRODUCTION_VARIANT ],
@@ -132,9 +137,57 @@ def test_deploy_endpoint_name(sagemaker_session):
132
137
)
133
138
134
139
140
+ @patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
141
+ @patch ("sagemaker.utils.name_from_base" )
142
+ @patch ("sagemaker.utils.base_from_name" )
143
+ @patch ("sagemaker.production_variant" )
144
+ def test_deploy_generates_endpoint_name_each_time_from_model_name (
145
+ production_variant , base_from_name , name_from_base , sagemaker_session
146
+ ):
147
+ model = Model (
148
+ MODEL_IMAGE , MODEL_DATA , name = MODEL_NAME , role = ROLE , sagemaker_session = sagemaker_session
149
+ )
150
+
151
+ model .deploy (
152
+ instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT ,
153
+ )
154
+ model .deploy (
155
+ instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT ,
156
+ )
157
+
158
+ base_from_name .assert_called_with (MODEL_NAME )
159
+ name_from_base .assert_called_with (base_from_name .return_value )
160
+ assert 2 == name_from_base .call_count
161
+
162
+
163
+ @patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
164
+ @patch ("sagemaker.utils.name_from_base" )
165
+ @patch ("sagemaker.utils.base_from_name" )
166
+ @patch ("sagemaker.production_variant" )
167
+ def test_deploy_generates_endpoint_name_each_time_from_base_name (
168
+ production_variant , base_from_name , name_from_base , sagemaker_session
169
+ ):
170
+ model = Model (MODEL_IMAGE , MODEL_DATA , role = ROLE , sagemaker_session = sagemaker_session )
171
+
172
+ base_name = "foo"
173
+ model ._base_name = base_name
174
+
175
+ model .deploy (
176
+ instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT ,
177
+ )
178
+ model .deploy (
179
+ instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT ,
180
+ )
181
+
182
+ base_from_name .assert_not_called ()
183
+ name_from_base .assert_called_with (base_name )
184
+ assert 2 == name_from_base .call_count
185
+
186
+
187
+ @patch ("sagemaker.utils.name_from_base" , return_value = ENDPOINT_NAME )
135
188
@patch ("sagemaker.production_variant" , return_value = BASE_PRODUCTION_VARIANT )
136
189
@patch ("sagemaker.model.Model._create_sagemaker_model" )
137
- def test_deploy_tags (create_sagemaker_model , production_variant , sagemaker_session ):
190
+ def test_deploy_tags (create_sagemaker_model , production_variant , name_from_base , sagemaker_session ):
138
191
model = Model (
139
192
MODEL_IMAGE , MODEL_DATA , role = ROLE , name = MODEL_NAME , sagemaker_session = sagemaker_session
140
193
)
@@ -144,7 +197,7 @@ def test_deploy_tags(create_sagemaker_model, production_variant, sagemaker_sessi
144
197
145
198
create_sagemaker_model .assert_called_with (INSTANCE_TYPE , None , tags )
146
199
sagemaker_session .endpoint_from_production_variants .assert_called_with (
147
- name = MODEL_NAME ,
200
+ name = ENDPOINT_NAME ,
148
201
production_variants = [BASE_PRODUCTION_VARIANT ],
149
202
tags = tags ,
150
203
kms_key = None ,
@@ -154,8 +207,9 @@ def test_deploy_tags(create_sagemaker_model, production_variant, sagemaker_sessi
154
207
155
208
156
209
@patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
210
+ @patch ("sagemaker.utils.name_from_base" , return_value = ENDPOINT_NAME )
157
211
@patch ("sagemaker.production_variant" , return_value = BASE_PRODUCTION_VARIANT )
158
- def test_deploy_kms_key (production_variant , sagemaker_session ):
212
+ def test_deploy_kms_key (production_variant , name_from_base , sagemaker_session ):
159
213
model = Model (
160
214
MODEL_IMAGE , MODEL_DATA , role = ROLE , name = MODEL_NAME , sagemaker_session = sagemaker_session
161
215
)
@@ -164,7 +218,7 @@ def test_deploy_kms_key(production_variant, sagemaker_session):
164
218
model .deploy (instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT , kms_key = key )
165
219
166
220
sagemaker_session .endpoint_from_production_variants .assert_called_with (
167
- name = MODEL_NAME ,
221
+ name = ENDPOINT_NAME ,
168
222
production_variants = [BASE_PRODUCTION_VARIANT ],
169
223
tags = None ,
170
224
kms_key = key ,
@@ -174,16 +228,17 @@ def test_deploy_kms_key(production_variant, sagemaker_session):
174
228
175
229
176
230
@patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
231
+ @patch ("sagemaker.utils.name_from_base" , return_value = ENDPOINT_NAME )
177
232
@patch ("sagemaker.production_variant" , return_value = BASE_PRODUCTION_VARIANT )
178
- def test_deploy_async (production_variant , sagemaker_session ):
233
+ def test_deploy_async (production_variant , name_from_base , sagemaker_session ):
179
234
model = Model (
180
235
MODEL_IMAGE , MODEL_DATA , role = ROLE , name = MODEL_NAME , sagemaker_session = sagemaker_session
181
236
)
182
237
183
238
model .deploy (instance_type = INSTANCE_TYPE , initial_instance_count = INSTANCE_COUNT , wait = False )
184
239
185
240
sagemaker_session .endpoint_from_production_variants .assert_called_with (
186
- name = MODEL_NAME ,
241
+ name = ENDPOINT_NAME ,
187
242
production_variants = [BASE_PRODUCTION_VARIANT ],
188
243
tags = None ,
189
244
kms_key = None ,
@@ -193,8 +248,9 @@ def test_deploy_async(production_variant, sagemaker_session):
193
248
194
249
195
250
@patch ("sagemaker.model.Model._create_sagemaker_model" , Mock ())
251
+ @patch ("sagemaker.utils.name_from_base" , return_value = ENDPOINT_NAME )
196
252
@patch ("sagemaker.production_variant" , return_value = BASE_PRODUCTION_VARIANT )
197
- def test_deploy_data_capture_config (production_variant , sagemaker_session ):
253
+ def test_deploy_data_capture_config (production_variant , name_from_base , sagemaker_session ):
198
254
model = Model (
199
255
MODEL_IMAGE , MODEL_DATA , role = ROLE , name = MODEL_NAME , sagemaker_session = sagemaker_session
200
256
)
@@ -210,7 +266,7 @@ def test_deploy_data_capture_config(production_variant, sagemaker_session):
210
266
211
267
data_capture_config ._to_request_dict .assert_called_with ()
212
268
sagemaker_session .endpoint_from_production_variants .assert_called_with (
213
- name = MODEL_NAME ,
269
+ name = ENDPOINT_NAME ,
214
270
production_variants = [BASE_PRODUCTION_VARIANT ],
215
271
tags = None ,
216
272
kms_key = None ,
0 commit comments