2525 TEST_VERSIONED_BUCKET ,
2626 TEST_ZONAL_BUCKET ,
2727)
28+ from gcsfs .tests .utils import is_real_gcs
2829
2930files = {
3031 "test/accounts.1.json" : (
6061 "zonal/test/c" : b"ab\n " + b"a" * (2 ** 18 ) + b"\n ab" ,
6162}
6263
63- _MULTI_THREADED_TEST_DATA_SIZE = 5 * 1024 * 1024 # 5MB
64- pattern = b"0123456789abcdef"
65- text_files ["multi_threaded_test_file" ] = (
66- pattern * (_MULTI_THREADED_TEST_DATA_SIZE // len (pattern ))
67- + pattern [: _MULTI_THREADED_TEST_DATA_SIZE % len (pattern )]
68- )
69-
7064allfiles = dict (** files , ** csv_files , ** text_files )
7165a = TEST_BUCKET + "/tmp/test/a"
7266b = TEST_BUCKET + "/tmp/test/b"
8276}
8377
8478
79+ @pytest .fixture (autouse = True )
80+ def _avoid_adc_timeout (monkeypatch ):
81+ """Avoid slow ADC lookups and Metadata Server requests in tests."""
82+ # Do not apply if tests are explicitly running against real GCS
83+ if is_real_gcs ():
84+ yield
85+ return
86+
87+ # Disable GCE metadata check in google-auth and gcsfs
88+ monkeypatch .setenv ("NO_GCE_CHECK" , "true" )
89+
90+ # Set a dummy project to avoid project ID lookup timeouts if not set
91+ if "GOOGLE_CLOUD_PROJECT" not in os .environ :
92+ monkeypatch .setenv ("GOOGLE_CLOUD_PROJECT" , "dummy-project" )
93+
94+ yield
95+
96+
97+ @pytest .fixture (autouse = True )
98+ def _mock_get_bucket_type_on_emulator ():
99+ """Mock _get_bucket_type to return UNKNOWN instantly on emulator."""
100+ if not is_real_gcs ():
101+ with mock .patch (
102+ "gcsfs.extended_gcsfs.ExtendedGcsFileSystem._get_bucket_type" ,
103+ return_value = BucketType .UNKNOWN ,
104+ ):
105+ yield
106+ else :
107+ yield
108+
109+
85110def stop_docker (container ):
86111 cmd = shlex .split ('docker ps -a -q --filter "name=%s"' % container )
87112 cid = subprocess .check_output (cmd ).strip ().decode ()
@@ -92,6 +117,8 @@ def stop_docker(container):
92117@pytest .fixture (scope = "session" )
93118def docker_gcs ():
94119 if "STORAGE_EMULATOR_HOST" in os .environ :
120+ if not is_real_gcs ():
121+ params ["token" ] = "anon"
95122 # assume using real API or otherwise have a server already set up
96123 yield os .getenv ("STORAGE_EMULATOR_HOST" )
97124 return
@@ -290,9 +317,6 @@ def final_cleanup(gcs_factory, buckets_to_delete):
290317def gcs_versioned (gcs_factory , buckets_to_delete ):
291318 gcs = gcs_factory ()
292319 gcs .version_aware = True
293- is_real_gcs = (
294- os .environ .get ("STORAGE_EMULATOR_HOST" ) == "https://storage.googleapis.com"
295- )
296320 try : # ensure we're empty.
297321 # The versioned bucket might be created by `is_versioning_enabled`
298322 # in test_core_versioned.py. We must register it for cleanup only if
@@ -306,7 +330,7 @@ def gcs_versioned(gcs_factory, buckets_to_delete):
306330 buckets_to_delete .add (TEST_VERSIONED_BUCKET )
307331 except ImportError :
308332 pass # test_core_versioned is not being run
309- if is_real_gcs :
333+ if is_real_gcs () :
310334 cleanup_versioned_bucket (gcs , TEST_VERSIONED_BUCKET )
311335 else :
312336 # For emulators, we delete and recreate the bucket for a clean state
@@ -321,7 +345,7 @@ def gcs_versioned(gcs_factory, buckets_to_delete):
321345 finally :
322346 # Ensure the bucket is empty after the test.
323347 try :
324- if is_real_gcs :
348+ if is_real_gcs () :
325349 cleanup_versioned_bucket (gcs , TEST_VERSIONED_BUCKET )
326350 except Exception as e :
327351 logging .warning (
@@ -367,13 +391,9 @@ def cleanup_versioned_bucket(gcs, bucket_name, prefix=None):
367391
368392
369393def _create_extended_gcsfs (gcs_factory , buckets_to_delete , populate_bucket , ** kwargs ):
370- is_real_gcs = (
371- os .environ .get ("STORAGE_EMULATOR_HOST" ) == "https://storage.googleapis.com"
372- )
373-
374394 extended_gcsfs = gcs_factory (** kwargs )
375395 # Only create/delete/populate the bucket if we are NOT using the real GCS endpoint.
376- if not is_real_gcs :
396+ if not is_real_gcs () :
377397 if not extended_gcsfs .exists (TEST_ZONAL_BUCKET ):
378398 extended_gcsfs .mkdir (TEST_ZONAL_BUCKET )
379399 buckets_to_delete .add (TEST_ZONAL_BUCKET )
@@ -433,7 +453,7 @@ def gcs_hns(gcs_factory, buckets_to_delete):
433453def zonal_write_mocks ():
434454 """A fixture for mocking Zonal bucket write functionality."""
435455
436- if os . environ . get ( "STORAGE_EMULATOR_HOST" ) == "https://storage.googleapis.com" :
456+ if is_real_gcs () :
437457 yield None
438458 return
439459
0 commit comments