Skip to content

Commit 271f32c

Browse files
committed
Fix tests
Also update the headers for the report by classes, not that we specifically need it for that report, but it's just good to keep it consistent.
1 parent f76b2d2 commit 271f32c

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

openshift_metrics/tests/test_utils.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def test_write_metrics_log(self):
109109

110110

111111
class TestWriteMetricsByNamespace(TestCase):
112+
def setUp(self) -> None:
113+
self.report_metadata = invoice.ReportMetadata(
114+
report_month="2023-01",
115+
cluster_name="test-cluster",
116+
report_start_time=datetime(2023, 1, 1, tzinfo=UTC),
117+
report_end_time=datetime(2023, 1, 3, tzinfo=UTC),
118+
generated_at=datetime(2023, 1, 5, tzinfo=UTC),
119+
)
120+
112121
def test_write_metrics_log(self):
113122
test_metrics_dict = {
114123
"namespace1": {
@@ -180,21 +189,20 @@ def test_write_metrics_log(self):
180189
}
181190

182191
expected_output = (
183-
"Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost\n"
184-
"2023-01,namespace1,namespace1,,test-cluster,,,,,1128,OpenShift CPU,0.013,14.66\n"
185-
"2023-01,namespace2,namespace2,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25\n"
186-
"2023-01,namespace2,namespace2,,test-cluster,,,,,48,OpenShift GPUA100,1.803,86.54\n"
187-
"2023-01,namespace2,namespace2,,test-cluster,,,,,48,OpenShift GPUA100SXM4,2.078,99.74\n"
192+
"Invoice Month,Report Start Time,Report End Time,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost,Generated At\n"
193+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace1,namespace1,,test-cluster,,,,,1128,OpenShift CPU,0.013,14.66,2023-01-0500:00:00Z\n"
194+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2,namespace2,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25,2023-01-0500:00:00Z\n"
195+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2,namespace2,,test-cluster,,,,,48,OpenShift GPUA100,1.803,86.54,2023-01-0500:00:00Z\n"
196+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2,namespace2,,test-cluster,,,,,48,OpenShift GPUA100SXM4,2.078,99.74,2023-01-0500:00:00Z\n"
188197
)
189198

190199
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
191200
utils.write_metrics_by_namespace(
192201
condensed_metrics_dict=test_metrics_dict,
193202
file_name=tmp.name,
194-
report_month="2023-01",
203+
report_metadata=self.report_metadata,
195204
rates=RATES,
196205
su_definitions=SU_DEFINITIONS,
197-
cluster_name="test-cluster",
198206
)
199207
self.assertEqual(tmp.read(), expected_output)
200208

@@ -229,24 +237,32 @@ def test_write_metrics_for_vms(self):
229237
}
230238

231239
expected_output = (
232-
"Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost\n"
233-
"2023-01,namespace1,namespace1,,test-cluster,,,,,24,OpenShift GPUA100SXM4,2.078,49.87\n"
234-
"2023-01,namespace1,namespace1,,test-cluster,,,,,24,OpenShift GPUH100,6.04,144.96\n"
240+
"Invoice Month,Report Start Time,Report End Time,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost,Generated At\n"
241+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace1,namespace1,,test-cluster,,,,,24,OpenShift GPUA100SXM4,2.078,49.87,2023-01-0500:00:00Z\n"
242+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace1,namespace1,,test-cluster,,,,,24,OpenShift GPUH100,6.04,144.96,2023-01-0500:00:00Z\n"
235243
)
236244

237245
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
238246
utils.write_metrics_by_namespace(
239247
condensed_metrics_dict=test_metrics_dict,
240248
file_name=tmp.name,
241-
report_month="2023-01",
249+
report_metadata=self.report_metadata,
242250
rates=RATES,
243251
su_definitions=SU_DEFINITIONS,
244-
cluster_name="test-cluster",
245252
)
246253
self.assertEqual(tmp.read(), expected_output)
247254

248255

249256
class TestWriteMetricsByClasses(TestCase):
257+
def setUp(self) -> None:
258+
self.report_metadata = invoice.ReportMetadata(
259+
report_month="2023-01",
260+
cluster_name="test-cluster",
261+
report_start_time=datetime(2023, 1, 1, tzinfo=UTC),
262+
report_end_time=datetime(2023, 1, 3, tzinfo=UTC),
263+
generated_at=datetime(2023, 1, 5, tzinfo=UTC),
264+
)
265+
250266
def test_write_metrics_log(self):
251267
test_metrics_dict = {
252268
"namespace1": { # namespace is ignored entirely from the report
@@ -321,22 +337,21 @@ def test_write_metrics_log(self):
321337
}
322338

323339
expected_output = (
324-
"Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost\n"
325-
"2023-01,namespace2:noclass,namespace2:noclass,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25\n"
326-
"2023-01,namespace2:math-201,namespace2:math-201,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25\n"
327-
"2023-01,namespace2:math-201,namespace2:math-201,,test-cluster,,,,,24,OpenShift GPUA100,1.803,43.27\n"
328-
"2023-01,namespace2:cs-101,namespace2:cs-101,,test-cluster,,,,,48,OpenShift GPUA100SXM4,2.078,99.74\n"
340+
"Invoice Month,Report Start Time,Report End Time,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost,Generated At\n"
341+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2:noclass,namespace2:noclass,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25,2023-01-0500:00:00Z\n"
342+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2:math-201,namespace2:math-201,,test-cluster,,,,,96,OpenShift CPU,0.013,1.25,2023-01-0500:00:00Z\n"
343+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2:math-201,namespace2:math-201,,test-cluster,,,,,24,OpenShift GPUA100,1.803,43.27,2023-01-0500:00:00Z\n"
344+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2:cs-101,namespace2:cs-101,,test-cluster,,,,,48,OpenShift GPUA100SXM4,2.078,99.74,2023-01-0500:00:00Z\n"
329345
)
330346

331347
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
332348
utils.write_metrics_by_classes(
333349
condensed_metrics_dict=test_metrics_dict,
334350
file_name=tmp.name,
335-
report_month="2023-01",
351+
report_metadata=self.report_metadata,
336352
rates=RATES,
337353
su_definitions=SU_DEFINITIONS,
338354
namespaces_with_classes=["namespace2"],
339-
cluster_name="test-cluster",
340355
)
341356
self.assertEqual(tmp.read(), expected_output)
342357

@@ -370,18 +385,17 @@ def test_write_metrics_by_namespace_decimal(self):
370385
self.assertEqual(cost, 0.45)
371386

372387
expected_output = (
373-
"Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost\n"
374-
"2023-01,namespace1,namespace1,,test-cluster,,,,,35,OpenShift CPU,0.013,0.46\n"
388+
"Invoice Month,Report Start Time,Report End Time,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost,Generated At\n"
389+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace1,namespace1,,test-cluster,,,,,35,OpenShift CPU,0.013,0.46,2023-01-0500:00:00Z\n"
375390
)
376391

377392
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
378393
utils.write_metrics_by_namespace(
379394
condensed_metrics_dict=test_metrics_dict,
380395
file_name=tmp.name,
381-
report_month="2023-01",
396+
report_metadata=self.report_metadata,
382397
su_definitions=SU_DEFINITIONS,
383398
rates=RATES,
384-
cluster_name="test-cluster",
385399
)
386400
self.assertEqual(tmp.read(), expected_output)
387401

@@ -447,22 +461,29 @@ def setUp(self):
447461
},
448462
}
449463

464+
self.report_metadata = invoice.ReportMetadata(
465+
report_month="2023-01",
466+
cluster_name="test-cluster",
467+
report_start_time=datetime(2023, 1, 1, tzinfo=UTC),
468+
report_end_time=datetime(2023, 1, 3, tzinfo=UTC),
469+
generated_at=datetime(2023, 1, 5, tzinfo=UTC),
470+
)
471+
450472
def test_write_metrics_by_namespace_with_ignore_hours(self):
451473
expected_output = (
452-
"Invoice Month,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost\n"
453-
"2023-01,namespace1,namespace1,,test-cluster,,,,,12,OpenShift CPU,0.013,0.16\n"
454-
"2023-01,namespace2,namespace2,,test-cluster,,,,,170,OpenShift CPU,0.013,2.21\n"
455-
"2023-01,namespace2,namespace2,,test-cluster,,,,,37,OpenShift GPUA100SXM4,2.078,76.89\n"
474+
"Invoice Month,Report Start Time,Report End Time,Project - Allocation,Project - Allocation ID,Manager (PI),Cluster Name,Invoice Email,Invoice Address,Institution,Institution - Specific Code,SU Hours (GBhr or SUhr),SU Type,Rate,Cost,Generated At\n"
475+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace1,namespace1,,test-cluster,,,,,12,OpenShift CPU,0.013,0.16,2023-01-0500:00:00Z\n"
476+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2,namespace2,,test-cluster,,,,,170,OpenShift CPU,0.013,2.21,2023-01-0500:00:00Z\n"
477+
"2023-01,2023-01-0100:00:00Z,2023-01-0300:00:00Z,namespace2,namespace2,,test-cluster,,,,,37,OpenShift GPUA100SXM4,2.078,76.89,2023-01-0500:00:00Z\n"
456478
)
457479

458480
with tempfile.NamedTemporaryFile(mode="w+") as tmp:
459481
utils.write_metrics_by_namespace(
460482
condensed_metrics_dict=self.test_metrics_dict,
461483
file_name=tmp.name,
462-
report_month="2023-01",
484+
report_metadata=self.report_metadata,
463485
rates=RATES,
464486
su_definitions=SU_DEFINITIONS,
465-
cluster_name="test-cluster",
466487
ignore_hours=self.ignore_times,
467488
)
468489
self.assertEqual(tmp.read(), expected_output)

openshift_metrics/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ def write_metrics_by_classes(
196196
rows = []
197197
headers = [
198198
"Invoice Month",
199+
"Report Start Time",
200+
"Report End Time",
199201
"Project - Allocation",
200202
"Project - Allocation ID",
201203
"Manager (PI)",
@@ -208,6 +210,7 @@ def write_metrics_by_classes(
208210
"SU Type",
209211
"Rate",
210212
"Cost",
213+
"Generated At",
211214
]
212215

213216
rows.append(headers)

0 commit comments

Comments
 (0)