Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/s3/syncstrategy/test_base.py
1569 views
1
# Copyright 2014 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
import datetime
14
15
from awscli.customizations.s3.filegenerator import FileStat
16
from awscli.customizations.s3.syncstrategy.base import BaseSync, \
17
SizeAndLastModifiedSync, MissingFileSync, NeverSync
18
from awscli.testutils import mock, unittest
19
20
21
class TestBaseSync(unittest.TestCase):
22
def setUp(self):
23
self.sync_strategy = BaseSync()
24
25
def test_init(self):
26
valid_sync_types = ['file_at_src_and_dest', 'file_not_at_dest',
27
'file_not_at_src']
28
for sync_type in valid_sync_types:
29
strategy = BaseSync(sync_type)
30
self.assertEqual(strategy.sync_type, sync_type)
31
32
# Check for invalid ``sync_type`` options.
33
with self.assertRaises(ValueError):
34
BaseSync('wrong_sync_type')
35
36
def test_register_strategy(self):
37
"""
38
Ensures that the class registers all of the necessary handlers
39
"""
40
session = mock.Mock()
41
self.sync_strategy.register_strategy(session)
42
register_args = session.register.call_args_list
43
self.assertEqual(register_args[0][0][0],
44
'building-arg-table.sync')
45
self.assertEqual(register_args[0][0][1],
46
self.sync_strategy.add_sync_argument)
47
self.assertEqual(register_args[1][0][0], 'choosing-s3-sync-strategy')
48
self.assertEqual(register_args[1][0][1],
49
self.sync_strategy.use_sync_strategy)
50
51
def test_determine_should_sync(self):
52
"""
53
Ensure that this class cannot be directly used as the sync strategy.
54
"""
55
with self.assertRaises(NotImplementedError):
56
self.sync_strategy.determine_should_sync(None, None)
57
58
def test_arg_name(self):
59
"""
60
Ensure that the ``arg_name`` property works as expected.
61
"""
62
self.assertEqual(self.sync_strategy.arg_name, None)
63
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}
64
self.assertEqual(self.sync_strategy.arg_name, 'my-sync-strategy')
65
66
def test_arg_dest(self):
67
"""
68
Ensure that the ``arg_dest`` property works as expected.
69
"""
70
self.assertEqual(self.sync_strategy.arg_dest, None)
71
self.sync_strategy.ARGUMENT = {'dest': 'my-dest'}
72
self.assertEqual(self.sync_strategy.arg_dest, 'my-dest')
73
74
def test_add_sync_argument(self):
75
"""
76
Ensures the sync argument is properly added to the
77
the command's ``arg_table``.
78
"""
79
arg_table = [{'name': 'original_argument'}]
80
self.sync_strategy.ARGUMENT = {'name': 'sync_argument'}
81
self.sync_strategy.add_sync_argument(arg_table)
82
self.assertEqual(arg_table,
83
[{'name': 'original_argument'},
84
{'name': 'sync_argument'}])
85
86
def test_no_add_sync_argument_for_no_argument_specified(self):
87
"""
88
Ensures nothing is added to the command's ``arg_table`` if no
89
``ARGUMENT`` table is specified.
90
"""
91
arg_table = [{'name': 'original_argument'}]
92
self.sync_strategy.add_sync_argument(arg_table)
93
self.assertEqual(arg_table, [{'name': 'original_argument'}])
94
95
def test_no_use_sync_strategy_for_no_argument_specified(self):
96
"""
97
Test if that the sync strategy is not returned if it has no argument.
98
"""
99
params = {'my_sync_strategy': True}
100
self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)
101
102
def test_use_sync_strategy_for_name_and_no_dest(self):
103
"""
104
Test if sync strategy argument has ``name`` but no ``dest`` and the
105
strategy was called in ``params``.
106
"""
107
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}
108
params = {'my_sync_strategy': True}
109
self.assertEqual(self.sync_strategy.use_sync_strategy(params),
110
self.sync_strategy)
111
112
def test_no_use_sync_strategy_for_name_and_no_dest(self):
113
"""
114
Test if sync strategy argument has ``name`` but no ``dest`` but
115
the strategy was not called in ``params``.
116
"""
117
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}
118
params = {'my_sync_strategy': False}
119
self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)
120
121
def test_no_use_sync_strategy_for_not_in_params(self):
122
"""
123
Test if sync strategy argument has a ``name`` but for whatever reason
124
the strategy is not in ``params``.
125
"""
126
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy'}
127
self.assertEqual(self.sync_strategy.use_sync_strategy({}), None)
128
129
def test_use_sync_strategy_for_name_and_dest(self):
130
"""
131
Test if sync strategy argument has ``name`` and ``dest`` and the
132
strategy was called in ``params``.
133
"""
134
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',
135
'dest': 'my-dest'}
136
params = {'my-dest': True}
137
self.assertEqual(self.sync_strategy.use_sync_strategy(params),
138
self.sync_strategy)
139
140
def test_no_use_sync_strategy_for_name_and_dest(self):
141
"""
142
Test if sync strategy argument has ``name`` and ``dest`` but the
143
the strategy was not called in ``params``.
144
"""
145
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',
146
'dest': 'my-dest'}
147
params = {'my-dest': False}
148
self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)
149
150
def test_no_use_sync_strategy_for_dest_but_only_name_in_params(self):
151
"""
152
Test if sync strategy argument has ``name`` and ``dest`` but the
153
the strategy was not called in ``params`` even though the ``name`` was
154
called in ``params``.
155
"""
156
self.sync_strategy.ARGUMENT = {'name': 'my-sync-strategy',
157
'dest': 'my-dest'}
158
params = {'my-sync-strategy': True}
159
self.assertEqual(self.sync_strategy.use_sync_strategy(params), None)
160
161
162
class TestSizeAndLastModifiedSync(unittest.TestCase):
163
def setUp(self):
164
self.sync_strategy = SizeAndLastModifiedSync()
165
166
def test_compare_size(self):
167
"""
168
Confirms compare size works.
169
"""
170
time = datetime.datetime.now()
171
src_file = FileStat(src='', dest='',
172
compare_key='comparator_test.py', size=11,
173
last_update=time, src_type='local',
174
dest_type='s3', operation_name='upload')
175
dest_file = FileStat(src='', dest='',
176
compare_key='comparator_test.py', size=10,
177
last_update=time, src_type='s3',
178
dest_type='local', operation_name='')
179
should_sync = self.sync_strategy.determine_should_sync(
180
src_file, dest_file)
181
self.assertTrue(should_sync)
182
183
def test_compare_lastmod_upload(self):
184
"""
185
Confirms compare time works for uploads.
186
"""
187
time = datetime.datetime.now()
188
future_time = time + datetime.timedelta(0, 3)
189
src_file = FileStat(src='', dest='',
190
compare_key='comparator_test.py', size=10,
191
last_update=future_time, src_type='local',
192
dest_type='s3', operation_name='upload')
193
dest_file = FileStat(src='', dest='',
194
compare_key='comparator_test.py', size=10,
195
last_update=time, src_type='s3',
196
dest_type='local', operation_name='')
197
should_sync = self.sync_strategy.determine_should_sync(
198
src_file, dest_file)
199
self.assertTrue(should_sync)
200
201
def test_compare_lastmod_copy(self):
202
"""
203
Confirms compare time works for copies.
204
"""
205
time = datetime.datetime.now()
206
future_time = time + datetime.timedelta(0, 3)
207
src_file = FileStat(src='', dest='',
208
compare_key='comparator_test.py', size=10,
209
last_update=future_time, src_type='s3',
210
dest_type='s3', operation_name='copy')
211
dest_file = FileStat(src='', dest='',
212
compare_key='comparator_test.py', size=10,
213
last_update=time, src_type='s3',
214
dest_type='s3', operation_name='')
215
should_sync = self.sync_strategy.determine_should_sync(
216
src_file, dest_file)
217
self.assertTrue(should_sync)
218
219
def test_compare_lastmod_download(self):
220
"""
221
Confirms compare time works for downloads.
222
"""
223
time = datetime.datetime.now()
224
future_time = time + datetime.timedelta(0, 3)
225
src_file = FileStat(src='', dest='',
226
compare_key='comparator_test.py', size=10,
227
last_update=time, src_type='s3',
228
dest_type='local', operation_name='download')
229
dest_file = FileStat(src='', dest='',
230
compare_key='comparator_test.py', size=10,
231
last_update=future_time, src_type='local',
232
dest_type='s3', operation_name='')
233
234
should_sync = self.sync_strategy.determine_should_sync(
235
src_file, dest_file)
236
self.assertTrue(should_sync)
237
238
# If the source is newer than the destination do not download.
239
src_file = FileStat(src='', dest='',
240
compare_key='comparator_test.py', size=10,
241
last_update=future_time, src_type='s3',
242
dest_type='local', operation_name='download')
243
dest_file = FileStat(src='', dest='',
244
compare_key='comparator_test.py', size=10,
245
last_update=time, src_type='local',
246
dest_type='s3', operation_name='')
247
248
should_sync = self.sync_strategy.determine_should_sync(
249
src_file, dest_file)
250
self.assertFalse(should_sync)
251
252
253
class TestNeverSync(unittest.TestCase):
254
def setUp(self):
255
self.sync_strategy = NeverSync()
256
257
def test_constructor(self):
258
self.assertEqual(self.sync_strategy.sync_type, 'file_not_at_src')
259
260
def test_determine_should_sync(self):
261
time_dst = datetime.datetime.now()
262
263
dst_file = FileStat(src='', dest='',
264
compare_key='test.py', size=10,
265
last_update=time_dst, src_type='s3',
266
dest_type='local', operation_name='')
267
268
should_sync = self.sync_strategy.determine_should_sync(
269
None, dst_file)
270
self.assertFalse(should_sync)
271
272
273
class TestMissingFileSync(unittest.TestCase):
274
def setUp(self):
275
self.sync_strategy = MissingFileSync()
276
277
def test_constructor(self):
278
self.assertEqual(self.sync_strategy.sync_type, 'file_not_at_dest')
279
280
def test_determine_should_sync(self):
281
time_src = datetime.datetime.now()
282
283
src_file = FileStat(src='', dest='',
284
compare_key='test.py', size=10,
285
last_update=time_src, src_type='s3',
286
dest_type='local', operation_name='')
287
288
should_sync = self.sync_strategy.determine_should_sync(
289
src_file, None)
290
self.assertTrue(should_sync)
291
292
293
if __name__ == "__main__":
294
unittest.main()
295
296