PageRenderTime 42ms CodeModel.GetById 31ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

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