PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/tests/devpay/test_s3.py

#
Python | 181 lines | 131 code | 10 blank | 40 comment | 5 complexity | 4ab1495a09910c99dd88473c77623a65 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a
  5. # copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish, dis-
  8. # tribute, sublicense, and/or sell copies of the Software, and to permit
  9. # persons to whom the Software is furnished to do so, subject to the fol-
  10. # lowing conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  17. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  18. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. # IN THE SOFTWARE.
  22. """
  23. Some unit tests for the S3Connection
  24. """
  25. import time
  26. import os
  27. import urllib
  28. from boto.s3.connection import S3Connection
  29. from boto.exception import S3PermissionsError
  30. # this test requires a devpay product and user token to run:
  31. AMAZON_USER_TOKEN = '{UserToken}...your token here...'
  32. DEVPAY_HEADERS = { 'x-amz-security-token': AMAZON_USER_TOKEN }
  33. def test():
  34. print '--- running S3Connection tests (DevPay) ---'
  35. c = S3Connection()
  36. # create a new, empty bucket
  37. bucket_name = 'test-%d' % int(time.time())
  38. bucket = c.create_bucket(bucket_name, headers=DEVPAY_HEADERS)
  39. # now try a get_bucket call and see if it's really there
  40. bucket = c.get_bucket(bucket_name, headers=DEVPAY_HEADERS)
  41. # test logging
  42. logging_bucket = c.create_bucket(bucket_name + '-log', headers=DEVPAY_HEADERS)
  43. logging_bucket.set_as_logging_target(headers=DEVPAY_HEADERS)
  44. bucket.enable_logging(target_bucket=logging_bucket, target_prefix=bucket.name, headers=DEVPAY_HEADERS)
  45. bucket.disable_logging(headers=DEVPAY_HEADERS)
  46. c.delete_bucket(logging_bucket, headers=DEVPAY_HEADERS)
  47. # create a new key and store it's content from a string
  48. k = bucket.new_key()
  49. k.name = 'foobar'
  50. s1 = 'This is a test of file upload and download'
  51. s2 = 'This is a second string to test file upload and download'
  52. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  53. fp = open('foobar', 'wb')
  54. # now get the contents from s3 to a local file
  55. k.get_contents_to_file(fp, headers=DEVPAY_HEADERS)
  56. fp.close()
  57. fp = open('foobar')
  58. # check to make sure content read from s3 is identical to original
  59. assert s1 == fp.read(), 'corrupted file'
  60. fp.close()
  61. # test generated URLs
  62. url = k.generate_url(3600, headers=DEVPAY_HEADERS)
  63. file = urllib.urlopen(url)
  64. assert s1 == file.read(), 'invalid URL %s' % url
  65. url = k.generate_url(3600, force_http=True, headers=DEVPAY_HEADERS)
  66. file = urllib.urlopen(url)
  67. assert s1 == file.read(), 'invalid URL %s' % url
  68. bucket.delete_key(k, headers=DEVPAY_HEADERS)
  69. # test a few variations on get_all_keys - first load some data
  70. # for the first one, let's override the content type
  71. phony_mimetype = 'application/x-boto-test'
  72. headers = {'Content-Type': phony_mimetype}
  73. headers.update(DEVPAY_HEADERS)
  74. k.name = 'foo/bar'
  75. k.set_contents_from_string(s1, headers)
  76. k.name = 'foo/bas'
  77. k.set_contents_from_filename('foobar', headers=DEVPAY_HEADERS)
  78. k.name = 'foo/bat'
  79. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  80. k.name = 'fie/bar'
  81. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  82. k.name = 'fie/bas'
  83. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  84. k.name = 'fie/bat'
  85. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  86. # try resetting the contents to another value
  87. md5 = k.md5
  88. k.set_contents_from_string(s2, headers=DEVPAY_HEADERS)
  89. assert k.md5 != md5
  90. os.unlink('foobar')
  91. all = bucket.get_all_keys(headers=DEVPAY_HEADERS)
  92. assert len(all) == 6
  93. rs = bucket.get_all_keys(prefix='foo', headers=DEVPAY_HEADERS)
  94. assert len(rs) == 3
  95. rs = bucket.get_all_keys(prefix='', delimiter='/', headers=DEVPAY_HEADERS)
  96. assert len(rs) == 2
  97. rs = bucket.get_all_keys(maxkeys=5, headers=DEVPAY_HEADERS)
  98. assert len(rs) == 5
  99. # test the lookup method
  100. k = bucket.lookup('foo/bar', headers=DEVPAY_HEADERS)
  101. assert isinstance(k, bucket.key_class)
  102. assert k.content_type == phony_mimetype
  103. k = bucket.lookup('notthere', headers=DEVPAY_HEADERS)
  104. assert k == None
  105. # try some metadata stuff
  106. k = bucket.new_key()
  107. k.name = 'has_metadata'
  108. mdkey1 = 'meta1'
  109. mdval1 = 'This is the first metadata value'
  110. k.set_metadata(mdkey1, mdval1)
  111. mdkey2 = 'meta2'
  112. mdval2 = 'This is the second metadata value'
  113. k.set_metadata(mdkey2, mdval2)
  114. k.set_contents_from_string(s1, headers=DEVPAY_HEADERS)
  115. k = bucket.lookup('has_metadata', headers=DEVPAY_HEADERS)
  116. assert k.get_metadata(mdkey1) == mdval1
  117. assert k.get_metadata(mdkey2) == mdval2
  118. k = bucket.new_key()
  119. k.name = 'has_metadata'
  120. k.get_contents_as_string(headers=DEVPAY_HEADERS)
  121. assert k.get_metadata(mdkey1) == mdval1
  122. assert k.get_metadata(mdkey2) == mdval2
  123. bucket.delete_key(k, headers=DEVPAY_HEADERS)
  124. # test list and iterator
  125. rs1 = bucket.list(headers=DEVPAY_HEADERS)
  126. num_iter = 0
  127. for r in rs1:
  128. num_iter = num_iter + 1
  129. rs = bucket.get_all_keys(headers=DEVPAY_HEADERS)
  130. num_keys = len(rs)
  131. assert num_iter == num_keys
  132. # try a key with a funny character
  133. k = bucket.new_key()
  134. k.name = 'testnewline\n'
  135. k.set_contents_from_string('This is a test', headers=DEVPAY_HEADERS)
  136. rs = bucket.get_all_keys(headers=DEVPAY_HEADERS)
  137. assert len(rs) == num_keys + 1
  138. bucket.delete_key(k, headers=DEVPAY_HEADERS)
  139. rs = bucket.get_all_keys(headers=DEVPAY_HEADERS)
  140. assert len(rs) == num_keys
  141. # try some acl stuff
  142. bucket.set_acl('public-read', headers=DEVPAY_HEADERS)
  143. policy = bucket.get_acl(headers=DEVPAY_HEADERS)
  144. assert len(policy.acl.grants) == 2
  145. bucket.set_acl('private', headers=DEVPAY_HEADERS)
  146. policy = bucket.get_acl(headers=DEVPAY_HEADERS)
  147. assert len(policy.acl.grants) == 1
  148. k = bucket.lookup('foo/bar', headers=DEVPAY_HEADERS)
  149. k.set_acl('public-read', headers=DEVPAY_HEADERS)
  150. policy = k.get_acl(headers=DEVPAY_HEADERS)
  151. assert len(policy.acl.grants) == 2
  152. k.set_acl('private', headers=DEVPAY_HEADERS)
  153. policy = k.get_acl(headers=DEVPAY_HEADERS)
  154. assert len(policy.acl.grants) == 1
  155. # try the convenience methods for grants
  156. # this doesn't work with devpay
  157. #bucket.add_user_grant('FULL_CONTROL',
  158. # 'c1e724fbfa0979a4448393c59a8c055011f739b6d102fb37a65f26414653cd67',
  159. # headers=DEVPAY_HEADERS)
  160. try:
  161. bucket.add_email_grant('foobar', 'foo@bar.com', headers=DEVPAY_HEADERS)
  162. except S3PermissionsError:
  163. pass
  164. # now delete all keys in bucket
  165. for k in all:
  166. bucket.delete_key(k, headers=DEVPAY_HEADERS)
  167. # now delete bucket
  168. c.delete_bucket(bucket, headers=DEVPAY_HEADERS)
  169. print '--- tests completed ---'
  170. if __name__ == '__main__':
  171. test()