OLD | NEW |
(Empty) | |
| 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 Bucket |
| 27 """ |
| 28 |
| 29 import unittest |
| 30 import time |
| 31 |
| 32 from boto.exception import S3ResponseError |
| 33 from boto.s3.connection import S3Connection |
| 34 from boto.s3.bucketlogging import BucketLogging |
| 35 from boto.s3.lifecycle import Lifecycle |
| 36 from boto.s3.lifecycle import Transition |
| 37 from boto.s3.lifecycle import Rule |
| 38 from boto.s3.acl import Grant |
| 39 from boto.s3.tagging import Tags, TagSet |
| 40 from boto.s3.lifecycle import Lifecycle, Expiration, Transition |
| 41 from boto.s3.website import RedirectLocation |
| 42 |
| 43 |
| 44 class S3BucketTest (unittest.TestCase): |
| 45 s3 = True |
| 46 |
| 47 def setUp(self): |
| 48 self.conn = S3Connection() |
| 49 self.bucket_name = 'bucket-%d' % int(time.time()) |
| 50 self.bucket = self.conn.create_bucket(self.bucket_name) |
| 51 |
| 52 def tearDown(self): |
| 53 for key in self.bucket: |
| 54 key.delete() |
| 55 self.bucket.delete() |
| 56 |
| 57 def test_next_marker(self): |
| 58 expected = ["a/", "b", "c"] |
| 59 for key_name in expected: |
| 60 key = self.bucket.new_key(key_name) |
| 61 key.set_contents_from_string(key_name) |
| 62 |
| 63 # Normal list of first 2 keys will have |
| 64 # no NextMarker set, so we use last key to iterate |
| 65 # last element will be "b" so no issue. |
| 66 rs = self.bucket.get_all_keys(max_keys=2) |
| 67 for element in rs: |
| 68 pass |
| 69 self.assertEqual(element.name, "b") |
| 70 self.assertEqual(rs.next_marker, None) |
| 71 |
| 72 # list using delimiter of first 2 keys will have |
| 73 # a NextMarker set (when truncated). As prefixes |
| 74 # are grouped together at the end, we get "a/" as |
| 75 # last element, but luckily we have next_marker. |
| 76 rs = self.bucket.get_all_keys(max_keys=2, delimiter="/") |
| 77 for element in rs: |
| 78 pass |
| 79 self.assertEqual(element.name, "a/") |
| 80 self.assertEqual(rs.next_marker, "b") |
| 81 |
| 82 # ensure bucket.list() still works by just |
| 83 # popping elements off the front of expected. |
| 84 rs = self.bucket.list() |
| 85 for element in rs: |
| 86 self.assertEqual(element.name, expected.pop(0)) |
| 87 self.assertEqual(expected, []) |
| 88 |
| 89 def test_logging(self): |
| 90 # use self.bucket as the target bucket so that teardown |
| 91 # will delete any log files that make it into the bucket |
| 92 # automatically and all we have to do is delete the |
| 93 # source bucket. |
| 94 sb_name = "src-" + self.bucket_name |
| 95 sb = self.conn.create_bucket(sb_name) |
| 96 # grant log write perms to target bucket using canned-acl |
| 97 self.bucket.set_acl("log-delivery-write") |
| 98 target_bucket = self.bucket_name |
| 99 target_prefix = u"jp/ログ/" |
| 100 # Check existing status is disabled |
| 101 bls = sb.get_logging_status() |
| 102 self.assertEqual(bls.target, None) |
| 103 # Create a logging status and grant auth users READ PERM |
| 104 authuri = "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" |
| 105 authr = Grant(permission="READ", type="Group", uri=authuri) |
| 106 sb.enable_logging(target_bucket, target_prefix=target_prefix, grants=[au
thr]) |
| 107 # Check the status and confirm its set. |
| 108 bls = sb.get_logging_status() |
| 109 self.assertEqual(bls.target, target_bucket) |
| 110 self.assertEqual(bls.prefix, target_prefix) |
| 111 self.assertEqual(len(bls.grants), 1) |
| 112 self.assertEqual(bls.grants[0].type, "Group") |
| 113 self.assertEqual(bls.grants[0].uri, authuri) |
| 114 # finally delete the src bucket |
| 115 sb.delete() |
| 116 |
| 117 def test_tagging(self): |
| 118 tagging = """ |
| 119 <Tagging> |
| 120 <TagSet> |
| 121 <Tag> |
| 122 <Key>tagkey</Key> |
| 123 <Value>tagvalue</Value> |
| 124 </Tag> |
| 125 </TagSet> |
| 126 </Tagging> |
| 127 """ |
| 128 self.bucket.set_xml_tags(tagging) |
| 129 response = self.bucket.get_tags() |
| 130 self.assertEqual(response[0][0].key, 'tagkey') |
| 131 self.assertEqual(response[0][0].value, 'tagvalue') |
| 132 self.bucket.delete_tags() |
| 133 try: |
| 134 self.bucket.get_tags() |
| 135 except S3ResponseError, e: |
| 136 self.assertEqual(e.code, 'NoSuchTagSet') |
| 137 except Exception, e: |
| 138 self.fail("Wrong exception raised (expected S3ResponseError): %s" |
| 139 % e) |
| 140 else: |
| 141 self.fail("Expected S3ResponseError, but no exception raised.") |
| 142 |
| 143 def test_tagging_from_objects(self): |
| 144 """Create tags from python objects rather than raw xml.""" |
| 145 t = Tags() |
| 146 tag_set = TagSet() |
| 147 tag_set.add_tag('akey', 'avalue') |
| 148 tag_set.add_tag('anotherkey', 'anothervalue') |
| 149 t.add_tag_set(tag_set) |
| 150 self.bucket.set_tags(t) |
| 151 response = self.bucket.get_tags() |
| 152 self.assertEqual(response[0][0].key, 'akey') |
| 153 self.assertEqual(response[0][0].value, 'avalue') |
| 154 self.assertEqual(response[0][1].key, 'anotherkey') |
| 155 self.assertEqual(response[0][1].value, 'anothervalue') |
| 156 |
| 157 def test_website_configuration(self): |
| 158 response = self.bucket.configure_website('index.html') |
| 159 self.assertTrue(response) |
| 160 config = self.bucket.get_website_configuration() |
| 161 self.assertEqual(config, {'WebsiteConfiguration': |
| 162 {'IndexDocument': {'Suffix': 'index.html'}}}) |
| 163 config2, xml = self.bucket.get_website_configuration_with_xml() |
| 164 self.assertEqual(config, config2) |
| 165 self.assertTrue('<Suffix>index.html</Suffix>' in xml, xml) |
| 166 |
| 167 def test_website_redirect_all_requests(self): |
| 168 response = self.bucket.configure_website( |
| 169 redirect_all_requests_to=RedirectLocation('example.com')) |
| 170 config = self.bucket.get_website_configuration() |
| 171 self.assertEqual(config, { |
| 172 'WebsiteConfiguration': { |
| 173 'RedirectAllRequestsTo': { |
| 174 'HostName': 'example.com'}}}) |
| 175 |
| 176 # Can configure the protocol as well. |
| 177 response = self.bucket.configure_website( |
| 178 redirect_all_requests_to=RedirectLocation('example.com', 'https')) |
| 179 config = self.bucket.get_website_configuration() |
| 180 self.assertEqual(config, { |
| 181 'WebsiteConfiguration': {'RedirectAllRequestsTo': { |
| 182 'HostName': 'example.com', |
| 183 'Protocol': 'https', |
| 184 }}} |
| 185 ) |
| 186 |
| 187 def test_lifecycle(self): |
| 188 lifecycle = Lifecycle() |
| 189 lifecycle.add_rule('myid', '', 'Enabled', 30) |
| 190 self.assertTrue(self.bucket.configure_lifecycle(lifecycle)) |
| 191 response = self.bucket.get_lifecycle_config() |
| 192 self.assertEqual(len(response), 1) |
| 193 actual_lifecycle = response[0] |
| 194 self.assertEqual(actual_lifecycle.id, 'myid') |
| 195 self.assertEqual(actual_lifecycle.prefix, '') |
| 196 self.assertEqual(actual_lifecycle.status, 'Enabled') |
| 197 self.assertEqual(actual_lifecycle.transition, None) |
| 198 |
| 199 def test_lifecycle_with_glacier_transition(self): |
| 200 lifecycle = Lifecycle() |
| 201 transition = Transition(days=30, storage_class='GLACIER') |
| 202 rule = Rule('myid', prefix='', status='Enabled', expiration=None, |
| 203 transition=transition) |
| 204 lifecycle.append(rule) |
| 205 self.assertTrue(self.bucket.configure_lifecycle(lifecycle)) |
| 206 response = self.bucket.get_lifecycle_config() |
| 207 transition = response[0].transition |
| 208 self.assertEqual(transition.days, 30) |
| 209 self.assertEqual(transition.storage_class, 'GLACIER') |
| 210 self.assertEqual(transition.date, None) |
| 211 |
| 212 def test_lifecycle_multi(self): |
| 213 date = '2022-10-12T00:00:00.000Z' |
| 214 sc = 'GLACIER' |
| 215 lifecycle = Lifecycle() |
| 216 lifecycle.add_rule("1", "1/", "Enabled", 1) |
| 217 lifecycle.add_rule("2", "2/", "Enabled", Expiration(days=2)) |
| 218 lifecycle.add_rule("3", "3/", "Enabled", Expiration(date=date)) |
| 219 lifecycle.add_rule("4", "4/", "Enabled", None, |
| 220 Transition(days=4, storage_class=sc)) |
| 221 lifecycle.add_rule("5", "5/", "Enabled", None, |
| 222 Transition(date=date, storage_class=sc)) |
| 223 # set the lifecycle |
| 224 self.bucket.configure_lifecycle(lifecycle) |
| 225 # read the lifecycle back |
| 226 readlifecycle = self.bucket.get_lifecycle_config(); |
| 227 for rule in readlifecycle: |
| 228 if rule.id == "1": |
| 229 self.assertEqual(rule.prefix, "1/") |
| 230 self.assertEqual(rule.expiration.days, 1) |
| 231 elif rule.id == "2": |
| 232 self.assertEqual(rule.prefix, "2/") |
| 233 self.assertEqual(rule.expiration.days, 2) |
| 234 elif rule.id == "3": |
| 235 self.assertEqual(rule.prefix, "3/") |
| 236 self.assertEqual(rule.expiration.date, date) |
| 237 elif rule.id == "4": |
| 238 self.assertEqual(rule.prefix, "4/") |
| 239 self.assertEqual(rule.transition.days, 4) |
| 240 self.assertEqual(rule.transition.storage_class, sc) |
| 241 elif rule.id == "5": |
| 242 self.assertEqual(rule.prefix, "5/") |
| 243 self.assertEqual(rule.transition.date, date) |
| 244 self.assertEqual(rule.transition.storage_class, sc) |
| 245 else: |
| 246 self.fail("unexpected id %s" % rule.id) |
| 247 |
| 248 def test_lifecycle_jp(self): |
| 249 # test lifecycle with Japanese prefix |
| 250 name = "Japanese files" |
| 251 prefix = u"日本語/" |
| 252 days = 30 |
| 253 lifecycle = Lifecycle() |
| 254 lifecycle.add_rule(name, prefix, "Enabled", days) |
| 255 # set the lifecycle |
| 256 self.bucket.configure_lifecycle(lifecycle) |
| 257 # read the lifecycle back |
| 258 readlifecycle = self.bucket.get_lifecycle_config(); |
| 259 for rule in readlifecycle: |
| 260 self.assertEqual(rule.id, name) |
| 261 self.assertEqual(rule.expiration.days, days) |
| 262 #Note: Boto seems correct? AWS seems broken? |
| 263 #self.assertEqual(rule.prefix, prefix) |
OLD | NEW |