11"""Tests for d4science pusher"""
22
3- from unittest .mock import patch
3+ import datetime
4+ from unittest .mock import call , patch
45
56from .. import d4science
67from .. import recordpusher
8+ from .conftest import TestHelpers
79
810
911def 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