OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # Copyright (c) 2006-2011 Mitch Garnaat http://garnaat.org/ |
| 3 # Copyright (c) 2010, Eucalyptus Systems, Inc. |
| 4 # Copyright (c) 2011, Nexenta Systems, Inc. |
| 5 # Copyright (c) 2012, Google, Inc. |
| 6 # All rights reserved. |
| 7 # |
| 8 # Permission is hereby granted, free of charge, to any person obtaining a |
| 9 # copy of this software and associated documentation files (the |
| 10 # "Software"), to deal in the Software without restriction, including |
| 11 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 12 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 13 # persons to whom the Software is furnished to do so, subject to the fol- |
| 14 # lowing conditions: |
| 15 # |
| 16 # The above copyright notice and this permission notice shall be included |
| 17 # in all copies or substantial portions of the Software. |
| 18 # |
| 19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 20 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 21 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 22 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 25 # IN THE SOFTWARE. |
| 26 |
| 27 """ |
| 28 Some unit tests for the GSConnection |
| 29 """ |
| 30 |
| 31 import os |
| 32 import re |
| 33 import StringIO |
| 34 import xml.sax |
| 35 |
| 36 from boto import handler |
| 37 from boto import storage_uri |
| 38 from boto.gs.acl import ACL |
| 39 from boto.gs.cors import Cors |
| 40 from tests.integration.gs.testcase import GSTestCase |
| 41 |
| 42 |
| 43 CORS_EMPTY = '<CorsConfig></CorsConfig>' |
| 44 CORS_DOC = ('<CorsConfig><Cors><Origins><Origin>origin1.example.com' |
| 45 '</Origin><Origin>origin2.example.com</Origin></Origins>' |
| 46 '<Methods><Method>GET</Method><Method>PUT</Method>' |
| 47 '<Method>POST</Method></Methods><ResponseHeaders>' |
| 48 '<ResponseHeader>foo</ResponseHeader>' |
| 49 '<ResponseHeader>bar</ResponseHeader></ResponseHeaders>' |
| 50 '</Cors></CorsConfig>') |
| 51 |
| 52 # Regexp for matching project-private default object ACL. |
| 53 PROJECT_PRIVATE_RE = ('\s*<AccessControlList>\s*<Entries>\s*<Entry>' |
| 54 '\s*<Scope type="GroupById"><ID>[0-9a-fA-F]+</ID></Scope>' |
| 55 '\s*<Permission>FULL_CONTROL</Permission>\s*</Entry>\s*<Entry>' |
| 56 '\s*<Scope type="GroupById"><ID>[0-9a-fA-F]+</ID></Scope>' |
| 57 '\s*<Permission>FULL_CONTROL</Permission>\s*</Entry>\s*<Entry>' |
| 58 '\s*<Scope type="GroupById"><ID>[0-9a-fA-F]+</ID></Scope>' |
| 59 '\s*<Permission>READ</Permission></Entry>\s*</Entries>' |
| 60 '\s*</AccessControlList>\s*') |
| 61 |
| 62 |
| 63 class GSBasicTest(GSTestCase): |
| 64 """Tests some basic GCS functionality.""" |
| 65 |
| 66 def test_read_write(self): |
| 67 """Tests basic read/write to keys.""" |
| 68 bucket = self._MakeBucket() |
| 69 bucket_name = bucket.name |
| 70 # now try a get_bucket call and see if it's really there |
| 71 bucket = self._GetConnection().get_bucket(bucket_name) |
| 72 key_name = 'foobar' |
| 73 k = bucket.new_key(key_name) |
| 74 s1 = 'This is a test of file upload and download' |
| 75 k.set_contents_from_string(s1) |
| 76 tmpdir = self._MakeTempDir() |
| 77 fpath = os.path.join(tmpdir, key_name) |
| 78 fp = open(fpath, 'wb') |
| 79 # now get the contents from gcs to a local file |
| 80 k.get_contents_to_file(fp) |
| 81 fp.close() |
| 82 fp = open(fpath) |
| 83 # check to make sure content read from gcs is identical to original |
| 84 self.assertEqual(s1, fp.read()) |
| 85 fp.close() |
| 86 # check to make sure set_contents_from_file is working |
| 87 sfp = StringIO.StringIO('foo') |
| 88 k.set_contents_from_file(sfp) |
| 89 self.assertEqual(k.get_contents_as_string(), 'foo') |
| 90 sfp2 = StringIO.StringIO('foo2') |
| 91 k.set_contents_from_file(sfp2) |
| 92 self.assertEqual(k.get_contents_as_string(), 'foo2') |
| 93 |
| 94 def test_get_all_keys(self): |
| 95 """Tests get_all_keys.""" |
| 96 phony_mimetype = 'application/x-boto-test' |
| 97 headers = {'Content-Type': phony_mimetype} |
| 98 tmpdir = self._MakeTempDir() |
| 99 fpath = os.path.join(tmpdir, 'foobar1') |
| 100 fpath2 = os.path.join(tmpdir, 'foobar') |
| 101 with open(fpath2, 'w') as f: |
| 102 f.write('test-data') |
| 103 bucket = self._MakeBucket() |
| 104 |
| 105 # First load some data for the first one, overriding content type. |
| 106 k = bucket.new_key('foobar') |
| 107 s1 = 'test-contents' |
| 108 s2 = 'test-contents2' |
| 109 k.name = 'foo/bar' |
| 110 k.set_contents_from_string(s1, headers) |
| 111 k.name = 'foo/bas' |
| 112 k.set_contents_from_filename(fpath2) |
| 113 k.name = 'foo/bat' |
| 114 k.set_contents_from_string(s1) |
| 115 k.name = 'fie/bar' |
| 116 k.set_contents_from_string(s1) |
| 117 k.name = 'fie/bas' |
| 118 k.set_contents_from_string(s1) |
| 119 k.name = 'fie/bat' |
| 120 k.set_contents_from_string(s1) |
| 121 # try resetting the contents to another value |
| 122 md5 = k.md5 |
| 123 k.set_contents_from_string(s2) |
| 124 self.assertNotEqual(k.md5, md5) |
| 125 |
| 126 fp2 = open(fpath2, 'rb') |
| 127 k.md5 = None |
| 128 k.base64md5 = None |
| 129 k.set_contents_from_stream(fp2) |
| 130 fp = open(fpath, 'wb') |
| 131 k.get_contents_to_file(fp) |
| 132 fp.close() |
| 133 fp2.seek(0, 0) |
| 134 fp = open(fpath, 'rb') |
| 135 self.assertEqual(fp2.read(), fp.read()) |
| 136 fp.close() |
| 137 fp2.close() |
| 138 all = bucket.get_all_keys() |
| 139 self.assertEqual(len(all), 6) |
| 140 rs = bucket.get_all_keys(prefix='foo') |
| 141 self.assertEqual(len(rs), 3) |
| 142 rs = bucket.get_all_keys(prefix='', delimiter='/') |
| 143 self.assertEqual(len(rs), 2) |
| 144 rs = bucket.get_all_keys(maxkeys=5) |
| 145 self.assertEqual(len(rs), 5) |
| 146 |
| 147 def test_bucket_lookup(self): |
| 148 """Test the bucket lookup method.""" |
| 149 bucket = self._MakeBucket() |
| 150 k = bucket.new_key('foo/bar') |
| 151 phony_mimetype = 'application/x-boto-test' |
| 152 headers = {'Content-Type': phony_mimetype} |
| 153 k.set_contents_from_string('testdata', headers) |
| 154 |
| 155 k = bucket.lookup('foo/bar') |
| 156 self.assertIsInstance(k, bucket.key_class) |
| 157 self.assertEqual(k.content_type, phony_mimetype) |
| 158 k = bucket.lookup('notthere') |
| 159 self.assertIsNone(k) |
| 160 |
| 161 def test_metadata(self): |
| 162 """Test key metadata operations.""" |
| 163 bucket = self._MakeBucket() |
| 164 k = self._MakeKey(bucket=bucket) |
| 165 key_name = k.name |
| 166 s1 = 'This is a test of file upload and download' |
| 167 |
| 168 mdkey1 = 'meta1' |
| 169 mdval1 = 'This is the first metadata value' |
| 170 k.set_metadata(mdkey1, mdval1) |
| 171 mdkey2 = 'meta2' |
| 172 mdval2 = 'This is the second metadata value' |
| 173 k.set_metadata(mdkey2, mdval2) |
| 174 |
| 175 # Test unicode character. |
| 176 mdval3 = u'föö' |
| 177 mdkey3 = 'meta3' |
| 178 k.set_metadata(mdkey3, mdval3) |
| 179 k.set_contents_from_string(s1) |
| 180 |
| 181 k = bucket.lookup(key_name) |
| 182 self.assertEqual(k.get_metadata(mdkey1), mdval1) |
| 183 self.assertEqual(k.get_metadata(mdkey2), mdval2) |
| 184 self.assertEqual(k.get_metadata(mdkey3), mdval3) |
| 185 k = bucket.new_key(key_name) |
| 186 k.get_contents_as_string() |
| 187 self.assertEqual(k.get_metadata(mdkey1), mdval1) |
| 188 self.assertEqual(k.get_metadata(mdkey2), mdval2) |
| 189 self.assertEqual(k.get_metadata(mdkey3), mdval3) |
| 190 |
| 191 def test_list_iterator(self): |
| 192 """Test list and iterator.""" |
| 193 bucket = self._MakeBucket() |
| 194 num_iter = len([k for k in bucket.list()]) |
| 195 rs = bucket.get_all_keys() |
| 196 num_keys = len(rs) |
| 197 self.assertEqual(num_iter, num_keys) |
| 198 |
| 199 def test_acl(self): |
| 200 """Test bucket and key ACLs.""" |
| 201 bucket = self._MakeBucket() |
| 202 |
| 203 # try some acl stuff |
| 204 bucket.set_acl('public-read') |
| 205 acl = bucket.get_acl() |
| 206 self.assertEqual(len(acl.entries.entry_list), 2) |
| 207 bucket.set_acl('private') |
| 208 acl = bucket.get_acl() |
| 209 self.assertEqual(len(acl.entries.entry_list), 1) |
| 210 k = self._MakeKey(bucket=bucket) |
| 211 k.set_acl('public-read') |
| 212 acl = k.get_acl() |
| 213 self.assertEqual(len(acl.entries.entry_list), 2) |
| 214 k.set_acl('private') |
| 215 acl = k.get_acl() |
| 216 self.assertEqual(len(acl.entries.entry_list), 1) |
| 217 |
| 218 # Test case-insensitivity of XML ACL parsing. |
| 219 acl_xml = ( |
| 220 '<ACCESSControlList><EntrIes><Entry>' + |
| 221 '<Scope type="AllUsers"></Scope><Permission>READ</Permission>' + |
| 222 '</Entry></EntrIes></ACCESSControlList>') |
| 223 acl = ACL() |
| 224 h = handler.XmlHandler(acl, bucket) |
| 225 xml.sax.parseString(acl_xml, h) |
| 226 bucket.set_acl(acl) |
| 227 self.assertEqual(len(acl.entries.entry_list), 1) |
| 228 aclstr = k.get_xml_acl() |
| 229 self.assertGreater(aclstr.count('/Entry', 1), 0) |
| 230 |
| 231 def test_logging(self): |
| 232 """Test set/get raw logging subresource.""" |
| 233 bucket = self._MakeBucket() |
| 234 empty_logging_str="<?xml version='1.0' encoding='UTF-8'?><Logging/>" |
| 235 logging_str = ( |
| 236 "<?xml version='1.0' encoding='UTF-8'?><Logging>" |
| 237 "<LogBucket>log-bucket</LogBucket>" + |
| 238 "<LogObjectPrefix>example</LogObjectPrefix>" + |
| 239 "</Logging>") |
| 240 bucket.set_subresource('logging', logging_str) |
| 241 self.assertEqual(bucket.get_subresource('logging'), logging_str) |
| 242 # try disable/enable logging |
| 243 bucket.disable_logging() |
| 244 self.assertEqual(bucket.get_subresource('logging'), empty_logging_str) |
| 245 bucket.enable_logging('log-bucket', 'example') |
| 246 self.assertEqual(bucket.get_subresource('logging'), logging_str) |
| 247 |
| 248 def test_copy_key(self): |
| 249 """Test copying a key from one bucket to another.""" |
| 250 # create two new, empty buckets |
| 251 bucket1 = self._MakeBucket() |
| 252 bucket2 = self._MakeBucket() |
| 253 bucket_name_1 = bucket1.name |
| 254 bucket_name_2 = bucket2.name |
| 255 # verify buckets got created |
| 256 bucket1 = self._GetConnection().get_bucket(bucket_name_1) |
| 257 bucket2 = self._GetConnection().get_bucket(bucket_name_2) |
| 258 # create a key in bucket1 and give it some content |
| 259 key_name = 'foobar' |
| 260 k1 = bucket1.new_key(key_name) |
| 261 self.assertIsInstance(k1, bucket1.key_class) |
| 262 k1.name = key_name |
| 263 s = 'This is a test.' |
| 264 k1.set_contents_from_string(s) |
| 265 # copy the new key from bucket1 to bucket2 |
| 266 k1.copy(bucket_name_2, key_name) |
| 267 # now copy the contents from bucket2 to a local file |
| 268 k2 = bucket2.lookup(key_name) |
| 269 self.assertIsInstance(k2, bucket2.key_class) |
| 270 tmpdir = self._MakeTempDir() |
| 271 fpath = os.path.join(tmpdir, 'foobar') |
| 272 fp = open(fpath, 'wb') |
| 273 k2.get_contents_to_file(fp) |
| 274 fp.close() |
| 275 fp = open(fpath) |
| 276 # check to make sure content read is identical to original |
| 277 self.assertEqual(s, fp.read()) |
| 278 fp.close() |
| 279 # delete keys |
| 280 bucket1.delete_key(k1) |
| 281 bucket2.delete_key(k2) |
| 282 |
| 283 def test_default_object_acls(self): |
| 284 """Test default object acls.""" |
| 285 # create a new bucket |
| 286 bucket = self._MakeBucket() |
| 287 # get default acl and make sure it's project-private |
| 288 acl = bucket.get_def_acl() |
| 289 self.assertIsNotNone(re.search(PROJECT_PRIVATE_RE, acl.to_xml())) |
| 290 # set default acl to a canned acl and verify it gets set |
| 291 bucket.set_def_acl('public-read') |
| 292 acl = bucket.get_def_acl() |
| 293 # save public-read acl for later test |
| 294 public_read_acl = acl |
| 295 self.assertEqual(acl.to_xml(), ('<AccessControlList><Entries><Entry>' |
| 296 '<Scope type="AllUsers"></Scope><Permission>READ</Permission>' |
| 297 '</Entry></Entries></AccessControlList>')) |
| 298 # back to private acl |
| 299 bucket.set_def_acl('private') |
| 300 acl = bucket.get_def_acl() |
| 301 self.assertEqual(acl.to_xml(), |
| 302 '<AccessControlList></AccessControlList>') |
| 303 # set default acl to an xml acl and verify it gets set |
| 304 bucket.set_def_acl(public_read_acl) |
| 305 acl = bucket.get_def_acl() |
| 306 self.assertEqual(acl.to_xml(), ('<AccessControlList><Entries><Entry>' |
| 307 '<Scope type="AllUsers"></Scope><Permission>READ</Permission>' |
| 308 '</Entry></Entries></AccessControlList>')) |
| 309 # back to private acl |
| 310 bucket.set_def_acl('private') |
| 311 acl = bucket.get_def_acl() |
| 312 self.assertEqual(acl.to_xml(), |
| 313 '<AccessControlList></AccessControlList>') |
| 314 |
| 315 def test_default_object_acls_storage_uri(self): |
| 316 """Test default object acls using storage_uri.""" |
| 317 # create a new bucket |
| 318 bucket = self._MakeBucket() |
| 319 bucket_name = bucket.name |
| 320 uri = storage_uri('gs://' + bucket_name) |
| 321 # get default acl and make sure it's project-private |
| 322 acl = uri.get_def_acl() |
| 323 self.assertIsNotNone(re.search(PROJECT_PRIVATE_RE, acl.to_xml())) |
| 324 # set default acl to a canned acl and verify it gets set |
| 325 uri.set_def_acl('public-read') |
| 326 acl = uri.get_def_acl() |
| 327 # save public-read acl for later test |
| 328 public_read_acl = acl |
| 329 self.assertEqual(acl.to_xml(), ('<AccessControlList><Entries><Entry>' |
| 330 '<Scope type="AllUsers"></Scope><Permission>READ</Permission>' |
| 331 '</Entry></Entries></AccessControlList>')) |
| 332 # back to private acl |
| 333 uri.set_def_acl('private') |
| 334 acl = uri.get_def_acl() |
| 335 self.assertEqual(acl.to_xml(), |
| 336 '<AccessControlList></AccessControlList>') |
| 337 # set default acl to an xml acl and verify it gets set |
| 338 uri.set_def_acl(public_read_acl) |
| 339 acl = uri.get_def_acl() |
| 340 self.assertEqual(acl.to_xml(), ('<AccessControlList><Entries><Entry>' |
| 341 '<Scope type="AllUsers"></Scope><Permission>READ</Permission>' |
| 342 '</Entry></Entries></AccessControlList>')) |
| 343 # back to private acl |
| 344 uri.set_def_acl('private') |
| 345 acl = uri.get_def_acl() |
| 346 self.assertEqual(acl.to_xml(), |
| 347 '<AccessControlList></AccessControlList>') |
| 348 |
| 349 def test_cors_xml_bucket(self): |
| 350 """Test setting and getting of CORS XML documents on Bucket.""" |
| 351 # create a new bucket |
| 352 bucket = self._MakeBucket() |
| 353 bucket_name = bucket.name |
| 354 # now call get_bucket to see if it's really there |
| 355 bucket = self._GetConnection().get_bucket(bucket_name) |
| 356 # get new bucket cors and make sure it's empty |
| 357 cors = re.sub(r'\s', '', bucket.get_cors().to_xml()) |
| 358 self.assertEqual(cors, CORS_EMPTY) |
| 359 # set cors document on new bucket |
| 360 bucket.set_cors(CORS_DOC) |
| 361 cors = re.sub(r'\s', '', bucket.get_cors().to_xml()) |
| 362 self.assertEqual(cors, CORS_DOC) |
| 363 |
| 364 def test_cors_xml_storage_uri(self): |
| 365 """Test setting and getting of CORS XML documents with storage_uri.""" |
| 366 # create a new bucket |
| 367 bucket = self._MakeBucket() |
| 368 bucket_name = bucket.name |
| 369 uri = storage_uri('gs://' + bucket_name) |
| 370 # get new bucket cors and make sure it's empty |
| 371 cors = re.sub(r'\s', '', uri.get_cors().to_xml()) |
| 372 self.assertEqual(cors, CORS_EMPTY) |
| 373 # set cors document on new bucket |
| 374 cors_obj = Cors() |
| 375 h = handler.XmlHandler(cors_obj, None) |
| 376 xml.sax.parseString(CORS_DOC, h) |
| 377 uri.set_cors(cors_obj) |
| 378 cors = re.sub(r'\s', '', uri.get_cors().to_xml()) |
| 379 self.assertEqual(cors, CORS_DOC) |
OLD | NEW |