def test_put_object_copy_error(self): obj = 'object' self.conn.make_request('PUT', self.bucket, obj) dst_bucket = 'dst-bucket' self.conn.make_request('PUT', dst_bucket) dst_obj = 'dst_object' headers = {'x-oss-copy-source': '/%s/%s' % (self.bucket, obj)} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('PUT', dst_bucket, dst_obj, headers) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') self.assertEqual(headers['Content-Type'], 'application/xml') # /src/nothing -> /dst/dst headers = {'X-Oss-Copy-Source': '/%s/%s' % (self.bucket, 'nothing')} status, headers, body = \ self.conn.make_request('PUT', dst_bucket, dst_obj, headers) self.assertEqual(get_error_code(body), 'NoSuchKey') self.assertEqual(headers['Content-Type'], 'application/xml') # /nothing/src -> /dst/dst headers = {'X-Oss-Copy-Source': '/%s/%s' % ('nothing', obj)} status, headers, body = \ self.conn.make_request('PUT', dst_bucket, dst_obj, headers) # TODO: source bucket is not check. # self.assertEqual(get_error_code(body), 'NoSuchBucket') # /src/src -> /nothing/dst headers = {'X-Oss-Copy-Source': '/%s/%s' % (self.bucket, obj)} status, headers, body = \ self.conn.make_request('PUT', 'nothing', dst_obj, headers) self.assertEqual(get_error_code(body), 'NoSuchBucket') self.assertEqual(headers['Content-Type'], 'application/xml')
def test_upload_part_error(self): bucket = 'bucket' self.conn.make_request('PUT', bucket) query = {'uploads': ''} key = 'obj' status, headers, body = \ self.conn.make_request('POST', bucket, key, query=query) elem = fromstring(body, 'InitiateMultipartUploadResult') upload_id = elem.find('UploadId').text query = {'partNumber': '1', 'uploadId': upload_id} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('PUT', bucket, key, query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('PUT', 'nothing', key, query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket') query = {'partNumber': '1', 'uploadId': 'nothing'} status, headers, body = \ self.conn.make_request('PUT', bucket, key, query=query) self.assertEqual(get_error_code(body), 'NoSuchUpload') query = {'partNumber': '0', 'uploadId': upload_id} status, headers, body = \ self.conn.make_request('PUT', bucket, key, query=query) self.assertEqual(get_error_code(body), 'InvalidArgument') err_msg = 'Part number must be an integer between 1 and' self.assertTrue(err_msg in get_error_msg(body))
def test_abort_multi_upload_error(self): bucket = 'bucket' self.conn.make_request('PUT', bucket) obj = 'obj' query = {'uploads': ''} status, headers, body = \ self.conn.make_request('POST', bucket, obj, query=query) elem = fromstring(body, 'InitiateMultipartUploadResult') upload_id = elem.find('UploadId').text self._upload_part(bucket, obj, upload_id) query = {'uploadId': upload_id} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('DELETE', bucket, obj, query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('DELETE', 'nothing', obj, query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket') query = {'uploadId': 'nothing'} status, headers, body = \ self.conn.make_request('DELETE', bucket, obj, query=query) self.assertEqual(get_error_code(body), 'NoSuchUpload')
class Oss2swiftFunctionalTestCase(unittest.TestCase): def __init__(self, method_name): super(Oss2swiftFunctionalTestCase, self).__init__(method_name) self.method_name = method_name def setUp(self): try: self.conn = Connection() self.conn.reset() except Exception: message = '%s got an error during initialize process.\n\n%s' % \ (self.method_name, traceback.format_exc()) # TODO: Find a way to make this go to FAIL instead of Error self.fail(message) def assertCommonResponseHeaders(self, headers, etag=None): """ asserting common response headers with args :param headers: a dict of response headers :param etag: a string of md5(content).hexdigest() if not given, this won't assert anything about etag. (e.g. DELETE obj) """ self.assertTrue(headers['x-oss-id-2'] is not None) self.assertTrue(headers['x-oss-request-id'] is not None) self.assertTrue(headers['Date'] is not None) # TODO; requires consideration # self.assertTrue(headers['server'] is not None) if etag is not None: self.assertTrue('ETag' in headers) # sanity self.assertEqual(etag, headers['ETag'].strip('"'))
def setUp(self): try: self.conn = Connection() self.conn.reset() except Exception: message = '%s got an error during initialize process.\n\n%s' % \ (self.method_name, traceback.format_exc()) # TODO: Find a way to make this go to FAIL instead of Error self.fail(message)
def test_put_object_error(self): auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('PUT', self.bucket, 'object') self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') self.assertEqual(headers['Content-Type'], 'application/xml') status, headers, body = \ self.conn.make_request('PUT', 'bucket2', 'object') self.assertEqual(get_error_code(body), 'NoSuchBucket') self.assertEqual(headers['Content-Type'], 'application/xml')
def test_delete_bucket_error(self): status, headers, body = \ self.conn.make_request('DELETE', 'bucket+invalid') self.assertEqual(get_error_code(body), 'InvalidBucketName') auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('DELETE', 'bucket') self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = self.conn.make_request('DELETE', 'bucket') self.assertEqual(get_error_code(body), 'NoSuchBucket')
def test_get_bucket_acl_error(self): oss_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ oss_error_conn.make_request('GET', self.bucket, query={'acl':''}) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('GET', 'nothing', query={'acl':''}) self.assertEqual(get_error_code(body), 'NoSuchBucket') status, headers, body = \ self.conn2.make_request('GET', self.bucket,query={'acl':''}) self.assertEqual(get_error_code(body), 'AccessDenied')
def test_put_bucket_error(self): status, headers, body = \ self.conn.make_request('PUT', 'bucket+invalid') self.assertEqual(get_error_code(body), 'InvalidBucketName') auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = auth_error_conn.make_request('PUT', 'bucket') self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') self.conn.make_request('PUT', 'bucket') status, headers, body = self.conn.make_request('PUT', 'bucket') self.assertEqual(get_error_code(body), 'BucketAlreadyExists')
def test_list_multi_uploads_error(self): bucket = 'bucket' self.conn.make_request('PUT', bucket) query = {'uploads': ''} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('GET', bucket,query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('GET', 'nothing', query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket')
def test_put_bucket_acl_error(self): req_headers = {'x-oss-acl': 'public-read'} oss_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ oss_error_conn.make_request('PUT', self.bucket, headers=req_headers, query={'acl':''}) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('PUT', 'nothing', headers=req_headers, query={'acl':''}) self.assertEqual(get_error_code(body), 'NoSuchBucket') status, headers, body = \ self.conn2.make_request('PUT', self.bucket, headers=req_headers, query={'acl':''}) self.assertEqual(get_error_code(body), 'MethodNotAllowed')
def test_upload_part_copy_error(self): src_bucket = 'src' src_obj = 'src' self.conn.make_request('PUT', src_bucket) self.conn.make_request('PUT', src_bucket, src_obj) src_path = '%s/%s' % (src_bucket, src_obj) bucket = 'bucket' self.conn.make_request('PUT', bucket) key = 'obj' query = {'uploads': ''} status, headers, body = \ self.conn.make_request('POST', bucket, key, query=query) elem = fromstring(body, 'InitiateMultipartUploadResult') upload_id = elem.find('UploadId').text query = {'partNumber': '1', 'uploadId': upload_id} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('PUT', bucket, key, headers={ 'X-Oss-Copy-Source': src_path }, query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('PUT', 'nothing', key, headers={'X-Oss-Copy-Source': src_path}, query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket') query = {'partNumber': '1', 'uploadId': 'nothing'} status, headers, body = \ self.conn.make_request('PUT', bucket, key, headers={'X-Oss-Copy-Source': src_path}, query=query) self.assertEqual(get_error_code(body), 'NoSuchUpload') src_path = '%s/%s' % (src_bucket, 'nothing') query = {'partNumber': '1', 'uploadId': upload_id} status, headers, body = \ self.conn.make_request('PUT', bucket, key, headers={'X-Oss-Copy-Source': src_path}, query=query) self.assertEqual(get_error_code(body), 'NoSuchKey')
def test_head_bucket_error(self): self.conn.make_request('PUT', 'bucket') status, headers, body = \ self.conn.make_request('HEAD', 'bucket+invalid') self.assertEqual(status, 400) self.assertEqual(body, '') # sanity auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('HEAD', 'bucket') self.assertEqual(status, 403) self.assertEqual(body, '') # sanity status, headers, body = self.conn.make_request('HEAD', 'nothing') self.assertEqual(status, 404) self.assertEqual(body, '') # sanity
def test_get_object_error(self): obj = 'object' self.conn.make_request('PUT', self.bucket, obj) auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('GET', self.bucket, obj) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') self.assertEqual(headers['Content-Type'], 'application/xml') status, headers, body = \ self.conn.make_request('GET', self.bucket, 'invalid') self.assertEqual(get_error_code(body), 'NoSuchKey') self.assertEqual(headers['Content-Type'], 'application/xml') status, headers, body = self.conn.make_request('GET', 'invalid', obj) # TODO; requires consideration # self.assertEqual(get_error_code(body), 'NoSuchBucket') self.assertEqual(get_error_code(body), 'NoSuchKey') self.assertEqual(headers['Content-Type'], 'application/xml')
def test_head_object_error(self): obj = 'object' self.conn.make_request('PUT', self.bucket, obj) auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('HEAD', self.bucket, obj) self.assertEqual(status, 403) self.assertEqual(body, '') # sanity self.assertEqual(headers['Content-Type'], 'application/xml') status, headers, body = \ self.conn.make_request('HEAD', self.bucket, 'invalid') self.assertEqual(status, 404) self.assertEqual(body, '') # sanity self.assertEqual(headers['Content-Type'], 'application/xml') status, headers, body = \ self.conn.make_request('HEAD', 'invalid', obj) self.assertEqual(status, 404) self.assertEqual(body, '') # sanity self.assertEqual(headers['Content-Type'], 'application/xml')
def test_complete_multi_upload_error(self): bucket = 'bucket' keys = ['obj', 'obj2'] self.conn.make_request('PUT', bucket) query = {'uploads': ''} status, headers, body = \ self.conn.make_request('POST', bucket, keys[0], query=query) elem = fromstring(body, 'InitiateMultipartUploadResult') upload_id = elem.find('UploadId').text etags = [] for i in xrange(1, 3): query = {'partNumber': str(i), 'uploadId': upload_id} status, headers, body = \ self.conn.make_request('PUT', bucket, keys[0], query=query) etags.append(headers['ETag']) xml = self._gen_comp_xml(etags) # part 1 too small query = {'uploadId': upload_id} status, headers, body = \ self.conn.make_request('POST', bucket, keys[0], body=xml, query=query) self.assertEqual(get_error_code(body), 'EntityTooSmall') # invalid credentials auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('POST', bucket, keys[0], body=xml, query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') # wrong/missing bucket status, headers, body = \ self.conn.make_request('POST', 'nothing', keys[0], query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket') # wrong upload ID query = {'uploadId': 'nothing'} status, headers, body = \ self.conn.make_request('POST', bucket, keys[0], body=xml, query=query) self.assertEqual(get_error_code(body), 'NoSuchUpload') # without Part tag in xml query = {'uploadId': upload_id} xml = self._gen_comp_xml([]) status, headers, body = \ self.conn.make_request('POST', bucket, keys[0], body=xml, query=query) self.assertEqual(get_error_code(body), 'MalformedXML') # with invalid etag in xml invalid_etag = 'invalid' xml = self._gen_comp_xml([invalid_etag]) status, headers, body = \ self.conn.make_request('POST', bucket, keys[0], body=xml, query=query) self.assertEqual(get_error_code(body), 'InvalidPart') # without part in Swift query = {'uploads': ''} status, headers, body = \ self.conn.make_request('POST', bucket, keys[1], query=query) elem = fromstring(body, 'InitiateMultipartUploadResult') upload_id = elem.find('UploadId').text query = {'uploadId': upload_id} xml = self._gen_comp_xml([etags[0]]) status, headers, body = \ self.conn.make_request('POST', bucket, keys[1], body=xml, query=query) self.assertEqual(get_error_code(body), 'InvalidPart')
def test_delete_multi_objects_error(self): bucket = 'bucket' put_objects = ['obj'] self._prepare_test_delete_multi_objects(bucket, put_objects) xml = self._gen_multi_delete_xml(put_objects) content_md5 = calculate_md5(xml) query = {'delete': ''} auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = \ auth_error_conn.make_request('POST', bucket, body=xml, headers={ 'Content-MD5': content_md5 }, query=query) self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') status, headers, body = \ self.conn.make_request('POST', 'nothing', body=xml, headers={'Content-MD5': content_md5}, query=query) self.assertEqual(get_error_code(body), 'NoSuchBucket') # without Object tag xml = self._gen_invalid_multi_delete_xml() content_md5 = calculate_md5(xml) status, headers, body = \ self.conn.make_request('POST', bucket,body=xml, headers={'Content-MD5': content_md5}, query=query) self.assertEqual(get_error_code(body), 'MalformedXML') # without value of Key tag xml = self._gen_invalid_multi_delete_xml(hasObjectTag=True) content_md5 = calculate_md5(xml) status, headers, body = \ self.conn.make_request('POST', bucket, body=xml, headers={'Content-MD5': content_md5}, query=query) self.assertEqual(get_error_code(body), 'UserKeyMustBeSpecified') # specified number of objects are over CONF.max_multi_delete_objects # (Default 1000), but xml size is smaller than 61365 bytes. req_objects = ['obj%s' for var in xrange(1001)] xml = self._gen_multi_delete_xml(req_objects) self.assertTrue(len(xml.encode('utf-8')) <= MAX_MULTI_DELETE_BODY_SIZE) content_md5 = calculate_md5(xml) status, headers, body = \ self.conn.make_request('POST', bucket,body=xml, headers={'Content-MD5': content_md5}, query=query) self.assertEqual(get_error_code(body), 'MalformedXML') # specified xml size is over 61365 bytes, but number of objects are # smaller than CONF.max_multi_delete_objects. obj = 'a' * 1024 req_objects = [obj + str(var) for var in xrange(999)] xml = self._gen_multi_delete_xml(req_objects) self.assertTrue(len(xml.encode('utf-8')) > MAX_MULTI_DELETE_BODY_SIZE) content_md5 = calculate_md5(xml) status, headers, body = \ self.conn.make_request('POST', bucket,body=xml, headers={'Content-MD5': content_md5}, query=query) self.assertEqual(get_error_code(body), 'MalformedXML')
def test_service_error_signature_not_match(self): auth_error_conn = Connection(oss_secret_key='invalid') status, headers, body = auth_error_conn.make_request('GET') self.assertEqual(get_error_code(body), 'SignatureDoesNotMatch') self.assertEqual(headers['Content-Type'], 'application/xml')