| 1 |
""" |
| 2 |
Tests for generic UWS |
| 3 |
|
| 4 |
If it's TAP we're talking about, use taptest.py. |
| 5 |
""" |
| 6 |
|
| 7 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 8 |
#c |
| 9 |
#c This program is free software, covered by the GNU GPL. See the |
| 10 |
#c COPYING file in the source distribution. |
| 11 |
|
| 12 |
|
| 13 |
import datetime |
| 14 |
import queue |
| 15 |
import time |
| 16 |
import threading |
| 17 |
|
| 18 |
from gavo.helpers import testhelpers |
| 19 |
|
| 20 |
from gavo import base |
| 21 |
from gavo import rscdesc #noflake: for base.caches registration |
| 22 |
from gavo import svcs |
| 23 |
from gavo import utils |
| 24 |
from gavo.helpers import trialhelpers |
| 25 |
from gavo.protocols import dali |
| 26 |
from gavo.protocols import uws |
| 27 |
from gavo.protocols import uwsactions |
| 28 |
|
| 29 |
import tresc |
| 30 |
|
| 31 |
class _PlainTransitions(uws.UWSTransitions): |
| 32 |
def __init__(self): |
| 33 |
uws.UWSTransitions.__init__(self, "plain", [ |
| 34 |
(uws.PENDING, uws.QUEUED, "noOp"), |
| 35 |
(uws.PENDING, uws.EXECUTING, "run"), |
| 36 |
(uws.QUEUED, uws.EXECUTING, "run"), |
| 37 |
(uws.EXECUTING, uws.COMPLETED, "noOp"), |
| 38 |
(uws.QUEUED, uws.ABORTED, "noOp"), |
| 39 |
(uws.EXECUTING, uws.ABORTED, "noOp"),]) |
| 40 |
|
| 41 |
def run(self, newState, writableJob, ignored): |
| 42 |
writableJob.change(phase=uws.COMPLETED) |
| 43 |
|
| 44 |
|
| 45 |
class UWSTestJob(uws.BaseUWSJob): |
| 46 |
_jobsTDId = "data/uwstest#testjobs" |
| 47 |
_transitions = _PlainTransitions() |
| 48 |
|
| 49 |
|
| 50 |
_TEST_UWS = uws.UWS(UWSTestJob, uwsactions.JobActions()) |
| 51 |
|
| 52 |
|
| 53 |
class _TestUWSTable(tresc.RDDataResource): |
| 54 |
rdName = "data/uwstest" |
| 55 |
dataId = "import" |
| 56 |
|
| 57 |
_testUWSTable = _TestUWSTable() |
| 58 |
|
| 59 |
|
| 60 |
class UWSObjectTest(testhelpers.VerboseTest): |
| 61 |
resources = [("testUWSTable", _testUWSTable)] |
| 62 |
|
| 63 |
def testGetStatement(self): |
| 64 |
self.assertEqual(_TEST_UWS._statements["getById"][1], |
| 65 |
"SELECT jobId, phase, executionDuration, destructionTime," |
| 66 |
" owner, parameters, runId, startTime, endTime," |
| 67 |
" error, creationTime, magic" |
| 68 |
" FROM test.testjobs WHERE jobId=%(jobId)s ") |
| 69 |
|
| 70 |
def testExGetStatement(self): |
| 71 |
self.assertEqual(_TEST_UWS._statements["getByIdEx"][1], |
| 72 |
"SELECT jobId, phase, executionDuration, destructionTime," |
| 73 |
" owner, parameters, runId, startTime, endTime," |
| 74 |
" error, creationTime, magic" |
| 75 |
" FROM test.testjobs WHERE jobId=%(jobId)s FOR UPDATE ") |
| 76 |
|
| 77 |
def testFeedStatement(self): |
| 78 |
self.assertEqual(_TEST_UWS._statements["feedToIdEx"][1], |
| 79 |
'INSERT INTO test.testjobs (jobId, phase, executionDuration,' |
| 80 |
' destructionTime, owner, parameters, runId,' |
| 81 |
' startTime, endTime, error, creationTime, magic) VALUES' |
| 82 |
' (%(jobId)s, %(phase)s,' |
| 83 |
' %(executionDuration)s, %(destructionTime)s, %(owner)s,' |
| 84 |
' %(parameters)s, %(runId)s, %(startTime)s, %(endTime)s,' |
| 85 |
' %(error)s, %(creationTime)s, %(magic)s)') |
| 86 |
|
| 87 |
def testJobsTDCache(self): |
| 88 |
td1 = UWSTestJob.jobsTD |
| 89 |
td2 = UWSTestJob.jobsTD |
| 90 |
self.assertEqual(td1.columns[0].name, "jobId") |
| 91 |
self.assertTrue(td1 is td2) |
| 92 |
|
| 93 |
def testCountFunctions(self): |
| 94 |
jobId = _TEST_UWS.getNewJobId() |
| 95 |
self.assertEqual(_TEST_UWS.countQueuedJobs(), 0) |
| 96 |
_TEST_UWS.changeToPhase(jobId, uws.QUEUED) |
| 97 |
try: |
| 98 |
self.assertEqual(_TEST_UWS.countQueuedJobs(), 1) |
| 99 |
self.assertEqual(_TEST_UWS.countRunningJobs(), 0) |
| 100 |
finally: |
| 101 |
_TEST_UWS.destroy(jobId) |
| 102 |
|
| 103 |
def testNotFoundRaised(self): |
| 104 |
self.assertRaises(uws.JobNotFound, |
| 105 |
_TEST_UWS.getJob, |
| 106 |
"there's no way this id could ever exist") |
| 107 |
|
| 108 |
|
| 109 |
class TestWithUWSJob(testhelpers.VerboseTest): |
| 110 |
resources = [("testUWSTable", _testUWSTable)] |
| 111 |
|
| 112 |
def setUp(self): |
| 113 |
self.job = _TEST_UWS.getNewJob() |
| 114 |
testhelpers.VerboseTest.setUp(self) |
| 115 |
|
| 116 |
def tearDown(self): |
| 117 |
try: |
| 118 |
_TEST_UWS.destroy(self.job.jobId) |
| 119 |
except uws.JobNotFound: |
| 120 |
# tests may kill jobs themselves |
| 121 |
pass |
| 122 |
|
| 123 |
|
| 124 |
class SimpleJobsTest(TestWithUWSJob): |
| 125 |
def testNonPropertyRaises(self): |
| 126 |
self.assertRaises(AttributeError, |
| 127 |
lambda: self.job.foobar) |
| 128 |
|
| 129 |
def testCannotChangeRO(self): |
| 130 |
self.assertRaises(TypeError, |
| 131 |
self.job.change, owner="rupert") |
| 132 |
|
| 133 |
def testChangeWorks(self): |
| 134 |
self.assertEqual(self.job.owner, None) |
| 135 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 136 |
self.assertEqual(wjob.owner, None) |
| 137 |
wjob.change(owner="rupert") |
| 138 |
self.assertEqual(wjob.owner, "rupert") |
| 139 |
self.job.update() |
| 140 |
self.assertEqual(self.job.owner, "rupert") |
| 141 |
|
| 142 |
def testDefaultInPlace(self): |
| 143 |
self.assertEqual(self.job.phase, uws.PENDING) |
| 144 |
|
| 145 |
def testAssigningIsForbidden(self): |
| 146 |
def fails(): |
| 147 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 148 |
wjob.phase = uws.PENDING |
| 149 |
self.assertRaises(TypeError, fails) |
| 150 |
|
| 151 |
def testAssigningToNonexistingRaises(self): |
| 152 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 153 |
self.assertRaises(AttributeError, wjob.change, foo="bar") |
| 154 |
|
| 155 |
def testPropertyEncoding(self): |
| 156 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 157 |
wjob.change(parameters={"foo": 4}) |
| 158 |
self.job.update() |
| 159 |
self.assertEqual(self.job.parameters["foo"], 4) |
| 160 |
|
| 161 |
def testNoParameterSerInRO(self): |
| 162 |
def fails(): |
| 163 |
self.job.setPar("glob", "someString") |
| 164 |
self.assertRaises(TypeError, fails) |
| 165 |
|
| 166 |
def testNonmagicParameterSer(self): |
| 167 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 168 |
wjob.setPar("glob", "some string") |
| 169 |
self.job.update() |
| 170 |
self.assertEqual(self.job.parameters["glob"], "some string") |
| 171 |
|
| 172 |
def testCreationTime(self): |
| 173 |
timediff = self.job.creationTime-datetime.datetime.utcnow() |
| 174 |
self.assertTrue(abs(timediff.total_seconds())<2, |
| 175 |
"Job creation to test execution >2 seconds??") |
| 176 |
|
| 177 |
|
| 178 |
class PlainActionsTest(TestWithUWSJob): |
| 179 |
def testSimpleTransition(self): |
| 180 |
_TEST_UWS.changeToPhase(self.job.jobId, uws.QUEUED) |
| 181 |
self.job.update() |
| 182 |
self.assertEqual(self.job.phase, uws.QUEUED) |
| 183 |
|
| 184 |
def testTransitionWithCallback(self): |
| 185 |
_TEST_UWS.changeToPhase(self.job.jobId, uws.EXECUTING) |
| 186 |
self.job.update() |
| 187 |
self.assertEqual(self.job.phase, uws.COMPLETED) |
| 188 |
|
| 189 |
def testFailingTransition(self): |
| 190 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 191 |
wjob.change(phase=uws.EXECUTING) |
| 192 |
self.assertRaises(base.ValidationError, |
| 193 |
wjob.getTransitionTo, |
| 194 |
uws.QUEUED) |
| 195 |
|
| 196 |
def testFailingGoesToError(self): |
| 197 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 198 |
wjob.change(phase=uws.EXECUTING) |
| 199 |
_TEST_UWS.changeToPhase(self.job.jobId, uws.QUEUED) |
| 200 |
self.job.update() |
| 201 |
self.assertEqual(self.job.phase, uws.ERROR) |
| 202 |
|
| 203 |
def testNoEndstateActions(self): |
| 204 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 205 |
wjob.change(phase=uws.COMPLETED) |
| 206 |
_TEST_UWS.changeToPhase(self.job.jobId, uws.ERROR) |
| 207 |
self.job.update() |
| 208 |
self.assertEqual(self.job.phase, uws.COMPLETED) |
| 209 |
|
| 210 |
def testNullActionIgnored(self): |
| 211 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 212 |
wjob.change(phase=uws.QUEUED) |
| 213 |
_TEST_UWS.changeToPhase(self.job.jobId, uws.QUEUED) |
| 214 |
self.job.update() |
| 215 |
self.assertEqual(self.job.phase, uws.QUEUED) |
| 216 |
|
| 217 |
|
| 218 |
class JobHandlingTest(TestWithUWSJob): |
| 219 |
def testCleanupLeaves(self): |
| 220 |
_TEST_UWS.cleanupJobsTable() |
| 221 |
self.assertTrue(self.job.jobId in _TEST_UWS.getJobIds()) |
| 222 |
|
| 223 |
def testCleanupCatchesExpired(self): |
| 224 |
with _TEST_UWS.changeableJob(self.job.jobId) as wjob: |
| 225 |
wjob.change(destructionTime= |
| 226 |
datetime.datetime.utcnow()-datetime.timedelta(seconds=20)) |
| 227 |
_TEST_UWS.cleanupJobsTable() |
| 228 |
self.assertFalse(self.job.jobId in _TEST_UWS.getJobIds()) |
| 229 |
|
| 230 |
def testCleanupWithArgs(self): |
| 231 |
_TEST_UWS.cleanupJobsTable(includeAll=True) |
| 232 |
self.assertFalse(self.job.jobId in _TEST_UWS.getJobIds()) |
| 233 |
|
| 234 |
def testLocking(self): |
| 235 |
q = queue.Queue() |
| 236 |
def blockingJob(): |
| 237 |
# this is started in a thread while self.jobId is held |
| 238 |
q.put("Child started") |
| 239 |
with _TEST_UWS.changeableJob(self.job.jobId): |
| 240 |
q.put("Job created") |
| 241 |
|
| 242 |
with _TEST_UWS.changeableJob(self.job.jobId): |
| 243 |
child = threading.Thread(target=blockingJob) |
| 244 |
child.start() |
| 245 |
# see that child process has started but could not create the job |
| 246 |
self.assertEqual(q.get(True, 1), "Child started") |
| 247 |
# make sure we time out on waiting for another sign of the child -- |
| 248 |
# it should be blocking. |
| 249 |
self.assertRaises(queue.Empty, q.get, True, 0.05) |
| 250 |
# we've closed our handle on job, now child can run |
| 251 |
self.assertEqual(q.get(True, 1), "Job created") |
| 252 |
|
| 253 |
def testTimesOut(self): |
| 254 |
def timesOut(jobId): |
| 255 |
with _TEST_UWS.changeableJob(jobId, timeout=0.001): |
| 256 |
self.fail("No lock happened???") |
| 257 |
|
| 258 |
with _TEST_UWS.changeableJob(self.job.jobId): |
| 259 |
self.assertRaisesWithMsg(base.ReportableError, |
| 260 |
"Could not access the jobs table. This probably means there" |
| 261 |
" is a stale lock on it. Please notify the service operators.", |
| 262 |
timesOut, |
| 263 |
(self.job.jobId,)) |
| 264 |
|
| 265 |
|
| 266 |
def _makeUWSRequest(inArgs, inMethod="GET"): |
| 267 |
"""returns a FakeRequest prepared the way the UWS renderers prepare |
| 268 |
normal twisted requests. |
| 269 |
""" |
| 270 |
res = trialhelpers.FakeRequest(args=inArgs) |
| 271 |
res.uwsArgs = uws._getUWSGrammar().parseStrargs(inArgs) |
| 272 |
res.method = utils.bytify(inMethod) |
| 273 |
dali.mangleUploads(res) |
| 274 |
return res |
| 275 |
|
| 276 |
|
| 277 |
class UserUWSTest(testhelpers.VerboseTest): |
| 278 |
def testBasicJob(self): |
| 279 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 280 |
job = worker.getNewJob() |
| 281 |
try: |
| 282 |
self.assertEqual(job.jobClass, "data/cores#pc") |
| 283 |
with job.getWritable() as wjob: |
| 284 |
wjob.setPar("opre", 2.5) |
| 285 |
wjob.setPar("powers", [1,2,3]) |
| 286 |
job.update() |
| 287 |
self.assertEqual(job.parameters["opre"], 2.5) |
| 288 |
self.assertEqual(job.parameters["powers"], [1,2,3]) |
| 289 |
finally: |
| 290 |
worker.destroy(job.jobId) |
| 291 |
|
| 292 |
def testFloatParameter(self): |
| 293 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 294 |
jobId = worker.getNewIdFromRequest( |
| 295 |
_makeUWSRequest({"opre": ["2.5489923488e10"], "opim": ["3.5"]})) |
| 296 |
try: |
| 297 |
job = worker.getJob(jobId) |
| 298 |
self.assertEqual(job.parameters["opre"], 2.5489923488e10) |
| 299 |
finally: |
| 300 |
worker.destroy(jobId) |
| 301 |
|
| 302 |
def testArrayParameter(self): |
| 303 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 304 |
jobId = worker.getNewIdFromRequest( |
| 305 |
_makeUWSRequest({"powers": ["2 4 78"]})) |
| 306 |
try: |
| 307 |
job = worker.getJob(jobId) |
| 308 |
self.assertEqual(job.parameters["powers"], [2, 4, 78]) |
| 309 |
finally: |
| 310 |
worker.destroy(jobId) |
| 311 |
|
| 312 |
def testParametersElement(self): |
| 313 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 314 |
jobId = worker.getNewIdFromRequest(_makeUWSRequest({ |
| 315 |
"powers": ["2 4 78"], |
| 316 |
"upload": ["stuff,param:quux"], |
| 317 |
"quux": trialhelpers.FakeFile("honk.txt", "gacker")})) |
| 318 |
try: |
| 319 |
res = uwsactions.doJobAction(worker, _makeUWSRequest({}), |
| 320 |
(jobId,)) |
| 321 |
tree = testhelpers.getXMLTree(res, debug=False) |
| 322 |
|
| 323 |
self.assertEqual(len(tree.xpath("//parameter")), 5) |
| 324 |
self.assertEqual(tree.xpath("//parameter[@id='opim']")[0].text, |
| 325 |
"1.0") |
| 326 |
|
| 327 |
upEl = tree.xpath("//parameter[@id='stuff']")[0] |
| 328 |
self.assertEqual(upEl.attrib["byReference"], "True") |
| 329 |
self.assertEqual(upEl.text, |
| 330 |
utils.EqualingRE("http://localhost:8080/data/cores/" |
| 331 |
"pc/async/.*/results/stuff")) |
| 332 |
finally: |
| 333 |
worker.destroy(jobId) |
| 334 |
|
| 335 |
def testUpload(self): |
| 336 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 337 |
jobId = worker.getNewIdFromRequest( |
| 338 |
_makeUWSRequest({ |
| 339 |
"upload": ["stuff,param:quux"], |
| 340 |
"quux": trialhelpers.FakeFile("honk.txt", "gacker")})) |
| 341 |
try: |
| 342 |
job = worker.getJob(jobId) |
| 343 |
|
| 344 |
with job.openFile(job.parameters["stuff"][1]) as f: |
| 345 |
self.assertEqual(f.read(), b"gacker") |
| 346 |
|
| 347 |
try: |
| 348 |
uwsactions.doJobAction(worker, _makeUWSRequest({ |
| 349 |
"upload": ["stuff,param:foo"], |
| 350 |
"foo": trialhelpers.FakeFile("bigbig.txt", "bigbig")}, |
| 351 |
inMethod="POST"), |
| 352 |
(job.jobId, "parameters")) |
| 353 |
except svcs.SeeOther: |
| 354 |
pass |
| 355 |
|
| 356 |
with job.openFile(job.parameters["stuff"][1]) as f: |
| 357 |
self.assertEqual(f.read(), b"bigbig") |
| 358 |
finally: |
| 359 |
worker.destroy(job.jobId) |
| 360 |
|
| 361 |
def testProperClassDeserialisation(self): |
| 362 |
worker1 = base.resolveCrossId("data/cores#pc").getUWS() |
| 363 |
worker2 = base.resolveCrossId("data/cores#uc").getUWS() |
| 364 |
jobId = worker1.getNewIdFromRequest( |
| 365 |
_makeUWSRequest({"opre": ["29"], "powers": "2 3 4".split()})) |
| 366 |
try: |
| 367 |
try: |
| 368 |
worker2.getJob(jobId) |
| 369 |
except svcs.WebRedirect as ex: |
| 370 |
self.assertEqual(ex.dest, |
| 371 |
"http://localhost:8080/data/cores/pc/async/"+jobId) |
| 372 |
else: |
| 373 |
self.fail("Construction of job with wrong UWS doesn't redirect") |
| 374 |
finally: |
| 375 |
worker1.destroy(jobId) |
| 376 |
|
| 377 |
def testRunningAndDefaulting(self): |
| 378 |
worker = base.resolveCrossId("data/cores#pc").getUWS() |
| 379 |
jobId = worker.getNewIdFromRequest( |
| 380 |
_makeUWSRequest({"powers": ["2 4"], "opre": ["4"]})) |
| 381 |
try: |
| 382 |
job = worker.getJob(jobId) |
| 383 |
worker.changeToPhase(job.jobId, uws.QUEUED, timeout=0) |
| 384 |
for _ in range(100): |
| 385 |
time.sleep(0.3) |
| 386 |
if job.update().phase==uws.COMPLETED: |
| 387 |
break |
| 388 |
else: |
| 389 |
raise AssertionError( |
| 390 |
"useruws test job didn't complete within 30 seconds.") |
| 391 |
|
| 392 |
with job.openFile("result") as f: |
| 393 |
res = f.read() |
| 394 |
|
| 395 |
# that's the info with opim, which is defaulted and hence |
| 396 |
# must come from the context grammar. |
| 397 |
self.assertTrue(b"1.0</INFO" in res) |
| 398 |
# when this breaks, add proper VOTable parsing here |
| 399 |
self.assertTrue(b"QXAAAEEAAABANVNeQyEAAENwAABAtVNe" in res) |
| 400 |
finally: |
| 401 |
worker.destroy(jobId) |
| 402 |
|
| 403 |
|
| 404 |
if __name__=="__main__": |
| 405 |
testhelpers.main(JobHandlingTest) |