def test_download_request_bytes_range(): dr = CDownloadRequest(CPath('/foo'), MemoryByteSink()) dr.range(None, None) assert dr.get_http_headers() == {} dr.range(None, 100) assert dr.get_http_headers() == { 'Range': 'bytes=-100'} dr.range(10, 100) assert dr.get_http_headers() == { 'Range': 'bytes=10-109'} dr.range(100, None) assert dr.get_http_headers() == { 'Range': 'bytes=100-'}
def test_download_request_bytes_range(): dr = CDownloadRequest(CPath("/foo"), MemoryByteSink()) dr.range(None, None) assert dr.get_http_headers() == {} dr.range(None, 100) assert dr.get_http_headers() == {"Range": "bytes=-100"} dr.range(10, 100) assert dr.get_http_headers() == {"Range": "bytes=10-109"} dr.range(100, None) assert dr.get_http_headers() == {"Range": "bytes=100-"}
def test_file_operations(storage): # We'll use a temp folder for tests: temp_root_path = miscutils.generate_test_path() logger.info('Will use test folder: %s', temp_root_path) # Create a sub-folder: sub_path = temp_root_path.add('sub_folder') logger.info('Creating sub_folder: %s', sub_path) assert storage.create_folder(sub_path) is True # True because actually created assert storage.create_folder(sub_path) is False # False because not created # Check back: sub_folder = storage.get_file(sub_path) assert sub_folder.path == sub_path assert sub_folder.is_folder() assert not sub_folder.is_blob() if sub_folder.modification_time: # Not all providers have a modif time on folders miscutils.assert_datetime_is_almost_now(sub_folder.modification_time) # Upload 2 files into this sub-folder: fpath1 = sub_path.add('a_test_file1') content_file1 = b'This is binary cont€nt of test file 1...' logger.info('Uploading blob to: %s', fpath1) upload_request = CUploadRequest(fpath1, MemoryByteSource(content_file1)) storage.upload(upload_request) fpath2 = sub_path.add('a_test_file2') # Generate a quite big random data: content_file2 = bytearray(os.urandom(500000)) logger.info('Uploading blob to: %s', fpath2) upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) # Check uploaded blobs informations: # we check file2 first because has just been uploaded / for modif time check cblob = storage.get_file(fpath2) assert cblob.is_blob() assert not cblob.is_folder() assert cblob.length == len(content_file2) miscutils.assert_datetime_is_almost_now(cblob.modification_time) cblob = storage.get_file(fpath1) assert cblob.is_blob() assert not cblob.is_folder() assert cblob.length == len(content_file1) # Download data, and check: logger.info('Downloading back and checking file: %s', fpath1) mbs = MemoryByteSink() download_request = CDownloadRequest(fpath1, mbs) storage.download(download_request) assert mbs.get_bytes() == content_file1 logger.info('Downloading back and checking file ranges: %s', fpath1) download_request.range(5, None) # starting at offset 5 storage.download(download_request) assert mbs.get_bytes() == content_file1[5:] if storage.provider_name() != 'rapidshare': # rapidshare does not support such requests: download_request.range(None, 5) # last 5 bytes storage.download(download_request); assert mbs.get_bytes() == content_file1[-5:] download_request.range(2, 5); # 5 bytes at offset 2 storage.download(download_request); assert mbs.get_bytes() == content_file1[2:7] logger.info('Downloading back and checking file: %s', fpath2) download_request = CDownloadRequest(fpath2, mbs) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Check that if we upload again, blob is replaced: logger.info('Checking file overwrite: %s', fpath2) content_file2 = bytearray(os.urandom(300000)) # 300kB file upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Check that we can replace existing blob with empty content: logger.info('Checking file overwrite with empty file: %s', fpath2) content_file2 = b'' upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Create a sub_sub_folder: sub_sub_path = sub_path.add('a_sub_sub_folder') logger.info('Creating a_sub_sub_folder: %s', sub_sub_path) created = storage.create_folder(sub_sub_path) assert created logger.info('Check uploaded blobs and sub_sub_folder all appear in folder list') folder_content = storage.list_folder(sub_folder) logger.info('sub_folder contains files: %r', folder_content) # It happened once here that hubic did not list fpath1 'a_test_file1' in folder_content: # only 2 files were present ?! assert len(folder_content) == 3 assert fpath1 in folder_content assert folder_content[fpath1].is_blob() assert not folder_content[fpath1].is_folder() assert fpath2 in folder_content assert folder_content[fpath2].is_blob() assert not folder_content[fpath2].is_folder() assert sub_sub_path in folder_content assert not folder_content[sub_sub_path].is_blob() assert folder_content[sub_sub_path].is_folder() logger.info('Check that list of sub_sub folder is empty: %s', sub_sub_path) assert storage.list_folder(sub_sub_path) == {} logger.info('Check that listing content of a blob raises: %s', fpath1) try: storage.list_folder(fpath1) pytest.fail('Listing a blob should raise') except CInvalidFileTypeError as e: assert e.path == fpath1 assert e.expected_blob is False logger.info('Delete file1: %s', fpath1) assert storage.delete(fpath1) is True # We have deleted the file assert storage.delete(fpath1) is False # We have not deleted anything logger.info('Check file1 does not appear anymore in folder: %s', sub_folder) assert fpath1 not in storage.list_folder(sub_folder) tmp = storage.get_file(fpath1) assert tmp is None logger.info('Delete whole test folder: %s', temp_root_path) ret = storage.delete(temp_root_path) assert ret is True # We have deleted at least one file logger.info('Deleting again returns False') ret = storage.delete(temp_root_path) assert ret is False # We have not deleted anything logger.info('Listing a deleted folder returns None: %s', temp_root_path) assert storage.list_folder(temp_root_path) is None assert storage.get_file(temp_root_path) is None
def test_file_operations(storage): # We'll use a temp folder for tests: temp_root_path = miscutils.generate_test_path() logger.info('Will use test folder: %s', temp_root_path) # Create a sub-folder: sub_path = temp_root_path.add('sub_folder') logger.info('Creating sub_folder: %s', sub_path) assert storage.create_folder( sub_path) is True # True because actually created assert storage.create_folder( sub_path) is False # False because not created # Check back: sub_folder = storage.get_file(sub_path) assert sub_folder.path == sub_path assert sub_folder.is_folder() assert not sub_folder.is_blob() if sub_folder.modification_time: # Not all providers have a modif time on folders miscutils.assert_datetime_is_almost_now(sub_folder.modification_time) # Upload 2 files into this sub-folder: fpath1 = sub_path.add('a_test_file1') content_file1 = b'This is binary cont€nt of test file 1...' logger.info('Uploading blob to: %s', fpath1) upload_request = CUploadRequest(fpath1, MemoryByteSource(content_file1)) storage.upload(upload_request) fpath2 = sub_path.add('a_test_file2') # Generate a quite big random data: content_file2 = bytearray(os.urandom(500000)) logger.info('Uploading blob to: %s', fpath2) upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) # Check uploaded blobs informations: # we check file2 first because has just been uploaded / for modif time check cblob = storage.get_file(fpath2) assert cblob.is_blob() assert not cblob.is_folder() assert cblob.length == len(content_file2) miscutils.assert_datetime_is_almost_now(cblob.modification_time) cblob = storage.get_file(fpath1) assert cblob.is_blob() assert not cblob.is_folder() assert cblob.length == len(content_file1) # Download data, and check: logger.info('Downloading back and checking file: %s', fpath1) mbs = MemoryByteSink() download_request = CDownloadRequest(fpath1, mbs) storage.download(download_request) assert mbs.get_bytes() == content_file1 logger.info('Downloading back and checking file ranges: %s', fpath1) download_request.range(5, None) # starting at offset 5 storage.download(download_request) assert mbs.get_bytes() == content_file1[5:] if storage.provider_name() != 'rapidshare': # rapidshare does not support such requests: download_request.range(None, 5) # last 5 bytes storage.download(download_request) assert mbs.get_bytes() == content_file1[-5:] download_request.range(2, 5) # 5 bytes at offset 2 storage.download(download_request) assert mbs.get_bytes() == content_file1[2:7] logger.info('Downloading back and checking file: %s', fpath2) download_request = CDownloadRequest(fpath2, mbs) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Check that if we upload again, blob is replaced: logger.info('Checking file overwrite: %s', fpath2) content_file2 = bytearray(os.urandom(300000)) # 300kB file upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Check that we can replace existing blob with empty content: logger.info('Checking file overwrite with empty file: %s', fpath2) content_file2 = b'' upload_request = CUploadRequest(fpath2, MemoryByteSource(content_file2)) storage.upload(upload_request) storage.download(download_request) assert mbs.get_bytes() == content_file2 # Create a sub_sub_folder: sub_sub_path = sub_path.add('a_sub_sub_folder') logger.info('Creating a_sub_sub_folder: %s', sub_sub_path) created = storage.create_folder(sub_sub_path) assert created logger.info( 'Check uploaded blobs and sub_sub_folder all appear in folder list') folder_content = storage.list_folder(sub_folder) logger.info('sub_folder contains files: %r', folder_content) # It happened once here that hubic did not list fpath1 'a_test_file1' in folder_content: # only 2 files were present ?! assert len(folder_content) == 3 assert fpath1 in folder_content assert folder_content[fpath1].is_blob() assert not folder_content[fpath1].is_folder() assert fpath2 in folder_content assert folder_content[fpath2].is_blob() assert not folder_content[fpath2].is_folder() assert sub_sub_path in folder_content assert not folder_content[sub_sub_path].is_blob() assert folder_content[sub_sub_path].is_folder() logger.info('Check that list of sub_sub folder is empty: %s', sub_sub_path) assert storage.list_folder(sub_sub_path) == {} logger.info('Check that listing content of a blob raises: %s', fpath1) try: storage.list_folder(fpath1) pytest.fail('Listing a blob should raise') except CInvalidFileTypeError as e: assert e.path == fpath1 assert e.expected_blob is False logger.info('Delete file1: %s', fpath1) assert storage.delete(fpath1) is True # We have deleted the file assert storage.delete(fpath1) is False # We have not deleted anything logger.info('Check file1 does not appear anymore in folder: %s', sub_folder) assert fpath1 not in storage.list_folder(sub_folder) tmp = storage.get_file(fpath1) assert tmp is None logger.info('Delete whole test folder: %s', temp_root_path) ret = storage.delete(temp_root_path) assert ret is True # We have deleted at least one file logger.info('Deleting again returns False') ret = storage.delete(temp_root_path) assert ret is False # We have not deleted anything logger.info('Listing a deleted folder returns None: %s', temp_root_path) assert storage.list_folder(temp_root_path) is None assert storage.get_file(temp_root_path) is None