26
26
27
27
from six .moves .urllib import parse as urlparse
28
28
29
- from google .cloud import storage
30
29
from google .auth .credentials import AnonymousCredentials
30
+ from google .cloud import storage
31
+ from google .cloud .exceptions import NotFound
31
32
from google .cloud .storage .hmac_key import HMACKeyMetadata
32
33
33
34
from . import _read_local_json
55
56
56
57
_STRING_CONTENT = "hello world"
57
58
_BYTE_CONTENT = b"12345678"
58
- _BUCKET_ACL_PATCH_MSG = "BucketACL patch operations call storage.buckets.patch, but are never idempotent; Preconditions are irrelevant."
59
- _DEFAULT_OBJECT_ACL_PATCH_MSG = "DefaultObjectACL patch operations call storage.buckets.patch, but are never idempotent; Preconditions are irrelevant."
60
- _OBJECT_ACL_PATCH_MSG = "ObjectACL patch operations call storage.objects.patch, but are never idempotent; Preconditions are irrelevant."
61
59
62
60
63
61
########################################################################################################################################
@@ -192,7 +190,7 @@ def client_get_service_account_email(client, _preconditions, **_):
192
190
193
191
194
192
def notification_create (client , _preconditions , ** resources ):
195
- bucket = client .get_bucket (resources .get ("bucket" ).name )
193
+ bucket = client .bucket (resources .get ("bucket" ).name )
196
194
notification = bucket .notification ()
197
195
notification .create ()
198
196
@@ -278,8 +276,8 @@ def hmac_key_update(client, _preconditions, **resources):
278
276
279
277
280
278
def bucket_patch (client , _preconditions , ** resources ):
281
- bucket = client .get_bucket (resources .get ("bucket" ).name )
282
- metageneration = bucket .metageneration
279
+ bucket = client .bucket (resources .get ("bucket" ).name )
280
+ metageneration = resources . get ( " bucket" ) .metageneration
283
281
bucket .storage_class = "COLDLINE"
284
282
if _preconditions :
285
283
bucket .patch (if_metageneration_match = metageneration )
@@ -288,8 +286,8 @@ def bucket_patch(client, _preconditions, **resources):
288
286
289
287
290
288
def bucket_update (client , _preconditions , ** resources ):
291
- bucket = client .get_bucket (resources .get ("bucket" ).name )
292
- metageneration = bucket .metageneration
289
+ bucket = client .bucket (resources .get ("bucket" ).name )
290
+ metageneration = resources . get ( " bucket" ) .metageneration
293
291
bucket ._properties = {"storageClass" : "STANDARD" }
294
292
if _preconditions :
295
293
bucket .update (if_metageneration_match = metageneration )
@@ -298,7 +296,7 @@ def bucket_update(client, _preconditions, **resources):
298
296
299
297
300
298
def bucket_set_iam_policy (client , _preconditions , ** resources ):
301
- bucket = client .get_bucket (resources .get ("bucket" ).name )
299
+ bucket = client .bucket (resources .get ("bucket" ).name )
302
300
role = "roles/storage.objectViewer"
303
301
member = _CONF_TEST_SERVICE_ACCOUNT_EMAIL
304
302
@@ -480,35 +478,43 @@ def blob_create_resumable_upload_session(client, _preconditions, **resources):
480
478
481
479
482
480
def blob_make_private (client , _preconditions , ** resources ):
483
- if _preconditions :
484
- pytest .skip (_OBJECT_ACL_PATCH_MSG )
485
481
bucket = resources .get ("bucket" )
486
482
object = resources .get ("object" )
487
483
blob = client .bucket (bucket .name ).blob (object .name )
488
- blob .make_private ()
484
+ if _preconditions :
485
+ blob .make_private (if_metageneration_match = object .metageneration )
486
+ else :
487
+ blob .make_private ()
489
488
490
489
491
490
def blob_make_public (client , _preconditions , ** resources ):
492
- if _preconditions :
493
- pytest .skip (_OBJECT_ACL_PATCH_MSG )
494
491
bucket = resources .get ("bucket" )
495
492
object = resources .get ("object" )
496
493
blob = client .bucket (bucket .name ).blob (object .name )
497
- blob .make_public ()
494
+ if _preconditions :
495
+ blob .make_public (if_metageneration_match = object .metageneration )
496
+ else :
497
+ blob .make_public ()
498
498
499
499
500
500
def bucket_make_private (client , _preconditions , ** resources ):
501
- if _preconditions :
502
- pytest .skip (_BUCKET_ACL_PATCH_MSG )
503
501
bucket = client .bucket (resources .get ("bucket" ).name )
504
- bucket .make_private ()
502
+ if _preconditions :
503
+ bucket .make_private (
504
+ if_metageneration_match = resources .get ("bucket" ).metageneration
505
+ )
506
+ else :
507
+ bucket .make_private ()
505
508
506
509
507
510
def bucket_make_public (client , _preconditions , ** resources ):
508
- if _preconditions :
509
- pytest .skip (_BUCKET_ACL_PATCH_MSG )
510
511
bucket = client .bucket (resources .get ("bucket" ).name )
511
- bucket .make_public ()
512
+ if _preconditions :
513
+ bucket .make_public (
514
+ if_metageneration_match = resources .get ("bucket" ).metageneration
515
+ )
516
+ else :
517
+ bucket .make_public ()
512
518
513
519
514
520
def bucket_acl_reload (client , _preconditions , ** resources ):
@@ -517,55 +523,68 @@ def bucket_acl_reload(client, _preconditions, **resources):
517
523
518
524
519
525
def bucket_acl_save (client , _preconditions , ** resources ):
520
- if _preconditions :
521
- pytest .skip (_BUCKET_ACL_PATCH_MSG )
522
526
bucket = client .bucket (resources .get ("bucket" ).name )
523
- bucket .acl .reload ()
524
527
bucket .acl .user (_CONF_TEST_SERVICE_ACCOUNT_EMAIL ).grant_owner ()
525
- bucket .acl .save ()
528
+ if _preconditions :
529
+ bucket .acl .save (if_metageneration_match = resources .get ("bucket" ).metageneration )
530
+ else :
531
+ bucket .acl .save ()
526
532
527
533
528
534
def bucket_acl_save_predefined (client , _preconditions , ** resources ):
529
- if _preconditions :
530
- pytest .skip (_BUCKET_ACL_PATCH_MSG )
531
535
bucket = client .bucket (resources .get ("bucket" ).name )
532
- bucket .acl .save_predefined ("bucketOwnerFullControl" )
536
+ if _preconditions :
537
+ bucket .acl .save_predefined (
538
+ "bucketOwnerFullControl" ,
539
+ if_metageneration_match = resources .get ("bucket" ).metageneration ,
540
+ )
541
+ else :
542
+ bucket .acl .save_predefined ("bucketOwnerFullControl" )
533
543
534
544
535
545
def bucket_acl_clear (client , _preconditions , ** resources ):
536
- if _preconditions :
537
- pytest .skip (_BUCKET_ACL_PATCH_MSG )
538
546
bucket = client .bucket (resources .get ("bucket" ).name )
539
- bucket .acl .clear ()
547
+ if _preconditions :
548
+ bucket .acl .clear (if_metageneration_match = resources .get ("bucket" ).metageneration )
549
+ else :
550
+ bucket .acl .clear ()
540
551
541
552
542
553
def default_object_acl_reload (client , _preconditions , ** resources ):
543
554
bucket = client .bucket (resources .get ("bucket" ).name )
544
- print (bucket .default_object_acl )
545
555
bucket .default_object_acl .reload ()
546
556
547
557
548
558
def default_object_acl_save (client , _preconditions , ** resources ):
549
- if _preconditions :
550
- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
551
559
bucket = client .bucket (resources .get ("bucket" ).name )
552
- bucket .default_object_acl .reload ()
553
560
bucket .default_object_acl .user (_CONF_TEST_SERVICE_ACCOUNT_EMAIL ).grant_owner ()
554
- bucket .default_object_acl .save ()
561
+ if _preconditions :
562
+ bucket .default_object_acl .save (
563
+ if_metageneration_match = resources .get ("bucket" ).metageneration
564
+ )
565
+ else :
566
+ bucket .default_object_acl .save ()
555
567
556
568
557
569
def default_object_acl_save_predefined (client , _preconditions , ** resources ):
558
- if _preconditions :
559
- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
560
570
bucket = client .bucket (resources .get ("bucket" ).name )
561
- bucket .default_object_acl .save_predefined ("bucketOwnerFullControl" )
571
+ if _preconditions :
572
+ bucket .default_object_acl .save_predefined (
573
+ "bucketOwnerFullControl" ,
574
+ if_metageneration_match = resources .get ("bucket" ).metageneration ,
575
+ )
576
+ else :
577
+ bucket .default_object_acl .save_predefined ("bucketOwnerFullControl" )
562
578
563
579
564
580
def default_object_acl_clear (client , _preconditions , ** resources ):
565
- if _preconditions :
566
- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
567
581
bucket = client .bucket (resources .get ("bucket" ).name )
568
- bucket .default_object_acl .clear ()
582
+ if _preconditions :
583
+ bucket .default_object_acl .clear (
584
+ if_metageneration_match = resources .get ("bucket" ).metageneration
585
+ )
586
+ else :
587
+ bucket .default_object_acl .clear ()
569
588
570
589
571
590
def object_acl_reload (client , _preconditions , ** resources ):
@@ -576,32 +595,36 @@ def object_acl_reload(client, _preconditions, **resources):
576
595
577
596
578
597
def object_acl_save (client , _preconditions , ** resources ):
579
- if _preconditions :
580
- pytest .skip (_OBJECT_ACL_PATCH_MSG )
581
598
bucket = resources .get ("bucket" )
582
599
object = resources .get ("object" )
583
600
blob = client .bucket (bucket .name ).blob (object .name )
584
- blob .acl .reload ()
585
601
blob .acl .user (_CONF_TEST_SERVICE_ACCOUNT_EMAIL ).grant_owner ()
586
- blob .acl .save ()
602
+ if _preconditions :
603
+ blob .acl .save (if_metageneration_match = object .metageneration )
604
+ else :
605
+ blob .acl .save ()
587
606
588
607
589
608
def object_acl_save_predefined (client , _preconditions , ** resources ):
590
- if _preconditions :
591
- pytest .skip (_OBJECT_ACL_PATCH_MSG )
592
609
bucket = resources .get ("bucket" )
593
610
object = resources .get ("object" )
594
611
blob = client .bucket (bucket .name ).blob (object .name )
595
- blob .acl .save_predefined ("bucketOwnerFullControl" )
612
+ if _preconditions :
613
+ blob .acl .save_predefined (
614
+ "bucketOwnerFullControl" , if_metageneration_match = object .metageneration
615
+ )
616
+ else :
617
+ blob .acl .save_predefined ("bucketOwnerFullControl" )
596
618
597
619
598
620
def object_acl_clear (client , _preconditions , ** resources ):
599
- if _preconditions :
600
- pytest .skip (_OBJECT_ACL_PATCH_MSG )
601
621
bucket = resources .get ("bucket" )
602
622
object = resources .get ("object" )
603
623
blob = client .bucket (bucket .name ).blob (object .name )
604
- blob .acl .clear ()
624
+ if _preconditions :
625
+ blob .acl .clear (if_metageneration_match = object .metageneration )
626
+ else :
627
+ blob .acl .clear ()
605
628
606
629
607
630
########################################################################################################################################
@@ -721,9 +744,7 @@ def bucket(client):
721
744
yield bucket
722
745
try :
723
746
bucket .delete (force = True )
724
- except Exception :
725
- # in cases where resources are deleted within the test
726
- # TODO(cathyo@): narrow except to NotFound once the emulator response issue is resolved
747
+ except NotFound : # in cases where bucket is deleted within the test
727
748
pass
728
749
729
750
@@ -735,7 +756,7 @@ def object(client, bucket):
735
756
yield blob
736
757
try :
737
758
blob .delete ()
738
- except Exception : # in cases where resources are deleted within the test
759
+ except NotFound : # in cases where object is deleted within the test
739
760
pass
740
761
741
762
@@ -747,7 +768,7 @@ def notification(client, bucket):
747
768
yield notification
748
769
try :
749
770
notification .delete ()
750
- except Exception : # in cases where resources are deleted within the test
771
+ except NotFound : # in cases where notification is deleted within the test
751
772
pass
752
773
753
774
@@ -762,7 +783,7 @@ def hmac_key(client):
762
783
hmac_key .state = "INACTIVE"
763
784
hmac_key .update ()
764
785
hmac_key .delete ()
765
- except Exception : # in cases where resources are deleted within the test
786
+ except NotFound : # in cases where hmac_key is deleted within the test
766
787
pass
767
788
768
789
0 commit comments