Skip to content

Commit e91d940

Browse files
boomanaiden154github-actions[bot]
authored andcommitted
Automerge: [CI] Add unittesting for metrics collection script
This patch adds some initial unittests for the metrics collection script. This initial patch focuses on getting the files setup and adding unittests for uploading metrics. A subsequent patch will add tests for the workflow collection which is significantly more complicated. Reviewers: Keenuts, gburgessiv, dschuff, cmtice, lnihlen Reviewed By: cmtice Pull Request: llvm/llvm-project#150360
2 parents 08b40e5 + c12dfd5 commit e91d940

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

.ci/metrics/metrics_test.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
"""Tests for metrics.py"""
5+
6+
from dataclasses import dataclass
7+
import requests
8+
import unittest
9+
import unittest.mock
10+
11+
import metrics
12+
13+
14+
class TestMetrics(unittest.TestCase):
15+
def test_upload_gauge_metric(self):
16+
"""Test that we can upload a gauge metric correctly.
17+
18+
Also verify that we pass around parameters like API keys and user IDs
19+
correctly to the HTTP POST request.
20+
"""
21+
test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
22+
return_value = requests.Response()
23+
return_value.status_code = 204
24+
with unittest.mock.patch(
25+
"requests.post", return_value=return_value
26+
) as post_mock:
27+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
28+
self.assertSequenceEqual(post_mock.call_args.args, [metrics.GRAFANA_URL])
29+
self.assertEqual(
30+
post_mock.call_args.kwargs["data"], "gauge_test value=5 1000"
31+
)
32+
self.assertEqual(
33+
post_mock.call_args.kwargs["auth"], ("test_userid", "test_api_key")
34+
)
35+
36+
def test_upload_job_metric(self):
37+
"""Test that we can upload a job metric correctly."""
38+
test_metrics = [
39+
metrics.JobMetrics("test_job", 5, 10, 1, 1000, 7, "test_workflow")
40+
]
41+
return_value = requests.Response()
42+
return_value.status_code = 204
43+
with unittest.mock.patch(
44+
"requests.post", return_value=return_value
45+
) as post_mock:
46+
metrics.upload_metrics(test_metrics, "test_userid", "test_aoi_key")
47+
self.assertEqual(
48+
post_mock.call_args.kwargs["data"],
49+
"test_job queue_time=5,run_time=10,status=1 1000",
50+
)
51+
52+
def test_upload_unknown_metric(self):
53+
"""Test we report an error if we encounter an unknown metric type."""
54+
55+
@dataclass
56+
class FakeMetric:
57+
fake_data: str
58+
59+
test_metrics = [FakeMetric("test")]
60+
61+
with self.assertRaises(ValueError):
62+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
63+
64+
def test_bad_response_code(self):
65+
"""Test that we gracefully handle HTTP response errors."""
66+
test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
67+
return_value = requests.Response()
68+
return_value.status_code = 403
69+
# Just assert that we continue running here and do not raise anything.
70+
with unittest.mock.patch("requests.post", return_value=return_value) as _:
71+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
72+
73+
74+
if __name__ == "__main__":
75+
unittest.main()

0 commit comments

Comments
 (0)