Skip to content

Commit 87571e9

Browse files
committed
Bring tests
1 parent 46887c7 commit 87571e9

File tree

4 files changed

+90
-31
lines changed

4 files changed

+90
-31
lines changed

egi_notebooks_accounting/config-tests.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ installation_id=test-site
1919
# add every flavor to be reported as follows
2020
flava=id_flava
2121

22+
[d4science]
23+
host=example.com

egi_notebooks_accounting/d4science.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import json
5959
import logging
6060
import os
61-
from datetime import timezone
6261

6362
import escapism
6463
import requests
@@ -115,31 +114,6 @@ def push_records(self, context, records):
115114
logging.debug(f"Response: {response.text}")
116115
response.raise_for_status()
117116

118-
def update_pod_metric(self, pod, metrics, period_start, period_end):
119-
if not pod.flavor or pod.flavor not in self.flavor_config:
120-
# cannot report
121-
logging.debug(f"Flavor {pod.flavor} does not have a configured metric")
122-
return
123-
user, group = (pod.global_user_name, pod.fqan)
124-
user_metrics = metrics.get((user, group), {})
125-
flavor_metric = self.flavor_config[pod.flavor]
126-
metrics[(user, group)] = user_metrics
127-
128-
if pod.start_time is None:
129-
report_start_time = period_start
130-
else:
131-
report_start_time = max(
132-
period_start, pod.start_time.replace(tzinfo=timezone.utc)
133-
)
134-
if pod.end_time is None:
135-
report_end_time = period_end
136-
else:
137-
report_end_time = min(period_end, pod.end_time.replace(tzinfo=timezone.utc))
138-
flavor_metric_value = user_metrics.get(flavor_metric, 0)
139-
user_metrics[flavor_metric] = (
140-
flavor_metric_value + (report_end_time - report_start_time).total_seconds()
141-
)
142-
143117
def generate_record(self, pod):
144118
# minInvocationTime
145119
# maxInvocationTime

egi_notebooks_accounting/tests/conftest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class TestHelpers:
6060
FQAN = "tsuite"
6161

6262
@staticmethod
63-
def pod(i: int, start_time: datetime, wall: float | None) -> VM:
63+
def pod(i: int, start_time: datetime, wall: float | None = None, **kwargs) -> VM:
6464
"""
6565
Insert pod into local accounting database.
6666
@@ -83,7 +83,7 @@ def pod(i: int, start_time: datetime, wall: float | None) -> VM:
8383
end_time = None
8484
# long running pod
8585
wall = 7 * 24 * 3600
86-
return VM.create(
86+
vm = VM.create(
8787
local_id=local_id,
8888
machine=f"machine{i}",
8989
local_user_id=TestHelpers.LUSER,
@@ -96,3 +96,7 @@ def pod(i: int, start_time: datetime, wall: float | None) -> VM:
9696
flavor=TestHelpers.flavor_name,
9797
cpu_duration=0.1 * wall,
9898
)
99+
for k, v in kwargs.items():
100+
setattr(vm, k, v)
101+
vm.save()
102+
return vm

egi_notebooks_accounting/tests/test_d4science.py

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""Tests for d4science pusher"""
22

3-
from unittest.mock import patch
3+
import datetime
4+
from unittest.mock import call, patch
45

56
from .. import d4science
67
from .. import recordpusher
8+
from .conftest import TestHelpers
79

810

911
def test_push_records(requests_mock, config_file):
@@ -18,7 +20,84 @@ def test_push_records(requests_mock, config_file):
1820
m_token.assert_called_with(
1921
d4science.DEFAULT_TOKEN_URL, "", "", "openid d4s-context:foo"
2022
)
23+
requests_mock
2124

2225

23-
def test_go():
24-
assert True
26+
def test_generate_day_metrics(config_file):
27+
start_time = datetime.datetime.today()
28+
# not finished pod
29+
TestHelpers.pod(0, start_time)
30+
# finished within the period
31+
TestHelpers.pod(1, start_time, 5 * 60)
32+
TestHelpers.pod(2, start_time - datetime.timedelta(minutes=2), 10 * 60)
33+
TestHelpers.pod(3, start_time + datetime.timedelta(minutes=1), 5 * 60)
34+
# finished before the period
35+
TestHelpers.pod(4, start_time - datetime.timedelta(days=1), 20 * 60 * 60)
36+
with patch.object(d4science.D4ScienceRecordPusher, "push_records") as m_push:
37+
with patch.object(d4science.D4ScienceRecordPusher, "generate_record") as m_rec:
38+
pusher = d4science.D4ScienceRecordPusher()
39+
pusher.configure(config_file)
40+
m_rec.side_effect = [{"scope": "foo"}, {"scope": "foo"}, {"scope": "bar"}]
41+
pusher.generate_day_metrics(
42+
start_time, start_time + datetime.timedelta(days=1)
43+
)
44+
assert m_push.mock_calls == [
45+
call("foo", [{"scope": "foo"}, {"scope": "foo"}]),
46+
call("bar", [{"scope": "bar"}]),
47+
]
48+
49+
50+
def test_generate_record(config_file):
51+
start_time = datetime.datetime.today()
52+
start_ts = int(start_time.timestamp() * 1000)
53+
pusher = d4science.D4ScienceRecordPusher()
54+
pusher.configure(config_file)
55+
pod_1 = TestHelpers.pod(
56+
0,
57+
start_time,
58+
100,
59+
machine="jupyter-helmi-2esaidib4d4f--rname-2d-52-53tudio-53erver-4fption",
60+
)
61+
record_1 = pusher.generate_record(pod_1)
62+
assert record_1 == {
63+
"aggregated": False,
64+
"callerQualifier": "TOKEN",
65+
"consumerId": "gtuser",
66+
"creationTime": start_ts,
67+
"duration": 100,
68+
"endTime": start_ts + 100 * 1000,
69+
"host": "example.com",
70+
"id": "00000000-0000-0000-0000-000000000000",
71+
"jobName": "00000000-0000-0000-0000-000000000000",
72+
"maxInvocationTime": 100,
73+
"minInvocationTime": 100,
74+
"operationCount": 1,
75+
"operationResult": "SUCCESS",
76+
"recordType": "JobUsageRecord",
77+
"scope": "tsuite",
78+
"serviceClass": "RStudio",
79+
"serviceName": "Jupyter",
80+
"startTime": start_ts,
81+
}
82+
pod_2 = TestHelpers.pod(1, start_time, 500)
83+
record_2 = pusher.generate_record(pod_2)
84+
assert record_2 == {
85+
"aggregated": False,
86+
"callerQualifier": "TOKEN",
87+
"consumerId": "gtuser",
88+
"creationTime": start_ts,
89+
"duration": 500,
90+
"endTime": start_ts + 500 * 1000,
91+
"host": "example.com",
92+
"id": "00000000-0000-0000-0000-000000000001",
93+
"jobName": "00000000-0000-0000-0000-000000000001",
94+
"maxInvocationTime": 500,
95+
"minInvocationTime": 500,
96+
"operationCount": 1,
97+
"operationResult": "SUCCESS",
98+
"recordType": "JobUsageRecord",
99+
"scope": "tsuite",
100+
"serviceClass": "Jupyter",
101+
"serviceName": "Jupyter",
102+
"startTime": start_ts,
103+
}

0 commit comments

Comments
 (0)