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(