PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Python | 107 lines | 52 code | 12 blank | 43 comment | 5 complexity | 2a0532d3789063b5f3cbc48794e44899 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 Bucket
  25. """
  26. import unittest
  27. import time
  28. from boto.s3.connection import S3Connection
  29. from boto.s3.bucketlogging import BucketLogging
  30. from boto.s3.acl import Grant
  31. class S3BucketTest (unittest.TestCase):
  32. s3 = True
  33. def setUp(self):
  34. self.conn = S3Connection()
  35. self.bucket_name = 'bucket-%d' % int(time.time())
  36. self.bucket = self.conn.create_bucket(self.bucket_name)
  37. def tearDown(self):
  38. for key in self.bucket:
  39. key.delete()
  40. self.bucket.delete()
  41. def test_next_marker(self):
  42. expected = ["a/", "b", "c"]
  43. for key_name in expected:
  44. key = self.bucket.new_key(key_name)
  45. key.set_contents_from_string(key_name)
  46. # Normal list of first 2 keys will have
  47. # no NextMarker set, so we use last key to iterate
  48. # last element will be "b" so no issue.
  49. rs = self.bucket.get_all_keys(max_keys=2)
  50. for element in rs:
  51. pass
  52. self.assertEqual(element.name, "b")
  53. self.assertEqual(rs.next_marker, None)
  54. # list using delimiter of first 2 keys will have
  55. # a NextMarker set (when truncated). As prefixes
  56. # are grouped together at the end, we get "a/" as
  57. # last element, but luckily we have next_marker.
  58. rs = self.bucket.get_all_keys(max_keys=2, delimiter="/")
  59. for element in rs:
  60. pass
  61. self.assertEqual(element.name, "a/")
  62. self.assertEqual(rs.next_marker, "b")
  63. # ensure bucket.list() still works by just
  64. # popping elements off the front of expected.
  65. rs = self.bucket.list()
  66. for element in rs:
  67. self.assertEqual(element.name, expected.pop(0))
  68. self.assertEqual(expected, [])
  69. def test_logging(self):
  70. # use self.bucket as the target bucket so that teardown
  71. # will delete any log files that make it into the bucket
  72. # automatically and all we have to do is delete the
  73. # source bucket.
  74. sb_name = "src-" + self.bucket_name
  75. sb = self.conn.create_bucket(sb_name)
  76. # grant log write perms to target bucket using canned-acl
  77. self.bucket.set_acl("log-delivery-write")
  78. target_bucket = self.bucket_name
  79. target_prefix = u"jp/?ƒ­?‚°/"
  80. # Check existing status is disabled
  81. bls = sb.get_logging_status()
  82. self.assertEqual(bls.target, None)
  83. # Create a logging status and grant auth users READ PERM
  84. authuri = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers"
  85. authr = Grant(permission="READ", type="Group", uri=authuri)
  86. sb.enable_logging(target_bucket, target_prefix=target_prefix, grants=[authr])
  87. # Check the status and confirm its set.
  88. bls = sb.get_logging_status()
  89. self.assertEqual(bls.target, target_bucket)
  90. self.assertEqual(bls.prefix, target_prefix)
  91. self.assertEqual(len(bls.grants), 1)
  92. self.assertEqual(bls.grants[0].type, "Group")
  93. self.assertEqual(bls.grants[0].uri, authuri)
  94. # finally delete the src bucket
  95. sb.delete()