20
20
21
21
from google .cloud import pubsub
22
22
from google .cloud import storage
23
- import mock
24
23
import pytest
25
24
import requests
26
25
30
29
sys .path .append (os .path .join (os .path .dirname (__file__ ), ".." , "manager" )) # noqa
31
30
import manager # noqa
32
31
33
-
34
- gcs_bucket = os .environ ["CLOUD_STORAGE_BUCKET" ]
35
32
project_id = os .environ ["GOOGLE_CLOUD_PROJECT" ]
36
33
service_account_json = os .environ ["GOOGLE_APPLICATION_CREDENTIALS" ]
37
34
44
41
destination_file_name = "destination-file.bin"
45
42
gcs_file_name = "my-config"
46
43
44
+ storage_client = storage .Client ()
45
+
46
+
47
+ @pytest .fixture (scope = "module" )
48
+ def test_bucket_name ():
49
+ bucket_name = "python-docs-samples-iot-{}" .format (uuid .uuid4 ())
50
+
51
+ yield bucket_name
52
+
53
+ bucket = storage_client .bucket (bucket_name )
54
+ bucket .delete (force = True )
55
+
47
56
48
57
@pytest .fixture (scope = "module" )
49
- def test_blob ():
58
+ def test_bucket (test_bucket_name ):
59
+ """Yields a bucket that is deleted after the test completes."""
60
+ bucket = storage_client .bucket (test_bucket_name )
61
+
62
+ if not bucket .exists ():
63
+ bucket = storage_client .create_bucket (test_bucket_name )
64
+
65
+ yield bucket .name
66
+
67
+
68
+ @pytest .fixture (scope = "module" )
69
+ def test_blob (test_bucket ):
50
70
"""Provides a pre-existing blob in the test bucket."""
51
- bucket = storage . Client (). bucket (gcs_bucket )
71
+ bucket = storage_client . bucket (test_bucket )
52
72
# Name of the blob
53
- blob = bucket .blob ("iot_core_store_file_gcs" )
73
+ blob = bucket .blob ("iot_core_store_file_gcs-{}" . format ( uuid . uuid4 ()) )
54
74
# Text in the blob
55
75
blob .upload_from_string ("This file on GCS will go to a device." )
56
76
@@ -63,39 +83,35 @@ def test_blob():
63
83
pass
64
84
65
85
66
- @mock .patch ("google.cloud.storage.client.Client.create_bucket" )
67
- def test_create_bucket (create_bucket_mock , capsys ):
68
- # Unlike other tests for sending a config, this one mocks out the creation
69
- # because buckets are expensive, globally-namespaced objects.
70
- create_bucket_mock .return_value = mock .sentinel .bucket
71
-
72
- gcs_to_device .create_bucket (gcs_bucket )
86
+ def test_create_bucket (test_bucket_name , capsys ):
87
+ gcs_to_device .create_bucket (test_bucket_name )
73
88
74
- create_bucket_mock .assert_called_with (gcs_bucket )
89
+ out , _ = capsys .readouterr ()
90
+ assert f"Bucket { test_bucket_name } created" in out
75
91
76
92
77
- def test_upload_local_file (capsys ):
93
+ def test_upload_local_file (test_bucket , capsys ):
78
94
# Creates a temporary source file that gets uploaded
79
95
# to GCS. All other tests use the blob in test_blob().
80
96
with tempfile .NamedTemporaryFile () as source_file :
81
97
source_file .write (b"This is a source file." )
82
98
83
- gcs_to_device .upload_local_file (gcs_bucket , gcs_file_name , source_file .name )
99
+ gcs_to_device .upload_local_file (test_bucket , gcs_file_name , source_file .name )
84
100
85
101
out , _ = capsys .readouterr ()
86
102
assert "File {} uploaded as {}." .format (source_file .name , gcs_file_name ) in out
87
103
88
104
89
- def test_make_file_public (test_blob ):
90
- gcs_to_device .make_file_public (gcs_bucket , test_blob .name )
105
+ def test_make_file_public (test_bucket , test_blob ):
106
+ gcs_to_device .make_file_public (test_bucket , test_blob .name )
91
107
92
108
r = requests .get (test_blob .public_url )
93
109
# Test for the content of the file to verify that
94
110
# it's publicly accessible.
95
111
assert r .text == "This file on GCS will go to a device."
96
112
97
113
98
- def test_send_to_device (capsys ):
114
+ def test_send_to_device (test_bucket , capsys ):
99
115
manager .create_iot_topic (project_id , topic_id )
100
116
manager .open_registry (
101
117
service_account_json , project_id , cloud_region , pubsub_topic , registry_id
@@ -106,7 +122,7 @@ def test_send_to_device(capsys):
106
122
)
107
123
108
124
gcs_to_device .send_to_device (
109
- gcs_bucket ,
125
+ test_bucket ,
110
126
gcs_file_name ,
111
127
destination_file_name ,
112
128
project_id ,
0 commit comments