| 1 |
""" |
| 2 |
Twisted trial-based tests for TAP and UWS. |
| 3 |
Tests not requiring the TAP renderer go to taptest.py |
| 4 |
|
| 5 |
The real big integration tap tests use the HTTP interface and are in |
| 6 |
an external resource package taptest. |
| 7 |
""" |
| 8 |
|
| 9 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 10 |
#c |
| 11 |
#c This program is free software, covered by the GNU GPL. See the |
| 12 |
#c COPYING file in the source distribution. |
| 13 |
|
| 14 |
|
| 15 |
import atexit |
| 16 |
import datetime |
| 17 |
import io |
| 18 |
import re |
| 19 |
import os |
| 20 |
import time |
| 21 |
|
| 22 |
from twisted.internet import reactor |
| 23 |
from twisted.python import threadable |
| 24 |
threadable.init() |
| 25 |
|
| 26 |
from gavo.helpers import trialhelpers |
| 27 |
|
| 28 |
from gavo import base |
| 29 |
from gavo import rscdesc #noflake: for registration |
| 30 |
from gavo import utils |
| 31 |
from gavo.protocols import scs #noflake: for table's q3c mixin |
| 32 |
from gavo.protocols import tap |
| 33 |
from gavo.protocols import uws |
| 34 |
|
| 35 |
base.DEBUG = True |
| 36 |
|
| 37 |
_B = "__system__/tap/run/" |
| 38 |
|
| 39 |
|
| 40 |
votWithOldGeom = b'eJxdUctugzAQvOcrVj72YAfUEwIkClRCigIiNNdqC26DRDCxHUj69bVDUKNcPLPa2dmH/X1eRW+b\nFEYuVSv6gDjUJSu4HLteBeSg9eAxNk0TbUeBtOeamRQzVfjVcTZadbjyy3SXf5RxaujNzuB7lm4S\nyJKAKIWfnahRG38CKCVeVfvLA/JCoEGN+jqYoD6gJNDjkT9XnOsmIINQlJ9M0Lc6IA3/MXQuVLr2\nIqWliIWQjaKFUK0tdBO6x+7MXQKXWYnNqfOKPNtWduYk3cVlVlRZvg2zuNzB0hHEN+DApT5LDjXv\nNZc+e1SvfHbbzppEVbQsPXMAvyotWJKEyzRwa+HS9doB59WCz0zaypnVm/ffw7S743JN9nBhdv+z\n8A+9NIXB\n' |
| 41 |
votWithDALIGeom = b'eJxdkcFugzAMhu88hZUHSFq2UwVIDJiEVI0KWK9TBpmGRBOWBGj39HNK0ape8tvJ599xEhyLOn7Z\nZzAJbTolQ7KlT8SD86mXJiTf1g47xuZ5pt2kOJXCMjxiWMU/e8EmR0deUGZV8V4mGYZXO9TXPNun\nkKchMYZ/9KrhFv0JcK35xXS/IiQ+gZZbbi8DJq0a0ZGA5CfxWDM2bUgGZaj4wWThjW12sbFaJUrp\n1tCDMp2j/ZQeeT8KND8v5KA6ad0t06xKyvxQ58VblCdlBWsHUF/AB6HtqAU0QlqhA3ZPewG7zuNM\n4jpex1xigKAunbggjXy62Wxh++wkYLjhAOYIXP+rsMFN1xdjd6/Ibv8Sed4fDoV9KQ==\n' |
| 42 |
|
| 43 |
|
| 44 |
class TAPRenderTest(trialhelpers.ArchiveTest): |
| 45 |
pass |
| 46 |
|
| 47 |
|
| 48 |
class SyncMetaTest(TAPRenderTest): |
| 49 |
"""tests for non-doQuery sync queries. |
| 50 |
""" |
| 51 |
def testVersionRejected(self): |
| 52 |
"""requests with bad version are rejected. |
| 53 |
""" |
| 54 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 55 |
"REQUEST": "queryData", "VERSION": "2.1", "LAnG": "ADQL", |
| 56 |
"QUeRY": "ignored"}, |
| 57 |
['<INFO name="QUERY_STATUS" value="ERROR">', 'Version mismatch']) |
| 58 |
|
| 59 |
def testNoSyncPaths(self): |
| 60 |
"""segments below sync are 404. |
| 61 |
""" |
| 62 |
return trialhelpers.runQuery(self.renderer, "GET", f"{_B}sync/foo", {} |
| 63 |
).addCallback(lambda res: ddt #noflake: test instrumentation |
| 64 |
).addErrback(lambda res: None) |
| 65 |
|
| 66 |
def testCapabilities(self): |
| 67 |
"""simple get capabilities response looks ok. |
| 68 |
""" |
| 69 |
return self.assertGETHasStrings(f"{_B}sync", |
| 70 |
{"REQUEST": "getCapabilities"}, [ |
| 71 |
'<capability standardID="ivo://ivoa.net/std/TAP', |
| 72 |
'ParamHTTP">']) |
| 73 |
|
| 74 |
def testTAPRootIsInfo(self): |
| 75 |
return self.assertGETHasStrings(f"{_B}", {}, [ |
| 76 |
">ivo://x-testing/tap<", |
| 77 |
"The Unittest Suite's TAP end point."]) |
| 78 |
|
| 79 |
def testAvailability(self): |
| 80 |
return self.assertGETHasStrings(f"{_B}availability", {}, [ |
| 81 |
"<avl:available>true</avl:available>"]) |
| 82 |
|
| 83 |
def testCapabilitiesPath(self): |
| 84 |
return self.assertGETHasStrings(f"{_B}capabilities", {}, [ |
| 85 |
'<capability standardID="ivo://ivoa.net/std/TAP"']) |
| 86 |
|
| 87 |
def testTables(self): |
| 88 |
return self.assertGETHasStrings(f"{_B}tables", {}, [ |
| 89 |
"<description> Unittest Suite's Table Access Protocol (TAP) service"]) |
| 90 |
|
| 91 |
def testExamples(self): |
| 92 |
return self.assertGETHasStrings(f"{_B}examples", {}, [ |
| 93 |
'<body vocab="http://www.ivoa.net/rdf/examples#"', |
| 94 |
'<div id="exampleslist"></div>']) |
| 95 |
|
| 96 |
|
| 97 |
class SyncQueryTest(TAPRenderTest): |
| 98 |
"""tests for querying sync queries. |
| 99 |
""" |
| 100 |
aVOTable = os.path.join(base.getConfig("inputsdir"), |
| 101 |
"data/vizier_votable.vot") |
| 102 |
|
| 103 |
def testNoLangRejected(self): |
| 104 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 105 |
"QUERY": 'SELECT alpha FROM test.adql WHERE alpha<3' |
| 106 |
}, [ |
| 107 |
"<INFO", "Required parameter 'lang' missing.</INFO>"]) |
| 108 |
|
| 109 |
def testBadLangRejected(self): |
| 110 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 111 |
"LANG": "Furz", |
| 112 |
"QUERY": 'SELECT alpha FROM test.adql WHERE alpha<3' |
| 113 |
}, [ |
| 114 |
'<INFO name="QUERY_STATUS" value="ERROR">This service does' |
| 115 |
' not support the query language Furz']) |
| 116 |
|
| 117 |
def testSimpleQuery(self): |
| 118 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 119 |
"REQUEST": "doQuery", |
| 120 |
"LANG": "ADQL", |
| 121 |
"FORMAT": "text/xml", |
| 122 |
"QUERY": 'SELECT alpha, NULL as n FROM test.adql WHERE alpha<200' |
| 123 |
}, [ |
| 124 |
'<RESOURCE type="results"', |
| 125 |
'ucd="pos.eq.ra;meta.main"', |
| 126 |
'ID="alpha"', |
| 127 |
'unit="deg"', |
| 128 |
'name="alpha"', |
| 129 |
'<FIELD ID="n" datatype="float" name="n"/>', |
| 130 |
'<TD>2.0</TD>', |
| 131 |
'<TD>NaN</TD>']) |
| 132 |
|
| 133 |
def testNameError(self): |
| 134 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 135 |
"REQUEST": "doQuery", |
| 136 |
"LANG": "ADQL", |
| 137 |
"QUERY": 'SELECT doesnotexist FROM test.adql' |
| 138 |
}, [ |
| 139 |
'INFO name="QUERY_STATUS" value="ERROR">Field query:' |
| 140 |
" No such field known: column 'doesnotexist' could not" |
| 141 |
' be located in table metadata' |
| 142 |
]) |
| 143 |
|
| 144 |
def testMultipleRequest(self): |
| 145 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 146 |
"REQUEST": ["doQuery", "doQuery"], |
| 147 |
"LANG": "ADQL", |
| 148 |
"QUERY": 'SELECT alpha FROM test.adql WHERE alpha<2' |
| 149 |
}, [ |
| 150 |
'<RESOURCE type="results"', |
| 151 |
'ucd="pos.eq.ra;meta.main"', |
| 152 |
'ID="alpha"', |
| 153 |
'unit="deg"', |
| 154 |
'name="alpha"']) |
| 155 |
|
| 156 |
def testOverflowAndCast(self): |
| 157 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 158 |
"LANG": "ADQL", |
| 159 |
"MAXREC": "1", |
| 160 |
"QUERY": 'SELECT cast(alpha as national char(3)) as foo FROM test.adql', |
| 161 |
"FORMAT": "votable/td", |
| 162 |
}, [ |
| 163 |
'<INFO name="QUERY_STATUS" value="OVERFLOW"', |
| 164 |
'datatype="unicodeChar"', 'arraysize="*"', |
| 165 |
'sample RA -- *TAINTED*', |
| 166 |
'<TD>2 </TD>']) |
| 167 |
|
| 168 |
def testNoOverflowWithTOP(self): |
| 169 |
return self.assertGETLacksStrings(f"{_B}sync", { |
| 170 |
"REQUEST": "doQuery", |
| 171 |
"LANG": "ADQL", |
| 172 |
"MAXREC": "1", |
| 173 |
"QUERY": 'SELECT TOP 1 alpha FROM test.adql' |
| 174 |
}, [ |
| 175 |
'value="OVERFLOW"', ]) |
| 176 |
|
| 177 |
def testMAXREC0(self): |
| 178 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 179 |
"REQUEST": "doQuery", |
| 180 |
"LANG": "ADQL", |
| 181 |
"MAXREC": "0", |
| 182 |
"QUERY": 'SELECT alpha FROM test.adql' |
| 183 |
}, [ |
| 184 |
'Query successful', |
| 185 |
'<COOSYS', |
| 186 |
'datatype="float"']) |
| 187 |
|
| 188 |
def testBadFormat(self): |
| 189 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 190 |
"REQUEST": "doQuery", |
| 191 |
"LANG": "ADQL", |
| 192 |
"QUERY": 'SELECT alpha FROM test.adql WHERE alpha<2', |
| 193 |
"RESPONSEFORMAT": 'xls' |
| 194 |
}, [ |
| 195 |
'<INFO name="QUERY_STATUS" value="ERROR">Field responseformat:' |
| 196 |
" Value 'xls' is not consistent with legal values"]) |
| 197 |
|
| 198 |
def testClearVOT(self): |
| 199 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 200 |
"REQUEST": "doQuery", |
| 201 |
"LANG": "ADQL", |
| 202 |
"QUERY": 'SELECT CIRCLE(\'ICRS\', alpha, delta+1e-10, 10)' |
| 203 |
' FROM test.adql WHERE alpha<3', |
| 204 |
"FORMAT": "votable/td" |
| 205 |
}, [ |
| 206 |
'<TABLEDATA><TR><TD>2.0', '14.0', '10.0', |
| 207 |
'xtype="circle"']) |
| 208 |
|
| 209 |
def testCSV(self): |
| 210 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 211 |
"REQUEST": "doQuery", |
| 212 |
"LANG": "ADQL", |
| 213 |
"QUERY": 'SELECT alpha,delta FROM test.adql WHERE alpha<3', |
| 214 |
"FORMAT": "text/csv" |
| 215 |
}, [ |
| 216 |
'2.0,14.0']) |
| 217 |
|
| 218 |
def testTSV(self): |
| 219 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 220 |
"REQUEST": "doQuery", |
| 221 |
"LANG": "ADQL", |
| 222 |
"QUERY": 'SELECT alpha, delta FROM test.adql WHERE alpha<3', |
| 223 |
"FORMAT": "TSV" |
| 224 |
}, [ |
| 225 |
'2.0\t14.0']) |
| 226 |
|
| 227 |
def testJSON(self): |
| 228 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 229 |
"REQUEST": "doQuery", |
| 230 |
"LANG": "ADQL", |
| 231 |
"QUERY": "SELECT TOP 3 column_name, datatype FROM TAP_SCHEMA.columns" |
| 232 |
" WHERE table_name='tap_schema.tables' ORDER BY column_name", |
| 233 |
"RESPONSEFORMAT": "application/json", |
| 234 |
"MAXREC": "1" |
| 235 |
}, [ |
| 236 |
'"contains": "table"', |
| 237 |
'"description": "ADQL datatype"', |
| 238 |
# DaCHS buglet: maxrec=1 returns two rows. |
| 239 |
'["description", "unicodeChar"], ["nrows", "long"]', |
| 240 |
'"queryStatus": "OVERFLOW"']) |
| 241 |
|
| 242 |
def testBin2Table(self): |
| 243 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 244 |
"REQUEST": "doQuery", |
| 245 |
"LANG": "ADQL", |
| 246 |
"QUERY": 'SELECT alpha, delta FROM test.adql WHERE alpha<3', |
| 247 |
"FORMAT": "application/x-votable+xml;serialization=BINARY2" |
| 248 |
}, [ |
| 249 |
'BINARY2', 'AEAAAABBYAAA']) |
| 250 |
|
| 251 |
def testFITSTable(self): |
| 252 |
return self.assertGETHasStrings(f"{_B}sync", { |
| 253 |
"REQUEST": "doQuery", |
| 254 |
"LANG": "ADQL", |
| 255 |
"QUERY": 'SELECT alpha, delta FROM test.adql WHERE alpha<3', |
| 256 |
"RESPONSEFORMAT": "fits", |
| 257 |
"MAXREC": "0", |
| 258 |
}, [ |
| 259 |
'SIMPLE =', |
| 260 |
"TYPE1 = 'alpha '", |
| 261 |
"TCOMM1 = 'A sample RA'"]) |
| 262 |
|
| 263 |
def testBadUploadSyntax(self): |
| 264 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 265 |
"REQUEST": "doQuery", |
| 266 |
"UPLOAD": "bar", |
| 267 |
"LANG": "ADQL", |
| 268 |
"QUERY": 'SELECT * FROM test.adql' |
| 269 |
}, [ |
| 270 |
"only allow regular SQL identifiers"]) |
| 271 |
|
| 272 |
def testBadUploadSyntax2(self): |
| 273 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 274 |
"REQUEST": "doQuery", |
| 275 |
"UPLOAD": "bar,http://x.y;", |
| 276 |
"LANG": "ADQL", |
| 277 |
"QUERY": 'SELECT * FROM test.adql'}, [ |
| 278 |
"only allow regular SQL identifiers" |
| 279 |
]) |
| 280 |
|
| 281 |
def testNonExistingUpload(self): |
| 282 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 283 |
"REQUEST": "doQuery", |
| 284 |
"UPLOAD": "bar,http://127.0.0.1:65000", |
| 285 |
"LANG": "ADQL", |
| 286 |
"QUERY": 'SELECT * FROM test.adql' |
| 287 |
}, [ |
| 288 |
"'http://127.0.0.1:65000' cannot be retrieved</INFO", |
| 289 |
"Connection refused"]) |
| 290 |
|
| 291 |
def testUploadCannotReadLocalFile(self): |
| 292 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 293 |
"REQUEST": "doQuery", |
| 294 |
"UPLOAD": "bar,file:///etc/passwd", |
| 295 |
"LANG": "ADQL", |
| 296 |
"QUERY": 'SELECT * FROM test.adql'}, [ |
| 297 |
"'file:///etc/passwd' cannot be retrieved</INFO", |
| 298 |
"unknown url type" |
| 299 |
]).addErrback(lambda failure: None) |
| 300 |
|
| 301 |
def testMalformedUploadURL(self): |
| 302 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 303 |
"REQUEST": "doQuery", |
| 304 |
"UPLOAD": "http://fit://file://x.ab", |
| 305 |
"LANG": "ADQL", |
| 306 |
"QUERY": 'SELECT * FROM test.adql'}, [ |
| 307 |
'<INFO name="QUERY_STATUS" value="ERROR">Field UPLOAD:' |
| 308 |
' Syntax error in UPLOAD parameter' |
| 309 |
]) |
| 310 |
|
| 311 |
def testInlineUploadFromArgsWorks(self): |
| 312 |
with open(self.aVOTable, "rb") as f: |
| 313 |
uploadFile = trialhelpers.FakeFile("../Hägar/klause", f.read()) |
| 314 |
|
| 315 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 316 |
"REQUEST": "doQuery", |
| 317 |
"UPLOAD": "bar,param:HoNk", |
| 318 |
"LANG": "ADQL", |
| 319 |
"QUERY": 'SELECT * FROM tap_upload.bar join test.adql on' |
| 320 |
' (alpha-"_RAJ2000"<0)', |
| 321 |
"HoNk": uploadFile, |
| 322 |
}, [ |
| 323 |
'xmlns="http://www.ivoa.net/xml/VOTable/', |
| 324 |
'ucd="pos.eq.ra;meta.main"', |
| 325 |
'encoding="base64"']) |
| 326 |
|
| 327 |
def testSensibleErrorOnMalformedUpload(self): |
| 328 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 329 |
"REQUEST": "doQuery", |
| 330 |
"UPLOAD": "bar,param:HoNk", |
| 331 |
"LANG": "ADQL", |
| 332 |
"QUERY": 'SELECT * FROM tap_upload.bar join test.adql on' |
| 333 |
' (alpha-"_RAJ2000"<0)', |
| 334 |
"HoNk": trialhelpers.FakeFile("../Hägar/klause", |
| 335 |
b"""<VOTABLE><RESOURCE><TABLE> |
| 336 |
<FIELD name="x" datatype="double"/><DATA><TABLEDATA> |
| 337 |
<TR><TD>13</TD></TR><TR><TD>break here</TD></TR> |
| 338 |
</TABLEDATA></DATA></TABLE></RESOURCE></VOTABLE>"""), |
| 339 |
}, [ |
| 340 |
"While ingesting upload bar: Invalid literal for double (field x):" |
| 341 |
" 'break here'</INFO>"]) |
| 342 |
|
| 343 |
def testMissingInlineParameter(self): |
| 344 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 345 |
"REQUEST": "doQuery", |
| 346 |
"UPLOAD": "bar,param:HoNk", |
| 347 |
"LANG": "ADQL", |
| 348 |
"QUERY": 'SELECT top 1 * FROM tap_upload.bar', |
| 349 |
"MoNk": ("upl", open(self.aVOTable, "rb")), |
| 350 |
}, [ |
| 351 |
'<INFO name="QUERY_STATUS" value="ERROR">' |
| 352 |
"Field UPLOAD: No inline upload 'HoNk' found"]) |
| 353 |
|
| 354 |
def testWithUploadOldGeom(self): |
| 355 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 356 |
"REQUEST": "doQuery", |
| 357 |
"UPLOAD": "t1,param:HoNk", |
| 358 |
"LANG": "ADQL", |
| 359 |
"FORMAT": "votable/td", |
| 360 |
"QUERY": "SELECT ssa_location," |
| 361 |
" POINT('ICRS', COORD1(ssa_location), COORD2(ssa_location)) AS np" |
| 362 |
" FROM test.adql join tap_upload.t1" |
| 363 |
" ON 1=contains(ssa_location, circle('', alpha, delta, 0.002))", |
| 364 |
"HoNk": trialhelpers.FakeFile( |
| 365 |
"upl", utils.getDirtyBytes(votWithOldGeom)) |
| 366 |
}, [ |
| 367 |
"<TD>Position UNKNOWNFrame 2.0009", |
| 368 |
"14.00100", |
| 369 |
"<TD>2.00", " 14.00"]) |
| 370 |
|
| 371 |
def testWithUploadGeom(self): |
| 372 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 373 |
"REQUEST": "doQuery", |
| 374 |
"UPLOAD": "t1,param:HoNk", |
| 375 |
"LANG": "ADQL", |
| 376 |
"FORMAT": "votable/td", |
| 377 |
"QUERY": 'SELECT * FROM test.adql join tap_upload.t1' |
| 378 |
" on 1=contains(ssa_location, circle('', alpha, delta, 0.002))", |
| 379 |
"HoNk": trialhelpers.FakeFile("upl", |
| 380 |
utils.getDirtyBytes(votWithDALIGeom)), |
| 381 |
}, ["<TD>2.00", " 14.00"]) |
| 382 |
|
| 383 |
def testArraySerialisation(self): |
| 384 |
return self.assertPOSTHasStrings(f"{_B}sync", { |
| 385 |
"REQUEST": "doQuery", |
| 386 |
"LANG": "ADQL", |
| 387 |
"QUERY": 'SELECT gavo_histogram(alpha, 0, 360, 4) as h FROM test.adql'}, [ |
| 388 |
'AAAABgAAAAAAAAACAAAAAAAAAAAAAAABAAAAAA==', |
| 389 |
'arraysize="*" datatype="int" name="h"']) |
| 390 |
|
| 391 |
|
| 392 |
class SimpleAsyncTest(TAPRenderTest): |
| 393 |
"""tests for some non-ADQL async queries. |
| 394 |
""" |
| 395 |
def testAsyncDirRedirects(self): |
| 396 |
def assertLocation(result): |
| 397 |
self.assertTrue(result[1].getLocationValue().endswith(f"{_B}async")) |
| 398 |
return result |
| 399 |
|
| 400 |
return self.assertStatus(f"{_B}async/", 303 |
| 401 |
).addCallback(assertLocation) |
| 402 |
|
| 403 |
def testVersionRejected(self): |
| 404 |
return self.assertPOSTHasStrings(f"{_B}async", { |
| 405 |
"REQUEST": "doQuery", |
| 406 |
"LANG": "ADQL", |
| 407 |
"VERSION": "0.1"}, |
| 408 |
['<INFO name="QUERY_STATUS" value="ERROR">', 'Version mismatch; this']) |
| 409 |
|
| 410 |
def testJobList(self): |
| 411 |
return self.assertGETHasStrings(f"{_B}async", {}, [ |
| 412 |
'<uws:jobs ', |
| 413 |
' xmlns:uws="http://www.ivoa.net/xml/UWS/v1.0" ', |
| 414 |
' version="1.1" ', |
| 415 |
]) |
| 416 |
|
| 417 |
def testNonExistingPhase(self): |
| 418 |
return self.assertGETHasStrings(f"{_B}async/23/phase", {}, |
| 419 |
['<VOTABLE ', 'ERROR">UWS job \'23\' could not']) |
| 420 |
|
| 421 |
def testLifecycle(self): |
| 422 |
"""tests job creation, redirection, phase, and deletion. |
| 423 |
""" |
| 424 |
# This one is too huge and much too slow for a unit test. Still |
| 425 |
# I want at least one integration-type test in here since the |
| 426 |
# big test probably won't be run at every commit. |
| 427 |
def assertDeleted(result, jobId): |
| 428 |
self.assertEqual(result[1].code, 303) |
| 429 |
next = result[1].getLocationValue() |
| 430 |
self.assertTrue(next.endswith(f"{_B}async"), |
| 431 |
"Deletion redirect doesn't point to job list but to %s"%next) |
| 432 |
return self.assertGETLacksStrings(next, {}, ['jobref id="%s"'%jobId] |
| 433 |
).addCallback(lambda res: reactor.disconnectAll()) |
| 434 |
|
| 435 |
def delete(ignored, jobId): |
| 436 |
return trialhelpers.runQuery(self.renderer, "DELETE", |
| 437 |
f"{_B}async/"+jobId, {} |
| 438 |
).addCallback(assertDeleted, jobId) |
| 439 |
|
| 440 |
def checkResult(ignored, jobId): |
| 441 |
return self.assertGETHasStrings( |
| 442 |
f"{_B}async/%s/results/result"%jobId, {}, [ |
| 443 |
'name="sql_query"', |
| 444 |
'QAAAAA=='] |
| 445 |
).addCallback(delete, jobId) |
| 446 |
|
| 447 |
def waitForCompletion(result, jobId): |
| 448 |
if b"EXECUTING" in result[0]: |
| 449 |
# wait for completion once more |
| 450 |
return self.assertGETHasStrings(f"{_B}async/%s"%jobId, {"WAIT": "10"}, |
| 451 |
["COMPLETED"] |
| 452 |
).addCallback(checkResult, jobId) |
| 453 |
else: |
| 454 |
self.assertTrue(b"COMPLETED" in result[0], |
| 455 |
"'%s' doesn't have COMPLETED in it") |
| 456 |
return checkResult(result, jobId) |
| 457 |
|
| 458 |
def assertStarted(lastRes, jobId): |
| 459 |
# lastRes must be a redirect to the job info page |
| 460 |
req = lastRes[1] |
| 461 |
self.assertEqual(req.code, 303) |
| 462 |
self.assertEqual(req.getLocationValue(), |
| 463 |
"http://localhost:8080/__system__/tap/run/async/"+jobId) |
| 464 |
return self.assertGETHasStrings(f"{_B}async/%s"%jobId, {"WAIT": "10"}, |
| 465 |
[""]).addCallback(waitForCompletion, jobId) |
| 466 |
|
| 467 |
def promote(ignored, jobId): |
| 468 |
return trialhelpers.runQuery(self.renderer, "POST", |
| 469 |
f"{_B}async/%s/phase"%jobId, {"PHASE": "RUN"} |
| 470 |
).addCallback(assertStarted, jobId) |
| 471 |
|
| 472 |
def checkPlan(ignored, jobId): |
| 473 |
return self.assertGETHasStrings(f"{_B}async/%s/plan"%jobId, {}, |
| 474 |
["Seq Scan on adql", "alpha < '3'::double"] |
| 475 |
).addCallback(promote, jobId) |
| 476 |
|
| 477 |
def checkQuote(ingored, jobId): |
| 478 |
return self.assertGETHasStrings(f"{_B}async/%s/quote"%jobId, {}, |
| 479 |
['-'] |
| 480 |
).addCallback(checkPlan, jobId) |
| 481 |
|
| 482 |
def checkPhase(ignored, jobId): |
| 483 |
return self.assertGETHasStrings(f"{_B}async/%s/phase"%jobId, {}, |
| 484 |
['PENDING'] |
| 485 |
).addCallback(checkQuote, jobId) |
| 486 |
|
| 487 |
def checkJobInfo(jobId): |
| 488 |
return self.assertGETHasStrings(f"{_B}async/%s"%jobId, {}, [ |
| 489 |
'<uws:job ', |
| 490 |
'<uws:ownerId xsi:nil="true"', |
| 491 |
'<uws:startTime xsi:nil="true"', |
| 492 |
'<uws:parameter id="maxrec"'] |
| 493 |
).addCallback(checkPhase, jobId) |
| 494 |
|
| 495 |
def checkPosted(result): |
| 496 |
# jobId is in location of result[1] |
| 497 |
request = result[1] |
| 498 |
self.assertEqual(request.code, 303) |
| 499 |
next = request.getLocationValue() |
| 500 |
self.assertFalse(f"{_B}async" not in next) |
| 501 |
jobId = next.split("/")[-1] |
| 502 |
return checkJobInfo(jobId) |
| 503 |
|
| 504 |
def fail(failure): |
| 505 |
raise failure.value |
| 506 |
|
| 507 |
return trialhelpers.runQuery(self.renderer, "POST", f"{_B}async", { |
| 508 |
"LANG": "ADQL", |
| 509 |
"maxrec": "33", |
| 510 |
"QUERY": "SELECT alpha FROM test.adql WHERE alpha<3"} |
| 511 |
).addCallback(checkPosted |
| 512 |
).addErrback(fail) |
| 513 |
|
| 514 |
def testBadConstructionArgument(self): |
| 515 |
def checkPosted(result): |
| 516 |
request = result[1] |
| 517 |
self.assertEqual(request.code, 400) |
| 518 |
with base.getTableConn() as conn: |
| 519 |
nJobsPost = list( |
| 520 |
conn.query("SELECT count(*) from tap_schema.tapjobs"))[0][0] |
| 521 |
self.assertEqual(nJobsPost, nJobsPre, |
| 522 |
"Abortive job creation created a job?") |
| 523 |
|
| 524 |
with base.getTableConn() as conn: |
| 525 |
nJobsPre = list( |
| 526 |
conn.query("SELECT count(*) from tap_schema.tapjobs"))[0][0] |
| 527 |
|
| 528 |
return self.assertPOSTHasStrings(f"{_B}async", { |
| 529 |
"LANG": "ADQL", |
| 530 |
"MAXREC": "kaputt", |
| 531 |
"QUERY": "SELECT ra FROM test.adql WHERE ra<3"}, |
| 532 |
["<VOTABLE", 'value="ERROR"', "'kaputt' is not a valid literal"] |
| 533 |
).addCallback(checkPosted) |
| 534 |
|
| 535 |
def testJoblistLAST(self): |
| 536 |
def assertMaxOneResult(result): |
| 537 |
self.assertTrue('xmlns:uws="http://www.ivoa.net/xml/UWS/v1.0', |
| 538 |
result[0]) |
| 539 |
nJobs = len(re.findall(b'<uws.jobref ', result[0])) |
| 540 |
self.assertTrue(nJobs<2, "LAST=1 returns %s jobrefs"%nJobs) |
| 541 |
self.assertEqual(result[1].code, 200) |
| 542 |
|
| 543 |
return trialhelpers.runQuery(self.renderer, "GET", f"{_B}async", { |
| 544 |
"LAST": "1"} |
| 545 |
).addCallback(assertMaxOneResult) |
| 546 |
|
| 547 |
def testJoblistAFTER(self): |
| 548 |
def assertNoResult(result): |
| 549 |
self.assertEqual(result[1].code, 200) |
| 550 |
self.assertTrue('xmlns:uws="http://www.ivoa.net/xml/UWS/v1.0', |
| 551 |
result[0]) |
| 552 |
nJobs = len(re.findall(b'<uws.jobref ', result[0])) |
| 553 |
self.assertEqual(nJobs, 0) |
| 554 |
|
| 555 |
return trialhelpers.runQuery(self.renderer, "GET", f"{_B}async", { |
| 556 |
"AFTER": (datetime.datetime.utcnow() |
| 557 |
+datetime.timedelta(seconds=1)).isoformat()} |
| 558 |
).addCallback(assertNoResult) |
| 559 |
|
| 560 |
def testJoblistPHASE(self): |
| 561 |
def assertMaxOneResult(result): |
| 562 |
self.assertEqual(result[1].code, 200) |
| 563 |
self.assertTrue(b'xmlns:uws="http://www.ivoa.net/xml/UWS/v1.0' in |
| 564 |
result[0]) |
| 565 |
nJobs = len(re.findall(b'<uws.jobref ', result[0])) |
| 566 |
self.assertTrue(nJobs==0, "PHASE=ABORTED returns a jobref?") |
| 567 |
|
| 568 |
return trialhelpers.runQuery(self.renderer, "GET", f"{_B}async", { |
| 569 |
"PHASE": 'ABORTED'} |
| 570 |
).addCallback(assertMaxOneResult) |
| 571 |
|
| 572 |
def testDestroyRunning(self): |
| 573 |
def assertDeleted(result, jobId, pid, wd): |
| 574 |
|
| 575 |
self.assertRaises(uws.JobNotFound, |
| 576 |
tap.WORKER_SYSTEM.getJob, |
| 577 |
(jobId,)) |
| 578 |
|
| 579 |
retries = 0 |
| 580 |
while True: |
| 581 |
try: |
| 582 |
self.assertEqual(os.waitpid(pid, os.WNOHANG), (0,0)) |
| 583 |
except AssertionError: |
| 584 |
retries += 1 |
| 585 |
if retries>5: |
| 586 |
raise |
| 587 |
time.sleep(0.05) |
| 588 |
except ChildProcessError: |
| 589 |
# I don't think these should come with WNOHANG, but... well |
| 590 |
# there they are, and since we assert the child isn't there, |
| 591 |
# I'm happy with them, too. |
| 592 |
break |
| 593 |
else: |
| 594 |
break |
| 595 |
return result |
| 596 |
|
| 597 |
def destroy(result, jobId): |
| 598 |
job = tap.WORKER_SYSTEM.getJob(jobId) |
| 599 |
self.assertEqual(job.phase, "EXECUTING") |
| 600 |
wd = job.getWD() |
| 601 |
pid = job.pid |
| 602 |
|
| 603 |
return trialhelpers.runQuery(self.renderer, "DELETE", |
| 604 |
f"/tap/async/{jobId}", {}, |
| 605 |
).addCallback(assertDeleted, jobId, pid, wd |
| 606 |
).addCallback(lambda res: reactor.disconnectAll()) |
| 607 |
|
| 608 |
def waitUntilRuns(result, jobId): |
| 609 |
return self.assertGETHasStrings(f"tap/async/{jobId}", {"WAIT": "10"}, |
| 610 |
['"query">JUST HANG']).addCallback(destroy, jobId) |
| 611 |
|
| 612 |
def startJob(result): |
| 613 |
jobURL = result[1].getLocationValue() |
| 614 |
self.assertTrue(re.search("tap/run/async", jobURL)) |
| 615 |
jobId = jobURL.split("/")[-1] |
| 616 |
return self.assertPOSTHasStrings(f"/tap/async/{jobId}/phase", |
| 617 |
{"PHASE": "RUN"}, |
| 618 |
["Unittest Suite -- See Other"] |
| 619 |
).addCallback(waitUntilRuns, jobId) |
| 620 |
|
| 621 |
return self.assertPOSTHasStrings("tap/async", { |
| 622 |
"LANG": "ADQL", |
| 623 |
"QUERY": "JUST HANG around"}, |
| 624 |
["Unittest Suite -- See Other"] |
| 625 |
).addCallback(startJob) |
| 626 |
|
| 627 |
|
| 628 |
class PersistentUploadTest(TAPRenderTest): |
| 629 |
with open( |
| 630 |
base.getConfig("inputsDir") / "data" / "vizier_votable.vot", |
| 631 |
"rb") as f: |
| 632 |
someVOT = io.BytesIO(f.read()) |
| 633 |
|
| 634 |
def assert404Status(self, res): |
| 635 |
self.assertEqual(res[1].code, 404) |
| 636 |
return res |
| 637 |
|
| 638 |
def testNoTable(self): |
| 639 |
return self.assertGETHasStrings("tap/user_tables", {}, [ |
| 640 |
'name="QUERY_STATUS"', |
| 641 |
"No persistent tables listing for anonymous users.<",]) |
| 642 |
|
| 643 |
def testTablesListing(self): |
| 644 |
return self.assertGETHasStrings("tap/user_tables", {}, [ |
| 645 |
"<vtm:tableset", |
| 646 |
'xmlns:vtm="http://www.ivoa.net/xml/VOSITables/v1.0"', |
| 647 |
"<name>tap_user</name>", |
| 648 |
"A schema containing users' uploads"], rm=trialhelpers.addCreds) |
| 649 |
|
| 650 |
def testBadTableName(self): |
| 651 |
return self.assertGETHasStrings("tap/user_tables/666", {}, [ |
| 652 |
'name="QUERY_STATUS"', |
| 653 |
'>You must give a valid upload table name here<'] |
| 654 |
).addCallback(self.assert404Status) |
| 655 |
|
| 656 |
def testNoMediaType(self): |
| 657 |
def makePUT(request): |
| 658 |
request.setPayload(self.someVOT, "application/octet-stream") |
| 659 |
request.method = "PUT" |
| 660 |
|
| 661 |
return self.assertPOSTHasStrings( |
| 662 |
f"{_B}user_tables/test_upload", {}, [ |
| 663 |
"Do not know how to ingest application/octet-stream tables<", |
| 664 |
"if in doubt, use application/x-votable+xml" |
| 665 |
], rm=makePUT) |
| 666 |
|
| 667 |
def testSuccessfulUpload(self): |
| 668 |
|
| 669 |
def makePUT(request): |
| 670 |
request.setPayload(self.someVOT, base.votableType) |
| 671 |
request.method = b"PUT" |
| 672 |
|
| 673 |
def makeDELETE(request): |
| 674 |
request.method = b"DELETE" |
| 675 |
|
| 676 |
def assertCleanedUp(result): |
| 677 |
self.someVOT.seek(0) |
| 678 |
|
| 679 |
self.assertEqual(result[0], b'Dropped user table test_upload\n') |
| 680 |
self.assertEqual(result[1].code, 200) |
| 681 |
|
| 682 |
with base.getTableConn() as conn: |
| 683 |
self.assertEqual(list(conn.query( |
| 684 |
"select user_name, table_name" |
| 685 |
" from tap_user.tables where user_name='anonymous' and" |
| 686 |
" table_name='test_upload'")), []) |
| 687 |
self.assertEqual(base.UnmanagedQuerier(conn).getTableType( |
| 688 |
"tap_user._anonymous_test_upload"), None) |
| 689 |
return result |
| 690 |
|
| 691 |
def tryDELETE(result): |
| 692 |
return self.assertGETHasStrings( |
| 693 |
f"{_B}user_tables/test_upload", {}, [ |
| 694 |
"Dropped user table test_upload"], rm=makeDELETE |
| 695 |
).addCallback(assertCleanedUp) |
| 696 |
|
| 697 |
def tryIndexJobDelete(result, jobURL): |
| 698 |
return self.assertGETHasStrings( |
| 699 |
jobURL, {}, [ |
| 700 |
"This is supposed to redirect to http://localhost:8080/__system__/tap_user/create-index/async"], |
| 701 |
rm=makeDELETE).addCallback(tryDELETE) |
| 702 |
|
| 703 |
def ensureIndexing(result): |
| 704 |
self.assertTrue(result[1].getLocationValue().startswith( |
| 705 |
"http://localhost:8080/__system__/tap_user/create-index")) |
| 706 |
|
| 707 |
time.sleep(0.5) # give the job time to go to EXECUTING |
| 708 |
jobURL = result[1].getLocationValue().split(":8080")[-1][1:] |
| 709 |
return self.assertGETHasStrings(jobURL, {"WAIT": ["10"]}, |
| 710 |
["COMPLETED"]).addCallback(tryIndexJobDelete, jobURL) |
| 711 |
|
| 712 |
def tryIndexing(result): |
| 713 |
return self.assertPOSTHasStrings( |
| 714 |
f"{_B}user_tables/test_upload/index", {"INDEX": ["Color"]}, [ |
| 715 |
"This is supposed to redirect to http://localhost:8080", |
| 716 |
]).addCallback(ensureIndexing) |
| 717 |
|
| 718 |
def assertExpiry(result, expectedExpiry): |
| 719 |
with base.getTableConn() as conn: |
| 720 |
foundExpiry = list(conn.query("SELECT expiry" |
| 721 |
" from tap_user.tables where table_name='test_upload'"))[0][0] |
| 722 |
self.assertEqual(foundExpiry, expectedExpiry) |
| 723 |
|
| 724 |
return self.assertGETHasStrings( |
| 725 |
f"{_B}user_tables/test_upload/index", {}, [ |
| 726 |
"Indexes on table tap_user.test_upload", |
| 727 |
"(none)",]).addCallback(tryIndexing) |
| 728 |
|
| 729 |
def changeDestructionTimeSoon(result): |
| 730 |
soon = datetime.datetime.utcnow().replace(microsecond=0 |
| 731 |
)+datetime.timedelta(seconds=3600) |
| 732 |
return self.assertPOSTHasStrings( |
| 733 |
f"{_B}user_tables/test_upload/destruction", { |
| 734 |
"DESTRUCTION": [utils.formatISODT(soon)]}, [ |
| 735 |
utils.formatISODT(soon)] |
| 736 |
).addCallback(assertExpiry, soon) |
| 737 |
|
| 738 |
def changeDestructionTimeLimited(result): |
| 739 |
inAYear = utils.formatISODT( |
| 740 |
datetime.datetime.now()+datetime.timedelta(days=366))[:8] |
| 741 |
return self.assertPOSTHasStrings( |
| 742 |
f"{_B}user_tables/test_upload/destruction", { |
| 743 |
"DESTRUCTION": ["2048-01-01T00:00:00"]}, [ |
| 744 |
inAYear] |
| 745 |
).addCallback(changeDestructionTimeSoon) |
| 746 |
|
| 747 |
def tryGET(result): |
| 748 |
self.assertEqual(result[1].code, 200) |
| 749 |
with base.getTableConn() as conn: |
| 750 |
self.assertEqual( |
| 751 |
list(conn.query("select color from tap_user._anonymous_test_upload" |
| 752 |
" where tyc2='1265-01176-1'")), |
| 753 |
[(0.25,)]) |
| 754 |
return self.assertGETHasStrings( |
| 755 |
f"{_B}user_tables/test_upload", {}, [ |
| 756 |
"<name>tap_user.test_upload</name>", |
| 757 |
"<column><name>tyc2</name>"] |
| 758 |
).addCallback(changeDestructionTimeLimited) |
| 759 |
|
| 760 |
return self.assertPOSTHasStrings( |
| 761 |
f"{_B}user_tables/test_upload", {}, [ |
| 762 |
"Query this table as tap_user.test_upload", |
| 763 |
], rm=makePUT |
| 764 |
).addCallback(tryGET) |
| 765 |
|
| 766 |
def testCreateAs(self): |
| 767 |
def makeDELETE(request): |
| 768 |
request.method = "DELETE" |
| 769 |
|
| 770 |
def cleanup(res): |
| 771 |
return self.assertGETHasStrings(f"{_B}user_tables/fromquery", {}, [ |
| 772 |
"Dropped user table fromquery"], rm=makeDELETE) |
| 773 |
|
| 774 |
def assertSoon(res): |
| 775 |
print(datetime.datetime.fromisoformat(res[0])) |
| 776 |
return res |
| 777 |
|
| 778 |
def tryGETDestruction(res): |
| 779 |
return self.assertGETHasStrings( |
| 780 |
f"{_B}user_tables/fromquery/destruction", {}, [ |
| 781 |
"-"]).addCallback(assertSoon) |
| 782 |
|
| 783 |
def tryQuery(res): |
| 784 |
return self.assertGETHasStrings( |
| 785 |
f"{_B}sync", {"LANG": "ADQL", |
| 786 |
"QUERY": "select * from tap_user.fromquery"}, [ |
| 787 |
'<STREAM encoding="base64">QAAAAA==</STREAM>']) |
| 788 |
|
| 789 |
return self.assertGETHasStrings( |
| 790 |
f"{_B}sync", {"LANG": "ADQL", |
| 791 |
"QUERY": "CREATE TABLE tap_user.fromquery AS" |
| 792 |
" SELECT alpha FROM test.adql WHERE alpha<3"}, [ |
| 793 |
'<DESCRIPTION>A sample RA</DESCRIPTION>', # metadata present |
| 794 |
'<STREAM encoding="base64"></STREAM>', # empty VOTable |
| 795 |
]).addCallback(tryQuery |
| 796 |
).addCallback(tryGETDestruction |
| 797 |
).addBoth(cleanup) |
| 798 |
|
| 799 |
def testNoReservedWords(self): |
| 800 |
def makePUT(request): |
| 801 |
request.setPayload(self.someVOT, base.votableType) |
| 802 |
request.method = "PUT" |
| 803 |
|
| 804 |
return self.assertPOSTHasStrings( |
| 805 |
f"{_B}user_tables/table", {}, [ |
| 806 |
'value="ERROR"', |
| 807 |
"'table' cannot be used as an upload table name (which must be" |
| 808 |
" regular ADQL identifiers, in particular not ADQL reserved words)." |
| 809 |
], rm=makePUT) |
| 810 |
|
| 811 |
|
| 812 |
atexit.register(trialhelpers.provideRDData("test", "ADQLTest")) |