PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/tests/integration/s3/test_multipart.py

#
Python | 139 lines | 76 code | 17 blank | 46 comment | 3 complexity | f360f0293177c83d3ab26c29b5e40ed1 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2011 Mitch Garnaat http://garnaat.org/
  3. # All rights reserved.
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the
  7. # "Software"), to deal in the Software without restriction, including
  8. # without limitation the rights to use, copy, modify, merge, publish, dis-
  9. # tribute, sublicense, and/or sell copies of the Software, and to permit
  10. # persons to whom the Software is furnished to do so, subject to the fol-
  11. # lowing conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included
  14. # in all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  18. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  19. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. """
  24. Some unit tests for the S3 MultiPartUpload
  25. """
  26. # Note:
  27. # Multipart uploads require at least one part. If you upload
  28. # multiple parts then all parts except the last part has to be
  29. # bigger than 5M. Hence we just use 1 part so we can keep
  30. # things small and still test logic.
  31. import unittest
  32. import time
  33. import StringIO
  34. from boto.s3.connection import S3Connection
  35. class S3MultiPartUploadTest(unittest.TestCase):
  36. s3 = True
  37. def setUp(self):
  38. self.conn = S3Connection(is_secure=False)
  39. self.bucket_name = 'multipart-%d' % int(time.time())
  40. self.bucket = self.conn.create_bucket(self.bucket_name)
  41. def tearDown(self):
  42. for key in self.bucket:
  43. key.delete()
  44. self.bucket.delete()
  45. def test_abort(self):
  46. key_name = u"???"
  47. mpu = self.bucket.initiate_multipart_upload(key_name)
  48. mpu.cancel_upload()
  49. def test_complete_ascii(self):
  50. key_name = "test"
  51. mpu = self.bucket.initiate_multipart_upload(key_name)
  52. fp = StringIO.StringIO("small file")
  53. mpu.upload_part_from_file(fp, part_num=1)
  54. fp.close()
  55. cmpu = mpu.complete_upload()
  56. self.assertEqual(cmpu.key_name, key_name)
  57. self.assertNotEqual(cmpu.etag, None)
  58. def test_complete_japanese(self):
  59. key_name = u"???"
  60. mpu = self.bucket.initiate_multipart_upload(key_name)
  61. fp = StringIO.StringIO("small file")
  62. mpu.upload_part_from_file(fp, part_num=1)
  63. fp.close()
  64. cmpu = mpu.complete_upload()
  65. # LOL... just found an Amazon bug when it returns the
  66. # key in the completemultipartupload result. AWS returns
  67. # ??? instead of the correctly encoded key name. We should
  68. # fix this to the comment line below when amazon fixes this
  69. # and this test starts failing due to below assertion.
  70. self.assertEqual(cmpu.key_name, "???")
  71. #self.assertEqual(cmpu.key_name, key_name)
  72. self.assertNotEqual(cmpu.etag, None)
  73. def test_list_japanese(self):
  74. key_name = u"???"
  75. mpu = self.bucket.initiate_multipart_upload(key_name)
  76. rs = self.bucket.list_multipart_uploads()
  77. # New bucket, so only one upload expected
  78. lmpu = iter(rs).next()
  79. self.assertEqual(lmpu.id, mpu.id)
  80. self.assertEqual(lmpu.key_name, key_name)
  81. # Abort using the one returned in the list
  82. lmpu.cancel_upload()
  83. def test_list_multipart_uploads(self):
  84. key_name = u"???"
  85. mpus = []
  86. mpus.append(self.bucket.initiate_multipart_upload(key_name))
  87. mpus.append(self.bucket.initiate_multipart_upload(key_name))
  88. rs = self.bucket.list_multipart_uploads()
  89. # uploads (for a key) are returned in time initiated asc order
  90. for lmpu in rs:
  91. ompu = mpus.pop(0)
  92. self.assertEqual(lmpu.key_name, ompu.key_name)
  93. self.assertEqual(lmpu.id, ompu.id)
  94. self.assertEqual(0, len(mpus))
  95. def test_four_part_file(self):
  96. key_name = "k"
  97. contents = "01234567890123456789"
  98. 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)
  105. sfp.close()
  106. etags = {}
  107. pn = 0
  108. for part in mpu:
  109. pn += 1
  110. self.assertEqual(5, part.size)
  111. etags[pn] = part.etag
  112. self.assertEqual(pn, 4)
  113. # etags for 01234
  114. self.assertEqual(etags[1], etags[3])
  115. # etags for 56789
  116. self.assertEqual(etags[2], etags[4])
  117. # etag 01234 != etag 56789
  118. self.assertNotEqual(etags[1], etags[2])
  119. # parts are too small to compete as each part must
  120. # be a min of 5MB so so we'll assume that is enough
  121. # testing and abort the upload.
  122. mpu.cancel_upload()