forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_upload_data.py
More file actions
137 lines (109 loc) · 4.97 KB
/
test_upload_data.py
File metadata and controls
137 lines (109 loc) · 4.97 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
# 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.
from __future__ import absolute_import
import os
from mock import MagicMock, Mock
import pytest
import sagemaker
from tests.unit import DATA_DIR
UPLOAD_DATA_TESTS_FILES_DIR = os.path.join(DATA_DIR, "upload_data_tests")
SINGLE_FILE_NAME = "file1.py"
UPLOAD_DATA_TESTS_SINGLE_FILE = os.path.join(UPLOAD_DATA_TESTS_FILES_DIR, SINGLE_FILE_NAME)
BUCKET_NAME = "mybucket"
AES_ENCRYPTION_ENABLED = {"ServerSideEncryption": "AES256"}
ENDPOINT_URL = "http://127.0.0.1:9000"
@pytest.fixture()
def sagemaker_session():
boto_mock = MagicMock(name="boto_session")
ims = sagemaker.Session(boto_session=boto_mock)
ims.default_bucket = Mock(name="default_bucket", return_value=BUCKET_NAME)
return ims
@pytest.fixture()
def sagemaker_session_custom_endpoint():
boto_session = MagicMock("boto_session")
resource_mock = Mock("resource")
client_mock = MagicMock("client")
boto_attrs = {"region_name": "us-east-1"}
boto_session.configure_mock(**boto_attrs)
boto_session.resource = Mock(name="resource", return_value=resource_mock)
boto_session.client = Mock(name="client", return_value=client_mock)
local_session = sagemaker.local.local_session.LocalSession(
boto_session=boto_session, s3_endpoint_url=ENDPOINT_URL
)
local_session.default_bucket = Mock(name="default_bucket", return_value=BUCKET_NAME)
return local_session
def test_upload_data_absolute_dir(sagemaker_session):
result_s3_uri = sagemaker_session.upload_data(UPLOAD_DATA_TESTS_FILES_DIR)
uploaded_files_with_args = [
(args[0], kwargs)
for name, args, kwargs in sagemaker_session.boto_session.mock_calls
if name == "resource().Object().upload_file"
]
assert result_s3_uri == "s3://{}/data".format(BUCKET_NAME)
assert len(uploaded_files_with_args) == 4
for file, kwargs in uploaded_files_with_args:
assert os.path.exists(file)
assert kwargs["ExtraArgs"] is None
def test_upload_data_absolute_dir_custom_endpoint(sagemaker_session_custom_endpoint):
sagemaker_session_custom_endpoint.s3_resource.Object = Mock()
result_s3_uri = sagemaker_session_custom_endpoint.upload_data(UPLOAD_DATA_TESTS_FILES_DIR)
uploaded_files_with_args = [
(args[0], kwargs)
for name, args, kwargs in sagemaker_session_custom_endpoint.s3_resource.mock_calls
if name == "Object().upload_file"
]
assert result_s3_uri == "s3://{}/data".format(BUCKET_NAME)
assert len(uploaded_files_with_args) == 4
for file, kwargs in uploaded_files_with_args:
assert os.path.exists(file)
assert kwargs["ExtraArgs"] is None
def test_upload_data_absolute_file(sagemaker_session):
result_s3_uri = sagemaker_session.upload_data(UPLOAD_DATA_TESTS_SINGLE_FILE)
uploaded_files_with_args = [
(args[0], kwargs)
for name, args, kwargs in sagemaker_session.boto_session.mock_calls
if name == "resource().Object().upload_file"
]
assert result_s3_uri == "s3://{}/data/{}".format(BUCKET_NAME, SINGLE_FILE_NAME)
assert len(uploaded_files_with_args) == 1
(file, kwargs) = uploaded_files_with_args[0]
assert os.path.exists(file)
assert kwargs["ExtraArgs"] is None
def test_upload_data_aes_encrypted_absolute_dir(sagemaker_session):
result_s3_uri = sagemaker_session.upload_data(
UPLOAD_DATA_TESTS_FILES_DIR, extra_args=AES_ENCRYPTION_ENABLED
)
uploaded_files_with_args = [
(args[0], kwargs)
for name, args, kwargs in sagemaker_session.boto_session.mock_calls
if name == "resource().Object().upload_file"
]
assert result_s3_uri == "s3://{}/data".format(BUCKET_NAME)
assert len(uploaded_files_with_args) == 4
for file, kwargs in uploaded_files_with_args:
assert os.path.exists(file)
assert kwargs["ExtraArgs"] == AES_ENCRYPTION_ENABLED
def test_upload_data_aes_encrypted_absolute_file(sagemaker_session):
result_s3_uri = sagemaker_session.upload_data(
UPLOAD_DATA_TESTS_SINGLE_FILE, extra_args=AES_ENCRYPTION_ENABLED
)
uploaded_files_with_args = [
(args[0], kwargs)
for name, args, kwargs in sagemaker_session.boto_session.mock_calls
if name == "resource().Object().upload_file"
]
assert result_s3_uri == "s3://{}/data/{}".format(BUCKET_NAME, SINGLE_FILE_NAME)
assert len(uploaded_files_with_args) == 1
(file, kwargs) = uploaded_files_with_args[0]
assert os.path.exists(file)
assert kwargs["ExtraArgs"] == AES_ENCRYPTION_ENABLED