-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_steps.py
More file actions
527 lines (366 loc) · 17.4 KB
/
test_steps.py
File metadata and controls
527 lines (366 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Unit tests for workflow steps."""
from __future__ import absolute_import
import pytest
from unittest.mock import Mock
from sagemaker.mlops.workflow.steps import CacheConfig
def test_cache_config_default():
config = CacheConfig()
assert config.enable_caching is False
assert config.expire_after is None
def test_cache_config_enabled():
config = CacheConfig(enable_caching=True, expire_after="P30D")
assert config.enable_caching is True
assert config.expire_after == "P30D"
assert config.config == {"CacheConfig": {"Enabled": True, "ExpireAfter": "P30D"}}
def test_training_step_requires_step_args():
from sagemaker.mlops.workflow.steps import TrainingStep
step = TrainingStep(name="training-step", step_args=None)
with pytest.raises(ValueError, match="step_args input is required"):
_ = step.arguments
def test_processing_step_requires_step_args():
from sagemaker.mlops.workflow.steps import ProcessingStep
with pytest.raises(ValueError, match="step_args is required"):
ProcessingStep(name="processing-step", step_args=None)
def test_transform_step_requires_step_args():
from sagemaker.mlops.workflow.steps import TransformStep
with pytest.raises(ValueError, match="step_args is required"):
TransformStep(name="transform-step", step_args=None)
def test_step_add_depends_on():
from sagemaker.mlops.workflow.steps import Step
step = Mock(spec=Step)
step.name = "test-step"
step._depends_on = None
Step.add_depends_on(step, ["other-step"])
assert step._depends_on == ["other-step"]
def test_step_depends_on_setter_with_none():
from sagemaker.mlops.workflow.steps import Step
step = Mock(spec=Step)
step.name = "test-step"
step._depends_on = ["existing"]
Step.depends_on.fset(step, None)
assert step._depends_on is None
def test_step_add_depends_on_empty_list():
from sagemaker.mlops.workflow.steps import Step
step = Mock(spec=Step)
step.name = "test-step"
step._depends_on = None
Step.add_depends_on(step, [])
assert step._depends_on is None
def test_step_add_depends_on_extends_existing():
from sagemaker.mlops.workflow.steps import Step
step = Mock(spec=Step)
step.name = "test-step"
step._depends_on = ["step1"]
Step.add_depends_on(step, ["step2", "step3"])
assert step._depends_on == ["step1", "step2", "step3"]
def test_step_get_step_name_from_str_undefined():
from sagemaker.mlops.workflow.steps import Step
with pytest.raises(ValueError, match="Step undefined-step is undefined"):
Step._get_step_name_from_str("undefined-step", {})
def test_step_get_step_name_from_str_step_collection():
from sagemaker.mlops.workflow.steps import Step
from sagemaker.mlops.workflow.step_collections import StepCollection
mock_step = Mock()
mock_step.name = "last-step"
mock_collection = Mock(spec=StepCollection)
mock_collection.steps = [Mock(), mock_step]
result = Step._get_step_name_from_str("collection", {"collection": mock_collection})
assert result == "last-step"
def test_step_get_step_name_from_str_regular_step():
from sagemaker.mlops.workflow.steps import Step
result = Step._get_step_name_from_str("step-name", {"step-name": Mock()})
assert result == "step-name"
def test_step_trim_experiment_config_with_display_name():
from sagemaker.mlops.workflow.steps import Step
request_dict = {
"ExperimentConfig": {
"TrialComponentDisplayName": "my-trial",
"ExperimentName": "my-experiment",
"TrialName": "my-trial-name"
}
}
Step._trim_experiment_config(request_dict)
assert request_dict["ExperimentConfig"] == {"TrialComponentDisplayName": "my-trial"}
def test_step_trim_experiment_config_without_display_name():
from sagemaker.mlops.workflow.steps import Step
request_dict = {
"ExperimentConfig": {
"ExperimentName": "my-experiment"
}
}
Step._trim_experiment_config(request_dict)
assert "ExperimentConfig" not in request_dict
def test_step_trim_experiment_config_no_config():
from sagemaker.mlops.workflow.steps import Step
request_dict = {"SomeOtherKey": "value"}
Step._trim_experiment_config(request_dict)
assert "ExperimentConfig" not in request_dict
def test_cache_config_without_expire_after():
config = CacheConfig(enable_caching=True)
assert config.config == {"CacheConfig": {"Enabled": True}}
def test_configurable_retry_step_add_retry_policy_empty():
from sagemaker.mlops.workflow.steps import TrainingStep
from sagemaker.mlops.workflow.retry import RetryPolicy
step = TrainingStep(name="test", step_args=None)
step.retry_policies = []
step.add_retry_policy(None)
assert step.retry_policies == []
def test_configurable_retry_step_add_retry_policy():
from sagemaker.mlops.workflow.steps import TrainingStep
from sagemaker.mlops.workflow.retry import RetryPolicy
step = TrainingStep(name="test", step_args=None)
step.retry_policies = None
policy = Mock(spec=RetryPolicy)
step.add_retry_policy(policy)
assert len(step.retry_policies) == 1
def test_configurable_retry_step_to_request_with_retry_policies():
from sagemaker.mlops.workflow.steps import TrainingStep
from sagemaker.mlops.workflow.retry import RetryPolicy
step = TrainingStep(name="test", step_args=None)
step._properties = Mock()
policy = Mock(spec=RetryPolicy)
policy.to_request.return_value = {"ExceptionType": ["ThrottlingException"]}
step.retry_policies = [policy]
with pytest.raises(ValueError):
request = step.to_request()
def test_step_find_dependencies_in_depends_on_list_with_step():
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
step1 = Mock(spec=Step)
step1.name = "step1"
step2 = Mock(spec=Step)
step2.name = "step2"
step2.step_type = StepTypeEnum.TRAINING
step2.depends_on = [step1]
dependencies = Step._find_dependencies_in_depends_on_list(step2, {})
assert "step1" in dependencies
def test_step_find_dependencies_in_depends_on_list_with_step_collection():
from sagemaker.mlops.workflow.steps import Step
from sagemaker.mlops.workflow.step_collections import StepCollection
last_step = Mock()
last_step.name = "last-step"
collection = Mock(spec=StepCollection)
collection.steps = [Mock(), last_step]
step = Mock(spec=Step)
step.name = "test-step"
step.depends_on = [collection]
dependencies = Step._find_dependencies_in_depends_on_list(step, {})
assert "last-step" in dependencies
def test_step_find_dependencies_in_depends_on_list_with_string():
from sagemaker.mlops.workflow.steps import Step
mock_other_step = Mock()
mock_other_step.name = "other-step"
step = Mock(spec=Step)
step.name = "test-step"
step.depends_on = ["other-step"]
step._get_step_name_from_str = Step._get_step_name_from_str
step_map = {"other-step": mock_other_step}
dependencies = Step._find_dependencies_in_depends_on_list(step, step_map)
assert "other-step" in dependencies
def test_step_validate_json_get_property_file_reference_invalid_step_type():
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
step = Mock(spec=Step)
step.name = "current-step"
processing_step = Mock()
processing_step.name = "training-step"
processing_step.step_type = StepTypeEnum.TRAINING
json_get = Mock(spec=JsonGet)
json_get.step_name = "training-step"
json_get.property_file = "property-file"
json_get.expr = "$.test"
step_map = {"training-step": processing_step}
with pytest.raises(ValueError, match="can only be evaluated on processing step outputs"):
Step._validate_json_get_property_file_reference(step, json_get, step_map)
def test_step_validate_json_get_property_file_reference_undefined_property_file():
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
from sagemaker.core.workflow.properties import PropertyFile
step = Mock(spec=Step)
step.name = "current-step"
processing_step = Mock()
processing_step.name = "processing-step"
processing_step.step_type = StepTypeEnum.PROCESSING
processing_step.property_files = []
json_get = Mock(spec=JsonGet)
json_get.step_name = "processing-step"
json_get.property_file = "undefined-file"
json_get.expr = "$.test"
step_map = {"processing-step": processing_step}
with pytest.raises(ValueError, match="is undefined in step"):
Step._validate_json_get_property_file_reference(step, json_get, step_map)
def test_step_validate_json_get_property_file_reference_missing_output():
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
from sagemaker.core.workflow.properties import PropertyFile
step = Mock(spec=Step)
step.name = "current-step"
prop_file = PropertyFile(name="prop-file", output_name="output1", path="path.json")
processing_step = Mock()
processing_step.name = "processing-step"
processing_step.step_type = StepTypeEnum.PROCESSING
processing_step.property_files = [prop_file]
processing_step.arguments = {
"ProcessingOutputConfig": {
"Outputs": [{"OutputName": "different-output"}]
}
}
json_get = Mock(spec=JsonGet)
json_get.step_name = "processing-step"
json_get.property_file = "prop-file"
json_get.expr = "$.test"
step_map = {"processing-step": processing_step}
with pytest.raises(ValueError, match="not found in processing step"):
Step._validate_json_get_property_file_reference(step, json_get, step_map)
def test_step_validate_json_get_property_file_reference_with_property_file_object():
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
from sagemaker.core.workflow.properties import PropertyFile
step = Mock(spec=Step)
step.name = "current-step"
prop_file = PropertyFile(name="prop-file", output_name="output1", path="path.json")
processing_step = Mock()
processing_step.name = "processing-step"
processing_step.step_type = StepTypeEnum.PROCESSING
processing_step.property_files = [prop_file]
processing_step.arguments = {
"ProcessingOutputConfig": {
"Outputs": [{"OutputName": "output1"}]
}
}
json_get = Mock(spec=JsonGet)
json_get.step_name = "processing-step"
json_get.property_file = prop_file
json_get.expr = "$.test"
step_map = {"processing-step": processing_step}
Step._validate_json_get_property_file_reference(step, json_get, step_map)
def test_step_validate_json_get_function_with_property_file():
from sagemaker.mlops.workflow.steps import Step
from sagemaker.core.workflow.functions import JsonGet
from sagemaker.core.workflow.properties import PropertyFile
step = Mock(spec=Step)
step.name = "current-step"
prop_file = PropertyFile(name="prop-file", output_name="output1", path="path.json")
processing_step = Mock()
processing_step.name = "processing-step"
processing_step.step_type = Mock()
processing_step.property_files = [prop_file]
processing_step.arguments = {
"ProcessingOutputConfig": {
"Outputs": [{"OutputName": "output1"}]
}
}
json_get = Mock(spec=JsonGet)
json_get.step_name = "processing-step"
json_get.property_file = prop_file
step_map = {"processing-step": processing_step}
Step._validate_json_get_function(step, json_get, step_map)
def test_step_find_dependencies_in_step_arguments_with_json_get():
from unittest.mock import patch
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
step1 = Mock(spec=Step)
step1.name = "step1"
step1.step_type = StepTypeEnum.PROCESSING
step1.property_files = []
step1.arguments = {}
json_get = Mock(spec=JsonGet)
json_get._referenced_steps = [step1]
json_get.property_file = None
step2 = Mock(spec=Step)
step2.name = "step2"
step2._validate_json_get_function = Mock()
step2._get_step_name_from_str = Step._get_step_name_from_str
obj = {"key": json_get}
with patch('sagemaker.mlops.workflow.steps.TYPE_CHECKING', False):
with patch.dict('sys.modules', {'sagemaker.mlops.workflow.function_step': Mock()}):
dependencies = Step._find_dependencies_in_step_arguments(step2, obj, {"step1": step1})
assert "step1" in dependencies
def test_step_find_dependencies_in_step_arguments_with_delayed_return():
from unittest.mock import patch
from sagemaker.mlops.workflow.steps import Step, StepTypeEnum
from sagemaker.core.workflow.functions import JsonGet
from sagemaker.core.helper.pipeline_variable import PipelineVariable
step1 = Mock(spec=Step)
step1.name = "step1"
step1.step_type = StepTypeEnum.PROCESSING
step1.property_files = []
step1.arguments = {}
json_get = Mock(spec=JsonGet)
json_get.property_file = None
delayed_return_class = type('DelayedReturn', (PipelineVariable,), {})
delayed_return = Mock(spec=delayed_return_class)
delayed_return._referenced_steps = [step1]
delayed_return._to_json_get = Mock(return_value=json_get)
delayed_return.__class__ = delayed_return_class
step2 = Mock(spec=Step)
step2.name = "step2"
step2._validate_json_get_function = Mock()
step2._get_step_name_from_str = Step._get_step_name_from_str
obj = {"key": delayed_return}
mock_module = Mock()
mock_module.DelayedReturn = delayed_return_class
with patch.dict('sys.modules', {'sagemaker.mlops.workflow.function_step': mock_module}):
dependencies = Step._find_dependencies_in_step_arguments(step2, obj, {"step1": step1})
assert "step1" in dependencies
def test_step_find_dependencies_in_step_arguments_with_string_reference():
from unittest.mock import patch
from sagemaker.mlops.workflow.steps import Step
from sagemaker.core.helper.pipeline_variable import PipelineVariable
step1 = Mock(spec=Step)
step1.name = "step1"
pipeline_var = Mock(spec=PipelineVariable)
pipeline_var._referenced_steps = ["step1"]
step2 = Mock(spec=Step)
step2.name = "step2"
step2._get_step_name_from_str = Step._get_step_name_from_str
obj = {"key": pipeline_var}
step_map = {"step1": step1}
delayed_return_class = type('DelayedReturn', (PipelineVariable,), {})
mock_module = Mock()
mock_module.DelayedReturn = delayed_return_class
with patch.dict('sys.modules', {'sagemaker.mlops.workflow.function_step': mock_module}):
dependencies = Step._find_dependencies_in_step_arguments(step2, obj, step_map)
assert "step1" in dependencies
def test_tuning_step_requires_step_args():
from sagemaker.mlops.workflow.steps import TuningStep
with pytest.raises(ValueError, match="step_args is required"):
TuningStep(name="tuning-step", step_args=None)
def test_tuning_step_get_top_model_s3_uri_with_prefix():
from sagemaker.mlops.workflow.steps import TuningStep
from sagemaker.core.workflow.pipeline_context import _JobStepArguments
step_args = Mock(spec=_JobStepArguments)
step_args.caller_name = "tune"
step = TuningStep(name="tuning-step", step_args=step_args)
result = step.get_top_model_s3_uri(top_k=0, s3_bucket="my-bucket", prefix="my-prefix")
from sagemaker.core.workflow.functions import Join
assert isinstance(result, Join)
def test_tuning_step_get_top_model_s3_uri_without_prefix():
from sagemaker.mlops.workflow.steps import TuningStep
from sagemaker.core.workflow.pipeline_context import _JobStepArguments
step_args = Mock(spec=_JobStepArguments)
step_args.caller_name = "tune"
step = TuningStep(name="tuning-step", step_args=step_args)
result = step.get_top_model_s3_uri(top_k=0, s3_bucket="my-bucket", prefix="")
from sagemaker.core.workflow.functions import Join
assert isinstance(result, Join)
def test_tuning_step_get_top_model_s3_uri_with_none_prefix():
from sagemaker.mlops.workflow.steps import TuningStep
from sagemaker.core.workflow.pipeline_context import _JobStepArguments
step_args = Mock(spec=_JobStepArguments)
step_args.caller_name = "tune"
step = TuningStep(name="tuning-step", step_args=step_args)
result = step.get_top_model_s3_uri(top_k=0, s3_bucket="my-bucket", prefix=None)
from sagemaker.core.workflow.functions import Join
assert isinstance(result, Join)