2323import requests
2424
2525import storage_add_bucket_label
26+ import storage_bucket_delete_default_kms_key
27+ import storage_compose_file
2628import storage_copy_file
29+ import storage_create_bucket_class_location
30+ import storage_define_bucket_website_configuration
2731import storage_delete_file
2832import storage_disable_bucket_lifecycle_management
2933import storage_disable_versioning
3034import storage_download_file
35+ import storage_download_public_file
3136import storage_enable_bucket_lifecycle_management
3237import storage_enable_versioning
3338import storage_generate_signed_post_policy_v4
3742import storage_get_bucket_labels
3843import storage_get_bucket_metadata
3944import storage_get_metadata
45+ import storage_get_service_account
4046import storage_list_buckets
4147import storage_list_files
4248import storage_list_files_with_prefix
4349import storage_make_public
4450import storage_move_file
51+ import storage_object_get_kms_key
4552import storage_remove_bucket_label
4653import storage_set_bucket_default_kms_key
4754import storage_set_metadata
@@ -100,6 +107,17 @@ def test_blob(test_bucket):
100107 yield blob
101108
102109
110+ @pytest .fixture
111+ def test_bucket_create ():
112+ """Yields a bucket object that is deleted after the test completes."""
113+ bucket = None
114+ while bucket is None or bucket .exists ():
115+ bucket_name = "storage-snippets-test-{}" .format (uuid .uuid4 ())
116+ bucket = storage .Client ().bucket (bucket_name )
117+ yield bucket
118+ bucket .delete (force = True )
119+
120+
103121def test_list_buckets (test_bucket , capsys ):
104122 storage_list_buckets .list_buckets ()
105123 out , _ = capsys .readouterr ()
@@ -204,9 +222,7 @@ def test_generate_upload_signed_url_v4(test_bucket, capsys):
204222 )
205223
206224 requests .put (
207- url ,
208- data = content ,
209- headers = {"content-type" : "application/octet-stream" },
225+ url , data = content , headers = {"content-type" : "application/octet-stream" },
210226 )
211227
212228 bucket = storage .Client ().bucket (test_bucket .name )
@@ -217,9 +233,7 @@ def test_generate_upload_signed_url_v4(test_bucket, capsys):
217233def test_generate_signed_policy_v4 (test_bucket , capsys ):
218234 blob_name = "storage_snippets_test_form"
219235 short_name = storage_generate_signed_post_policy_v4
220- form = short_name .generate_signed_post_policy_v4 (
221- test_bucket .name , blob_name
222- )
236+ form = short_name .generate_signed_post_policy_v4 (test_bucket .name , blob_name )
223237 assert "name='key' value='{}'" .format (blob_name ) in form
224238 assert "name='x-goog-signature'" in form
225239 assert "name='x-goog-date'" in form
@@ -238,9 +252,7 @@ def test_rename_blob(test_blob):
238252 except google .cloud .exceptions .exceptions .NotFound :
239253 pass
240254
241- storage_move_file .rename_blob (
242- bucket .name , test_blob .name , "test_rename_blob"
243- )
255+ storage_move_file .rename_blob (bucket .name , test_blob .name , "test_rename_blob" )
244256
245257 assert bucket .get_blob ("test_rename_blob" ) is not None
246258 assert bucket .get_blob (test_blob .name ) is None
@@ -275,15 +287,98 @@ def test_versioning(test_bucket, capsys):
275287
276288
277289def test_bucket_lifecycle_management (test_bucket , capsys ):
278- bucket = storage_enable_bucket_lifecycle_management .\
279- enable_bucket_lifecycle_management (test_bucket )
290+ bucket = storage_enable_bucket_lifecycle_management .enable_bucket_lifecycle_management (
291+ test_bucket
292+ )
280293 out , _ = capsys .readouterr ()
281294 assert "[]" in out
282295 assert "Lifecycle management is enable" in out
283296 assert len (list (bucket .lifecycle_rules )) > 0
284297
285- bucket = storage_disable_bucket_lifecycle_management .\
286- disable_bucket_lifecycle_management (test_bucket )
298+ bucket = storage_disable_bucket_lifecycle_management .disable_bucket_lifecycle_management (
299+ test_bucket
300+ )
287301 out , _ = capsys .readouterr ()
288302 assert "[]" in out
289303 assert len (list (bucket .lifecycle_rules )) == 0
304+
305+
306+ def test_create_bucket_class_location (test_bucket_create ):
307+ bucket = storage_create_bucket_class_location .create_bucket_class_location (
308+ test_bucket_create .name
309+ )
310+
311+ assert bucket .location == "US"
312+ assert bucket .storage_class == "COLDLINE"
313+
314+
315+ def test_bucket_delete_default_kms_key (test_bucket , capsys ):
316+ test_bucket .default_kms_key_name = KMS_KEY
317+ test_bucket .patch ()
318+
319+ assert test_bucket .default_kms_key_name == KMS_KEY
320+
321+ bucket = storage_bucket_delete_default_kms_key .bucket_delete_default_kms_key (
322+ test_bucket .name
323+ )
324+
325+ out , _ = capsys .readouterr ()
326+ assert bucket .default_kms_key_name is None
327+ assert bucket .name in out
328+
329+
330+ def test_get_service_account (capsys ):
331+ storage_get_service_account .get_service_account ()
332+
333+ out , _ = capsys .readouterr ()
334+
335+ assert "@gs-project-accounts.iam.gserviceaccount.com" in out
336+
337+
338+ def test_download_public_file (test_blob ):
339+ storage_make_public .make_blob_public (test_blob .bucket .name , test_blob .name )
340+ with tempfile .NamedTemporaryFile () as dest_file :
341+ storage_download_public_file .download_public_file (
342+ test_blob .bucket .name , test_blob .name , dest_file .name
343+ )
344+
345+ assert dest_file .read () == b"Hello, is it me you're looking for?"
346+
347+
348+ def test_define_bucket_website_configuration (test_bucket ):
349+ bucket = storage_define_bucket_website_configuration .define_bucket_website_configuration (
350+ test_bucket .name , "index.html" , "404.html"
351+ )
352+
353+ website_val = {"mainPageSuffix" : "index.html" , "notFoundPage" : "404.html" }
354+
355+ assert bucket ._properties ["website" ] == website_val
356+
357+
358+ def test_object_get_kms_key (test_bucket ):
359+ with tempfile .NamedTemporaryFile () as source_file :
360+ storage_upload_with_kms_key .upload_blob_with_kms (
361+ test_bucket .name , source_file .name , "test_upload_blob_encrypted" , KMS_KEY
362+ )
363+ kms_key = storage_object_get_kms_key .object_get_kms_key (
364+ test_bucket .name , "test_upload_blob_encrypted"
365+ )
366+
367+ assert kms_key .startswith (KMS_KEY )
368+
369+
370+ def test_storage_compose_file (test_bucket ):
371+ source_files = ["test_upload_blob_1" , "test_upload_blob_2" ]
372+ blob_list = []
373+ for source in source_files :
374+ blob = test_bucket .blob (source )
375+ blob .upload_from_string (source )
376+ blob_list .append (blob )
377+
378+ with tempfile .NamedTemporaryFile () as dest_file :
379+ destination = storage_compose_file .compose_file (
380+ test_bucket .name , blob_list , dest_file .name
381+ )
382+ composed = destination .download_as_string ()
383+
384+ assert composed .decode ("utf-8" ) == source_files [0 ] + source_files [1 ]
0 commit comments