~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to tests/s3/test_multipart.py

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2012-04-15 20:21:21 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120415202121-3fpf6q355s0xqpyu
Tags: 2.3.0-1
* New upstream release (Closes: #664478)
* Update debian/watch for Boto's move to Github.  Thanks Scott
  Moser. (Closes: #650480)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright (c) 2011 Mitch Garnaat http://garnaat.org/
 
4
# All rights reserved.
 
5
#
 
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-
 
12
# lowing conditions:
 
13
#
 
14
# The above copyright notice and this permission notice shall be included
 
15
# in all copies or substantial portions of the Software.
 
16
#
 
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
 
23
# IN THE SOFTWARE.
 
24
 
 
25
"""
 
26
Some unit tests for the S3 MultiPartUpload
 
27
"""
 
28
 
 
29
# Note:
 
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.
 
34
 
 
35
import unittest
 
36
import time
 
37
import StringIO
 
38
from boto.s3.connection import S3Connection
 
39
 
 
40
class S3MultiPartUploadTest (unittest.TestCase):
 
41
 
 
42
    def setUp(self):
 
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)
 
46
 
 
47
    def tearDown(self):
 
48
        for key in self.bucket:
 
49
            key.delete()
 
50
        self.bucket.delete()
 
51
 
 
52
    def test_abort(self):
 
53
        key_name = u"テスト"
 
54
        mpu = self.bucket.initiate_multipart_upload(key_name)
 
55
        mpu.cancel_upload()
 
56
 
 
57
    def test_complete_ascii(self):
 
58
        key_name = "test"
 
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)
 
62
        fp.close()
 
63
        cmpu = mpu.complete_upload()
 
64
        self.assertEqual(cmpu.key_name, key_name)
 
65
        self.assertNotEqual(cmpu.etag, None)
 
66
 
 
67
    def test_complete_japanese(self):
 
68
        key_name = u"テスト"
 
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)
 
72
        fp.close()
 
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)
 
82
 
 
83
    def test_list_japanese(self):
 
84
        key_name = u"テスト"
 
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
 
92
        lmpu.cancel_upload()
 
93
 
 
94
    def test_four_part_file(self):
 
95
        key_name = "k"
 
96
        contents = "01234567890123456789"
 
97
        sfp = StringIO.StringIO(contents)
 
98
 
 
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)
 
105
        sfp.close()
 
106
 
 
107
        etags = {}
 
108
        pn = 0
 
109
        for part in mpu:
 
110
            pn += 1
 
111
            self.assertEqual(5, part.size)
 
112
            etags[pn] = part.etag
 
113
        self.assertEqual(pn, 4)
 
114
        # etags for 01234
 
115
        self.assertEqual(etags[1], etags[3])
 
116
        # etags for 56789
 
117
        self.assertEqual(etags[2], etags[4])
 
118
        # etag 01234 != etag 56789
 
119
        self.assertNotEqual(etags[1], etags[2])
 
120
 
 
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.
 
124
        mpu.cancel_upload()