OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ |
| 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 """ |
| 25 Some unit tests for the S3Connection |
| 26 """ |
| 27 |
| 28 import time |
| 29 import os |
| 30 import urllib |
| 31 |
| 32 from boto.s3.connection import S3Connection |
| 33 from boto.exception import S3PermissionsError |
| 34 |
| 35 # this test requires a devpay product and user token to run: |
| 36 |
| 37 AMAZON_USER_TOKEN = '{UserToken}...your token here...' |
| 38 DEVPAY_HEADERS = { 'x-amz-security-token': AMAZON_USER_TOKEN } |
| 39 |
| 40 def test(): |
| 41 print '--- running S3Connection tests (DevPay) ---' |
| 42 c = S3Connection() |
| 43 # create a new, empty bucket |
| 44 bucket_name = 'test-%d' % int(time.time()) |
| 45 bucket = c.create_bucket(bucket_name, headers=DEVPAY_HEADERS) |
| 46 # now try a get_bucket call and see if it's really there |
| 47 bucket = c.get_bucket(bucket_name, headers=DEVPAY_HEADERS) |
| 48 # test logging |
| 49 logging_bucket = c.create_bucket(bucket_name + '-log', headers=DEVPAY_HEADER
S) |
| 50 logging_bucket.set_as_logging_target(headers=DEVPAY_HEADERS) |
| 51 bucket.enable_logging(target_bucket=logging_bucket, target_prefix=bucket.nam
e, headers=DEVPAY_HEADERS) |
| 52 bucket.disable_logging(headers=DEVPAY_HEADERS) |
| 53 c.delete_bucket(logging_bucket, headers=DEVPAY_HEADERS) |
| 54 # create a new key and store it's content from a string |
| 55 k = bucket.new_key() |
| 56 k.name = 'foobar' |
| 57 s1 = 'This is a test of file upload and download' |
| 58 s2 = 'This is a second string to test file upload and download' |
| 59 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 60 fp = open('foobar', 'wb') |
| 61 # now get the contents from s3 to a local file |
| 62 k.get_contents_to_file(fp, headers=DEVPAY_HEADERS) |
| 63 fp.close() |
| 64 fp = open('foobar') |
| 65 # check to make sure content read from s3 is identical to original |
| 66 assert s1 == fp.read(), 'corrupted file' |
| 67 fp.close() |
| 68 # test generated URLs |
| 69 url = k.generate_url(3600, headers=DEVPAY_HEADERS) |
| 70 file = urllib.urlopen(url) |
| 71 assert s1 == file.read(), 'invalid URL %s' % url |
| 72 url = k.generate_url(3600, force_http=True, headers=DEVPAY_HEADERS) |
| 73 file = urllib.urlopen(url) |
| 74 assert s1 == file.read(), 'invalid URL %s' % url |
| 75 bucket.delete_key(k, headers=DEVPAY_HEADERS) |
| 76 # test a few variations on get_all_keys - first load some data |
| 77 # for the first one, let's override the content type |
| 78 phony_mimetype = 'application/x-boto-test' |
| 79 headers = {'Content-Type': phony_mimetype} |
| 80 headers.update(DEVPAY_HEADERS) |
| 81 k.name = 'foo/bar' |
| 82 k.set_contents_from_string(s1, headers) |
| 83 k.name = 'foo/bas' |
| 84 k.set_contents_from_filename('foobar', headers=DEVPAY_HEADERS) |
| 85 k.name = 'foo/bat' |
| 86 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 87 k.name = 'fie/bar' |
| 88 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 89 k.name = 'fie/bas' |
| 90 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 91 k.name = 'fie/bat' |
| 92 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 93 # try resetting the contents to another value |
| 94 md5 = k.md5 |
| 95 k.set_contents_from_string(s2, headers=DEVPAY_HEADERS) |
| 96 assert k.md5 != md5 |
| 97 os.unlink('foobar') |
| 98 all = bucket.get_all_keys(headers=DEVPAY_HEADERS) |
| 99 assert len(all) == 6 |
| 100 rs = bucket.get_all_keys(prefix='foo', headers=DEVPAY_HEADERS) |
| 101 assert len(rs) == 3 |
| 102 rs = bucket.get_all_keys(prefix='', delimiter='/', headers=DEVPAY_HEADERS) |
| 103 assert len(rs) == 2 |
| 104 rs = bucket.get_all_keys(maxkeys=5, headers=DEVPAY_HEADERS) |
| 105 assert len(rs) == 5 |
| 106 # test the lookup method |
| 107 k = bucket.lookup('foo/bar', headers=DEVPAY_HEADERS) |
| 108 assert isinstance(k, bucket.key_class) |
| 109 assert k.content_type == phony_mimetype |
| 110 k = bucket.lookup('notthere', headers=DEVPAY_HEADERS) |
| 111 assert k == None |
| 112 # try some metadata stuff |
| 113 k = bucket.new_key() |
| 114 k.name = 'has_metadata' |
| 115 mdkey1 = 'meta1' |
| 116 mdval1 = 'This is the first metadata value' |
| 117 k.set_metadata(mdkey1, mdval1) |
| 118 mdkey2 = 'meta2' |
| 119 mdval2 = 'This is the second metadata value' |
| 120 k.set_metadata(mdkey2, mdval2) |
| 121 k.set_contents_from_string(s1, headers=DEVPAY_HEADERS) |
| 122 k = bucket.lookup('has_metadata', headers=DEVPAY_HEADERS) |
| 123 assert k.get_metadata(mdkey1) == mdval1 |
| 124 assert k.get_metadata(mdkey2) == mdval2 |
| 125 k = bucket.new_key() |
| 126 k.name = 'has_metadata' |
| 127 k.get_contents_as_string(headers=DEVPAY_HEADERS) |
| 128 assert k.get_metadata(mdkey1) == mdval1 |
| 129 assert k.get_metadata(mdkey2) == mdval2 |
| 130 bucket.delete_key(k, headers=DEVPAY_HEADERS) |
| 131 # test list and iterator |
| 132 rs1 = bucket.list(headers=DEVPAY_HEADERS) |
| 133 num_iter = 0 |
| 134 for r in rs1: |
| 135 num_iter = num_iter + 1 |
| 136 rs = bucket.get_all_keys(headers=DEVPAY_HEADERS) |
| 137 num_keys = len(rs) |
| 138 assert num_iter == num_keys |
| 139 # try a key with a funny character |
| 140 k = bucket.new_key() |
| 141 k.name = 'testnewline\n' |
| 142 k.set_contents_from_string('This is a test', headers=DEVPAY_HEADERS) |
| 143 rs = bucket.get_all_keys(headers=DEVPAY_HEADERS) |
| 144 assert len(rs) == num_keys + 1 |
| 145 bucket.delete_key(k, headers=DEVPAY_HEADERS) |
| 146 rs = bucket.get_all_keys(headers=DEVPAY_HEADERS) |
| 147 assert len(rs) == num_keys |
| 148 # try some acl stuff |
| 149 bucket.set_acl('public-read', headers=DEVPAY_HEADERS) |
| 150 policy = bucket.get_acl(headers=DEVPAY_HEADERS) |
| 151 assert len(policy.acl.grants) == 2 |
| 152 bucket.set_acl('private', headers=DEVPAY_HEADERS) |
| 153 policy = bucket.get_acl(headers=DEVPAY_HEADERS) |
| 154 assert len(policy.acl.grants) == 1 |
| 155 k = bucket.lookup('foo/bar', headers=DEVPAY_HEADERS) |
| 156 k.set_acl('public-read', headers=DEVPAY_HEADERS) |
| 157 policy = k.get_acl(headers=DEVPAY_HEADERS) |
| 158 assert len(policy.acl.grants) == 2 |
| 159 k.set_acl('private', headers=DEVPAY_HEADERS) |
| 160 policy = k.get_acl(headers=DEVPAY_HEADERS) |
| 161 assert len(policy.acl.grants) == 1 |
| 162 # try the convenience methods for grants |
| 163 # this doesn't work with devpay |
| 164 #bucket.add_user_grant('FULL_CONTROL', |
| 165 # 'c1e724fbfa0979a4448393c59a8c055011f739b6d102fb37a65f
26414653cd67', |
| 166 # headers=DEVPAY_HEADERS) |
| 167 try: |
| 168 bucket.add_email_grant('foobar', 'foo@bar.com', headers=DEVPAY_HEADERS) |
| 169 except S3PermissionsError: |
| 170 pass |
| 171 # now delete all keys in bucket |
| 172 for k in all: |
| 173 bucket.delete_key(k, headers=DEVPAY_HEADERS) |
| 174 # now delete bucket |
| 175 |
| 176 c.delete_bucket(bucket, headers=DEVPAY_HEADERS) |
| 177 |
| 178 print '--- tests completed ---' |
| 179 |
| 180 if __name__ == '__main__': |
| 181 test() |
OLD | NEW |