Skip to content

Commit 0ec9fee

Browse files
committed
Add tests for merge.py
Add tests for some of the important functions in merge.py. Since I wrote the tests for pytest, I switched to using pytest as the runner for the rest of the tests.
1 parent e341f28 commit 0ec9fee

File tree

4 files changed

+228
-1
lines changed

4 files changed

+228
-1
lines changed

.github/workflows/unit-tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
2323
- name: Run unit tests
2424
run: |
25-
python -m unittest openshift_metrics/tests/test_*
25+
pytest openshift_metrics/tests/
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def mock_metrics_file1():
6+
cpu_metrics = [
7+
{
8+
"metric": {
9+
"pod": "pod1",
10+
"namespace": "namespace1",
11+
"resource": "cpu",
12+
},
13+
"values": [
14+
[0, 10],
15+
[60, 15],
16+
[120, 20],
17+
],
18+
},
19+
{
20+
"metric": {
21+
"pod": "pod2",
22+
"namespace": "namespace1",
23+
"resource": "cpu",
24+
},
25+
"values": [
26+
[0, 30],
27+
[60, 35],
28+
[120, 40],
29+
],
30+
},
31+
]
32+
memory_metrics = [
33+
{
34+
"metric": {
35+
"pod": "pod1",
36+
"namespace": "namespace1",
37+
"resource": "memory",
38+
},
39+
"values": [
40+
[0, 10],
41+
[60, 15],
42+
[120, 20],
43+
],
44+
},
45+
{
46+
"metric": {
47+
"pod": "pod2",
48+
"namespace": "namespace1",
49+
"resource": "cpu",
50+
},
51+
"values": [
52+
[0, 30],
53+
[60, 35],
54+
[120, 40],
55+
],
56+
},
57+
]
58+
return {
59+
"cluster_name": "ocp-prod",
60+
"start_date": "2025-09-20",
61+
"end_date": "2025-09-20",
62+
"cpu_metrics": cpu_metrics,
63+
"memory_metrics": memory_metrics,
64+
}
65+
66+
67+
@pytest.fixture
68+
def mock_metrics_file2():
69+
cpu_metrics = [
70+
{
71+
"metric": {
72+
"pod": "pod1",
73+
"namespace": "namespace1",
74+
"resource": "cpu",
75+
},
76+
"values": [
77+
[180, 10],
78+
[240, 15],
79+
[300, 20],
80+
],
81+
},
82+
{
83+
"metric": {
84+
"pod": "pod2",
85+
"namespace": "namespace1",
86+
"resource": "cpu",
87+
},
88+
"values": [
89+
[180, 30],
90+
[240, 35],
91+
[300, 40],
92+
],
93+
},
94+
]
95+
memory_metrics = [
96+
{
97+
"metric": {
98+
"pod": "pod1",
99+
"namespace": "namespace1",
100+
"resource": "memory",
101+
},
102+
"values": [
103+
[180, 10],
104+
[240, 15],
105+
[300, 20],
106+
],
107+
},
108+
{
109+
"metric": {
110+
"pod": "pod2",
111+
"namespace": "namespace1",
112+
"resource": "cpu",
113+
},
114+
"values": [
115+
[180, 30],
116+
[240, 35],
117+
[300, 40],
118+
],
119+
},
120+
]
121+
return {
122+
"cluster_name": "ocp-prod",
123+
"start_date": "2025-09-21",
124+
"end_date": "2025-09-21",
125+
"cpu_metrics": cpu_metrics,
126+
"memory_metrics": memory_metrics,
127+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
import json
3+
from decimal import Decimal
4+
5+
from openshift_metrics.merge import (
6+
compare_dates,
7+
get_su_definitions,
8+
load_and_merge_metrics,
9+
load_metadata,
10+
)
11+
12+
13+
@pytest.mark.parametrize(
14+
"date1, date2, expected_result",
15+
[
16+
("2025-01-18", "2025-01-20", True),
17+
("2025-01-18", "2025-01-16", False),
18+
("2025-01-18", "2025-01-18", False),
19+
],
20+
)
21+
def test_compare_dates(date1, date2, expected_result):
22+
assert compare_dates(date1, date2) is expected_result
23+
24+
25+
def test_get_su_definitions(mocker):
26+
mock_rates = {
27+
"vCPUs in GPUV100 SU": Decimal("20"),
28+
"RAM in GPUV100 SU": Decimal("8192"),
29+
"GPUs in GPUV100 SU": Decimal("1"),
30+
"vCPUs in CPU SU": Decimal("5"),
31+
"RAM in CPU SU": Decimal("1024"),
32+
"GPUs in CPU SU": Decimal("0"),
33+
}
34+
mock_rates_data = mocker.MagicMock()
35+
36+
def mock_get_value_at(key, month, value_type):
37+
return mock_rates.get(key, Decimal("67"))
38+
39+
mock_rates_data.get_value_at.side_effect = mock_get_value_at
40+
mocker.patch(
41+
"openshift_metrics.merge.rates.load_from_url", return_value=mock_rates_data
42+
)
43+
report_month = "2025-10"
44+
su_definitions = get_su_definitions(report_month)
45+
46+
assert "OpenShift GPUV100" in su_definitions
47+
assert su_definitions["OpenShift GPUV100"]["vCPUs"] == Decimal("20")
48+
assert su_definitions["OpenShift GPUV100"]["RAM"] == Decimal("8192")
49+
assert su_definitions["OpenShift GPUV100"]["GPUs"] == Decimal("1")
50+
51+
assert "OpenShift CPU" in su_definitions
52+
assert su_definitions["OpenShift CPU"]["vCPUs"] == Decimal("5")
53+
assert su_definitions["OpenShift CPU"]["RAM"] == Decimal("1024")
54+
assert su_definitions["OpenShift CPU"]["GPUs"] == Decimal("0")
55+
56+
# This should get the default test value
57+
assert su_definitions["OpenShift GPUH100"]["GPUs"] == Decimal("67")
58+
59+
60+
def test_load_and_merge_data(tmp_path, mock_metrics_file1, mock_metrics_file2):
61+
"""
62+
Test that we can load metrics from the 2 files and merge the metrics from those.
63+
64+
Note that we already have tests that test the merging of the data, this mostly
65+
focuses on the loading part.
66+
"""
67+
p1 = tmp_path / "file1.json"
68+
p2 = tmp_path / "file2.json"
69+
70+
p1.write_text(json.dumps(mock_metrics_file1))
71+
p2.write_text(json.dumps(mock_metrics_file2))
72+
73+
processor = load_and_merge_metrics([p1, p2])
74+
75+
pod1_metrics = processor.merged_data["namespace1"]["pod1"]["metrics"]
76+
77+
# check values from file1.json are in the merged_data
78+
assert 60 in pod1_metrics # 60 is the epoch time stamp
79+
assert pod1_metrics[60]["cpu_request"] == 15
80+
assert pod1_metrics[60]["memory_request"] == 15
81+
82+
# check values from file2.json are in the merged_data
83+
assert 180 in pod1_metrics
84+
assert pod1_metrics[180]["cpu_request"] == 10
85+
assert pod1_metrics[180]["memory_request"] == 10
86+
87+
88+
def test_load_metadata(tmp_path, mock_metrics_file1, mock_metrics_file2):
89+
"""Test we can load metadata from the metrics files."""
90+
91+
p1 = tmp_path / "file1.json"
92+
p2 = tmp_path / "file2.json"
93+
p1.write_text(json.dumps(mock_metrics_file1))
94+
p2.write_text(json.dumps(mock_metrics_file2))
95+
metadata = load_metadata([p1, p2])
96+
assert metadata.cluster_name == "ocp-prod"
97+
assert metadata.report_start_date == "2025-09-20"
98+
assert metadata.report_end_date == "2025-09-21"

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
requests>=2.18.4
22
boto3<1.36
33
https://github.com/CCI-MOC/nerc-rates/archive/main.zip
4+
pytest
5+
pytest-mock

0 commit comments

Comments
 (0)