def test_bucket_cors(): buckets, zone_bucket = create_bucket_per_zone_in_realm() for _, bucket in zone_bucket: cors_cfg = CORSConfiguration() cors_cfg.add_rule(['DELETE'], 'https://www.example.com', allowed_header='*', max_age_seconds=3000) bucket.set_cors(cors_cfg) assert(bucket.get_cors().to_xml() == cors_cfg.to_xml())
def test_two_rules(self): cfg = CORSConfiguration() cfg.add_rule(['PUT', 'POST', 'DELETE'], 'http://www.example.com', allowed_header='*', max_age_seconds=3000, expose_header='x-amz-server-side-encryption') cfg.add_rule('GET', '*', allowed_header='*', max_age_seconds=3000) self.assertEqual(cfg.to_xml(), CORS_BODY_2)
def test_one_rule_with_id(self): cfg = CORSConfiguration() cfg.add_rule(['PUT', 'POST', 'DELETE'], 'http://www.example.com', allowed_header='*', max_age_seconds=3000, expose_header='x-amz-server-side-encryption', id='foobar_rule') self.assertEqual(cfg.to_xml(), CORS_BODY_1)
def test_cors_config_mgmt(self): ''' Method: Tests setting, getting, and deleting the CORS config on a bucket ''' test_bucket=self.bucket_prefix + "-simple-test-bucket" self.buckets_used.add(test_bucket) self.tester.debug("Starting CORS config management tests, using bucket name: " + test_bucket) try : bucket = self.tester.s3.create_bucket(test_bucket) if bucket == None: self.tester.s3.delete_bucket(test_bucket) self.fail(test_bucket + " was not created correctly") except (S3ResponseError, S3CreateError) as e: self.fail(test_bucket + " create caused exception: " + str(e)) # Get the CORS config (none yet). # Should get 404 Not Found, with "NoSuchCORSConfiguration" in the body. try : self.tester.debug("Getting (empty) CORS config") bucket.get_cors() #self.tester.s3.delete_bucket(test_bucket) #LPT self.fail("Did not get an S3ResponseError getting CORS config when none exists yet.") except S3ResponseError as e: if (e.status == 404 and e.reason == "Not Found" and e.code == "NoSuchCORSConfiguration"): self.tester.debug("Caught S3ResponseError with expected contents, " + "getting CORS config when none exists yet.") else: self.tester.s3.delete_bucket(test_bucket) self.fail("Caught S3ResponseError getting CORS config when none exists yet," + "but exception contents were unexpected: " + str(e)) # Set a simple CORS config. try : self.tester.debug("Setting a CORS config") bucket_cors_set = CORSConfiguration() bucket_rule_id = "ManuallyAssignedId1" bucket_allowed_methods = ['GET', 'PUT'] bucket_allowed_origins = ['*'] bucket_allowed_headers = ['*'] bucket_max_age_seconds = 3000 #bucket_expose_headers = [] bucket_cors_set.add_rule(bucket_allowed_methods, bucket_allowed_origins, bucket_rule_id, bucket_allowed_headers, bucket_max_age_seconds) bucket.set_cors(bucket_cors_set) except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail("Caught S3ResponseError setting CORS config: " + str(e)) # Get the CORS config. Should get the config we just set. try : self.tester.debug("Getting the CORS config we just set") bucket_cors_retrieved = bucket.get_cors() assert (bucket_cors_retrieved.to_xml() == bucket_cors_set.to_xml()), 'Bucket CORS config: Expected ' + bucket_cors_set.to_xml() + ', Retrieved ' + bucket_cors_retrieved.to_xml() except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail("Caught S3ResponseError getting CORS config, after setting it successfully: " + str(e)) # Delete the CORS config. try : self.tester.debug("Deleting the CORS config") bucket.delete_cors() except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail("Caught S3ResponseError deleting CORS config, after setting and validating it successfully: " + str(e)) # Get the CORS config (none anymore). # Should get 404 Not Found, with "NoSuchCORSConfiguration" in the body. try : self.tester.debug("Getting (empty again) CORS config") bucket.get_cors() self.tester.s3.delete_bucket(test_bucket) self.fail("Did not get an S3ResponseError getting CORS config after being deleted.") except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) if (e.status == 404 and e.reason == "Not Found" and e.code == "NoSuchCORSConfiguration"): self.tester.debug("Caught S3ResponseError with expected contents, " + "getting CORS config after being deleted.") else: self.fail("Caught S3ResponseError getting CORS config after being deleted," + "but exception contents were unexpected: " + str(e))
def test_cors_config_mgmt(self): ''' Method: Tests setting, getting, and deleting the CORS config on a bucket ''' test_bucket = self.bucket_prefix + "-simple-test-bucket" self.buckets_used.add(test_bucket) self.tester.debug( "Starting CORS config management tests, using bucket name: " + test_bucket) try: bucket = self.tester.s3.create_bucket(test_bucket) if bucket == None: self.tester.s3.delete_bucket(test_bucket) self.fail(test_bucket + " was not created correctly") except (S3ResponseError, S3CreateError) as e: self.fail(test_bucket + " create caused exception: " + str(e)) # Get the CORS config (none yet). # Should get 404 Not Found, with "NoSuchCORSConfiguration" in the body. try: self.tester.debug("Getting (empty) CORS config") bucket.get_cors() #self.tester.s3.delete_bucket(test_bucket) #LPT self.fail("Did not get an S3ResponseError getting CORS config when none exists yet.") except S3ResponseError as e: if (e.status == 404 and e.reason == "Not Found" and e.code == "NoSuchCORSConfiguration"): self.tester.debug( "Caught S3ResponseError with expected contents, " + "getting CORS config when none exists yet.") else: self.tester.s3.delete_bucket(test_bucket) self.fail( "Caught S3ResponseError getting CORS config when none exists yet," + "but exception contents were unexpected: " + str(e)) # Set a simple CORS config. try: self.tester.debug("Setting a CORS config") bucket_cors_set = CORSConfiguration() bucket_rule_id = "ManuallyAssignedId1" bucket_allowed_methods = ['GET', 'PUT'] bucket_allowed_origins = ['*'] bucket_allowed_headers = ['*'] bucket_max_age_seconds = 3000 #bucket_expose_headers = [] bucket_cors_set.add_rule(bucket_allowed_methods, bucket_allowed_origins, bucket_rule_id, bucket_allowed_headers, bucket_max_age_seconds) bucket.set_cors(bucket_cors_set) except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail("Caught S3ResponseError setting CORS config: " + str(e)) # Get the CORS config. Should get the config we just set. try: self.tester.debug("Getting the CORS config we just set") bucket_cors_retrieved = bucket.get_cors() assert (bucket_cors_retrieved.to_xml() == bucket_cors_set.to_xml( )), 'Bucket CORS config: Expected ' + bucket_cors_set.to_xml( ) + ', Retrieved ' + bucket_cors_retrieved.to_xml() except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail( "Caught S3ResponseError getting CORS config, after setting it successfully: " + str(e)) # Delete the CORS config. try: self.tester.debug("Deleting the CORS config") bucket.delete_cors() except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) self.fail( "Caught S3ResponseError deleting CORS config, after setting and validating it successfully: " + str(e)) # Get the CORS config (none anymore). # Should get 404 Not Found, with "NoSuchCORSConfiguration" in the body. try: self.tester.debug("Getting (empty again) CORS config") bucket.get_cors() self.tester.s3.delete_bucket(test_bucket) self.fail( "Did not get an S3ResponseError getting CORS config after being deleted." ) except S3ResponseError as e: self.tester.s3.delete_bucket(test_bucket) if (e.status == 404 and e.reason == "Not Found" and e.code == "NoSuchCORSConfiguration"): self.tester.debug( "Caught S3ResponseError with expected contents, " + "getting CORS config after being deleted.") else: self.fail( "Caught S3ResponseError getting CORS config after being deleted," + "but exception contents were unexpected: " + str(e))
except S3ResponseError, e: if e.error_code == "NoSuchCORSConfiguration": current_cors_xml = None else: module.fail_json(msg=e.message) if cors_xml is not None: cors_rule_change = False if current_cors_xml is None: cors_rule_change = True # Create else: # Convert cors_xml to a Boto CorsConfiguration object for comparison cors_config = CORSConfiguration() h = XmlHandler(cors_config, bucket) xml.sax.parseString(cors_xml, h) if cors_config.to_xml() != current_cors_config.to_xml(): cors_rule_change = True # Update if cors_rule_change: try: bucket.set_cors_xml(cors_xml) changed = True current_cors_xml = bucket.get_cors().to_xml() except S3ResponseError, e: module.fail_json(msg=e.message) elif current_cors_xml is not None: try: bucket.delete_cors() changed = True current_cors_xml = None except S3ResponseError, e:
def test_minimal(self): cfg = CORSConfiguration() cfg.add_rule('GET', '*') self.assertEqual(cfg.to_xml(), CORS_BODY_3)