-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_zonal.py
More file actions
930 lines (757 loc) · 30.4 KB
/
test_zonal.py
File metadata and controls
930 lines (757 loc) · 30.4 KB
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
# py standard imports
import asyncio
import datetime
import gc
import os
import random
import uuid
from io import BytesIO
# python additional imports
import google_crc32c
import pytest
from google.api_core.exceptions import FailedPrecondition, NotFound, OutOfRange
from google.cloud.storage.asyncio.async_appendable_object_writer import (
_DEFAULT_FLUSH_INTERVAL_BYTES,
AsyncAppendableObjectWriter,
)
# current library imports
from google.cloud import kms
from google.cloud.storage.asyncio.async_grpc_client import AsyncGrpcClient
from google.cloud.storage.asyncio.async_multi_range_downloader import (
AsyncMultiRangeDownloader,
)
pytestmark = pytest.mark.skipif(
os.getenv("RUN_ZONAL_SYSTEM_TESTS") != "True",
reason="Zonal system tests need to be explicitly enabled. This helps scheduling tests in Kokoro and Cloud Build.",
)
# TODO: replace this with a fixture once zonal bucket creation / deletion
# is supported in grpc client or json client.
_ZONAL_BUCKET = os.getenv("ZONAL_BUCKET")
_CROSS_REGION_BUCKET = os.getenv("CROSS_REGION_BUCKET")
_BYTES_TO_UPLOAD = b"dummy_bytes_to_write_read_and_delete_appendable_object"
async def create_async_grpc_client(attempt_direct_path=True):
"""Initializes async client and gets the current event loop."""
return AsyncGrpcClient(attempt_direct_path=attempt_direct_path)
@pytest.fixture(scope="session")
def zonal_kms_key(storage_client, kms_client):
"""Provisions a KMS key in the same location as of the zonal bucket."""
# Get the zonal bucket and extract its location
bucket = storage_client.get_bucket(_ZONAL_BUCKET)
location = bucket.location.lower()
project = storage_client.project
keyring_name = "gcs-test-zonal-ring"
key_name = "gcs-test-zonal-key"
keyring_path = kms_client.key_ring_path(project, location, keyring_name)
# Create the KeyRing if it doesn't exist
try:
kms_client.get_key_ring(name=keyring_path)
except NotFound:
parent = f"projects/{project}/locations/{location}"
kms_client.create_key_ring(
request={"parent": parent, "key_ring_id": keyring_name, "key_ring": {}}
)
# Grant GCS service account permissions to use the key
service_account_email = storage_client.get_service_account_email()
policy = {
"bindings": [
{
"role": "roles/cloudkms.cryptoKeyEncrypterDecrypter",
"members": [f"serviceAccount:{service_account_email}"],
}
]
}
kms_client.set_iam_policy(request={"resource": keyring_path, "policy": policy})
# Create the CryptoKey if it doesn't exist
key_path = kms_client.crypto_key_path(project, location, keyring_name, key_name)
try:
kms_client.get_crypto_key(name=key_path)
except NotFound:
purpose = kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
key = {"purpose": purpose}
kms_client.create_crypto_key(
request={
"parent": keyring_path,
"crypto_key_id": key_name,
"crypto_key": key,
}
)
return key_path
@pytest.fixture(scope="session")
def event_loop():
"""Redefine pytest-asyncio's event_loop fixture to be session-scoped."""
loop = asyncio.new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
def grpc_clients(event_loop):
# grpc clients has to be instantiated in the event loop,
# otherwise grpc creates it's own event loop and attaches to the client.
# Which will lead to deadlock because client running in one event loop and
# MRD or Appendable-Writer in another.
# https://github.com/grpc/grpc/blob/61fe9b40a986792ab7d4eb8924027b671faf26ba/src/python/grpcio/grpc/aio/_channel.py#L369
# https://github.com/grpc/grpc/blob/61fe9b40a986792ab7d4eb8924027b671faf26ba/src/python/grpcio/grpc/_cython/_cygrpc/aio/common.pyx.pxi#L249
clients = {
True: event_loop.run_until_complete(
create_async_grpc_client(attempt_direct_path=True)
),
False: event_loop.run_until_complete(
create_async_grpc_client(attempt_direct_path=False)
),
}
return clients
# This fixture is for tests that are NOT parametrized by attempt_direct_path
@pytest.fixture
def grpc_client(grpc_clients):
return grpc_clients[False]
@pytest.fixture
def grpc_client_direct(grpc_clients):
return grpc_clients[True]
def _get_equal_dist(a: int, b: int) -> tuple[int, int]:
step = (b - a) // 3
return a + step, a + 2 * step
@pytest.mark.parametrize(
"object_size",
[
256, # less than _chunk size
10 * 1024 * 1024, # less than _MAX_BUFFER_SIZE_BYTES
20 * 1024 * 1024, # greater than _MAX_BUFFER_SIZE
],
)
def test_basic_wrd_x_region(
storage_client,
blobs_to_delete,
object_size,
event_loop,
grpc_client,
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
async def _run():
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
writer = AsyncAppendableObjectWriter(
grpc_client, _CROSS_REGION_BUCKET, object_name
)
await writer.open()
await writer.append(object_data)
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
buffer = BytesIO()
mrd = AsyncMultiRangeDownloader(grpc_client, _CROSS_REGION_BUCKET, object_name)
async with mrd:
assert mrd._open_retries == 1
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
assert mrd.persisted_size == object_size
assert buffer.getvalue() == object_data
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(
storage_client.bucket(_CROSS_REGION_BUCKET).blob(object_name)
)
del writer
gc.collect()
event_loop.run_until_complete(_run())
@pytest.mark.parametrize(
"object_size",
[
256, # less than _chunk size
10 * 1024 * 1024, # less than _MAX_BUFFER_SIZE_BYTES
20 * 1024 * 1024, # greater than _MAX_BUFFER_SIZE
],
)
@pytest.mark.parametrize(
"attempt_direct_path",
[True, False],
)
def test_basic_wrd(
storage_client,
blobs_to_delete,
attempt_direct_path,
object_size,
event_loop,
grpc_clients,
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
async def _run():
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
grpc_client = grpc_clients[attempt_direct_path]
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(object_data)
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
buffer = BytesIO()
async with AsyncMultiRangeDownloader(
grpc_client, _ZONAL_BUCKET, object_name
) as mrd:
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
assert mrd.persisted_size == object_size
assert buffer.getvalue() == object_data
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
gc.collect()
event_loop.run_until_complete(_run())
@pytest.mark.parametrize(
"object_size",
[
10, # less than _chunk size,
10 * 1024 * 1024, # less than _MAX_BUFFER_SIZE_BYTES
20 * 1024 * 1024, # greater than _MAX_BUFFER_SIZE_BYTES
],
)
def test_basic_wrd_in_slices(
storage_client, blobs_to_delete, object_size, event_loop, grpc_client
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
async def _run():
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
mark1, mark2 = _get_equal_dist(0, object_size)
await writer.append(object_data[0:mark1])
await writer.append(object_data[mark1:mark2])
await writer.append(object_data[mark2:])
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == object_data
assert mrd.persisted_size == object_size
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
event_loop.run_until_complete(_run())
@pytest.mark.parametrize(
"flush_interval",
[
2 * 1024 * 1024,
4 * 1024 * 1024,
8 * 1024 * 1024,
_DEFAULT_FLUSH_INTERVAL_BYTES,
],
)
def test_wrd_with_non_default_flush_interval(
storage_client,
blobs_to_delete,
flush_interval,
event_loop,
grpc_client,
):
object_name = f"test_basic_wrd-{str(uuid.uuid4())}"
object_size = 9 * 1024 * 1024
async def _run():
object_data = os.urandom(object_size)
object_checksum = google_crc32c.value(object_data)
writer = AsyncAppendableObjectWriter(
grpc_client,
_ZONAL_BUCKET,
object_name,
writer_options={"FLUSH_INTERVAL_BYTES": flush_interval},
)
await writer.open()
mark1, mark2 = _get_equal_dist(0, object_size)
await writer.append(object_data[0:mark1])
await writer.append(object_data[mark1:mark2])
await writer.append(object_data[mark2:])
object_metadata = await writer.close(finalize_on_close=True)
assert object_metadata.size == object_size
assert int(object_metadata.checksums.crc32c) == object_checksum
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == object_data
assert mrd.persisted_size == object_size
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
event_loop.run_until_complete(_run())
def test_write_from_blob(
storage_client,
blobs_to_delete,
event_loop,
grpc_client,
):
object_name = f"test_from_blob-{str(uuid.uuid4())[:4]}"
content_type = "text/plain"
metadata = {"environment": "system-test"}
cache_control = "public, max-age=3600"
content_disposition = "attachment; filename=test.txt"
content_encoding = "identity"
content_language = "en"
custom_time = datetime.datetime(2025, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)
test_data = b"system-test-data"
async def _run():
# 1. Create a Blob instance
blob = storage_client.bucket(_ZONAL_BUCKET).blob(object_name)
blob.content_type = content_type
blob.metadata = metadata
blob.cache_control = cache_control
blob.content_disposition = content_disposition
blob.content_encoding = content_encoding
blob.content_language = content_language
blob.custom_time = custom_time
# 2. Use from_blob to create the writer
writer = AsyncAppendableObjectWriter.from_blob(grpc_client, blob)
await writer.open()
await writer.append(test_data)
await writer.close(finalize_on_close=True)
# 3. Verify the object metadata
obj = await grpc_client.get_object(
bucket_name=_ZONAL_BUCKET,
object_name=object_name,
)
assert obj.content_type == content_type
assert obj.metadata["environment"] == "system-test"
assert obj.cache_control == cache_control
assert obj.content_disposition == content_disposition
assert obj.content_encoding == content_encoding
assert obj.content_language == content_language
assert int(obj.custom_time.timestamp()) == int(custom_time.timestamp())
blobs_to_delete.append(blob)
event_loop.run_until_complete(_run())
def test_write_from_blob_with_kms_key(
storage_client,
blobs_to_delete,
event_loop,
grpc_client,
zonal_kms_key,
):
"""Verifies AsyncAppendableObjectWriter.from_blob correctly applies KMS encryption."""
object_name = f"test_from_blob_kms-{str(uuid.uuid4())[:4]}"
test_data = b"kms-protected-data"
async def _run():
# Create a local Blob instance with the KMS key
blob = storage_client.bucket(_ZONAL_BUCKET).blob(
object_name, kms_key_name=zonal_kms_key
)
writer = AsyncAppendableObjectWriter.from_blob(grpc_client, blob)
await writer.open()
await writer.append(test_data)
await writer.close(finalize_on_close=True)
# Verify the encryption metadata
obj = await grpc_client.get_object(
bucket_name=_ZONAL_BUCKET,
object_name=object_name,
)
# Assert that the object was encrypted with the correct key
# GCS appends a version suffix, so we use startswith()
assert obj.kms_key.startswith(zonal_kms_key)
blobs_to_delete.append(blob)
event_loop.run_until_complete(_run())
def test_read_unfinalized_appendable_object(
storage_client, blobs_to_delete, event_loop, grpc_client_direct
):
object_name = f"read_unfinalized_appendable_object-{str(uuid.uuid4())[:4]}"
async def _run():
grpc_client = grpc_client_direct
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.flush()
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
assert mrd.persisted_size == len(_BYTES_TO_UPLOAD)
# (0, 0) means read the whole object
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == _BYTES_TO_UPLOAD
# Clean up; use json client (i.e. `storage_client` fixture) to delete.
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del mrd
gc.collect()
event_loop.run_until_complete(_run())
@pytest.mark.skip(reason="Flaky test b/478129078")
def test_mrd_open_with_read_handle(event_loop, grpc_client_direct):
object_name = f"test_read_handl-{str(uuid.uuid4())[:4]}"
async def _run():
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name
)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.close()
mrd = AsyncMultiRangeDownloader(grpc_client_direct, _ZONAL_BUCKET, object_name)
await mrd.open()
read_handle = mrd.read_handle
await mrd.close()
# Open a new MRD using the `read_handle` obtained above
new_mrd = AsyncMultiRangeDownloader(
grpc_client_direct, _ZONAL_BUCKET, object_name, read_handle=read_handle
)
await new_mrd.open()
# persisted_size not set when opened with read_handle
assert new_mrd.persisted_size is None
buffer = BytesIO()
await new_mrd.download_ranges([(0, 0, buffer)])
await new_mrd.close()
assert buffer.getvalue() == _BYTES_TO_UPLOAD
del mrd
del new_mrd
gc.collect()
event_loop.run_until_complete(_run())
def test_mrd_open_with_read_handle_over_cloud_path(event_loop, grpc_client):
object_name = f"test_read_handl-{str(uuid.uuid4())[:4]}"
async def _run():
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.close()
mrd = AsyncMultiRangeDownloader(grpc_client, _ZONAL_BUCKET, object_name)
await mrd.open()
read_handle = mrd.read_handle
await mrd.close()
# Open a new MRD using the `read_handle` obtained above
new_mrd = AsyncMultiRangeDownloader(
grpc_client, _ZONAL_BUCKET, object_name, read_handle=read_handle
)
await new_mrd.open()
# persisted_size is set regardless of whether we use read_handle or not
# because read_handle won't work in CLOUD_PATH.
assert new_mrd.persisted_size == len(_BYTES_TO_UPLOAD)
buffer = BytesIO()
await new_mrd.download_ranges([(0, 0, buffer)])
await new_mrd.close()
assert buffer.getvalue() == _BYTES_TO_UPLOAD
del mrd
del new_mrd
gc.collect()
event_loop.run_until_complete(_run())
def test_wrd_open_with_write_handle(
event_loop, grpc_client_direct, storage_client, blobs_to_delete
):
object_name = f"test_write_handl-{str(uuid.uuid4())[:4]}"
async def _run():
# 1. Create an object and get its write_handle
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name
)
await writer.open()
write_handle = writer.write_handle
await writer.close()
# 2. Open a new writer using the obtained `write_handle` and generation
new_writer = AsyncAppendableObjectWriter(
grpc_client_direct,
_ZONAL_BUCKET,
object_name,
write_handle=write_handle,
generation=writer.generation,
)
await new_writer.open()
# Verify that the new writer is open and has the same write_handle
assert new_writer.is_stream_open
assert new_writer.generation == writer.generation
# 3. Append some data using the new writer
test_data = b"data_from_new_writer"
await new_writer.append(test_data)
await new_writer.close()
# 4. Verify the data was written correctly by reading it back
mrd = AsyncMultiRangeDownloader(grpc_client_direct, _ZONAL_BUCKET, object_name)
buffer = BytesIO()
await mrd.open()
await mrd.download_ranges([(0, 0, buffer)])
await mrd.close()
assert buffer.getvalue() == test_data
# Clean up
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del new_writer
del mrd
gc.collect()
event_loop.run_until_complete(_run())
def test_read_unfinalized_appendable_object_with_generation(
storage_client, blobs_to_delete, event_loop, grpc_client_direct
):
object_name = f"read_unfinalized_appendable_object-{str(uuid.uuid4())[:4]}"
grpc_client = grpc_client_direct
async def _run():
async def _read_and_verify(expected_content, generation=None):
"""Helper to read object content and verify against expected."""
mrd = AsyncMultiRangeDownloader(
grpc_client, _ZONAL_BUCKET, object_name, generation
)
buffer = BytesIO()
await mrd.open()
try:
assert mrd.persisted_size == len(expected_content)
await mrd.download_ranges([(0, 0, buffer)])
assert buffer.getvalue() == expected_content
finally:
await mrd.close()
return mrd
# First write
writer = AsyncAppendableObjectWriter(grpc_client, _ZONAL_BUCKET, object_name)
await writer.open()
await writer.append(_BYTES_TO_UPLOAD)
await writer.flush()
generation = writer.generation
# First read
mrd = await _read_and_verify(_BYTES_TO_UPLOAD)
# Second write, using generation from the first write.
writer_2 = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=generation
)
await writer_2.open()
await writer_2.append(_BYTES_TO_UPLOAD)
await writer_2.flush()
# Second read
mrd_2 = await _read_and_verify(_BYTES_TO_UPLOAD + _BYTES_TO_UPLOAD, generation)
# Clean up
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
del writer
del writer_2
del mrd
del mrd_2
gc.collect()
event_loop.run_until_complete(_run())
def test_open_with_generation_zero(
storage_client, blobs_to_delete, event_loop, grpc_client
):
"""Tests that using `generation=0` fails if the object already exists.
This test verifies that:
1. An object can be created using `AsyncAppendableObjectWriter` with `generation=0`.
2. Attempting to create the same object again with `generation=0` raises a
`FailedPrecondition` error with a 400 status code, because the
precondition (object must not exist) is not met.
"""
object_name = f"test_append_with_generation-{uuid.uuid4()}"
async def _run():
writer = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=0
)
# Empty object is created.
await writer.open()
assert writer.is_stream_open
await writer.close()
assert not writer.is_stream_open
with pytest.raises(FailedPrecondition) as exc_info:
writer_fail = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=0
)
await writer_fail.open()
assert exc_info.value.code == 400
# cleanup
del writer
gc.collect()
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
event_loop.run_until_complete(_run())
def test_open_existing_object_with_gen_None_overrides_existing(
storage_client, blobs_to_delete, event_loop, grpc_client
):
"""
Test that a new writer when specifies `None` overrides the existing object.
"""
object_name = f"test_append_with_generation-{uuid.uuid4()}"
async def _run():
writer = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=0
)
# Empty object is created.
await writer.open()
assert writer.is_stream_open
old_gen = writer.generation
await writer.close()
assert not writer.is_stream_open
new_writer = AsyncAppendableObjectWriter(
grpc_client, _ZONAL_BUCKET, object_name, generation=None
)
await new_writer.open()
assert new_writer.generation != old_gen
# cleanup
del writer
del new_writer
gc.collect()
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
event_loop.run_until_complete(_run())
def test_delete_object_using_grpc_client(event_loop, grpc_client_direct):
"""
Test that a new writer when specifies `None` overrides the existing object.
"""
object_name = f"test_append_with_generation-{uuid.uuid4()}"
async def _run():
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name, generation=0
)
# Empty object is created.
await writer.open()
await writer.append(b"some_bytes")
await writer.close()
await grpc_client_direct.delete_object(_ZONAL_BUCKET, object_name)
# trying to get raises raises 404.
with pytest.raises(NotFound):
# TODO: Remove this once GET_OBJECT is exposed in `AsyncGrpcClient`
await grpc_client_direct._grpc_client.get_object(
bucket=f"projects/_/buckets/{_ZONAL_BUCKET}", object_=object_name
)
# cleanup
del writer
gc.collect()
event_loop.run_until_complete(_run())
@pytest.mark.parametrize(
"ranges_desc, chunk_ranges",
[
("small", [(1, 100)] * 3),
("medium", [(100, 100000)] * 3),
("large", [(1000000, 2000000)] * 3),
("mixed", [(1, 100), (100, 100000), (1000000, 2000000)]),
],
)
def test_mrd_concurrent_download(
storage_client,
blobs_to_delete,
event_loop,
grpc_client_direct,
ranges_desc,
chunk_ranges,
):
"""
Test that mrd can handle concurrent `download_ranges` calls correctly.
Tests overlapping ranges, minimal concurrency,
parametrized chunk sizes (small/medium/large/mixed), and full object fetching alongside specific chunks.
"""
object_size = 15 * 1024 * 1024 # 15MB
object_name = f"test_mrd_concurrent-{uuid.uuid4()}"
async def _run():
object_data = os.urandom(object_size)
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name
)
await writer.open()
await writer.append(object_data)
await writer.close(finalize_on_close=True)
async with AsyncMultiRangeDownloader(
grpc_client_direct, _ZONAL_BUCKET, object_name
) as mrd:
tasks = []
ranges_to_fetch = []
for min_len, max_len in chunk_ranges:
start = random.randint(0, object_size - max_len)
length = random.randint(min_len, max_len)
ranges_to_fetch.append((start, length))
# Full object fetching concurrently
ranges_to_fetch.append((0, 0))
random.shuffle(ranges_to_fetch)
buffers = [BytesIO() for _ in range(len(ranges_to_fetch))]
for idx, (start, length) in enumerate(ranges_to_fetch):
tasks.append(
asyncio.create_task(
mrd.download_ranges([(start, length, buffers[idx])])
)
)
await asyncio.gather(*tasks)
# Validation
for idx, (start, length) in enumerate(ranges_to_fetch):
if length == 0:
expected_data = object_data[start:]
else:
expected_data = object_data[start : start + length]
assert buffers[idx].getvalue() == expected_data
del writer
gc.collect()
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
event_loop.run_until_complete(_run())
def test_mrd_concurrent_download_cancellation(
storage_client, blobs_to_delete, event_loop, grpc_client_direct
):
"""
Test task cancellation / abort mid-stream.
Tests that downloading gracefully manages memory and internal references
when tasks are canceled during active multiplexing, without breaking remaining downloads.
"""
object_size = 5 * 1024 * 1024 # 5MB
object_name = f"test_mrd_cancel-{uuid.uuid4()}"
async def _run():
object_data = os.urandom(object_size)
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name
)
await writer.open()
await writer.append(object_data)
await writer.close(finalize_on_close=True)
async with AsyncMultiRangeDownloader(
grpc_client_direct, _ZONAL_BUCKET, object_name
) as mrd:
tasks = []
num_chunks = 100
chunk_size = object_size // num_chunks
buffers = [BytesIO() for _ in range(num_chunks)]
for i in range(num_chunks):
start = i * chunk_size
tasks.append(
asyncio.create_task(
mrd.download_ranges([(start, chunk_size, buffers[i])])
)
)
# Let the loop start sending Bidi requests
await asyncio.sleep(0.01)
# Cancel a subset of evenly distributed tasks
for i in range(0, num_chunks, 2):
tasks[i].cancel()
results = await asyncio.gather(*tasks, return_exceptions=True)
for i in range(num_chunks):
if i % 2 == 0:
assert isinstance(results[i], asyncio.CancelledError)
else:
start = i * chunk_size
expected_data = object_data[start : start + chunk_size]
assert buffers[i].getvalue() == expected_data
del writer
gc.collect()
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
event_loop.run_until_complete(_run())
def test_mrd_concurrent_download_out_of_bounds(
storage_client, blobs_to_delete, event_loop, grpc_client_direct
):
"""
Test out-of-bounds & edge ranges concurrent with valid requests.
Verifies isolation: invalid bounds generate correct exceptions and don't stall the stream
for concurrently valid requests.
"""
object_size = 2 * 1024 * 1024 # 2MB
object_name = f"test_mrd_oob-{uuid.uuid4()}"
async def _run():
object_data = os.urandom(object_size)
writer = AsyncAppendableObjectWriter(
grpc_client_direct, _ZONAL_BUCKET, object_name
)
await writer.open()
await writer.append(object_data)
await writer.close(finalize_on_close=True)
async with AsyncMultiRangeDownloader(
grpc_client_direct, _ZONAL_BUCKET, object_name
) as mrd:
valid_buffer = BytesIO()
valid_task = asyncio.create_task(
mrd.download_ranges([(0, 100, valid_buffer)])
)
oob_buffer = BytesIO()
oob_task = asyncio.create_task(
mrd.download_ranges([(object_size + 1000, 100, oob_buffer)])
)
results = await asyncio.gather(valid_task, oob_task, return_exceptions=True)
# Verify valid one processed correctly
assert valid_buffer.getvalue() == object_data[:100]
# Verify fully OOB request returned Exception
assert isinstance(results[1], OutOfRange)
del writer
gc.collect()
blobs_to_delete.append(storage_client.bucket(_ZONAL_BUCKET).blob(object_name))
event_loop.run_until_complete(_run())