-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy paths3filesystem_test.py
More file actions
281 lines (234 loc) · 10.4 KB
/
s3filesystem_test.py
File metadata and controls
281 lines (234 loc) · 10.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
# -*- coding: utf-8 -*-
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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 the S3 File System"""
# pytype: skip-file
import logging
import unittest
import mock
from apache_beam.io.aws.clients.s3 import messages
from apache_beam.io.filesystem import BeamIOError
from apache_beam.io.filesystem import FileMetadata
from apache_beam.options.pipeline_options import PipelineOptions
# Protect against environments where boto3 library is not available.
# pylint: disable=wrong-import-order, wrong-import-position
try:
from apache_beam.io.aws import s3filesystem
except ImportError:
s3filesystem = None # type: ignore[assignment]
# pylint: enable=wrong-import-order, wrong-import-position
@unittest.skipIf(s3filesystem is None, 'AWS dependencies are not installed')
class S3FileSystemTest(unittest.TestCase):
def setUp(self):
pipeline_options = PipelineOptions()
self.fs = s3filesystem.S3FileSystem(pipeline_options=pipeline_options)
def test_scheme(self):
self.assertEqual(self.fs.scheme(), 's3')
self.assertEqual(s3filesystem.S3FileSystem.scheme(), 's3')
def test_join(self):
self.assertEqual(
's3://bucket/path/to/file',
self.fs.join('s3://bucket/path', 'to', 'file'))
self.assertEqual(
's3://bucket/path/to/file', self.fs.join('s3://bucket/path', 'to/file'))
self.assertEqual(
's3://bucket/path/to/file',
self.fs.join('s3://bucket/path', '/to/file'))
self.assertEqual(
's3://bucket/path/to/file',
self.fs.join('s3://bucket/path/', 'to', 'file'))
self.assertEqual(
's3://bucket/path/to/file',
self.fs.join('s3://bucket/path/', 'to/file'))
self.assertEqual(
's3://bucket/path/to/file',
self.fs.join('s3://bucket/path/', '/to/file'))
with self.assertRaises(ValueError):
self.fs.join('/bucket/path/', '/to/file')
def test_split(self):
self.assertEqual(('s3://foo/bar', 'baz'), self.fs.split('s3://foo/bar/baz'))
self.assertEqual(('s3://foo', ''), self.fs.split('s3://foo/'))
self.assertEqual(('s3://foo', ''), self.fs.split('s3://foo'))
with self.assertRaises(ValueError):
self.fs.split('/no/s3/prefix')
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_match_single(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
s3io_mock._status.return_value = {'size': 1, 'last_updated': 9999999.0}
expected_results = [FileMetadata('s3://bucket/file1', 1, 9999999.0)]
match_result = self.fs.match(['s3://bucket/file1'])[0]
self.assertEqual(match_result.metadata_list, expected_results)
s3io_mock._status.assert_called_once_with('s3://bucket/file1')
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_match_multiples(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
s3io_mock.list_files.return_value = iter([
('s3://bucket/file1', (1, 9999999.0)),
('s3://bucket/file2', (2, 8888888.0))
])
expected_results = set([
FileMetadata('s3://bucket/file1', 1, 9999999.0),
FileMetadata('s3://bucket/file2', 2, 8888888.0)
])
match_result = self.fs.match(['s3://bucket/'])[0]
self.assertEqual(set(match_result.metadata_list), expected_results)
s3io_mock.list_files.assert_called_once_with(
's3://bucket/', with_metadata=True)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_match_multiples_limit(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
limit = 1
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
s3io_mock.list_files.return_value = iter([
('s3://bucket/file1', (1, 99999.0))
])
expected_results = set([FileMetadata('s3://bucket/file1', 1, 99999.0)])
match_result = self.fs.match(['s3://bucket/'], [limit])[0]
self.assertEqual(set(match_result.metadata_list), expected_results)
self.assertEqual(len(match_result.metadata_list), limit)
s3io_mock.list_files.assert_called_once_with(
's3://bucket/', with_metadata=True)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_match_multiples_error(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
exception = IOError('Failed')
s3io_mock.list_files.side_effect = exception
with self.assertRaises(BeamIOError) as error:
self.fs.match(['s3://bucket/'])
self.assertIn('Match operation failed', str(error.exception))
s3io_mock.list_files.assert_called_once_with(
's3://bucket/', with_metadata=True)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_match_multiple_patterns(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
s3io_mock.list_files.side_effect = [
iter([('s3://bucket/file1', (1, 99999.0))]),
iter([('s3://bucket/file2', (2, 88888.0))]),
]
expected_results = [[FileMetadata('s3://bucket/file1', 1, 99999.0)],
[FileMetadata('s3://bucket/file2', 2, 88888.0)]]
result = self.fs.match(['s3://bucket/file1*', 's3://bucket/file2*'])
self.assertEqual([mr.metadata_list for mr in result], expected_results)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_create(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
# Issue file copy
_ = self.fs.create('s3://bucket/from1', 'application/octet-stream')
s3io_mock.open.assert_called_once_with(
's3://bucket/from1', 'wb', mime_type='application/octet-stream')
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_open(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
# Issue file copy
_ = self.fs.open('s3://bucket/from1', 'application/octet-stream')
s3io_mock.open.assert_called_once_with(
's3://bucket/from1', 'rb', mime_type='application/octet-stream')
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_copy_file(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
sources = ['s3://bucket/from1', 's3://bucket/from2']
destinations = ['s3://bucket/to1', 's3://bucket/to2']
# Issue file copy
self.fs.copy(sources, destinations)
src_dest_pairs = list(zip(sources, destinations))
s3io_mock.copy_paths.assert_called_once_with(src_dest_pairs)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_copy_file_error(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
sources = ['s3://bucket/from1', 's3://bucket/from2', 's3://bucket/from3']
destinations = ['s3://bucket/to1', 's3://bucket/to2']
# Issue file copy
with self.assertRaises(BeamIOError):
self.fs.copy(sources, destinations)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_delete(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
s3io_mock.size.return_value = 0
files = [
's3://bucket/from1',
's3://bucket/from2',
's3://bucket/from3',
]
# Issue batch delete.
self.fs.delete(files)
s3io_mock.delete_paths.assert_called_once_with(files)
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_delete_error(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
problematic_directory = 's3://nonexistent-bucket/tree/'
exception = messages.S3ClientError('Not found', 404)
s3io_mock.delete_paths.return_value = {
problematic_directory: exception,
's3://bucket/object1': None,
's3://bucket/object2': None,
}
s3io_mock.size.return_value = 0
files = [
problematic_directory,
's3://bucket/object1',
's3://bucket/object2',
]
expected_results = {problematic_directory: exception}
# Issue batch delete.
with self.assertRaises(BeamIOError) as error:
self.fs.delete(files)
self.assertIn('Delete operation failed', str(error.exception))
self.assertEqual(error.exception.exception_details, expected_results)
s3io_mock.delete_paths.assert_called()
@mock.patch('apache_beam.io.aws.s3filesystem.s3io')
def test_rename(self, unused_mock_arg):
# Prepare mocks.
s3io_mock = mock.MagicMock()
s3filesystem.s3io.S3IO = lambda options: s3io_mock # type: ignore[misc]
sources = ['s3://bucket/from1', 's3://bucket/from2']
destinations = ['s3://bucket/to1', 's3://bucket/to2']
# Issue file copy
self.fs.rename(sources, destinations)
src_dest_pairs = list(zip(sources, destinations))
s3io_mock.rename_files.assert_called_once_with(src_dest_pairs)
def test_lineage(self):
self._verify_lineage("s3://bucket/", ("bucket", ))
self._verify_lineage("s3://bucket/foo/bar.txt", ("bucket", "foo/bar.txt"))
def _verify_lineage(self, uri, expected_segments):
lineage_mock = mock.MagicMock()
self.fs.report_lineage(uri, lineage_mock)
lineage_mock.add.assert_called_once_with(
"s3", *expected_segments, last_segment_sep='/')
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
unittest.main()