From feffa50b0c6a5a68b3dc15b34ae67d6a04e8c318 Mon Sep 17 00:00:00 2001 From: Ivan Barba Date: Wed, 7 Oct 2026 23:30:33 +0000 Subject: [PATCH] [Metrics] Skip GCE detection on Swarming bots On Swarming, luci-auth context exports GCE_METADATA_HOST pointing to its local token-only metadata emulator. Since #5479, is_gce() honours that variable and only checks for a TCP connection, so Swarming bots were treated as GCE and crashed fetching instance/zone (404). Bug: b/571127789 --- .../_internal/google_cloud_utils/compute_metadata.py | 5 +++++ .../core/google_cloud_utils/compute_metadata_test.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/clusterfuzz/_internal/google_cloud_utils/compute_metadata.py b/src/clusterfuzz/_internal/google_cloud_utils/compute_metadata.py index 1b9a72df65..9b59620b06 100644 --- a/src/clusterfuzz/_internal/google_cloud_utils/compute_metadata.py +++ b/src/clusterfuzz/_internal/google_cloud_utils/compute_metadata.py @@ -61,6 +61,11 @@ def _metadata_host_port(): def is_gce(): """Return whether or not we're on GCE.""" + # On Swarming, GCE_METADATA_HOST points to luci-auth's local emulator, which + # only serves tokens (e.g. no instance/zone or instance/id). + if environment.is_running_on_swarming(): + return False + try: host, port = _metadata_host_port() logs.info(f'Connecting to metadata server at {host}:{port}') diff --git a/src/clusterfuzz/_internal/tests/core/google_cloud_utils/compute_metadata_test.py b/src/clusterfuzz/_internal/tests/core/google_cloud_utils/compute_metadata_test.py index c91c3cd2a7..72f2b26ee8 100644 --- a/src/clusterfuzz/_internal/tests/core/google_cloud_utils/compute_metadata_test.py +++ b/src/clusterfuzz/_internal/tests/core/google_cloud_utils/compute_metadata_test.py @@ -66,6 +66,18 @@ def test_is_gce_on_non_default_port(self): with mock.patch.object(compute_metadata, '_METADATA_SERVER', '127.0.0.1:1'): self.assertFalse(compute_metadata.is_gce()) + def test_is_gce_on_swarming(self): + """Verifies that is_gce() is False on Swarming bots even if the metadata host (luci-auth's emulator) is reachable.""" + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener: + listener.bind(('127.0.0.1', 0)) + listener.listen(1) + port = listener.getsockname()[1] + + with mock.patch.object(compute_metadata, '_METADATA_SERVER', + f'127.0.0.1:{port}'), \ + mock.patch.dict(os.environ, {'SWARMING_BOT': 'True'}): + self.assertFalse(compute_metadata.is_gce()) + def test_gce_metadata_host_env_override(self): """Verifies that GCE_METADATA_HOST overrides the default metadata server and URL.""" with mock.patch.dict(