-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[CI] Add unittesting for metrics collection script #150360
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
boomanaiden154
merged 3 commits into
main
from
users/boomanaiden154/ci-add-unittesting-for-metrics-collection-script
Jul 24, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
"""Tests for metrics.py""" | ||
|
||
from dataclasses import dataclass | ||
import requests | ||
import unittest | ||
import unittest.mock | ||
|
||
import metrics | ||
|
||
|
||
class TestMetrics(unittest.TestCase): | ||
def test_upload_gauge_metric(self): | ||
"""Test that we can upload a gauge metric correctly. | ||
Also verify that we pass around parameters like API keys and user IDs | ||
correctly to the HTTP POST request. | ||
""" | ||
test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)] | ||
return_value = requests.Response() | ||
return_value.status_code = 204 | ||
with unittest.mock.patch( | ||
"requests.post", return_value=return_value | ||
) as post_mock: | ||
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key") | ||
self.assertSequenceEqual(post_mock.call_args.args, [metrics.GRAFANA_URL]) | ||
self.assertEqual( | ||
post_mock.call_args.kwargs["data"], "gauge_test value=5 1000" | ||
) | ||
self.assertEqual( | ||
post_mock.call_args.kwargs["auth"], ("test_userid", "test_api_key") | ||
) | ||
|
||
def test_upload_job_metric(self): | ||
"""Test that we can upload a job metric correctly.""" | ||
test_metrics = [ | ||
metrics.JobMetrics("test_job", 5, 10, 1, 1000, 7, "test_workflow") | ||
] | ||
return_value = requests.Response() | ||
return_value.status_code = 204 | ||
with unittest.mock.patch( | ||
"requests.post", return_value=return_value | ||
) as post_mock: | ||
metrics.upload_metrics(test_metrics, "test_userid", "test_aoi_key") | ||
self.assertEqual( | ||
post_mock.call_args.kwargs["data"], | ||
"test_job queue_time=5,run_time=10,status=1 1000", | ||
) | ||
|
||
def test_upload_unknown_metric(self): | ||
"""Test we report an error if we encounter an unknown metric type.""" | ||
|
||
@dataclass | ||
class FakeMetric: | ||
fake_data: str | ||
|
||
test_metrics = [FakeMetric("test")] | ||
|
||
with self.assertRaises(ValueError): | ||
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key") | ||
|
||
def test_bad_response_code(self): | ||
"""Test that we gracefully handle HTTP response errors.""" | ||
test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)] | ||
return_value = requests.Response() | ||
return_value.status_code = 403 | ||
# Just assert that we continue running here and do not raise anything. | ||
with unittest.mock.patch("requests.post", return_value=return_value) as _: | ||
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.