@@ -281,47 +281,80 @@ def task(x, y):
281
281
node_0_function_url = (
282
282
f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/function"
283
283
)
284
+ node_0_function_file_url = f"{ server_url } /files/node_0_function"
285
+ node_0_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/output"
286
+ node_0_output_file_url = f"{ server_url } /files/node_0_output"
287
+ node_0_stdout_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/stdout"
288
+ node_0_stdout_file_url = f"{ server_url } /files/node_0_stdout"
289
+ node_0_stderr_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/stderr"
290
+ node_0_stderr_file_url = f"{ server_url } /files/node_0_stderr"
284
291
285
292
hooks_file = tempfile .NamedTemporaryFile ("wb" )
286
293
hooks_file .write (ser_hooks )
287
294
hooks_file .flush ()
288
295
hooks_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/hooks"
296
+ hooks_file_url = f"{ server_url } /files/hooks"
289
297
290
298
node_1_file = tempfile .NamedTemporaryFile ("wb" )
291
299
node_1_file .write (ser_x )
292
300
node_1_file .flush ()
293
301
node_1_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/1/assets/output"
302
+ node_1_output_file_url = f"{ server_url } /node_1_output"
294
303
295
304
node_2_file = tempfile .NamedTemporaryFile ("wb" )
296
305
node_2_file .write (ser_y )
297
306
node_2_file .flush ()
298
307
node_2_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/2/assets/output"
308
+ node_2_output_file_url = f"{ server_url } /node_2_output"
299
309
300
310
task_spec = TaskSpec (
301
311
electron_id = 0 ,
302
312
args = [1 , 2 ],
303
313
kwargs = {},
304
314
)
305
315
316
+ # GET/POST URLs
317
+ url_map = {
318
+ node_0_function_url : node_0_function_file_url ,
319
+ node_0_output_url : node_0_output_file_url ,
320
+ node_0_stdout_url : node_0_stdout_file_url ,
321
+ node_0_stderr_url : node_0_stderr_file_url ,
322
+ hooks_url : hooks_file_url ,
323
+ node_1_output_url : node_1_output_file_url ,
324
+ node_2_output_url : node_2_output_file_url ,
325
+ }
326
+
327
+ # GET/PUT files
306
328
resources = {
307
- node_0_function_url : ser_task ,
308
- node_1_output_url : ser_x ,
309
- node_2_output_url : ser_y ,
310
- hooks_url : ser_hooks ,
329
+ node_0_function_file_url : ser_task ,
330
+ node_1_output_file_url : ser_x ,
331
+ node_2_output_file_url : ser_y ,
332
+ hooks_file_url : ser_hooks ,
311
333
}
312
334
313
- def mock_req_get (url , stream ):
335
+ def mock_req_get (url , ** kwargs ):
314
336
mock_resp = MagicMock ()
315
337
mock_resp .status_code = 200
316
- mock_resp .content = resources [url ]
338
+ if url in url_map :
339
+ mock_resp .json .return_value = {"remote_uri" : url_map [url ]}
340
+ else :
341
+ mock_resp .content = resources [url ]
317
342
return mock_resp
318
343
319
- def mock_req_post (url , files ):
320
- resources [url ] = files ["asset_file" ].read ()
344
+ def mock_req_post (url , ** kwargs ):
345
+ mock_resp = MagicMock ()
346
+ mock_resp .status_code = 200
347
+ mock_resp .json .return_value = {"remote_uri" : url_map [url ]}
348
+ return mock_resp
349
+
350
+ def mock_req_put (url , data = None , headers = {}, json = {}):
351
+ if data is not None :
352
+ resources [url ] = data if isinstance (data , bytes ) else data .read ()
353
+ return MagicMock ()
321
354
322
355
mocker .patch ("requests.get" , mock_req_get )
323
356
mocker .patch ("requests.post" , mock_req_post )
324
- mock_put = mocker .patch ("requests.put" )
357
+ mocker .patch ("requests.put" , mock_req_put )
325
358
task_group_metadata = {
326
359
"dispatch_id" : dispatch_id ,
327
360
"node_ids" : [node_id ],
@@ -342,8 +375,7 @@ def mock_req_post(url, files):
342
375
server_url = server_url ,
343
376
)
344
377
345
- with open (result_file .name , "rb" ) as f :
346
- output = TransportableObject .deserialize (f .read ())
378
+ output = TransportableObject .deserialize (resources [node_0_output_file_url ])
347
379
assert output .get_deserialized () == 3
348
380
349
381
with open (cb_tmpfile .name , "r" ) as f :
@@ -352,8 +384,6 @@ def mock_req_post(url, files):
352
384
with open (ca_tmpfile .name , "r" ) as f :
353
385
assert f .read () == "Bye\n "
354
386
355
- mock_put .assert_called ()
356
-
357
387
358
388
def test_run_task_group_exception (mocker ):
359
389
"""Test the wrapper submitted to local"""
@@ -398,47 +428,80 @@ def task(x, y):
398
428
node_0_function_url = (
399
429
f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/function"
400
430
)
431
+ node_0_function_file_url = f"{ server_url } /files/node_0_function"
432
+ node_0_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/output"
433
+ node_0_output_file_url = f"{ server_url } /files/node_0_output"
434
+ node_0_stdout_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/stdout"
435
+ node_0_stdout_file_url = f"{ server_url } /files/node_0_stdout"
436
+ node_0_stderr_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/stderr"
437
+ node_0_stderr_file_url = f"{ server_url } /files/node_0_stderr"
401
438
402
439
hooks_file = tempfile .NamedTemporaryFile ("wb" )
403
440
hooks_file .write (ser_hooks )
404
441
hooks_file .flush ()
405
442
hooks_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/0/assets/hooks"
443
+ hooks_file_url = f"{ server_url } /files/hooks"
406
444
407
445
node_1_file = tempfile .NamedTemporaryFile ("wb" )
408
446
node_1_file .write (ser_x )
409
447
node_1_file .flush ()
410
448
node_1_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/1/assets/output"
449
+ node_1_output_file_url = f"{ server_url } /node_1_output"
411
450
412
451
node_2_file = tempfile .NamedTemporaryFile ("wb" )
413
452
node_2_file .write (ser_y )
414
453
node_2_file .flush ()
415
454
node_2_output_url = f"{ server_url } /api/v2/dispatches/{ dispatch_id } /electrons/2/assets/output"
455
+ node_2_output_file_url = f"{ server_url } /node_2_output"
416
456
417
457
task_spec = TaskSpec (
418
458
electron_id = 0 ,
419
459
args = [1 ],
420
460
kwargs = {"y" : 2 },
421
461
)
422
462
463
+ # GET/POST URLs
464
+ url_map = {
465
+ node_0_function_url : node_0_function_file_url ,
466
+ node_0_output_url : node_0_output_file_url ,
467
+ node_0_stdout_url : node_0_stdout_file_url ,
468
+ node_0_stderr_url : node_0_stderr_file_url ,
469
+ hooks_url : hooks_file_url ,
470
+ node_1_output_url : node_1_output_file_url ,
471
+ node_2_output_url : node_2_output_file_url ,
472
+ }
473
+
474
+ # GET/PUT files
423
475
resources = {
424
- node_0_function_url : ser_task ,
425
- node_1_output_url : ser_x ,
426
- node_2_output_url : ser_y ,
427
- hooks_url : ser_hooks ,
476
+ node_0_function_file_url : ser_task ,
477
+ node_1_output_file_url : ser_x ,
478
+ node_2_output_file_url : ser_y ,
479
+ hooks_file_url : ser_hooks ,
428
480
}
429
481
430
- def mock_req_get (url , stream ):
482
+ def mock_req_get (url , ** kwargs ):
431
483
mock_resp = MagicMock ()
432
484
mock_resp .status_code = 200
433
- mock_resp .content = resources [url ]
485
+ if url in url_map :
486
+ mock_resp .json .return_value = {"remote_uri" : url_map [url ]}
487
+ else :
488
+ mock_resp .content = resources [url ]
434
489
return mock_resp
435
490
436
- def mock_req_post (url , files ):
437
- resources [url ] = files ["asset_file" ].read ()
491
+ def mock_req_post (url , ** kwargs ):
492
+ mock_resp = MagicMock ()
493
+ mock_resp .status_code = 200
494
+ mock_resp .json .return_value = {"remote_uri" : url_map [url ]}
495
+ return mock_resp
496
+
497
+ def mock_req_put (url , data = None , headers = {}, json = {}):
498
+ if data is not None :
499
+ resources [url ] = data if isinstance (data , bytes ) else data .read ()
500
+ return MagicMock ()
438
501
439
502
mocker .patch ("requests.get" , mock_req_get )
440
503
mocker .patch ("requests.post" , mock_req_post )
441
- mocker .patch ("requests.put" )
504
+ mocker .patch ("requests.put" , mock_req_put )
442
505
task_group_metadata = {
443
506
"dispatch_id" : dispatch_id ,
444
507
"node_ids" : [node_id ],
@@ -459,6 +522,9 @@ def mock_req_post(url, files):
459
522
server_url = server_url ,
460
523
)
461
524
525
+ stderr = resources [node_0_stderr_file_url ].decode ("utf-8" )
526
+ assert "AssertionError" in stderr
527
+
462
528
summary_file_path = f"{ results_dir .name } /result-{ dispatch_id } :{ node_id } .json"
463
529
464
530
with open (summary_file_path , "r" ) as f :
@@ -571,7 +637,7 @@ def test_send_internal(
571
637
run_task_group ,
572
638
list (map (lambda t : t .dict (), test_case ["task_specs" ])),
573
639
test_case ["expected_output_uris" ],
574
- "mock_cache_dir" ,
640
+ local_exec . workdir ,
575
641
test_case ["task_group_metadata" ],
576
642
test_case ["expected_server_url" ],
577
643
)
0 commit comments