File size: 28,126 Bytes
f0f4f2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import os
import pickle
import tempfile
import uuid
import pytest
from botocore.awsrequest import AWSRequest
from fsspec.implementations.local import LocalFileSystem
from requests_mock import Mocker
from pyiceberg.exceptions import SignError
from pyiceberg.io import fsspec
from pyiceberg.io.fsspec import FsspecFileIO, s3v4_rest_signer
from pyiceberg.io.pyarrow import PyArrowFileIO
def test_fsspec_infer_local_fs_from_path(fsspec_fileio: FsspecFileIO) -> None:
"""Test path with `file` scheme and no scheme both use LocalFileSystem"""
assert isinstance(fsspec_fileio.new_output("file://tmp/warehouse")._fs, LocalFileSystem)
assert isinstance(fsspec_fileio.new_output("/tmp/warehouse")._fs, LocalFileSystem)
def test_fsspec_local_fs_can_create_path_without_parent_dir(fsspec_fileio: FsspecFileIO) -> None:
"""Test LocalFileSystem can create path without first creating the parent directories"""
with tempfile.TemporaryDirectory() as tmpdirname:
file_path = f"{tmpdirname}/foo/bar/baz.txt"
output_file = fsspec_fileio.new_output(file_path)
parent_path = os.path.dirname(file_path)
assert output_file._fs.exists(parent_path) is False
try:
with output_file.create() as f:
f.write(b"foo")
except Exception:
pytest.fail("Failed to write to file without parent directory")
@pytest.mark.s3
def test_fsspec_new_input_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test creating a new input file from a fsspec file-io"""
filename = str(uuid.uuid4())
input_file = fsspec_fileio.new_input(f"s3://warehouse/{filename}")
assert isinstance(input_file, fsspec.FsspecInputFile)
assert input_file.location == f"s3://warehouse/{filename}"
@pytest.mark.s3
def test_fsspec_new_s3_output_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test creating a new output file from an fsspec file-io"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(f"s3://warehouse/{filename}")
assert isinstance(output_file, fsspec.FsspecOutputFile)
assert output_file.location == f"s3://warehouse/{filename}"
@pytest.mark.s3
def test_fsspec_write_and_read_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test writing and reading a file using FsspecInputFile and FsspecOutputFile"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
with output_file.create() as f:
f.write(b"foo")
input_file = fsspec_fileio.new_input(f"s3://warehouse/{filename}")
assert input_file.open().read() == b"foo"
fsspec_fileio.delete(input_file)
@pytest.mark.s3
def test_fsspec_getting_length_of_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test getting the length of an FsspecInputFile and FsspecOutputFile"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
with output_file.create() as f:
f.write(b"foobar")
assert len(output_file) == 6
input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
assert len(input_file) == 6
fsspec_fileio.delete(output_file)
@pytest.mark.s3
def test_fsspec_file_tell(fsspec_fileio: FsspecFileIO) -> None:
"""Test finding cursor position for an fsspec file-io file"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
with output_file.create() as write_file:
write_file.write(b"foobar")
input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
f = input_file.open()
f.seek(0)
assert f.tell() == 0
f.seek(1)
assert f.tell() == 1
f.seek(3)
assert f.tell() == 3
f.seek(0)
assert f.tell() == 0
@pytest.mark.s3
def test_fsspec_read_specified_bytes_for_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test reading a specified number of bytes from an fsspec file-io file"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
with output_file.create() as write_file:
write_file.write(b"foo")
input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
f = input_file.open()
f.seek(0)
assert b"f" == f.read(1)
f.seek(0)
assert b"fo" == f.read(2)
f.seek(1)
assert b"o" == f.read(1)
f.seek(1)
assert b"oo" == f.read(2)
f.seek(0)
assert b"foo" == f.read(999) # test reading amount larger than entire content length
fsspec_fileio.delete(input_file)
@pytest.mark.s3
def test_fsspec_raise_on_opening_file_not_found(fsspec_fileio: FsspecFileIO) -> None:
"""Test that an fsspec input file raises appropriately when the s3 file is not found"""
filename = str(uuid.uuid4())
input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
with pytest.raises(FileNotFoundError) as exc_info:
input_file.open().read()
assert filename in str(exc_info.value)
@pytest.mark.s3
def test_checking_if_a_file_exists(fsspec_fileio: FsspecFileIO) -> None:
"""Test checking if a file exists"""
non_existent_file = fsspec_fileio.new_input(location="s3://warehouse/does-not-exist.txt")
assert not non_existent_file.exists()
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
assert not output_file.exists()
with output_file.create() as f:
f.write(b"foo")
existing_input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
assert existing_input_file.exists()
existing_output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
assert existing_output_file.exists()
fsspec_fileio.delete(existing_output_file)
@pytest.mark.s3
def test_closing_a_file(fsspec_fileio: FsspecFileIO) -> None:
"""Test closing an output file and input file"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
with output_file.create() as write_file:
write_file.write(b"foo")
assert not write_file.closed # type: ignore
assert write_file.closed # type: ignore
input_file = fsspec_fileio.new_input(location=f"s3://warehouse/{filename}")
f = input_file.open()
assert not f.closed # type: ignore
f.close()
assert f.closed # type: ignore
fsspec_fileio.delete(f"s3://warehouse/{filename}")
@pytest.mark.s3
def test_fsspec_converting_an_outputfile_to_an_inputfile(fsspec_fileio: FsspecFileIO) -> None:
"""Test converting an output file to an input file"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio.new_output(location=f"s3://warehouse/{filename}")
input_file = output_file.to_input_file()
assert input_file.location == output_file.location
@pytest.mark.s3
def test_writing_avro_file(generated_manifest_entry_file: str, fsspec_fileio: FsspecFileIO) -> None:
"""Test that bytes match when reading a local avro file, writing it using fsspec file-io, and then reading it again"""
filename = str(uuid.uuid4())
with PyArrowFileIO().new_input(location=generated_manifest_entry_file).open() as f:
b1 = f.read()
with fsspec_fileio.new_output(location=f"s3://warehouse/{filename}").create() as out_f:
out_f.write(b1)
with fsspec_fileio.new_input(location=f"s3://warehouse/{filename}").open() as in_f:
b2 = in_f.read()
assert b1 == b2 # Check that bytes of read from local avro file match bytes written to s3
fsspec_fileio.delete(f"s3://warehouse/{filename}")
@pytest.mark.s3
def test_fsspec_pickle_round_trip_s3(fsspec_fileio: FsspecFileIO) -> None:
_test_fsspec_pickle_round_trip(fsspec_fileio, "s3://warehouse/foo.txt")
@pytest.mark.adlfs
def test_fsspec_new_input_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test creating a new input file from an fsspec file-io"""
filename = str(uuid.uuid4())
input_file = adlfs_fsspec_fileio.new_input(f"abfss://tests/{filename}")
assert isinstance(input_file, fsspec.FsspecInputFile)
assert input_file.location == f"abfss://tests/{filename}"
@pytest.mark.adlfs
def test_fsspec_new_abfss_output_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test creating a new output file from an fsspec file-io"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(f"abfss://tests/{filename}")
assert isinstance(output_file, fsspec.FsspecOutputFile)
assert output_file.location == f"abfss://tests/{filename}"
@pytest.mark.adlfs
def test_fsspec_write_and_read_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test writing and reading a file using FsspecInputFile and FsspecOutputFile"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
with output_file.create() as f:
f.write(b"foo")
input_file = adlfs_fsspec_fileio.new_input(f"abfss://tests/{filename}")
assert input_file.open().read() == b"foo"
adlfs_fsspec_fileio.delete(input_file)
@pytest.mark.adlfs
def test_fsspec_getting_length_of_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test getting the length of an FsspecInputFile and FsspecOutputFile"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
with output_file.create() as f:
f.write(b"foobar")
assert len(output_file) == 6
input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
assert len(input_file) == 6
adlfs_fsspec_fileio.delete(output_file)
@pytest.mark.adlfs
def test_fsspec_file_tell_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test finding cursor position for an fsspec file-io file"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
with output_file.create() as write_file:
write_file.write(b"foobar")
input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
f = input_file.open()
f.seek(0)
assert f.tell() == 0
f.seek(1)
assert f.tell() == 1
f.seek(3)
assert f.tell() == 3
f.seek(0)
assert f.tell() == 0
adlfs_fsspec_fileio.delete(f"abfss://tests/{filename}")
@pytest.mark.adlfs
def test_fsspec_read_specified_bytes_for_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test reading a specified number of bytes from an fsspec file-io file"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
with output_file.create() as write_file:
write_file.write(b"foo")
input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
f = input_file.open()
f.seek(0)
assert b"f" == f.read(1)
f.seek(0)
assert b"fo" == f.read(2)
f.seek(1)
assert b"o" == f.read(1)
f.seek(1)
assert b"oo" == f.read(2)
f.seek(0)
assert b"foo" == f.read(999) # test reading amount larger than entire content length
adlfs_fsspec_fileio.delete(input_file)
@pytest.mark.adlfs
def test_fsspec_raise_on_opening_file_not_found_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test that an fsspec input file raises appropriately when the adlfs file is not found"""
filename = str(uuid.uuid4())
input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
with pytest.raises(FileNotFoundError) as exc_info:
input_file.open().read()
assert filename in str(exc_info.value)
@pytest.mark.adlfs
def test_checking_if_a_file_exists_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test checking if a file exists"""
non_existent_file = adlfs_fsspec_fileio.new_input(location="abfss://tests/does-not-exist.txt")
assert not non_existent_file.exists()
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
assert not output_file.exists()
with output_file.create() as f:
f.write(b"foo")
existing_input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
assert existing_input_file.exists()
existing_output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
assert existing_output_file.exists()
adlfs_fsspec_fileio.delete(existing_output_file)
@pytest.mark.adlfs
def test_closing_a_file_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test closing an output file and input file"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
with output_file.create() as write_file:
write_file.write(b"foo")
assert not write_file.closed # type: ignore
assert write_file.closed # type: ignore
input_file = adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}")
f = input_file.open()
assert not f.closed # type: ignore
f.close()
assert f.closed # type: ignore
adlfs_fsspec_fileio.delete(f"abfss://tests/{filename}")
@pytest.mark.adlfs
def test_fsspec_converting_an_outputfile_to_an_inputfile_adlfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test converting an output file to an input file"""
filename = str(uuid.uuid4())
output_file = adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}")
input_file = output_file.to_input_file()
assert input_file.location == output_file.location
@pytest.mark.adlfs
def test_writing_avro_file_adlfs(generated_manifest_entry_file: str, adlfs_fsspec_fileio: FsspecFileIO) -> None:
"""Test that bytes match when reading a local avro file, writing it using fsspec file-io, and then reading it again"""
filename = str(uuid.uuid4())
with PyArrowFileIO().new_input(location=generated_manifest_entry_file).open() as f:
b1 = f.read()
with adlfs_fsspec_fileio.new_output(location=f"abfss://tests/{filename}").create() as out_f:
out_f.write(b1)
with adlfs_fsspec_fileio.new_input(location=f"abfss://tests/{filename}").open() as in_f:
b2 = in_f.read()
assert b1 == b2 # Check that bytes of read from local avro file match bytes written to adlfs
adlfs_fsspec_fileio.delete(f"abfss://tests/{filename}")
@pytest.mark.adlfs
def test_fsspec_pickle_round_trip_aldfs(adlfs_fsspec_fileio: FsspecFileIO) -> None:
_test_fsspec_pickle_round_trip(adlfs_fsspec_fileio, "abfss://tests/foo.txt")
@pytest.mark.gcs
def test_fsspec_new_input_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test creating a new input file from a fsspec file-io"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
input_file = fsspec_fileio_gcs.new_input(location=location)
assert isinstance(input_file, fsspec.FsspecInputFile)
assert input_file.location == location
@pytest.mark.gcs
def test_fsspec_new_output_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test creating a new output file from an fsspec file-io"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
assert isinstance(output_file, fsspec.FsspecOutputFile)
assert output_file.location == location
@pytest.mark.gcs
def test_fsspec_write_and_read_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test writing and reading a file using FsspecInputFile and FsspecOutputFile"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
with output_file.create() as f:
f.write(b"foo")
input_file = fsspec_fileio_gcs.new_input(location)
with input_file.open() as f:
assert f.read() == b"foo"
fsspec_fileio_gcs.delete(input_file)
@pytest.mark.gcs
def test_fsspec_getting_length_of_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test getting the length of an FsspecInputFile and FsspecOutputFile"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
with output_file.create() as f:
f.write(b"foobar")
assert len(output_file) == 6
input_file = fsspec_fileio_gcs.new_input(location=location)
assert len(input_file) == 6
fsspec_fileio_gcs.delete(output_file)
@pytest.mark.gcs
def test_fsspec_file_tell_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test finding cursor position for an fsspec file-io file"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
with output_file.create() as write_file:
write_file.write(b"foobar")
input_file = fsspec_fileio_gcs.new_input(location=location)
with input_file.open() as f:
f.seek(0)
assert f.tell() == 0
f.seek(1)
assert f.tell() == 1
f.seek(3)
assert f.tell() == 3
f.seek(0)
assert f.tell() == 0
@pytest.mark.gcs
def test_fsspec_read_specified_bytes_for_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test reading a specified number of bytes from a fsspec file-io file"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
with output_file.create() as write_file:
write_file.write(b"foo")
input_file = fsspec_fileio_gcs.new_input(location=location)
with input_file.open() as f:
f.seek(0)
assert b"f" == f.read(1)
f.seek(0)
assert b"fo" == f.read(2)
f.seek(1)
assert b"o" == f.read(1)
f.seek(1)
assert b"oo" == f.read(2)
f.seek(0)
assert b"foo" == f.read(999) # test reading amount larger than entire content length
fsspec_fileio_gcs.delete(input_file)
@pytest.mark.gcs
def test_fsspec_raise_on_opening_file_not_found_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test that a fsspec input file raises appropriately when the gcs file is not found"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
input_file = fsspec_fileio_gcs.new_input(location=location)
with pytest.raises(FileNotFoundError) as exc_info:
input_file.open().read()
assert location in str(exc_info.value)
@pytest.mark.gcs
def test_checking_if_a_file_exists_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test checking if a file exists"""
non_existent_file = fsspec_fileio_gcs.new_input(location="gs://warehouse/does-not-exist.txt")
assert not non_existent_file.exists()
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
assert not output_file.exists()
with output_file.create() as f:
f.write(b"foo")
existing_input_file = fsspec_fileio_gcs.new_input(location=location)
assert existing_input_file.exists()
existing_output_file = fsspec_fileio_gcs.new_output(location=location)
assert existing_output_file.exists()
fsspec_fileio_gcs.delete(existing_output_file)
@pytest.mark.gcs
def test_closing_a_file_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test closing an output file and input file"""
location = f"gs://warehouse/{uuid.uuid4()}.txt"
output_file = fsspec_fileio_gcs.new_output(location=location)
with output_file.create() as write_file:
write_file.write(b"foo")
assert not write_file.closed # type: ignore
assert write_file.closed # type: ignore
input_file = fsspec_fileio_gcs.new_input(location=location)
f = input_file.open()
assert not f.closed # type: ignore
f.close()
assert f.closed # type: ignore
fsspec_fileio_gcs.delete(location=location)
@pytest.mark.gcs
def test_fsspec_converting_an_outputfile_to_an_inputfile_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test converting an output file to an input file"""
filename = str(uuid.uuid4())
output_file = fsspec_fileio_gcs.new_output(location=f"gs://warehouse/{filename}")
input_file = output_file.to_input_file()
assert input_file.location == output_file.location
@pytest.mark.gcs
def test_writing_avro_file_gcs(generated_manifest_entry_file: str, fsspec_fileio_gcs: FsspecFileIO) -> None:
"""Test that bytes match when reading a local avro file, writing it using fsspec file-io, and then reading it again"""
filename = str(uuid.uuid4())
with PyArrowFileIO().new_input(location=generated_manifest_entry_file).open() as f:
b1 = f.read()
with fsspec_fileio_gcs.new_output(location=f"gs://warehouse/{filename}").create() as out_f:
out_f.write(b1)
with fsspec_fileio_gcs.new_input(location=f"gs://warehouse/{filename}").open() as in_f:
b2 = in_f.read()
assert b1 == b2 # Check that bytes of read from local avro file match bytes written to s3
fsspec_fileio_gcs.delete(f"gs://warehouse/{filename}")
@pytest.mark.gcs
def test_fsspec_pickle_roundtrip_gcs(fsspec_fileio_gcs: FsspecFileIO) -> None:
_test_fsspec_pickle_round_trip(fsspec_fileio_gcs, "gs://warehouse/foo.txt")
def _test_fsspec_pickle_round_trip(fsspec_fileio: FsspecFileIO, location: str) -> None:
serialized_file_io = pickle.dumps(fsspec_fileio)
deserialized_file_io = pickle.loads(serialized_file_io)
output_file = deserialized_file_io.new_output(location)
with output_file.create() as f:
f.write(b"foo")
input_file = deserialized_file_io.new_input(location)
with input_file.open() as f:
data = f.read()
assert data == b"foo"
assert len(input_file) == 3
deserialized_file_io.delete(location)
TEST_URI = "https://iceberg-test-signer"
def test_s3v4_rest_signer(requests_mock: Mocker) -> None:
new_uri = "https://other-bucket/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro"
requests_mock.post(
f"{TEST_URI}/v1/aws/s3/sign",
json={
"uri": new_uri,
"headers": {
"Authorization": [
"AWS4-HMAC-SHA256 Credential=ASIAQPRZZYGHUT57DL3I/20221017/us-west-2/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=430582a17d61ab02c272896fa59195f277af4bdf2121c441685e589f044bbe02"
],
"Host": ["bucket.s3.us-west-2.amazonaws.com"],
"User-Agent": ["Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0"],
"x-amz-content-sha256": ["UNSIGNED-PAYLOAD"],
"X-Amz-Date": ["20221017T102940Z"],
"X-Amz-Security-Token": [
"YQoJb3JpZ2luX2VjEDoaCXVzLXdlc3QtMiJGMEQCID/fFxZP5oaEgQmcwP6XhZa0xSq9lmLSx8ffaWbySfUPAiAesa7sjd/WV4uwRTO0S03y/MWVtgpH+/NyZQ4bZgLVriqrAggTEAEaDDAzMzQwNzIyMjE1OSIMOeFOWhZIurMmAqjsKogCxMCqxX8ZjK0gacAkcDqBCyA7qTSLhdfKQIH/w7WpLBU1km+cRUWWCudan6gZsAq867DBaKEP7qI05DAWr9MChAkgUgyI8/G3Z23ET0gAedf3GsJbakB0F1kklx8jPmj4BPCht9RcTiXiJ5DxTS/cRCcalIQXmPFbaJSqpBusVG2EkWnm1v7VQrNPE2Os2b2P293vpbhwkyCEQiGRVva4Sw9D1sKvqSsK10QCRG+os6dFEOu1kARaXi6pStvR4OVmj7OYeAYjzaFchn7nz2CSae0M4IluiYQ01eQAywbfRo9DpKSmDM/DnPZWJnD/woLhaaaCrCxSSEaFsvGOHFhLd3Rknw1v0jADMILUtJoGOp4BpqKqyMz0CY3kpKL0jfR3ykTf/ge9wWVE0Alr7wRIkGCIURkhslGHqSyFRGoTqIXaxU+oPbwlw/0w/nYO7qQ6bTANOWye/wgw4h/NmJ6vU7wnZTXwREf1r6MF72++bE/fMk19LfVb8jN/qrUqAUXTc8gBAUxL5pgy8+oT/JnI2BkVrrLS4ilxEXP9Ahm+6GDUYXV4fBpqpZwdkzQ/5Gw="
],
},
"extensions": {},
},
status_code=200,
)
request = AWSRequest(
method="HEAD",
url="https://bucket/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro",
headers={"User-Agent": "Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0"},
data=b"",
params={},
auth_path="/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro",
)
request.context = {
"client_region": "us-west-2",
"has_streaming_input": False,
"auth_type": None,
"signing": {"bucket": "bucket"},
"retries": {"attempt": 1, "invocation-id": "75d143fb-0219-439b-872c-18213d1c8d54"},
}
signed_request = s3v4_rest_signer({"token": "abc", "uri": TEST_URI}, request)
assert signed_request.url == new_uri
assert dict(signed_request.headers) == {
"Authorization": "AWS4-HMAC-SHA256 Credential=ASIAQPRZZYGHUT57DL3I/20221017/us-west-2/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token, Signature=430582a17d61ab02c272896fa59195f277af4bdf2121c441685e589f044bbe02",
"Host": "bucket.s3.us-west-2.amazonaws.com",
"User-Agent": "Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0",
"X-Amz-Date": "20221017T102940Z",
"X-Amz-Security-Token": "YQoJb3JpZ2luX2VjEDoaCXVzLXdlc3QtMiJGMEQCID/fFxZP5oaEgQmcwP6XhZa0xSq9lmLSx8ffaWbySfUPAiAesa7sjd/WV4uwRTO0S03y/MWVtgpH+/NyZQ4bZgLVriqrAggTEAEaDDAzMzQwNzIyMjE1OSIMOeFOWhZIurMmAqjsKogCxMCqxX8ZjK0gacAkcDqBCyA7qTSLhdfKQIH/w7WpLBU1km+cRUWWCudan6gZsAq867DBaKEP7qI05DAWr9MChAkgUgyI8/G3Z23ET0gAedf3GsJbakB0F1kklx8jPmj4BPCht9RcTiXiJ5DxTS/cRCcalIQXmPFbaJSqpBusVG2EkWnm1v7VQrNPE2Os2b2P293vpbhwkyCEQiGRVva4Sw9D1sKvqSsK10QCRG+os6dFEOu1kARaXi6pStvR4OVmj7OYeAYjzaFchn7nz2CSae0M4IluiYQ01eQAywbfRo9DpKSmDM/DnPZWJnD/woLhaaaCrCxSSEaFsvGOHFhLd3Rknw1v0jADMILUtJoGOp4BpqKqyMz0CY3kpKL0jfR3ykTf/ge9wWVE0Alr7wRIkGCIURkhslGHqSyFRGoTqIXaxU+oPbwlw/0w/nYO7qQ6bTANOWye/wgw4h/NmJ6vU7wnZTXwREf1r6MF72++bE/fMk19LfVb8jN/qrUqAUXTc8gBAUxL5pgy8+oT/JnI2BkVrrLS4ilxEXP9Ahm+6GDUYXV4fBpqpZwdkzQ/5Gw=",
"x-amz-content-sha256": "UNSIGNED-PAYLOAD",
}
def test_s3v4_rest_signer_forbidden(requests_mock: Mocker) -> None:
requests_mock.post(
f"{TEST_URI}/v1/aws/s3/sign",
json={
"method": "HEAD",
"region": "us-west-2",
"uri": "https://bucket/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro",
"headers": {"User-Agent": ["Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0"]},
},
status_code=401,
)
request = AWSRequest(
method="HEAD",
url="https://bucket/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro",
headers={"User-Agent": "Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0"},
data=b"",
params={},
auth_path="/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro",
)
request.context = {
"client_region": "us-west-2",
"has_streaming_input": False,
"auth_type": None,
"signing": {"bucket": "bucket"},
"retries": {"attempt": 1, "invocation-id": "75d143fb-0219-439b-872c-18213d1c8d54"},
}
with pytest.raises(SignError) as exc_info:
_ = s3v4_rest_signer({"token": "abc", "uri": TEST_URI}, request)
assert (
"""Failed to sign request 401: {'method': 'HEAD', 'region': 'us-west-2', 'uri': 'https://bucket/metadata/snap-8048355899640248710-1-a5c8ea2d-aa1f-48e8-89f4-1fa69db8c742.avro', 'headers': {'User-Agent': ['Botocore/1.27.59 Python/3.10.7 Darwin/21.5.0']}}"""
in str(exc_info.value)
)
|