|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +from unittest.mock import Mock, patch |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from sagemaker.core.spark.processing import PySparkProcessor |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_session(): |
| 23 | + session = Mock() |
| 24 | + session.boto_session = Mock() |
| 25 | + session.boto_session.region_name = "us-west-2" |
| 26 | + session.boto_region_name = "us-west-2" |
| 27 | + session.sagemaker_client = Mock() |
| 28 | + session.default_bucket = Mock(return_value="test-bucket") |
| 29 | + session.default_bucket_prefix = "sagemaker" |
| 30 | + session.expand_role = Mock(side_effect=lambda x: x) |
| 31 | + session.sagemaker_config = {} |
| 32 | + return session |
| 33 | + |
| 34 | + |
| 35 | +def _make_processor(mock_session): |
| 36 | + processor = PySparkProcessor( |
| 37 | + role="arn:aws:iam::123456789012:role/SageMakerRole", |
| 38 | + image_uri="test-image:latest", |
| 39 | + instance_count=1, |
| 40 | + instance_type="ml.m5.xlarge", |
| 41 | + sagemaker_session=mock_session, |
| 42 | + ) |
| 43 | + processor._current_job_name = "test-job" |
| 44 | + return processor |
| 45 | + |
| 46 | + |
| 47 | +class TestPySparkProcessorV3ProcessingInputs: |
| 48 | + @patch("sagemaker.core.spark.processing.S3Uploader.upload_string_as_file_body") |
| 49 | + def test_stage_configuration_builds_v3_processing_input(self, mock_upload, mock_session): |
| 50 | + processor = _make_processor(mock_session) |
| 51 | + |
| 52 | + config_input = processor._stage_configuration( |
| 53 | + [{"Classification": "spark-defaults", "Properties": {"spark.app.name": "test"}}] |
| 54 | + ) |
| 55 | + |
| 56 | + mock_upload.assert_called_once() |
| 57 | + assert config_input.input_name == processor._conf_container_input_name |
| 58 | + assert config_input.s3_input.s3_uri == ( |
| 59 | + "s3://test-bucket/sagemaker/test-job/input/conf/configuration.json" |
| 60 | + ) |
| 61 | + assert config_input.s3_input.local_path == "/opt/ml/processing/input/conf" |
| 62 | + |
| 63 | + @patch("sagemaker.core.spark.processing.S3Uploader.upload") |
| 64 | + def test_stage_submit_deps_builds_v3_processing_input_for_local_dependencies( |
| 65 | + self, mock_upload, mock_session, tmp_path |
| 66 | + ): |
| 67 | + processor = _make_processor(mock_session) |
| 68 | + dep_file = tmp_path / "dep.py" |
| 69 | + dep_file.write_text("print('dep')", encoding="utf-8") |
| 70 | + |
| 71 | + input_channel, spark_opt = processor._stage_submit_deps( |
| 72 | + [str(dep_file)], processor._submit_py_files_input_channel_name |
| 73 | + ) |
| 74 | + |
| 75 | + mock_upload.assert_called_once() |
| 76 | + assert input_channel.input_name == processor._submit_py_files_input_channel_name |
| 77 | + assert input_channel.s3_input.s3_uri == "s3://test-bucket/sagemaker/test-job/input/py-files" |
| 78 | + assert input_channel.s3_input.local_path == "/opt/ml/processing/input/py-files" |
| 79 | + assert spark_opt == "/opt/ml/processing/input/py-files" |
0 commit comments