Repository navigation
Add live tests for gaps in what CI checks on real Drive #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
shcheklein
wants to merge
6
commits into
main
Choose a base branch
from
live-gap-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ea7612
Add live tests for gaps in what CI checks on real Drive
shcheklein 9094808
Make the service account sign-in tests read Drive
shcheklein 3328448
Compare listings without their order; check the sign-in with the root
shcheklein 1b21417
Trim the folder-ID live tests and check that uploads go in parts
shcheklein 8ff922e
Merge remote-tracking branch 'origin/main' into live-gap-tests
shcheklein d267cfc
Restore the trashed file in the rmdir test; one import style in test_…
shcheklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| """The filesystem against real Drive, mounted on a folder ID as DVC uses it.""" | ||
|
|
||
| import errno | ||
| import hashlib | ||
| import os | ||
| import unicodedata | ||
| import uuid | ||
|
|
||
| import fsspec | ||
| import pytest | ||
|
|
||
| from pydrive2.drive import GoogleDrive | ||
| from pydrive2.files import FOLDER_MIME_TYPE | ||
| from pydrive2.fs import GDriveFileSystem | ||
| from pydrive2.test.test_util import pydrive_retry, upload_in_parts | ||
|
|
||
| pytestmark = pytest.mark.drive | ||
|
|
||
| PART = 256 * 1024 | ||
|
|
||
|
|
||
| def create_folder(service_auth, name, parent=None): | ||
| folder = GoogleDrive(service_auth).CreateFile( | ||
| {"name": name, "mimeType": FOLDER_MIME_TYPE} | ||
| | ({"parents": [parent]} if parent else {}) | ||
| ) | ||
| pydrive_retry(folder.Upload) | ||
| return folder["id"] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def module_folder(service_auth): | ||
| folder_id = create_folder(service_auth, uuid.uuid4().hex) | ||
| yield folder_id | ||
| pydrive_retry( | ||
| GoogleDrive(service_auth).CreateFile({"id": folder_id}).Delete | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def root(service_auth, module_folder): | ||
| """A new folder for one test, used as the filesystem root by its ID.""" | ||
| return create_folder(service_auth, uuid.uuid4().hex, module_folder) | ||
|
|
||
|
|
||
| def filesystem(service_auth, root, **options): | ||
| """A new filesystem instance, with nothing cached from another one.""" | ||
| return GDriveFileSystem( | ||
| root, service_auth, skip_instance_cache=True, **options | ||
| ) | ||
|
|
||
|
|
||
| def ids_in(service_auth, folder_id): | ||
| query = f"'{folder_id}' in parents" | ||
| listing = GoogleDrive(service_auth).ListFile({"q": query}) | ||
| return { | ||
| item["name"]: item["id"] for item in pydrive_retry(listing.GetList) | ||
| } | ||
|
|
||
|
|
||
| def test_names_that_differ_only_in_case_are_two_files(service_auth, root): | ||
| fs = filesystem(service_auth, root) | ||
| fs.pipe_file(f"{root}/Report.txt", b"upper") | ||
| fs.pipe_file(f"{root}/report.txt", b"lower") | ||
|
|
||
| reader = filesystem(service_auth, root) | ||
| assert sorted(reader.ls(root)) == [ | ||
|
shcheklein marked this conversation as resolved.
Resolved
|
||
| f"{root}/Report.txt", | ||
| f"{root}/report.txt", | ||
| ] | ||
| assert reader.cat_file(f"{root}/Report.txt") == b"upper" | ||
| assert reader.cat_file(f"{root}/report.txt") == b"lower" | ||
| reader.rm_file(f"{root}/Report.txt") | ||
| after = filesystem(service_auth, root) | ||
| assert after.ls(root) == [f"{root}/report.txt"] | ||
| assert after.cat_file(f"{root}/report.txt") == b"lower" | ||
|
|
||
|
|
||
| NAMES = [ | ||
| "it's.txt", | ||
| "back\\slash.txt", | ||
| "two spaces.txt", | ||
| unicodedata.normalize("NFC", "café.txt"), | ||
| # macOS hands put() its file names in this form. | ||
| unicodedata.normalize("NFD", "naïve.txt"), | ||
| "日本語.txt", | ||
| ] | ||
|
|
||
|
|
||
| def test_names_with_quotes_slashes_spaces_or_accents_round_trip( | ||
| service_auth, root | ||
| ): | ||
| fs = filesystem(service_auth, root) | ||
| for name in NAMES: | ||
| fs.pipe_file(f"{root}/{name}", name.encode()) | ||
|
|
||
| reader = filesystem(service_auth, root) | ||
| assert sorted(reader.ls(root)) == sorted( | ||
| f"{root}/{name}" for name in NAMES | ||
| ) | ||
| read = {name: reader.cat_file(f"{root}/{name}") for name in NAMES} | ||
| assert read == {name: name.encode() for name in NAMES} | ||
|
|
||
|
|
||
| def test_checksum_and_size_are_those_of_the_bytes(service_auth, root): | ||
| data = os.urandom(1000) | ||
| path = f"{root}/data.bin" | ||
| filesystem(service_auth, root).pipe_file(path, data) | ||
|
|
||
| reader = filesystem(service_auth, root) | ||
| expected = {"size": len(data), "checksum": hashlib.md5(data).hexdigest()} | ||
| info = reader.info(path) | ||
| (listed,) = reader.ls(root, detail=True) | ||
| found = reader.find(root, detail=True)[path] | ||
| for entry in (info, listed, found): | ||
| assert {key: entry[key] for key in expected} == expected | ||
|
|
||
|
|
||
| def test_url_and_listing_of_a_folder_id_root_show_what_was_written( | ||
| service_auth, root | ||
| ): | ||
| fs = filesystem(service_auth, root) | ||
| fs.makedirs(f"{root}/a") | ||
| fs.pipe_file(f"{root}/a/x.txt", b"line one\nline two\n") | ||
|
|
||
| url = f"gdrive://{root}/a/x.txt" | ||
| with fsspec.open(url, "rt", google_auth=service_auth) as stream: | ||
| assert stream.readline() == "line one\n" | ||
| reader = filesystem(service_auth, root) | ||
| assert reader.info(root)["type"] == "directory" | ||
| assert reader.ls(root) == [f"{root}/a/"] | ||
| assert reader.find(root, withdirs=True) == [ | ||
| root, | ||
| f"{root}/a", | ||
| f"{root}/a/x.txt", | ||
| ] | ||
|
|
||
|
|
||
| def test_rmdir_for_good_refuses_a_folder_whose_files_are_in_the_trash( | ||
| service_auth, root | ||
| ): | ||
| fs = filesystem(service_auth, root) | ||
| fs.pipe_file(f"{root}/d/x.txt", b"x") | ||
| fs.rm_file(f"{root}/d/x.txt") # to the trash | ||
| folder_id = ids_in(service_auth, root)["d"] | ||
| file_id = ids_in(service_auth, folder_id)["x.txt"] | ||
|
|
||
| for_good = filesystem(service_auth, root, trash_only=False) | ||
| with pytest.raises(OSError) as error: | ||
| for_good.rmdir(f"{root}/d") | ||
| assert error.value.errno == errno.ENOTEMPTY | ||
| restored = GoogleDrive(service_auth).CreateFile({"id": file_id}) | ||
| pydrive_retry(restored.UnTrash) | ||
| pydrive_retry(restored.FetchMetadata, fields="trashed,parents") | ||
| assert (restored["trashed"], restored["parents"]) == (False, [folder_id]) | ||
|
|
||
| filesystem(service_auth, root).rm_file(f"{root}/d/x.txt") | ||
| filesystem(service_auth, root).rmdir(f"{root}/d") | ||
| assert not filesystem(service_auth, root).exists(f"{root}/d") | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def large_payload(): | ||
| """Over the one-request upload limit and over a 1 MiB write buffer.""" | ||
| return os.urandom(1024 * 1024 + 1) | ||
|
|
||
|
|
||
| def test_put_file_in_several_parts_stores_every_byte( | ||
| service_auth, root, large_payload, monkeypatch, tmp_path | ||
| ): | ||
| uploads = upload_in_parts(monkeypatch, PART) | ||
| source = tmp_path / "source" | ||
| source.write_bytes(large_payload) | ||
| progress = fsspec.Callback() | ||
| fs = filesystem(service_auth, root) | ||
|
|
||
| fs.put_file(str(source), f"{root}/big.bin", callback=progress) | ||
|
|
||
| assert {(u.resumable(), u.chunksize()) for u in uploads} == {(True, PART)} | ||
| size = len(large_payload) | ||
| assert (progress.size, progress.value) == (size, size) | ||
| reader = filesystem(service_auth, root) | ||
| assert reader.cat_file(f"{root}/big.bin") == large_payload | ||
|
|
||
|
|
||
| def test_write_larger_than_the_memory_buffer_stores_every_byte( | ||
| service_auth, root, large_payload | ||
| ): | ||
| fs = filesystem(service_auth, root) | ||
| # The writer keeps up to blocksize bytes in memory, the rest on disk. | ||
| fs.blocksize = 1024 * 1024 | ||
| with fs.open(f"{root}/spilled.bin", "wb") as stream: | ||
| for start in range(0, len(large_payload), 100_000): | ||
| stream.write(large_payload[start : start + 100_000]) | ||
|
|
||
| reader = filesystem(service_auth, root) | ||
| assert reader.cat_file(f"{root}/spilled.bin") == large_payload | ||
|
|
||
|
|
||
| def test_moving_a_folder_moves_what_is_in_it(service_auth, root): | ||
| fs = filesystem(service_auth, root) | ||
| fs.pipe_file(f"{root}/old/sub/x.txt", b"x") | ||
| fs.makedirs(f"{root}/other") | ||
|
|
||
| fs.mv(f"{root}/old", f"{root}/other/new") | ||
|
|
||
| reader = filesystem(service_auth, root) | ||
| assert reader.find(root) == [f"{root}/other/new/sub/x.txt"] | ||
| assert reader.cat_file(f"{root}/other/new/sub/x.txt") == b"x" | ||
|
|
||
|
|
||
| def test_moving_a_file_onto_a_folder_puts_it_in_the_folder(service_auth, root): | ||
| fs = filesystem(service_auth, root) | ||
| fs.pipe_file(f"{root}/x.txt", b"x") | ||
| fs.makedirs(f"{root}/dir") | ||
|
|
||
| fs.mv(f"{root}/x.txt", f"{root}/dir") | ||
|
|
||
| assert filesystem(service_auth, root).find(root) == [f"{root}/dir/x.txt"] | ||
|
|
||
|
|
||
| def test_moving_a_folder_into_itself_raises_and_changes_nothing( | ||
| service_auth, root | ||
| ): | ||
| fs = filesystem(service_auth, root) | ||
| fs.pipe_file(f"{root}/d/sub/x.txt", b"x") | ||
|
|
||
| with pytest.raises(OSError): | ||
| fs.mv(f"{root}/d", f"{root}/d/sub/inner") | ||
|
|
||
| assert filesystem(service_auth, root).find(root) == [f"{root}/d/sub/x.txt"] | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.