def vsifile_22(): # VSIOpenL doesn't set errorno gdal.VSIErrorReset() if gdal.VSIGetLastErrorNo() != 0: gdaltest.post_reason("Expected Err=0 after VSIErrorReset(), got %d" % gdal.VSIGetLastErrorNo()) return 'fail' fp = gdal.VSIFOpenL('tmp/not-existing', 'r') if fp is not None: gdaltest.post_reason("Expected None from VSIFOpenL") return 'fail' if gdal.VSIGetLastErrorNo() != 0: gdaltest.post_reason("Expected Err=0 from VSIFOpenL, got %d" % gdal.VSIGetLastErrorNo()) return 'fail' # VSIOpenExL does fp = gdal.VSIFOpenExL('tmp/not-existing', 'r', 1) if fp is not None: gdaltest.post_reason("Expected None from VSIFOpenExL") return 'fail' if gdal.VSIGetLastErrorNo() != 1: gdaltest.post_reason("Expected Err=1 from VSIFOpenExL, got %d" % gdal.VSIGetLastErrorNo()) return 'fail' if len(gdal.VSIGetLastErrorMsg()) == 0: gdaltest.post_reason("Expected a VSI error message") return 'fail' gdal.VSIErrorReset() if gdal.VSIGetLastErrorNo() != 0: gdaltest.post_reason("Expected Err=0 after VSIErrorReset(), got %d" % gdal.VSIGetLastErrorNo()) return 'fail' return 'success'
def test_vsifile_22(): # VSIOpenL doesn't set errorno gdal.VSIErrorReset() assert gdal.VSIGetLastErrorNo() == 0, \ ("Expected Err=0 after VSIErrorReset(), got %d" % gdal.VSIGetLastErrorNo()) fp = gdal.VSIFOpenL('tmp/not-existing', 'r') assert fp is None, "Expected None from VSIFOpenL" assert gdal.VSIGetLastErrorNo() == 0, \ ("Expected Err=0 from VSIFOpenL, got %d" % gdal.VSIGetLastErrorNo()) # VSIOpenExL does fp = gdal.VSIFOpenExL('tmp/not-existing', 'r', 1) assert fp is None, "Expected None from VSIFOpenExL" assert gdal.VSIGetLastErrorNo() == 1, \ ("Expected Err=1 from VSIFOpenExL, got %d" % gdal.VSIGetLastErrorNo()) assert len(gdal.VSIGetLastErrorMsg()) != 0, "Expected a VSI error message" gdal.VSIErrorReset() assert gdal.VSIGetLastErrorNo() == 0, \ ("Expected Err=0 after VSIErrorReset(), got %d" % gdal.VSIGetLastErrorNo())
def open_for_read(uri): """ Opens a test file for reading. """ return gdal.VSIFOpenExL(uri, 'rb', 1)
def test_vsiadls_fake_write(): if gdaltest.webserver_port == 0: pytest.skip() gdal.VSICurlClearCache() # Error case in initial PUT handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 400) with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is None # Empty file handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=flush&close=true&position=0', 200) with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is not None assert gdal.VSIFCloseL(f) == 0 # Small file handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201, expected_headers = {'x-ms-client-request-id': 'REQUEST_ID'}) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=0', 202, expected_body = b'foo') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=flush&close=true&position=3', 200) with webserver.install_http_handler(handler): f = gdal.VSIFOpenExL('/vsiadls/test_copy/file.bin', 'wb', 0, ['x-ms-client-request-id=REQUEST_ID']) assert f is not None assert gdal.VSIFWriteL('foo', 1, 3, f) == 3 assert gdal.VSIFCloseL(f) == 0 # Error case in PATCH append handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=0', 400, expected_body = b'foo') with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is not None assert gdal.VSIFWriteL('foo', 1, 3, f) == 3 assert gdal.VSIFCloseL(f) != 0 # Error case in PATCH close handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=0', 202, expected_body = b'foo') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=flush&close=true&position=3', 400) with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is not None assert gdal.VSIFWriteL('foo', 1, 3, f) == 3 assert gdal.VSIFCloseL(f) != 0 # Chunked output with gdaltest.config_option('VSIAZ_CHUNK_SIZE_BYTES', '10'): handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=0', 202, expected_body = b'0123456789') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=10', 202, expected_body = b'abcd') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=flush&close=true&position=14', 200) with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is not None assert gdal.VSIFWriteL('0123456789abcd', 1, 14, f) == 14 assert gdal.VSIFCloseL(f) == 0 # Chunked output with last chunk being the chunk size with gdaltest.config_option('VSIAZ_CHUNK_SIZE_BYTES', '5'): handler = webserver.SequentialHandler() handler.add('PUT', '/azure/blob/myaccount/test_copy/file.bin?resource=file', 201) handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=0', 202, expected_body = b'01234') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=append&position=5', 202, expected_body = b'56789') handler.add('PATCH', '/azure/blob/myaccount/test_copy/file.bin?action=flush&close=true&position=10', 200) with webserver.install_http_handler(handler): f = gdal.VSIFOpenL('/vsiadls/test_copy/file.bin', 'wb') assert f is not None assert gdal.VSIFWriteL('0123456789', 1, 10, f) == 10 assert gdal.VSIFCloseL(f) == 0