1
1
"""This is to test that the reduction tasks go to the correct autoreducer node
2
2
depending on if it requires high memoery or not"""
3
3
4
- import psycopg2
5
- import requests
6
4
import time
7
- from dotenv import dotenv_values
5
+
6
+ import tests .utils .db as db_utils
8
7
9
8
10
9
class TestAutoreducerQueues :
@@ -15,111 +14,16 @@ class TestAutoreducerQueues:
15
14
IPTS = "IPTS-1234"
16
15
run_number = 12345
17
16
18
- def setup_class (cls ):
19
- config = {** dotenv_values (".env" ), ** dotenv_values (".env.ci" )}
20
- assert config
21
- cls .conn = psycopg2 .connect (
22
- database = config ["DATABASE_NAME" ],
23
- user = config ["DATABASE_USER" ],
24
- password = config ["DATABASE_PASS" ],
25
- port = config ["DATABASE_PORT" ],
26
- host = "localhost" ,
27
- )
28
- time .sleep (1 )
29
-
30
- def teardown_class (cls ):
31
- cls .conn .close ()
32
-
33
- def login (self , next , username , password ):
34
- # taken from test_RunPageView.py - consolidate as helper somewhere?
35
- URL = "http://localhost/users/login?next="
36
- client = requests .session ()
37
-
38
- # Retrieve the CSRF token first
39
- client .get (URL ) # sets the cookie
40
- csrftoken = client .cookies ["csrftoken" ]
41
-
42
- login_data = dict (username = username , password = password , csrfmiddlewaretoken = csrftoken )
43
- return client .post (URL + next , data = login_data , timeout = None )
44
-
45
- def create_test_data (self ):
46
- """create the instrument, ipts and datarun if they don't already exist
47
-
48
- returns the id for the created rundata"""
49
- conn = TestAutoreducerQueues .conn
50
- cursor = conn .cursor ()
51
-
52
- cursor .execute ("SELECT id FROM report_instrument where name = %s;" , (self .instrument ,))
53
- inst_id = cursor .fetchone ()
54
-
55
- if inst_id is None :
56
- cursor .execute ("INSERT INTO report_instrument (name) VALUES (%s);" , (self .instrument ,))
57
- cursor .execute ("SELECT id FROM report_instrument where name = %s;" , (self .instrument ,))
58
- inst_id = cursor .fetchone ()
59
- conn .commit ()
60
-
61
- cursor .execute ("SELECT id FROM report_ipts WHERE expt_name = %s;" , (self .IPTS ,))
62
- ipts_id = cursor .fetchone ()
63
- if ipts_id is None :
64
- cursor .execute (
65
- "INSERT INTO report_ipts (expt_name, created_on) VALUES (%s, %s);" ,
66
- ("IPTS-1234" , "2020-05-20 13:02:52.281964;" ),
67
- )
68
- cursor .execute ("SELECT id FROM report_ipts WHERE expt_name = %s;" , (self .IPTS ,))
69
- ipts_id = cursor .fetchone ()
70
- conn .commit ()
71
-
72
- cursor .execute (
73
- "SELECT id FROM report_datarun WHERE run_number = %s AND ipts_id_id = %s AND instrument_id_id = %s;" ,
74
- (self .run_number , ipts_id [0 ], inst_id [0 ]),
75
- )
76
- run_id = cursor .fetchone ()
77
- if run_id is None :
78
- cursor .execute (
79
- "INSERT INTO report_datarun (run_number, ipts_id_id, instrument_id_id, file, created_on) "
80
- "VALUES (%s, %s, %s, %s, %s);" ,
81
- (
82
- self .run_number ,
83
- ipts_id [0 ],
84
- inst_id [0 ],
85
- "/SNS/VULCAN/IPTS-1234/nexus/VULCAN_12345.nxs.h5" ,
86
- "2020-05-20 13:02:52.281964;" ,
87
- ),
88
- )
89
- cursor .execute (
90
- "SELECT id FROM report_datarun WHERE run_number = %s AND ipts_id_id = %s AND instrument_id_id = %s;" ,
91
- (self .run_number , ipts_id [0 ], inst_id [0 ]),
92
- )
93
- run_id = cursor .fetchone ()
94
- conn .commit ()
95
-
96
- return run_id
97
-
98
- def get_status_queue_id (self , cursor , queue_name ):
99
- """return the if for the statusqueue for the provided name"""
100
- cursor .execute ("SELECT id FROM report_statusqueue where name = %s;" , (queue_name ,))
101
- queue_id = cursor .fetchone ()
102
-
103
- if queue_id is None :
104
- cursor .execute (
105
- "INSERT INTO report_statusqueue (name, is_workflow_input) VALUES (%s, %s);" , (queue_name , False )
106
- )
107
- cursor .execute ("SELECT id FROM report_statusqueue where name = %s;" , (queue_name ,))
108
- queue_id = cursor .fetchone ()
109
-
110
- return queue_id [0 ]
111
-
112
- def set_reduction_request_queue (self , queue_name ):
17
+ def set_reduction_request_queue (self , conn , queue_name ):
113
18
"""create the task to send REDUCTION.REQUEST to the provided queue"""
114
- conn = TestAutoreducerQueues .conn
115
19
cursor = conn .cursor ()
116
20
117
21
cursor .execute ("SELECT id FROM report_instrument where name = %s;" , (self .instrument ,))
118
22
inst_id = cursor .fetchone ()[0 ]
119
23
120
- queue_id = self .get_status_queue_id (cursor , queue_name )
121
- success_queue_id = self .get_status_queue_id (cursor , "REDUCTION.COMPLETE" )
122
- reduction_request_queue_id = self .get_status_queue_id (cursor , "REDUCTION.REQUEST" )
24
+ queue_id = db_utils .get_status_queue_id (conn , queue_name )
25
+ success_queue_id = db_utils .get_status_queue_id (conn , "REDUCTION.COMPLETE" )
26
+ reduction_request_queue_id = db_utils .get_status_queue_id (conn , "REDUCTION.REQUEST" )
123
27
124
28
cursor .execute (
125
29
"SELECT id FROM report_task where instrument_id_id = %s AND input_queue_id_id = %s;" ,
@@ -153,77 +57,55 @@ def set_reduction_request_queue(self, queue_name):
153
57
)
154
58
conn .commit ()
155
59
156
- def clear_previous_runstatus (self , run_id ):
157
- """remove all previous run statuses for the given run_id"""
158
- conn = TestAutoreducerQueues .conn
159
- cursor = conn .cursor ()
160
- # delete all information entries for runstatus
161
- cursor .execute (
162
- "DELETE FROM report_information WHERE run_status_id_id IN (SELECT id FROM report_runstatus "
163
- "WHERE run_id_id = %s);" ,
164
- run_id ,
165
- )
166
- cursor .execute ("DELETE FROM report_runstatus WHERE run_id_id = %s;" , run_id )
167
- conn .commit ()
168
-
169
- def get_autoreducer_hostname (self , run_id ):
60
+ def get_autoreducer_hostname (self , conn , run_id ):
170
61
"""return the hostname that executed the task that is stored in the report information"""
171
- conn = TestAutoreducerQueues .conn
172
62
cursor = conn .cursor ()
173
- queue_id = self .get_status_queue_id (cursor , "REDUCTION.STARTED" )
63
+ queue_id = db_utils .get_status_queue_id (conn , "REDUCTION.STARTED" )
174
64
cursor .execute ("SELECT id FROM report_runstatus WHERE run_id_id = %s AND queue_id_id = %s" , (run_id , queue_id ))
175
65
runstatus_id = cursor .fetchone ()[0 ]
176
66
cursor .execute ("SELECT description FROM report_information WHERE run_status_id_id = %s" , (runstatus_id ,))
177
67
description = cursor .fetchone ()[0 ]
178
68
return description
179
69
180
- def check_run_status_exist (self , run_id , queue_name ):
181
- """return if the run status was created for the given run_id and queue_name"""
182
- conn = TestAutoreducerQueues .conn
183
- cursor = conn .cursor ()
184
- queue_id = self .get_status_queue_id (cursor , queue_name )
185
- cursor .execute ("SELECT * FROM report_runstatus WHERE run_id_id = %s AND queue_id_id = %s" , (run_id , queue_id ))
186
- return cursor .fetchone () is not None
187
-
188
- def test_normal_reduction_queue (self ):
70
+ def test_normal_reduction_queue (self , db_connection , request_page ):
189
71
# switch to the REDUCTION.DATA_READY queue and check that the task goes to the correct node
190
- run_id = self . create_test_data ( )
191
- self .clear_previous_runstatus (run_id )
72
+ run_id = db_utils . add_instrument_data_run ( db_connection , self . instrument , self . IPTS , self . run_number )
73
+ db_utils .clear_previous_runstatus (db_connection , run_id )
192
74
193
- self .set_reduction_request_queue ("REDUCTION.DATA_READY" )
75
+ self .set_reduction_request_queue (db_connection , "REDUCTION.DATA_READY" )
194
76
195
77
# login and send reduction request
196
- response = self . login ("/report/vulcan/12345/reduce/" , self .user , self .pwd )
78
+ response = request_page ("/report/vulcan/12345/reduce/" , self .user , self .pwd )
197
79
assert response .status_code == 200
198
80
assert response .url .endswith ("/report/vulcan/12345/" )
199
81
200
82
# wait for database to get updated
201
83
time .sleep (1.0 )
202
84
203
- assert self .check_run_status_exist (run_id , "REDUCTION.REQUEST" )
204
- assert self .check_run_status_exist (run_id , "REDUCTION.STARTED" )
205
- assert self .check_run_status_exist (run_id , "REDUCTION.DATA_READY" )
206
- assert not self .check_run_status_exist (run_id , "REDUCTION.HIMEM.DATA_READY" )
85
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.REQUEST" )
86
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.STARTED" )
87
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.DATA_READY" )
88
+ assert not db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.HIMEM.DATA_READY" )
207
89
208
- assert self .get_autoreducer_hostname (run_id ) == "autoreducer"
90
+ assert self .get_autoreducer_hostname (db_connection , run_id ) == "autoreducer"
209
91
210
- def test_himem_reduction_queue (self ):
92
+ def test_himem_reduction_queue (self , db_connection , request_page ):
211
93
# switch to the REDUCTION.HIMEM.DATA_READY queue and check that the task goes to the correct node
212
- run_id = self . create_test_data ( )
213
- self .clear_previous_runstatus (run_id )
94
+ run_id = db_utils . add_instrument_data_run ( db_connection , self . instrument , self . IPTS , self . run_number )
95
+ db_utils .clear_previous_runstatus (db_connection , run_id )
214
96
215
- self .set_reduction_request_queue ("REDUCTION.HIMEM.DATA_READY" )
97
+ self .set_reduction_request_queue (db_connection , "REDUCTION.HIMEM.DATA_READY" )
216
98
# login and send reduction request
217
- response = self . login ("/report/vulcan/12345/reduce/" , self .user , self .pwd )
99
+ response = request_page ("/report/vulcan/12345/reduce/" , self .user , self .pwd )
218
100
assert response .status_code == 200
219
101
assert response .url .endswith ("/report/vulcan/12345/" )
220
102
221
103
# wait for database to get updated
222
104
time .sleep (1.0 )
223
105
224
- assert self .check_run_status_exist (run_id , "REDUCTION.REQUEST" )
225
- assert self .check_run_status_exist (run_id , "REDUCTION.STARTED" )
226
- assert not self .check_run_status_exist (run_id , "REDUCTION.DATA_READY" )
227
- assert self .check_run_status_exist (run_id , "REDUCTION.HIMEM.DATA_READY" )
106
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.REQUEST" )
107
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.STARTED" )
108
+ assert not db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.DATA_READY" )
109
+ assert db_utils .check_run_status_exist (db_connection , run_id , "REDUCTION.HIMEM.DATA_READY" )
228
110
229
- assert self .get_autoreducer_hostname (run_id ) == "autoreducer.himem"
111
+ assert self .get_autoreducer_hostname (db_connection , run_id ) == "autoreducer.himem"
0 commit comments