1
# -*- coding: utf-8 -*-
3
# Copyright (c) 2011 Mitch Garnaat http://garnaat.org/
6
# Permission is hereby granted, free of charge, to any person obtaining a
7
# copy of this software and associated documentation files (the
8
# "Software"), to deal in the Software without restriction, including
9
# without limitation the rights to use, copy, modify, merge, publish, dis-
10
# tribute, sublicense, and/or sell copies of the Software, and to permit
11
# persons to whom the Software is furnished to do so, subject to the fol-
14
# The above copyright notice and this permission notice shall be included
15
# in all copies or substantial portions of the Software.
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
19
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
20
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26
Some unit tests for the S3 MultiPartUpload
30
# Multipart uploads require at least one part. If you upload
31
# multiple parts then all parts except the last part has to be
32
# bigger than 5M. Hence we just use 1 part so we can keep
33
# things small and still test logic.
38
from boto.s3.connection import S3Connection
40
class S3MultiPartUploadTest (unittest.TestCase):
43
self.conn = S3Connection(is_secure=False)
44
self.bucket_name = 'multipart-%d' % int(time.time())
45
self.bucket = self.conn.create_bucket(self.bucket_name)
48
for key in self.bucket:
54
mpu = self.bucket.initiate_multipart_upload(key_name)
57
def test_complete_ascii(self):
59
mpu = self.bucket.initiate_multipart_upload(key_name)
60
fp = StringIO.StringIO("small file")
61
mpu.upload_part_from_file(fp, part_num=1)
63
cmpu = mpu.complete_upload()
64
self.assertEqual(cmpu.key_name, key_name)
65
self.assertNotEqual(cmpu.etag, None)
67
def test_complete_japanese(self):
69
mpu = self.bucket.initiate_multipart_upload(key_name)
70
fp = StringIO.StringIO("small file")
71
mpu.upload_part_from_file(fp, part_num=1)
73
cmpu = mpu.complete_upload()
74
# LOL... just found an Amazon bug when it returns the
75
# key in the completemultipartupload result. AWS returns
76
# ??? instead of the correctly encoded key name. We should
77
# fix this to the comment line below when amazon fixes this
78
# and this test starts failing due to below assertion.
79
self.assertEqual(cmpu.key_name, "???")
80
#self.assertEqual(cmpu.key_name, key_name)
81
self.assertNotEqual(cmpu.etag, None)
83
def test_list_japanese(self):
85
mpu = self.bucket.initiate_multipart_upload(key_name)
86
rs = self.bucket.list_multipart_uploads()
87
# New bucket, so only one upload expected
88
lmpu = iter(rs).next()
89
self.assertEqual(lmpu.id, mpu.id)
90
self.assertEqual(lmpu.key_name, key_name)
91
# Abort using the one returned in the list
94
def test_four_part_file(self):
96
contents = "01234567890123456789"
97
sfp = StringIO.StringIO(contents)
99
# upload 20 bytes in 4 parts of 5 bytes each
100
mpu = self.bucket.initiate_multipart_upload(key_name)
101
mpu.upload_part_from_file(sfp, part_num=1, size=5)
102
mpu.upload_part_from_file(sfp, part_num=2, size=5)
103
mpu.upload_part_from_file(sfp, part_num=3, size=5)
104
mpu.upload_part_from_file(sfp, part_num=4, size=5)
111
self.assertEqual(5, part.size)
112
etags[pn] = part.etag
113
self.assertEqual(pn, 4)
115
self.assertEqual(etags[1], etags[3])
117
self.assertEqual(etags[2], etags[4])
118
# etag 01234 != etag 56789
119
self.assertNotEqual(etags[1], etags[2])
121
# parts are too small to compete as each part must
122
# be a min of 5MB so so we'll assume that is enough
123
# testing and abort the upload.