55import shlex
66import time
77import sys
8-
8+ from gcsfs . core import GCSFileSystem
99
1010import pytest
1111from fsspec .implementations .local import LocalFileSystem
1212from fsspec .registry import register_implementation , _registry
1313
14+ import fsspec
15+ import requests
16+
1417
1518def pytest_addoption (parser ):
1619 parser .addoption (
@@ -45,13 +48,13 @@ def clear_registry():
4548 _registry .clear ()
4649
4750
48- @pytest .fixture ()
51+ @pytest .fixture (scope = "function" )
4952def tempdir (clear_registry ):
5053 with tempfile .TemporaryDirectory () as tempdir :
5154 yield tempdir
5255
5356
54- @pytest .fixture ()
57+ @pytest .fixture (scope = "function" )
5558def local_testdir (tempdir , clear_registry ):
5659 tmp = Path (tempdir )
5760 tmp .mkdir (exist_ok = True )
@@ -153,7 +156,7 @@ def s3_server():
153156 time .sleep (0.1 ) # pragma: no cover
154157 anon = False
155158 s3so = dict (
156- client_kwargs = {"endpoint_url" : endpoint_uri }, use_listings_cache = False
159+ client_kwargs = {"endpoint_url" : endpoint_uri }, use_listings_cache = True
157160 )
158161 yield anon , s3so
159162 proc .terminate ()
@@ -176,3 +179,74 @@ def s3(s3_server, tempdir, local_testdir):
176179 else :
177180 s3 .mkdir (str (x ))
178181 yield anon , s3so
182+
183+
184+ def stop_docker (container ):
185+ cmd = shlex .split ('docker ps -a -q --filter "name=%s"' % container )
186+ cid = subprocess .check_output (cmd ).strip ().decode ()
187+ if cid :
188+ subprocess .call (["docker" , "rm" , "-f" , "-v" , cid ])
189+
190+
191+ TEST_PROJECT = os .environ .get ("GCSFS_TEST_PROJECT" , "test_project" )
192+
193+
194+ @pytest .fixture (scope = "module" )
195+ def docker_gcs ():
196+ if "STORAGE_EMULATOR_HOST" in os .environ :
197+ # assume using real API or otherwise have a server already set up
198+ yield os .environ ["STORAGE_EMULATOR_HOST" ]
199+ return
200+ container = "gcsfs_test"
201+ cmd = (
202+ "docker run -d -p 4443:4443 --name gcsfs_test fsouza/fake-gcs-server:latest -scheme " # noqa: E501
203+ "http -public-host http://localhost:4443 -external-url http://localhost:4443" # noqa: E501
204+ )
205+ stop_docker (container )
206+ subprocess .check_output (shlex .split (cmd ))
207+ url = "http://0.0.0.0:4443"
208+ timeout = 10
209+ while True :
210+ try :
211+ r = requests .get (url + "/storage/v1/b" )
212+ if r .ok :
213+ print ("url: " , url )
214+ yield url
215+ break
216+ except Exception as e : # noqa: E722
217+ timeout -= 1
218+ if timeout < 0 :
219+ raise SystemError from e
220+ time .sleep (1 )
221+ stop_docker (container )
222+
223+
224+ @pytest .fixture
225+ def gcs (docker_gcs , tempdir , local_testdir , populate = True ):
226+ # from gcsfs.credentials import GoogleCredentials
227+ GCSFileSystem .clear_instance_cache ()
228+ gcs = fsspec .filesystem ("gcs" , endpoint_url = docker_gcs )
229+ try :
230+ # ensure we're empty.
231+ try :
232+ gcs .rm ("tmp" , recursive = True )
233+ except FileNotFoundError :
234+ pass
235+ try :
236+ gcs .mkdir ("tmp" )
237+ print ("made tmp dir" )
238+ except Exception :
239+ pass
240+ if populate :
241+ for x in Path (local_testdir ).glob ("**/*" ):
242+ if x .is_file ():
243+ gcs .upload (str (x ), str (x ))
244+ else :
245+ gcs .mkdir (str (x ))
246+ gcs .invalidate_cache ()
247+ yield docker_gcs
248+ finally :
249+ try :
250+ gcs .rm (gcs .find ("tmp" ))
251+ except : # noqa: E722
252+ pass
0 commit comments