@@ -61,8 +61,6 @@ def get_uri(key_name):
61
61
62
62
def read_file (key_name ):
63
63
response = client .get_object (Bucket = bucket_name , Key = key_name )
64
- print (bucket_name )
65
- print (key_name )
66
64
return response ["Body" ].read ()
67
65
68
66
def write_file (key_name , body ):
@@ -71,7 +69,39 @@ def write_file(key_name, body):
71
69
return get_uri , read_file , write_file
72
70
73
71
74
- MAP_URI_HELPER = {S3_URI : s3_init }
72
+ def gcs_init (monkeypatch ):
73
+ from google .cloud import storage
74
+
75
+ bucket_name = os .environ .get ("GCS_TEST_BUCKET" )
76
+ client = None
77
+ bucket = None
78
+ if bucket_name is None :
79
+ # This means we are running against emulator.
80
+ monkeypatch .setenv ("STORAGE_EMULATOR_HOST" , "http://localhost:9099" )
81
+ monkeypatch .setenv ("CLOUD_STORAGE_EMULATOR_ENDPOINT" , "http://localhost:9099" )
82
+ bucket_name = "tf-io-{}-{}" .format (GCS_URI , int (time .time ()))
83
+ client = storage .Client .create_anonymous_client ()
84
+ client .project = "test_project"
85
+ bucket = client .create_bucket (bucket_name )
86
+ else :
87
+ client = storage .Client ()
88
+ bucket = client .get_bucket (bucket )
89
+
90
+ def get_uri (key_name ):
91
+ return "{}://{}/{}" .format (GCS_URI , bucket_name , key_name )
92
+
93
+ def read_file (key_name ):
94
+ blob = bucket .get_blob (key_name )
95
+ return blob .download_as_bytes ()
96
+
97
+ def write_file (key_name , body ):
98
+ blob = bucket .blob (key_name )
99
+ blob .upload_from_string (body )
100
+
101
+ return get_uri , read_file , write_file
102
+
103
+
104
+ MAP_URI_HELPER = {S3_URI : s3_init , GCS_URI : gcs_init }
75
105
76
106
77
107
# ------------------------ URI CONDITION FOR EACH TEST ----------------------- #
@@ -138,13 +168,15 @@ def check_test_condition_and_setup_env(uri, envs, monkeypatch):
138
168
pytest .param (HDFS_URI , marks = pytest .mark .skip (reason = "TODO" )),
139
169
pytest .param (VIEWFS_URI , marks = pytest .mark .skip (reason = "TODO" )),
140
170
pytest .param (HAR_URI , marks = pytest .mark .skip (reason = "TODO" )),
141
- pytest . param ( GCS_URI , marks = pytest . mark . skip ( reason = "TODO" )) ,
171
+ GCS_URI ,
142
172
],
143
173
)
144
174
def uri_init (request ):
145
175
uri = request .param
146
176
monkeypatch = pytest .MonkeyPatch ()
147
- yield uri , * MAP_URI_HELPER [uri ](monkeypatch )
177
+
178
+ get_uri , read_file , write_file = MAP_URI_HELPER [uri ](monkeypatch )
179
+ yield uri , get_uri , read_file , write_file
148
180
monkeypatch .undo ()
149
181
150
182
@@ -186,7 +218,7 @@ def test_io_write_file(uri_init, envs, monkeypatch):
186
218
assert read_file (base_file_name ) == body
187
219
188
220
189
- @pytest .mark .parametrize ("envs" , [("+all" , None )])
221
+ @pytest .mark .parametrize ("envs" , [("+all:-gse " , None )])
190
222
def test_gfile_GFile_readable (uri_init , envs , monkeypatch ):
191
223
uri , get_uri , _ , write_file = uri_init
192
224
check_test_condition_and_setup_env (uri , envs , monkeypatch )
@@ -208,7 +240,7 @@ def test_gfile_GFile_readable(uri_init, envs, monkeypatch):
208
240
with pytest .raises (tf .errors .NotFoundError ) as excinfo :
209
241
fname_not_found = fname + "_not_found"
210
242
with tf .io .gfile .GFile (fname_not_found , "rb" ) as f :
211
- pass
243
+ _ = f . read ()
212
244
assert fname_not_found in str (excinfo .value )
213
245
214
246
# Read length
@@ -271,10 +303,3 @@ def test_gfile_GFile_writable(uri_init, envs, monkeypatch):
271
303
f .write (base_body )
272
304
f .flush ()
273
305
assert read_file (base_file_name ) == body + base_body
274
-
275
- # Notfound
276
- with pytest .raises (tf .errors .NotFoundError ) as excinfo :
277
- fname_not_found = fname + "_not_found"
278
- with tf .io .gfile .GFile (fname_not_found , "rb" ) as f :
279
- pass
280
- assert fname_not_found in str (excinfo .value )
0 commit comments