33
33
from sagemaker .workflow .step_collections import StepCollection
34
34
from tests .unit .sagemaker .workflow .helpers import ordered , CustomStep
35
35
from sagemaker .local .local_session import LocalSession
36
+ from botocore .exceptions import ClientError
37
+
36
38
37
39
38
40
@pytest .fixture
@@ -173,10 +175,17 @@ def test_large_pipeline_update(sagemaker_session_mock, role_arn):
173
175
)
174
176
175
177
176
- def test_pipeline_upsert (sagemaker_session_mock , role_arn ):
177
- sagemaker_session_mock .sagemaker_client .describe_pipeline .return_value = {
178
- "PipelineArn" : "pipeline-arn"
179
- }
178
+ def test_pipeline_upsert_resource_already_exists (sagemaker_session_mock , role_arn ):
179
+
180
+ # case 1: resource already exists
181
+ def _raise_does_already_exists_client_error (** kwargs ):
182
+ response = {"Error" : {"Code" : "ValidationException" , "Message" : "Resource already exists." }}
183
+ raise ClientError (error_response = response , operation_name = "create_pipeline" )
184
+
185
+ sagemaker_session_mock .sagemaker_client .create_pipeline = Mock (
186
+ name = "create_pipeline" , side_effect = _raise_does_already_exists_client_error
187
+ )
188
+
180
189
sagemaker_session_mock .sagemaker_client .update_pipeline .return_value = {
181
190
"PipelineArn" : "pipeline-arn"
182
191
}
@@ -197,9 +206,12 @@ def test_pipeline_upsert(sagemaker_session_mock, role_arn):
197
206
]
198
207
pipeline .upsert (role_arn = role_arn , tags = tags )
199
208
200
- sagemaker_session_mock .sagemaker_client .create_pipeline .assert_not_called ()
209
+ sagemaker_session_mock .sagemaker_client .create_pipeline .assert_called_once_with (
210
+ PipelineName = "MyPipeline" , RoleArn = role_arn , PipelineDefinition = pipeline .definition (),
211
+ Tags = tags
212
+ )
201
213
202
- assert sagemaker_session_mock .sagemaker_client .update_pipeline .called_with (
214
+ sagemaker_session_mock .sagemaker_client .update_pipeline .assert_called_once_with (
203
215
PipelineName = "MyPipeline" , PipelineDefinition = pipeline .definition (), RoleArn = role_arn
204
216
)
205
217
assert sagemaker_session_mock .sagemaker_client .list_tags .called_with (
@@ -211,6 +223,89 @@ def test_pipeline_upsert(sagemaker_session_mock, role_arn):
211
223
ResourceArn = "mock_pipeline_arn" , Tags = tags
212
224
)
213
225
226
+ def test_pipeline_upsert_create_unexpected_failure (sagemaker_session_mock , role_arn ):
227
+
228
+ # case 2: unexpected failure on create
229
+ def _raise_unexpected_client_error (** kwargs ):
230
+ response = {
231
+ "Error" : {"Code" : "ValidationException" , "Message" : "Name does not satisfy expression." }
232
+ }
233
+ raise ClientError (error_response = response , operation_name = "foo" )
234
+
235
+ sagemaker_session_mock .sagemaker_client .create_pipeline = Mock (
236
+ name = "create_pipeline" , side_effect = _raise_unexpected_client_error
237
+ )
238
+
239
+ sagemaker_session_mock .sagemaker_client .update_pipeline .return_value = {
240
+ "PipelineArn" : "pipeline-arn"
241
+ }
242
+ sagemaker_session_mock .sagemaker_client .list_tags .return_value = {
243
+ "Tags" : [{"Key" : "dummy" , "Value" : "dummy_tag" }]
244
+ }
245
+
246
+ tags = [
247
+ {"Key" : "foo" , "Value" : "abc" },
248
+ {"Key" : "bar" , "Value" : "xyz" },
249
+ ]
250
+
251
+ pipeline = Pipeline (
252
+ name = "MyPipeline" ,
253
+ parameters = [],
254
+ steps = [],
255
+ sagemaker_session = sagemaker_session_mock ,
256
+ )
257
+
258
+ with pytest .raises (ClientError ):
259
+ pipeline .upsert (role_arn = role_arn , tags = tags )
260
+
261
+
262
+
263
+ sagemaker_session_mock .sagemaker_client .create_pipeline .assert_called_once_with (
264
+ PipelineName = "MyPipeline" , RoleArn = role_arn , PipelineDefinition = pipeline .definition (),
265
+ Tags = tags
266
+ )
267
+ sagemaker_session_mock .sagemaker_client .update_pipeline .assert_not_called ()
268
+ sagemaker_session_mock .sagemaker_client .list_tags .assert_not_called ()
269
+ sagemaker_session_mock .sagemaker_client .add_tags .assert_not_called ()
270
+
271
+ def test_pipeline_upsert_resourse_doesnt_exist (sagemaker_session_mock , role_arn ):
272
+
273
+ # case 3: resource does not exist
274
+ sagemaker_session_mock .sagemaker_client .create_pipeline = Mock (name = "create_pipeline" )
275
+
276
+ sagemaker_session_mock .sagemaker_client .update_pipeline .return_value = {
277
+ "PipelineArn" : "pipeline-arn"
278
+ }
279
+ sagemaker_session_mock .sagemaker_client .list_tags .return_value = {
280
+ "Tags" : [{"Key" : "dummy" , "Value" : "dummy_tag" }]
281
+ }
282
+
283
+ tags = [
284
+ {"Key" : "foo" , "Value" : "abc" },
285
+ {"Key" : "bar" , "Value" : "xyz" },
286
+ ]
287
+
288
+ pipeline = Pipeline (
289
+ name = "MyPipeline" ,
290
+ parameters = [],
291
+ steps = [],
292
+ sagemaker_session = sagemaker_session_mock ,
293
+ )
294
+
295
+ try :
296
+ pipeline .upsert (role_arn = role_arn , tags = tags )
297
+ except ClientError :
298
+ assert False , f"Unexpected ClientError raised"
299
+
300
+ sagemaker_session_mock .sagemaker_client .create_pipeline .assert_called_once_with (
301
+ PipelineName = "MyPipeline" , RoleArn = role_arn , PipelineDefinition = pipeline .definition (),
302
+ Tags = tags
303
+ )
304
+
305
+ sagemaker_session_mock .sagemaker_client .update_pipeline .assert_not_called ()
306
+ sagemaker_session_mock .sagemaker_client .list_tags .assert_not_called ()
307
+ sagemaker_session_mock .sagemaker_client .add_tags .assert_not_called ()
308
+
214
309
215
310
def test_pipeline_delete (sagemaker_session_mock ):
216
311
pipeline = Pipeline (
0 commit comments