Skip to content

Commit 4c69055

Browse files
[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. Pull Request: llvm#150360
1 parent 2238365 commit 4c69055

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

.ci/metrics/metrics_test.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
17+
return_value = requests.Response()
18+
return_value.status_code = 204
19+
with unittest.mock.patch(
20+
"requests.post", return_value=return_value
21+
) as post_mock:
22+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
23+
self.assertSequenceEqual(post_mock.call_args.args, [metrics.GRAFANA_URL])
24+
self.assertEqual(
25+
post_mock.call_args.kwargs["data"], "gauge_test value=5 1000"
26+
)
27+
self.assertEqual(
28+
post_mock.call_args.kwargs["auth"], ("test_userid", "test_api_key")
29+
)
30+
31+
def test_upload_job_metric(self):
32+
test_metrics = [
33+
metrics.JobMetrics("test_job", 5, 10, 1, 1000, 7, "test_workflow")
34+
]
35+
return_value = requests.Response()
36+
return_value.status_code = 204
37+
with unittest.mock.patch(
38+
"requests.post", return_value=return_value
39+
) as post_mock:
40+
metrics.upload_metrics(test_metrics, "test_userid", "test_aoi_key")
41+
self.assertEqual(
42+
post_mock.call_args.kwargs["data"],
43+
"test_job queue_time=5,run_time=10,status=1 1000",
44+
)
45+
46+
def test_upload_unknown_metric(self):
47+
@dataclass
48+
class FakeMetric:
49+
fake_data: str
50+
51+
test_metrics = [FakeMetric("test")]
52+
53+
with self.assertRaises(ValueError):
54+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
55+
56+
def test_bad_response_code(self):
57+
test_metrics = [metrics.GaugeMetric("gauge_test", 5, 1000)]
58+
return_value = requests.Response()
59+
return_value.status_code = 403
60+
# Just assert that we continue running here and do not raise anything.
61+
with unittest.mock.patch("requests.post", return_value=return_value) as _:
62+
metrics.upload_metrics(test_metrics, "test_userid", "test_api_key")
63+
64+
65+
if __name__ == "__main__":
66+
unittest.main()

0 commit comments

Comments
 (0)