def test_resume_partial_download(syn, project, schedule_for_cleanup): original_file = utils.make_bogus_data_file(40000) entity = File(original_file, parent=project['id']) entity = syn.store(entity) # stash the original file for comparison later shutil.move(original_file, original_file+'.original') original_file += '.original' schedule_for_cleanup(original_file) temp_dir = tempfile.gettempdir() url = '%s/entity/%s/file' % (syn.repoEndpoint, entity.id) path = syn._download_from_URL(url, destination=temp_dir, fileHandleId=entity.dataFileHandleId, expected_md5=entity.md5) # simulate an imcomplete download by putting the # complete file back into its temporary location tmp_path = utils.temp_download_filename(temp_dir, entity.dataFileHandleId) shutil.move(path, tmp_path) # ...and truncating it to some fraction of its original size with open(tmp_path, 'r+') as f: f.truncate(3*os.path.getsize(original_file)//7) # this should complete the partial download path = syn._download_from_URL(url, destination=temp_dir, fileHandleId=entity.dataFileHandleId, expected_md5=entity.md5) assert filecmp.cmp(original_file, path), "File comparison failed"
def test_http_download__range_request_error(syn, project): # SYNPY-525 file_path = utils.make_bogus_data_file() file_entity = syn.store(File(file_path, parent=project)) syn.cache.purge(time.time()) # download once and rename to temp file to simulate range exceed file_entity = syn.get(file_entity) shutil.move(file_entity.path, utils.temp_download_filename(file_entity.path, file_entity.dataFileHandleId)) file_entity = syn.get(file_entity) assert file_path != file_entity.path assert filecmp.cmp(file_path, file_entity.path)
def test_md5_mismatch(self): with patch.object(multithread_download, "download_file"), \ patch.object(utils, "md5_for_file") as mock_md5_for_file, \ patch.object(os, "remove") as mock_os_remove, \ patch.object(shutil, "move") as mock_move: path = os.path.abspath("/myfakepath") mock_md5_for_file.return_value.hexdigest.return_value = "unexpetedMd5" assert_raises(SynapseMd5MismatchError, syn._download_from_url_multi_threaded, file_handle_id=123, object_id=456, object_type="FileEntity", destination=path, expected_md5="myExpectedMd5") mock_os_remove.assert_called_once_with(utils.temp_download_filename(path, 123)) mock_move.assert_not_called()
def test_temp_download_filename(): temp_destination = utils.temp_download_filename("/foo/bar/bat", 12345) assert temp_destination == "/foo/bar/bat.synapse_download_12345", temp_destination regex = r'/foo/bar/bat.synapse_download_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' assert re.match(regex, utils.temp_download_filename("/foo/bar/bat", None))