2626
2727from six .moves .urllib import parse as urlparse
2828
29- from google .cloud import storage
3029from google .auth .credentials import AnonymousCredentials
30+ from google .cloud import storage
31+ from google .cloud .exceptions import NotFound
3132from google .cloud .storage .hmac_key import HMACKeyMetadata
3233
3334from . import _read_local_json
5556
5657_STRING_CONTENT = "hello world"
5758_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."
6159
6260
6361########################################################################################################################################
@@ -192,7 +190,7 @@ def client_get_service_account_email(client, _preconditions, **_):
192190
193191
194192def notification_create (client , _preconditions , ** resources ):
195- bucket = client .get_bucket (resources .get ("bucket" ).name )
193+ bucket = client .bucket (resources .get ("bucket" ).name )
196194 notification = bucket .notification ()
197195 notification .create ()
198196
@@ -278,8 +276,8 @@ def hmac_key_update(client, _preconditions, **resources):
278276
279277
280278def 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
283281 bucket .storage_class = "COLDLINE"
284282 if _preconditions :
285283 bucket .patch (if_metageneration_match = metageneration )
@@ -288,8 +286,8 @@ def bucket_patch(client, _preconditions, **resources):
288286
289287
290288def 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
293291 bucket ._properties = {"storageClass" : "STANDARD" }
294292 if _preconditions :
295293 bucket .update (if_metageneration_match = metageneration )
@@ -298,7 +296,7 @@ def bucket_update(client, _preconditions, **resources):
298296
299297
300298def bucket_set_iam_policy (client , _preconditions , ** resources ):
301- bucket = client .get_bucket (resources .get ("bucket" ).name )
299+ bucket = client .bucket (resources .get ("bucket" ).name )
302300 role = "roles/storage.objectViewer"
303301 member = _CONF_TEST_SERVICE_ACCOUNT_EMAIL
304302
@@ -480,35 +478,43 @@ def blob_create_resumable_upload_session(client, _preconditions, **resources):
480478
481479
482480def blob_make_private (client , _preconditions , ** resources ):
483- if _preconditions :
484- pytest .skip (_OBJECT_ACL_PATCH_MSG )
485481 bucket = resources .get ("bucket" )
486482 object = resources .get ("object" )
487483 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 ()
489488
490489
491490def blob_make_public (client , _preconditions , ** resources ):
492- if _preconditions :
493- pytest .skip (_OBJECT_ACL_PATCH_MSG )
494491 bucket = resources .get ("bucket" )
495492 object = resources .get ("object" )
496493 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 ()
498498
499499
500500def bucket_make_private (client , _preconditions , ** resources ):
501- if _preconditions :
502- pytest .skip (_BUCKET_ACL_PATCH_MSG )
503501 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 ()
505508
506509
507510def bucket_make_public (client , _preconditions , ** resources ):
508- if _preconditions :
509- pytest .skip (_BUCKET_ACL_PATCH_MSG )
510511 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 ()
512518
513519
514520def bucket_acl_reload (client , _preconditions , ** resources ):
@@ -517,55 +523,68 @@ def bucket_acl_reload(client, _preconditions, **resources):
517523
518524
519525def bucket_acl_save (client , _preconditions , ** resources ):
520- if _preconditions :
521- pytest .skip (_BUCKET_ACL_PATCH_MSG )
522526 bucket = client .bucket (resources .get ("bucket" ).name )
523- bucket .acl .reload ()
524527 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 ()
526532
527533
528534def bucket_acl_save_predefined (client , _preconditions , ** resources ):
529- if _preconditions :
530- pytest .skip (_BUCKET_ACL_PATCH_MSG )
531535 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" )
533543
534544
535545def bucket_acl_clear (client , _preconditions , ** resources ):
536- if _preconditions :
537- pytest .skip (_BUCKET_ACL_PATCH_MSG )
538546 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 ()
540551
541552
542553def default_object_acl_reload (client , _preconditions , ** resources ):
543554 bucket = client .bucket (resources .get ("bucket" ).name )
544- print (bucket .default_object_acl )
545555 bucket .default_object_acl .reload ()
546556
547557
548558def default_object_acl_save (client , _preconditions , ** resources ):
549- if _preconditions :
550- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
551559 bucket = client .bucket (resources .get ("bucket" ).name )
552- bucket .default_object_acl .reload ()
553560 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 ()
555567
556568
557569def default_object_acl_save_predefined (client , _preconditions , ** resources ):
558- if _preconditions :
559- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
560570 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" )
562578
563579
564580def default_object_acl_clear (client , _preconditions , ** resources ):
565- if _preconditions :
566- pytest .skip (_DEFAULT_OBJECT_ACL_PATCH_MSG )
567581 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 ()
569588
570589
571590def object_acl_reload (client , _preconditions , ** resources ):
@@ -576,32 +595,36 @@ def object_acl_reload(client, _preconditions, **resources):
576595
577596
578597def object_acl_save (client , _preconditions , ** resources ):
579- if _preconditions :
580- pytest .skip (_OBJECT_ACL_PATCH_MSG )
581598 bucket = resources .get ("bucket" )
582599 object = resources .get ("object" )
583600 blob = client .bucket (bucket .name ).blob (object .name )
584- blob .acl .reload ()
585601 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 ()
587606
588607
589608def object_acl_save_predefined (client , _preconditions , ** resources ):
590- if _preconditions :
591- pytest .skip (_OBJECT_ACL_PATCH_MSG )
592609 bucket = resources .get ("bucket" )
593610 object = resources .get ("object" )
594611 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" )
596618
597619
598620def object_acl_clear (client , _preconditions , ** resources ):
599- if _preconditions :
600- pytest .skip (_OBJECT_ACL_PATCH_MSG )
601621 bucket = resources .get ("bucket" )
602622 object = resources .get ("object" )
603623 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 ()
605628
606629
607630########################################################################################################################################
@@ -721,9 +744,7 @@ def bucket(client):
721744 yield bucket
722745 try :
723746 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
727748 pass
728749
729750
@@ -735,7 +756,7 @@ def object(client, bucket):
735756 yield blob
736757 try :
737758 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
739760 pass
740761
741762
@@ -747,7 +768,7 @@ def notification(client, bucket):
747768 yield notification
748769 try :
749770 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
751772 pass
752773
753774
@@ -762,7 +783,7 @@ def hmac_key(client):
762783 hmac_key .state = "INACTIVE"
763784 hmac_key .update ()
764785 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
766787 pass
767788
768789
0 commit comments