Repository navigation
[Better Tests] Support custom host in compute_metadata #5479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+95
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6246c14
[Better Tests] Support GCE_METADATA_HOST with port in compute_metadata
IvanBM18 a4dadd2
[Better Tests] Refine comment
IvanBM18 e2b677d
[Better tests] Better parsing in compute_metadata
IvanBM18 b3a108b
[Better tests] Adds logs for monitoring metadata server
IvanBM18 278548e
[Better tests] Make assertions cleaner by asserting per host & port"
IvanBM18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/clusterfuzz/_internal/tests/core/google_cloud_utils/compute_metadata_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Unit tests for compute_metadata.""" | ||
|
|
||
| import importlib | ||
| import os | ||
| import socket | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from clusterfuzz._internal.google_cloud_utils import compute_metadata | ||
|
|
||
|
|
||
| class ComputeMetadataTest(unittest.TestCase): | ||
| """Tests for compute_metadata host and port resolution.""" | ||
|
|
||
| def test_host_port_split(self): | ||
| """Verifies that _metadata_host_port() defaults to port 80 for bare hostnames and parses explicit host:port values.""" | ||
| with mock.patch.object(compute_metadata, '_METADATA_SERVER', | ||
| 'metadata.google.internal'): | ||
| host, port = compute_metadata._metadata_host_port() # pylint: disable=protected-access | ||
| self.assertEqual('metadata.google.internal', host) | ||
| self.assertEqual(80, port) | ||
|
|
||
| with mock.patch.object(compute_metadata, '_METADATA_SERVER', | ||
| '127.0.0.1:41234'): | ||
| host, port = compute_metadata._metadata_host_port() # pylint: disable=protected-access | ||
| self.assertEqual('127.0.0.1', host) | ||
| self.assertEqual(41234, port) | ||
|
|
||
| def test_host_port_split_ipv6(self): | ||
| """Verifies that _metadata_host_port() strips brackets from IPv6 hosts and parses an optional port.""" | ||
| with mock.patch.object(compute_metadata, '_METADATA_SERVER', | ||
| '[fd20:ce::254]'): | ||
| host, port = compute_metadata._metadata_host_port() # pylint: disable=protected-access | ||
| self.assertEqual('fd20:ce::254', host) | ||
| self.assertEqual(80, port) | ||
|
|
||
| with mock.patch.object(compute_metadata, '_METADATA_SERVER', '[::1]:41234'): | ||
| host, port = compute_metadata._metadata_host_port() # pylint: disable=protected-access | ||
| self.assertEqual('::1', host) | ||
| self.assertEqual(41234, port) | ||
|
|
||
| def test_is_gce_on_non_default_port(self): | ||
| """Verifies that compute_metadata.is_gce() connects to the port in _METADATA_SERVER rather than hardcoding port 80.""" | ||
| 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}'): | ||
| self.assertTrue(compute_metadata.is_gce()) | ||
|
|
||
| with mock.patch.object(compute_metadata, '_METADATA_SERVER', '127.0.0.1:1'): | ||
| 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( | ||
| os.environ, {'GCE_METADATA_HOST': '127.0.0.1:45678'}, clear=False): | ||
| importlib.reload(compute_metadata) | ||
| self.assertEqual('127.0.0.1:45678', compute_metadata._METADATA_SERVER) # pylint: disable=protected-access | ||
| self.assertEqual('http://127.0.0.1:45678/computeMetadata/v1/', | ||
| compute_metadata._METADATA_URL) # pylint: disable=protected-access | ||
|
|
||
| importlib.reload(compute_metadata) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, are we sure that
GCE_METADATA_HOSTis not used by ClusterFuzz? In this case it could change the value of_METADATA_SERVER.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't found any usages of the GCE_METADATA_HOST env var inside of CF:

Only the ones introduces by this changes.
And a job that defines
GCE_METADATA_HOSTwill not override it, since this resolves at import time, where as job env vars resolve afterwards.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks. I found one usage in the configuration repo (which I will not link here as it is private) that may be worth checking. Alternatively, a safer approach could be to add a log and monitor it after the deployment, with the option to delete it in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I'll add monitoring for this.
Regarding the CF-Config definition:
We shouldn't worry about that, its only usage is for the swarming bots, and that's were i got the inspiration for this :D
To not dive deep into details, Swarming bots also use a custom metadata server to provide auth credentials.