Skip to content

Commit 5a46727

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 a608b0c commit 5a46727

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-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()

.github/workflows/check-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- name: Install Python Dependencies
3232
run: |
3333
pip3 install -r .ci/all_requirements.txt
34+
pip3 install -r .ci/metrics/requirements.lock.txt
3435
pip3 install pytest==8.4.1
3536
- name: Run Tests
3637
working-directory: .ci

0 commit comments

Comments
 (0)