forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plugin.py
More file actions
2440 lines (2125 loc) · 90.5 KB
/
test_plugin.py
File metadata and controls
2440 lines (2125 loc) · 90.5 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2013 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.
# The following tests are performed to ensure that the commands work.
# It does not check every possible parameter that can be thrown as
# those are checked by tests in other classes
import contextlib
import copy
import datetime
import os
import platform
import re
import signal
import socket
import stat
import tempfile
import time
import pytest
from dateutil.tz import tzutc
from awscli.compat import BytesIO, urlopen
from awscli.customizations.s3.transferconfig import DEFAULTS
from awscli.testutils import aws as _aws
from awscli.testutils import (
get_stdout_encoding,
random_bucket_name,
random_chars,
skip_if_windows,
unittest,
)
from tests.integration.customizations.s3 import BaseS3IntegrationTest
_NON_EXISTENT_BUCKET = random_bucket_name()
@pytest.fixture
def symlink_files(files):
nested_dir = os.path.join(files.rootdir, 'realfiles')
os.mkdir(nested_dir)
sample_file = files.create_file(
os.path.join(nested_dir, 'foo.txt'), contents='foo.txt contents'
)
# Create a symlink to foo.txt.
os.symlink(sample_file, os.path.join(files.rootdir, 'a-goodsymlink'))
# Create a bad symlink.
os.symlink(
'non-existent-file', os.path.join(files.rootdir, 'b-badsymlink')
)
# Create a symlink to directory where foo.txt is.
os.symlink(nested_dir, os.path.join(files.rootdir, 'c-goodsymlink'))
@pytest.fixture
def config_with_profile(session, files):
config_file = os.path.join(files.rootdir, 'tmpconfig')
with open(config_file, 'w') as f:
creds = session.get_credentials()
f.write(
"[profile testprofile]\n"
"aws_access_key_id=%s\n"
"aws_secret_access_key=%s\n" % (creds.access_key, creds.secret_key)
)
if creds.token is not None:
f.write("aws_session_token=%s\n" % creds.token)
f.flush()
yield config_file
@pytest.fixture
def aws_config_copy(monkeypatch, tmpdir, session):
config_contents = _get_config_contents(session)
tmp_config_file = os.path.join(tmpdir, 'config')
with open(tmp_config_file, 'w') as f:
f.write(config_contents)
f.flush()
monkeypatch.setenv('AWS_CONFIG_FILE', tmp_config_file)
yield tmp_config_file
def _get_config_contents(session):
config_file = session.get_config_variable('config_file')
if config_file:
config_path = os.path.expanduser(os.path.expandvars(config_file))
if os.path.exists(config_path):
with open(config_path) as f:
return f.read()
return ''
@pytest.fixture
def preferred_transfer_client(request, aws_config_copy):
configure_preferred_transfer_client(request.param)
@pytest.fixture
def encrypt_key():
return 'a' * 32
@pytest.fixture
def other_encrypt_key():
return 'b' * 32
@contextlib.contextmanager
def cd(directory):
original = os.getcwd()
try:
os.chdir(directory)
yield
finally:
os.chdir(original)
def aws(
command,
collect_memory=False,
env_vars=None,
wait_for_finish=True,
input_data=None,
input_file=None,
):
if not env_vars:
env_vars = os.environ.copy()
env_vars['AWS_DEFAULT_REGION'] = "us-west-2"
return _aws(
command,
collect_memory=collect_memory,
env_vars=env_vars,
wait_for_finish=wait_for_finish,
input_data=input_data,
input_file=input_file,
)
def wait_for_process_exit(process, timeout=60):
deadline = time.time() + timeout
while time.time() < deadline:
rc = process.poll()
if rc is not None:
break
time.sleep(1)
else:
process.kill()
raise AssertionError(
"CLI did not exist within %s seconds of "
"receiving a Ctrl+C" % timeout
)
def configure_preferred_transfer_client(value):
aws(f'configure set s3.preferred_transfer_client {value}')
def _running_on_rhel():
return (
hasattr(platform, 'linux_distribution')
and platform.linux_distribution()[0]
== 'Red Hat Enterprise Linux Server'
)
class TestMakeBucketAccountRegionalNamespace(BaseS3IntegrationTest):
def test_short_an_suffix_sends_namespace_header(self):
p = aws('s3 mb s3://xyz-an')
assert p.rc != 0
assert 'InvalidBucketNamespace' in p.stderr
@pytest.mark.parametrize(
'preferred_transfer_client', ['classic', 'crt'], indirect=True
)
@pytest.mark.usefixtures('preferred_transfer_client')
class BaseParameterizedS3ClientTest(BaseS3IntegrationTest):
# Use this base class if you want to run tests once for
# when the preferred_transfer_client is set to default and
# once when the preferred_transfer_client is set to crt.
pass
class TestMoveCommand(BaseParameterizedS3ClientTest):
@pytest.mark.parametrize(
's3_bucket', ['shared_bucket', 'shared_dir_bucket']
)
def test_mv_local_to_s3(self, s3_bucket, files, s3_utils, request):
shared_bucket = request.getfixturevalue(s3_bucket)
full_path = files.create_file('foo.txt', 'this is foo.txt')
p = aws('s3 mv %s s3://%s/foo.txt' % (full_path, shared_bucket))
self.assert_no_errors(p)
# When we move an object, the local file is gone:
assert not os.path.exists(full_path)
# And now resides in s3.
s3_utils.assert_key_contents_equal(
shared_bucket, 'foo.txt', 'this is foo.txt'
)
@pytest.mark.parametrize(
's3_bucket', ['shared_bucket', 'shared_dir_bucket']
)
def test_mv_s3_to_local(self, s3_bucket, files, s3_utils, request):
shared_bucket = request.getfixturevalue(s3_bucket)
s3_utils.put_object(shared_bucket, 'foo.txt', 'this is foo.txt')
full_path = files.full_path(f'{s3_bucket}_foo.txt')
assert s3_utils.key_exists(shared_bucket, key_name='foo.txt')
p = aws('s3 mv s3://%s/foo.txt %s' % (shared_bucket, full_path))
self.assert_no_errors(p)
assert os.path.exists(full_path)
with open(full_path) as f:
assert f.read() == 'this is foo.txt'
# The s3 file should not be there anymore.
assert s3_utils.key_not_exists(shared_bucket, key_name='foo.txt')
@pytest.mark.parametrize(
's3_bucket, copy_s3_bucket',
[
('shared_bucket', 'shared_copy_bucket'),
('shared_bucket', 'shared_copy_dir_bucket'),
('shared_dir_bucket', 'shared_copy_dir_bucket'),
('shared_dir_bucket', 'shared_copy_bucket'),
],
)
def test_mv_s3_to_s3(self, s3_bucket, copy_s3_bucket, s3_utils, request):
from_bucket = request.getfixturevalue(s3_bucket)
to_bucket = request.getfixturevalue(copy_s3_bucket)
from_key = f'{s3_bucket}_foo.txt'
to_key = f'{copy_s3_bucket}_foo.txt'
s3_utils.put_object(from_bucket, from_key, 'this is foo.txt')
p = aws(
f"s3 mv s3://{from_bucket}/{from_key} s3://{to_bucket}/{to_key}"
)
self.assert_no_errors(p)
contents = s3_utils.get_key_contents(to_bucket, to_key)
assert contents == 'this is foo.txt'
# And verify that the object no longer exists in the from_bucket.
assert s3_utils.key_not_exists(from_bucket, key_name=from_key)
@pytest.mark.slow
def test_mv_s3_to_s3_multipart(
self, s3_utils, shared_bucket, shared_copy_bucket
):
from_bucket = shared_bucket
to_bucket = shared_copy_bucket
file_contents = BytesIO(b'abcd' * (1024 * 1024 * 10))
s3_utils.put_object(from_bucket, 'foo.txt', file_contents)
p = aws(
's3 mv s3://%s/foo.txt s3://%s/foo.txt' % (from_bucket, to_bucket)
)
self.assert_no_errors(p)
s3_utils.assert_key_contents_equal(to_bucket, 'foo.txt', file_contents)
# And verify that the object no longer exists in the from_bucket.
assert s3_utils.key_not_exists(from_bucket, key_name='foo.txt')
def test_mv_s3_to_s3_multipart_recursive(
self, s3_utils, shared_bucket, shared_copy_bucket
):
from_bucket = shared_bucket
to_bucket = shared_copy_bucket
large_file_contents = BytesIO(b'abcd' * (1024 * 1024 * 10))
small_file_contents = 'small file contents'
s3_utils.put_object(from_bucket, 'largefile', large_file_contents)
s3_utils.put_object(from_bucket, 'smallfile', small_file_contents)
p = aws(
's3 mv s3://%s/ s3://%s/ --recursive' % (from_bucket, to_bucket)
)
self.assert_no_errors(p)
# Nothing's in the from_bucket.
assert s3_utils.key_not_exists(from_bucket, key_name='largefile')
assert s3_utils.key_not_exists(from_bucket, key_name='smallfile')
# And both files are in the to_bucket.
assert s3_utils.key_exists(to_bucket, key_name='largefile')
assert s3_utils.key_exists(to_bucket, key_name='smallfile')
# And the contents are what we expect.
s3_utils.assert_key_contents_equal(
to_bucket, 'smallfile', small_file_contents
)
s3_utils.assert_key_contents_equal(
to_bucket, 'largefile', large_file_contents
)
def test_mv_s3_to_s3_with_sig4(
self, s3_utils, shared_bucket, shared_cross_region_bucket
):
to_region = 'eu-central-1'
from_region = 'us-west-2'
from_bucket = shared_bucket
to_bucket = shared_cross_region_bucket
file_name = 'hello.txt'
file_contents = 'hello'
s3_utils.put_object(from_bucket, file_name, file_contents)
p = aws(
f's3 mv s3://{from_bucket}/{file_name} s3://{to_bucket}/{file_name} '
f'--source-region {from_region} --region {to_region}'
)
self.assert_no_errors(p)
assert s3_utils.key_not_exists(from_bucket, file_name)
assert s3_utils.key_exists(to_bucket, file_name)
def test_mv_with_large_file(self, files, s3_utils, shared_bucket):
# 40MB will force a multipart upload.
file_contents = BytesIO(b'abcd' * (1024 * 1024 * 10))
foo_txt = files.create_file(
'foo.txt', file_contents.getvalue().decode('utf-8')
)
p = aws('s3 mv %s s3://%s/foo.txt' % (foo_txt, shared_bucket))
self.assert_no_errors(p)
# When we move an object, the local file is gone:
assert not os.path.exists(foo_txt)
# And now resides in s3.
s3_utils.assert_key_contents_equal(
shared_bucket, 'foo.txt', file_contents
)
# Now verify we can download this file.
p = aws('s3 mv s3://%s/foo.txt %s' % (shared_bucket, foo_txt))
self.assert_no_errors(p)
assert os.path.exists(foo_txt)
assert os.path.getsize(foo_txt) == len(file_contents.getvalue())
def test_mv_to_nonexistent_bucket(self, files):
full_path = files.create_file('foo.txt', 'this is foo.txt')
p = aws(f's3 mv {full_path} s3://{_NON_EXISTENT_BUCKET}/foo.txt')
assert p.rc == 1
def test_cant_move_file_onto_itself_small_file(
self, s3_utils, shared_bucket
):
# We don't even need a remote file in this case. We can
# immediately validate that we can't move a file onto itself.
s3_utils.put_object(shared_bucket, key_name='key.txt', contents='foo')
p = aws(
's3 mv s3://%s/key.txt s3://%s/key.txt'
% (shared_bucket, shared_bucket)
)
assert p.rc == 252
assert 'Cannot mv a file onto itself' in p.stderr
def test_cant_move_large_file_onto_itself(self, s3_utils, shared_bucket):
# At the API level, you can multipart copy an object onto itself,
# but a mv command doesn't make sense because a mv is just a
# cp + an rm of the src file. We should be consistent and
# not allow large files to be mv'd onto themselves.
file_contents = BytesIO(b'a' * (1024 * 1024 * 10))
s3_utils.put_object(
shared_bucket, key_name='key.txt', contents=file_contents
)
p = aws(
's3 mv s3://%s/key.txt s3://%s/key.txt'
% (shared_bucket, shared_bucket)
)
assert p.rc == 252
assert 'Cannot mv a file onto itself' in p.stderr
class TestRm(BaseParameterizedS3ClientTest):
@skip_if_windows('Newline in filename test not valid on windows.')
# Windows won't let you do this. You'll get:
# [Errno 22] invalid mode ('w') or filename:
# 'c:\\windows\\temp\\tmp0fv8uu\\foo\r.txt'
def test_rm_with_newlines(self, files, s3_utils, shared_bucket):
# Note the carriage return in the key name.
foo_txt = files.create_file('foo\r.txt', 'this is foo.txt')
p = aws('s3 cp %s s3://%s/foo\r.txt' % (foo_txt, shared_bucket))
self.assert_no_errors(p)
# Make sure object is in bucket.
assert s3_utils.key_exists(shared_bucket, key_name='foo\r.txt')
# Then delete the file.
aws('s3 rm s3://%s/ --recursive' % (shared_bucket,))
# And verify it's gone.
assert s3_utils.key_not_exists(shared_bucket, key_name='foo\r.txt')
@pytest.mark.parametrize(
's3_bucket', ['shared_bucket', 'shared_dir_bucket']
)
def test_rm_with_page_size(self, s3_bucket, s3_utils, request):
shared_bucket = request.getfixturevalue(s3_bucket)
s3_utils.put_object(shared_bucket, 'foo.txt', contents='hello world')
s3_utils.put_object(shared_bucket, 'bar.txt', contents='hello world2')
p = aws('s3 rm s3://%s/ --recursive --page-size 1' % shared_bucket)
self.assert_no_errors(p)
assert s3_utils.key_not_exists(shared_bucket, key_name='foo.txt')
assert s3_utils.key_not_exists(shared_bucket, key_name='bar.txt')
class TestCp(BaseParameterizedS3ClientTest):
@pytest.mark.parametrize(
's3_bucket', ['shared_bucket', 'shared_dir_bucket']
)
def test_cp_to_and_from_s3(self, s3_bucket, files, s3_utils, request):
# This tests the ability to put a single file in s3
# move it to a different bucket.
# and download the file locally
shared_bucket = request.getfixturevalue(s3_bucket)
# copy file into bucket.
foo_txt = files.create_file(f'{s3_bucket}_foo.txt', 'this is foo.txt')
p = aws('s3 cp %s s3://%s/foo.txt' % (foo_txt, shared_bucket))
self.assert_no_errors(p)
# Make sure object is in bucket.
assert s3_utils.key_exists(shared_bucket, key_name='foo.txt')
contents = s3_utils.get_key_contents(shared_bucket, key_name='foo.txt')
assert contents == 'this is foo.txt'
content_type = s3_utils.content_type_for_key(
shared_bucket, key_name='foo.txt'
)
assert content_type == 'text/plain'
# Make a new name for the file and copy it locally.
full_path = files.full_path(f'{s3_bucket}_bar.txt')
p = aws('s3 cp s3://%s/foo.txt %s' % (shared_bucket, full_path))
self.assert_no_errors(p)
with open(full_path) as f:
assert f.read() == 'this is foo.txt'
def test_cp_without_trailing_slash(self, files, s3_utils, shared_bucket):
# There's a unit test for this, but we still want to verify this
# with an integration test.
# copy file into bucket.
foo_txt = files.create_file('foo.txt', 'this is foo.txt')
# Note that the destination has no trailing slash.
p = aws('s3 cp %s s3://%s' % (foo_txt, shared_bucket))
self.assert_no_errors(p)
# Make sure object is in bucket.
assert s3_utils.key_exists(shared_bucket, key_name='foo.txt')
contents = s3_utils.get_key_contents(shared_bucket, key_name='foo.txt')
assert contents == 'this is foo.txt'
@pytest.mark.slow
def test_cp_s3_s3_multipart(
self, s3_utils, shared_bucket, shared_copy_bucket
):
from_bucket = shared_bucket
to_bucket = shared_copy_bucket
file_contents = BytesIO(b'abcd' * (1024 * 1024 * 10))
s3_utils.put_object(from_bucket, 'foo.txt', file_contents)
p = aws(
's3 cp s3://%s/foo.txt s3://%s/foo.txt' % (from_bucket, to_bucket)
)
self.assert_no_errors(p)
s3_utils.assert_key_contents_equal(to_bucket, 'foo.txt', file_contents)
assert s3_utils.key_exists(from_bucket, key_name='foo.txt')
def test_guess_mime_type(self, files, s3_utils, shared_bucket):
bar_png = files.create_file('bar.jpeg', 'fake png image')
p = aws('s3 cp %s s3://%s/bar.jpeg' % (bar_png, shared_bucket))
self.assert_no_errors(p)
# We should have correctly guessed the content type based on the
# filename extension.
content_type = s3_utils.content_type_for_key(
shared_bucket, key_name='bar.jpeg'
)
assert content_type == 'image/jpeg'
def test_download_large_file(self, files, s3_utils, shared_bucket):
# This will force a multipart download.
foo_contents = BytesIO(b'abcd' * (1024 * 1024 * 10))
s3_utils.put_object(
shared_bucket, key_name='foo.txt', contents=foo_contents
)
local_foo_txt = files.full_path('foo.txt')
p = aws('s3 cp s3://%s/foo.txt %s' % (shared_bucket, local_foo_txt))
self.assert_no_errors(p)
assert os.path.getsize(local_foo_txt) == len(foo_contents.getvalue())
@skip_if_windows('SIGINT not supported on Windows.')
def test_download_ctrl_c_does_not_hang(
self, files, s3_utils, shared_bucket
):
foo_contents = BytesIO(b'abcd' * (1024 * 1024 * 40))
s3_utils.put_object(
shared_bucket, key_name='foo.txt', contents=foo_contents
)
local_foo_txt = files.full_path('foo.txt')
# --quiet is added to make sure too much output is not communicated
# to the PIPE, causing a deadlock when not consumed.
process = aws(
's3 cp s3://%s/foo.txt %s --quiet'
% (shared_bucket, local_foo_txt),
wait_for_finish=False,
)
# Give it some time to start up and enter it's main task loop.
time.sleep(3)
# The process has 60 seconds to finish after being sent a Ctrl+C,
# otherwise the test fails.
process.send_signal(signal.SIGINT)
wait_for_process_exit(process, timeout=60)
# A Ctrl+C should have a non-zero RC.
# We either caught the process in
# its main polling loop (rc=1), or it was successfully terminated by
# the SIGINT (rc=-2).
#
# There is also the chance the interrupt happened before the transfer
# process started or even after transfer process finished. So the
# signal may have never been encountered, resulting in an rc of 0.
# Therefore, it is acceptable to have an rc of 0 as the important part
# about this test is that it does not hang.
assert process.returncode in [0, 1, -2]
@skip_if_windows('SIGINT not supported on Windows.')
def test_cleans_up_aborted_uploads(self, files, s3_utils, shared_bucket):
foo_txt = files.create_file('foo.txt', '')
with open(foo_txt, 'wb') as f:
for i in range(20):
f.write(b'a' * 1024 * 1024)
# --quiet is added to make sure too much output is not communicated
# to the PIPE, causing a deadlock when not consumed.
process = aws(
's3 cp %s s3://%s/ --quiet' % (foo_txt, shared_bucket),
wait_for_finish=False,
)
time.sleep(3)
# The process has 60 seconds to finish after being sent a Ctrl+C,
# otherwise the test fails.
process.send_signal(signal.SIGINT)
wait_for_process_exit(process, timeout=60)
uploads_after = s3_utils.list_multipart_uploads(shared_bucket)
assert uploads_after == [], (
f"Not all multipart uploads were properly "
f"aborted after receiving Ctrl-C: {uploads_after}"
)
def test_cp_to_nonexistent_bucket(self, files):
foo_txt = files.create_file('foo.txt', 'this is foo.txt')
p = aws(f's3 cp {foo_txt} s3://{_NON_EXISTENT_BUCKET}/foo.txt')
assert p.rc == 1
def test_cp_empty_file(self, files, s3_utils, shared_bucket):
foo_txt = files.create_file('foo.txt', contents='')
p = aws('s3 cp %s s3://%s/' % (foo_txt, shared_bucket))
assert p.rc == 0
assert 'failed' not in p.stderr
assert s3_utils.key_exists(shared_bucket, 'foo.txt')
def test_download_non_existent_key(self):
p = aws(f's3 cp s3://{_NON_EXISTENT_BUCKET}/foo.txt foo.txt')
assert p.rc == 1
expected_err_msg = (
'An error occurred (404) when calling the '
'HeadObject operation: Key "foo.txt" does not exist'
)
assert expected_err_msg in p.stderr
def test_download_encrypted_kms_object(
self, s3_utils, files, shared_cross_region_bucket
):
bucket_name = shared_cross_region_bucket
extra_args = {
'ServerSideEncryption': 'aws:kms',
'SSEKMSKeyId': 'alias/aws/s3',
}
object_name = 'foo.txt'
contents = 'this is foo.txt'
s3_utils.put_object(
bucket_name, object_name, contents, extra_args=extra_args
)
local_filename = files.full_path('foo.txt')
p = aws(
's3 cp s3://%s/%s %s --region eu-central-1'
% (bucket_name, object_name, local_filename)
)
assert p.rc == 0
# Assert that the file was downloaded properly.
with open(local_filename) as f:
assert f.read() == contents
def test_download_empty_object(self, files, s3_utils, shared_bucket):
object_name = 'empty-object'
s3_utils.put_object(shared_bucket, object_name, '')
local_filename = files.full_path('empty.txt')
p = aws(
's3 cp s3://%s/%s %s'
% (shared_bucket, object_name, local_filename)
)
assert p.rc == 0
# Assert that the file was downloaded and has no content.
with open(local_filename) as f:
assert f.read() == ''
def test_website_redirect_ignore_paramfile(
self, files, s3_utils, shared_bucket
):
foo_txt = files.create_file('foo.txt', 'bar')
website_redirect = 'http://someserver'
p = aws(
's3 cp %s s3://%s/foo.txt --website-redirect %s'
% (foo_txt, shared_bucket, website_redirect)
)
self.assert_no_errors(p)
# Ensure that the web address is used as opposed to the contents
# of the web address. We can check via a head object.
response = s3_utils.head_object(shared_bucket, 'foo.txt')
assert response['WebsiteRedirectLocation'] == website_redirect
def test_copy_large_file_signature_v4(
self, s3_utils, files, shared_cross_region_bucket
):
# Just verify that we can upload a large file to a region
# that uses signature version 4.
bucket_name = shared_cross_region_bucket
num_mb = 200
foo_txt = files.create_file('foo.txt', '')
with open(foo_txt, 'wb') as f:
for i in range(num_mb):
f.write(b'a' * 1024 * 1024)
p = aws(
's3 cp %s s3://%s/ --region eu-central-1' % (foo_txt, bucket_name)
)
self.assert_no_errors(p)
assert s3_utils.key_exists(bucket_name, key_name='foo.txt')
def test_copy_metadata(self, s3_utils, files, shared_bucket):
# Copy the same style of parsing as the CLI session. This is needed
# For comparing expires timestamp.
key = random_chars(6)
filename = files.create_file(key, contents='')
p = aws(
's3 cp %s s3://%s/%s --metadata keyname=value'
% (filename, shared_bucket, key)
)
self.assert_no_errors(p)
response = s3_utils.head_object(shared_bucket, key)
# These values should have the metadata of the source object
assert response['Metadata'].get('keyname') == 'value'
def test_copy_metadata_directive(self, s3_utils, shared_bucket):
# Copy the same style of parsing as the CLI session. This is needed
# For comparing expires timestamp.
original_key = '%s-a' % random_chars(6)
new_key = '%s-b' % random_chars(6)
metadata = {
'ContentType': 'foo',
'ContentDisposition': 'foo',
'ContentEncoding': 'foo',
'ContentLanguage': 'foo',
'CacheControl': '90',
'Expires': '0',
}
s3_utils.put_object(
shared_bucket, original_key, contents='foo', extra_args=metadata
)
p = aws(
's3 cp s3://%s/%s s3://%s/%s'
% (shared_bucket, original_key, shared_bucket, new_key)
)
self.assert_no_errors(p)
response = s3_utils.head_object(shared_bucket, new_key)
# These values should have the metadata of the source object
metadata_ref = copy.copy(metadata)
expires_datetime = datetime.datetime.utcfromtimestamp(0)
expires_datetime = expires_datetime.replace(tzinfo=tzutc())
metadata_ref['Expires'] = expires_datetime
for name, value in metadata_ref.items():
assert response[name] == value
# Use REPLACE to wipe out all of the metadata when copying to a new
# key.
new_key = '%s-c' % random_chars(6)
p = aws(
's3 cp s3://%s/%s s3://%s/%s --metadata-directive REPLACE'
% (shared_bucket, original_key, shared_bucket, new_key)
)
self.assert_no_errors(p)
response = s3_utils.head_object(shared_bucket, new_key)
# Make sure all of the original metadata is gone.
for name, value in metadata_ref.items():
assert response.get(name) != value
# Use REPLACE to wipe out all of the metadata but include a new
# metadata value.
new_key = '%s-d' % random_chars(6)
p = aws(
's3 cp s3://%s/%s s3://%s/%s --metadata-directive REPLACE '
'--content-type bar'
% (shared_bucket, original_key, shared_bucket, new_key)
)
self.assert_no_errors(p)
response = s3_utils.head_object(shared_bucket, new_key)
# Make sure the content type metadata is included
assert response['ContentType'] == 'bar'
# Make sure all of the original metadata is gone.
for name, value in metadata_ref.items():
assert response.get(name) != value
def test_cp_with_request_payer(self, files, s3_utils, shared_bucket):
foo_txt = files.create_file('foo.txt', 'this is foo.txt')
p = aws(
's3 cp %s s3://%s/mykey --request-payer' % (foo_txt, shared_bucket)
)
# From the S3 API, the only way to for sure know that request payer is
# working is to set up a bucket with request payer and have another
# account with permissions make a request to that bucket. If they
# do not include request payer, they will get an access denied error.
# Setting this up for an integration test would be tricky as it
# requires having/creating another account outside of the one running
# the integration tests. So instead at the very least we want to
# make sure we can use the parameter, have the command run
# successfully, and correctly upload the key to S3.
self.assert_no_errors(p)
assert s3_utils.key_exists(shared_bucket, key_name='mykey')
contents = s3_utils.get_key_contents(shared_bucket, key_name='mykey')
assert contents == 'this is foo.txt'
class TestSync(BaseParameterizedS3ClientTest):
def test_sync_with_plus_chars_paginate(self, files, shared_bucket):
# This test ensures pagination tokens are url decoded.
# 1. Create > 2 files with '+' in the filename.
# 2. Sync up to s3 while the page size is 2.
# 3. Sync up to s3 while the page size is 2.
# 4. Verify nothing was synced up down from s3 in step 3.
filenames = []
for i in range(4):
# Create a file with a space char and a '+' char in the filename.
# We're interested in testing the filename comparisons, not the
# mtime comparisons so we're setting the mtime to some time
# in the past to avoid mtime comparisons interfering with
# test results.
mtime = time.time() - 300
filenames.append(
files.create_file('foo +%06d' % i, contents='', mtime=mtime)
)
p = aws(
's3 sync %s s3://%s/ --page-size 2'
% (files.rootdir, shared_bucket)
)
self.assert_no_errors(p)
time.sleep(1)
p2 = aws(
's3 sync %s s3://%s/ --page-size 2'
% (files.rootdir, shared_bucket)
)
assert 'upload:' not in p2.stdout
assert '' == p2.stdout
def test_s3_to_s3_sync_with_plus_char_paginate(
self, files, s3_utils, shared_bucket, shared_copy_bucket
):
keynames = []
for i in range(4):
keyname = 'foo+%d' % i
keynames.append(keyname)
files.create_file(keyname, contents='')
bucket_name = shared_bucket
bucket_name_2 = shared_copy_bucket
p = aws('s3 sync %s s3://%s' % (files.rootdir, bucket_name))
self.assert_no_errors(p)
for key in keynames:
assert s3_utils.key_exists(bucket_name, key)
p = aws(
's3 sync s3://%s/ s3://%s/ --page-size 2'
% (bucket_name, bucket_name_2)
)
self.assert_no_errors(p)
for key in keynames:
assert s3_utils.key_exists(bucket_name_2, key)
p2 = aws(
's3 sync s3://%s/ s3://%s/ --page-size 2'
% (bucket_name, bucket_name_2)
)
assert 'copy:' not in p2.stdout
assert '' == p2.stdout
def test_sync_no_resync(self, files, s3_utils, shared_bucket):
files.create_file('xyz123456789', contents='test1')
files.create_file(os.path.join('xyz1', 'test'), contents='test2')
files.create_file(os.path.join('xyz', 'test'), contents='test3')
p = aws('s3 sync %s s3://%s' % (files.rootdir, shared_bucket))
self.assert_no_errors(p)
time.sleep(2)
assert s3_utils.key_exists(shared_bucket, 'xyz123456789')
assert s3_utils.key_exists(shared_bucket, 'xyz1/test')
assert s3_utils.key_exists(shared_bucket, 'xyz/test')
p2 = aws('s3 sync %s s3://%s/' % (files.rootdir, shared_bucket))
assert 'upload:' not in p2.stdout
assert '' == p2.stdout
def test_sync_to_from_s3(self, files, s3_utils, shared_bucket):
foo_txt = files.create_file('foo.txt', 'foo contents')
bar_txt = files.create_file('bar.txt', 'bar contents')
# Sync the directory and the bucket.
p = aws('s3 sync %s s3://%s' % (files.rootdir, shared_bucket))
self.assert_no_errors(p)
# Ensure both files are in the bucket.
assert s3_utils.key_exists(shared_bucket, 'foo.txt')
assert s3_utils.key_exists(shared_bucket, 'bar.txt')
# Sync back down. First remote the local files.
os.remove(foo_txt)
os.remove(bar_txt)
p = aws('s3 sync s3://%s %s' % (shared_bucket, files.rootdir))
# The files should be back now.
assert os.path.isfile(foo_txt)
assert os.path.isfile(bar_txt)
with open(foo_txt) as f:
assert f.read() == 'foo contents'
with open(bar_txt) as f:
assert f.read() == 'bar contents'
def test_sync_to_nonexistent_bucket(self, files):
files.create_file('foo.txt', 'foo contents')
files.create_file('bar.txt', 'bar contents')
# Sync the directory and the bucket.
p = aws(f's3 sync {files.rootdir} s3://{_NON_EXISTENT_BUCKET}')
assert p.rc == 1
def test_sync_with_empty_files(self, files, s3_utils, shared_bucket):
files.create_file('foo.txt', 'foo contents')
files.create_file('bar.txt', contents='')
p = aws('s3 sync %s s3://%s/' % (files.rootdir, shared_bucket))
assert p.rc == 0
assert 'failed' not in p.stderr
assert s3_utils.key_exists(shared_bucket, 'bar.txt')
def test_sync_with_delete_option_with_same_prefix(
self, files, shared_bucket
):
# Test for issue 440 (https://github.com/aws/aws-cli/issues/440)
# First, we need to create a directory structure that has a dir with
# the same prefix as some of the files:
#
# test/foo.txt
# test-123.txt
# test-321.txt
# test.txt
# create test/foo.txt
nested_dir = os.path.join(files.rootdir, 'test')
os.mkdir(nested_dir)
files.create_file(
os.path.join(nested_dir, 'foo.txt'), contents='foo.txt contents'
)
# Then create test-123.txt, test-321.txt, test.txt.
files.create_file('test-123.txt', 'test-123.txt contents')
files.create_file('test-321.txt', 'test-321.txt contents')
files.create_file('test.txt', 'test.txt contents')
# Now sync this content up to s3.
# Allow settling time so that we have a different time between
# source and destination.
time.sleep(2)
p = aws('s3 sync %s s3://%s/' % (files.rootdir, shared_bucket))
self.assert_no_errors(p)
# Now here's the issue. If we try to sync the contents down
# with the --delete flag we should *not* see any output, the
# sync operation should determine that nothing is different and
# therefore do nothing. We can just use --dryrun to show the issue.
p = aws(
's3 sync s3://%s/ %s --dryrun --delete'
% (shared_bucket, files.rootdir)
)
self.assert_no_errors(p)
# These assertion methods will give better error messages than just
# checking if the output is empty.
assert 'download:' not in p.stdout
assert 'delete:' not in p.stdout
assert '' == p.stdout
def test_sync_with_delete_across_sig4_regions(
self, files, s3_utils, shared_bucket, shared_cross_region_bucket
):
src_region = 'us-west-2'
dst_region = 'eu-central-1'
src_bucket = shared_bucket
dst_bucket = shared_cross_region_bucket
src_key_name = 'hello.txt'
files.create_file(src_key_name, contents='hello')
p = aws(
's3 sync %s s3://%s --region %s'
% (files.rootdir, src_bucket, src_region)
)
self.assert_no_errors(p)
assert s3_utils.key_exists(src_bucket, src_key_name)
files.remove_all()
dst_key_name = 'goodbye.txt'
files.create_file(dst_key_name, contents='goodbye')
p = aws(
's3 sync %s s3://%s --region %s'
% (files.rootdir, dst_bucket, dst_region)
)
self.assert_no_errors(p)
assert s3_utils.key_exists(dst_bucket, dst_key_name)
assert s3_utils.key_not_exists(dst_bucket, src_key_name)
p = aws(
's3 sync --delete s3://%s s3://%s '
'--source-region %s --region %s'
% (src_bucket, dst_bucket, src_region, dst_region)
)
self.assert_no_errors(p)
assert s3_utils.key_exists(src_bucket, src_key_name)
assert s3_utils.key_exists(dst_bucket, src_key_name)
assert s3_utils.key_not_exists(src_bucket, dst_key_name)
assert s3_utils.key_not_exists(dst_bucket, dst_key_name)
def test_sync_delete_locally(self, files, s3_utils, shared_bucket):
file_to_delete = files.create_file('foo.txt', contents='foo contents')
s3_utils.put_object(shared_bucket, 'bar.txt', contents='bar contents')
p = aws(
's3 sync s3://%s/ %s --delete' % (shared_bucket, files.rootdir)
)
self.assert_no_errors(p)
# Make sure the uploaded file got downloaded and the previously
# existing local file got deleted
assert os.path.exists(os.path.join(files.rootdir, 'bar.txt'))
assert not os.path.exists(file_to_delete)
class TestSourceRegion(BaseParameterizedS3ClientTest):
def test_cp_region(
self,
files,
s3_utils,
shared_non_dns_compatible_bucket,
shared_non_dns_compatible_us_east_1_bucket,
):
src_region = 'us-west-2'
src_bucket = shared_non_dns_compatible_bucket
dest_region = 'us-east-1'
dest_bucket = shared_non_dns_compatible_us_east_1_bucket
files.create_file('foo.txt', 'foo')
p = aws(
's3 sync %s s3://%s/ --region %s'
% (files.rootdir, src_bucket, src_region)
)
self.assert_no_errors(p)
p2 = aws(
's3 cp s3://%s/ s3://%s/ --region %s --source-region %s '
'--recursive' % (src_bucket, dest_bucket, dest_region, src_region)
)
assert p2.rc == 0, p2.stdout
assert s3_utils.key_exists(dest_bucket, 'foo.txt')
def test_sync_region(
self,
files,
s3_utils,
shared_non_dns_compatible_bucket,
shared_non_dns_compatible_us_east_1_bucket,
):
src_region = 'us-west-2'
src_bucket = shared_non_dns_compatible_bucket
dest_region = 'us-east-1'
dest_bucket = shared_non_dns_compatible_us_east_1_bucket
files.create_file('foo.txt', 'foo')
p = aws(
's3 sync %s s3://%s/ --region %s'
% (files.rootdir, src_bucket, src_region)
)