forked from hail-is/hail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_batch.py
1426 lines (1151 loc) · 50.1 KB
/
test_batch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import inspect
import secrets
import unittest
import pytest
import os
import subprocess as sp
import tempfile
from shlex import quote as shq
import uuid
import re
import orjson
import hailtop.fs as hfs
import hailtop.batch_client.client as bc
from hailtop import pip_version
from hailtop.batch import Batch, ServiceBackend, LocalBackend, ResourceGroup
from hailtop.batch.resource import JobResourceFile
from hailtop.batch.exceptions import BatchException
from hailtop.batch.globals import arg_max
from hailtop.utils import grouped, async_to_blocking
from hailtop.config import get_remote_tmpdir, configuration_of
from hailtop.batch.utils import concatenate
from hailtop.aiotools.router_fs import RouterAsyncFS
from hailtop.test_utils import skip_in_azure
from hailtop.httpx import ClientResponseError
from hailtop.config.variables import ConfigVariable
DOCKER_ROOT_IMAGE = os.environ.get('DOCKER_ROOT_IMAGE', 'ubuntu:20.04')
PYTHON_DILL_IMAGE = 'hailgenetics/python-dill:3.9-slim'
HAIL_GENETICS_HAIL_IMAGE = os.environ.get('HAIL_GENETICS_HAIL_IMAGE', f'hailgenetics/hail:{pip_version()}')
class LocalTests(unittest.TestCase):
def batch(self, requester_pays_project=None):
return Batch(backend=LocalBackend(),
requester_pays_project=requester_pays_project)
def read(self, file):
with open(file, 'r') as f:
result = f.read().rstrip()
return result
def assert_same_file(self, file1, file2):
assert self.read(file1).rstrip() == self.read(file2).rstrip()
def test_read_input_and_write_output(self):
with tempfile.NamedTemporaryFile('w') as input_file, \
tempfile.NamedTemporaryFile('w') as output_file:
input_file.write('abc')
input_file.flush()
b = self.batch()
input = b.read_input(input_file.name)
b.write_output(input, output_file.name)
b.run()
self.assert_same_file(input_file.name, output_file.name)
def test_read_input_group(self):
with tempfile.NamedTemporaryFile('w') as input_file1, \
tempfile.NamedTemporaryFile('w') as input_file2, \
tempfile.NamedTemporaryFile('w') as output_file1, \
tempfile.NamedTemporaryFile('w') as output_file2:
input_file1.write('abc')
input_file2.write('123')
input_file1.flush()
input_file2.flush()
b = self.batch()
input = b.read_input_group(in1=input_file1.name,
in2=input_file2.name)
b.write_output(input.in1, output_file1.name)
b.write_output(input.in2, output_file2.name)
b.run()
self.assert_same_file(input_file1.name, output_file1.name)
self.assert_same_file(input_file2.name, output_file2.name)
def test_write_resource_group(self):
with tempfile.NamedTemporaryFile('w') as input_file1, \
tempfile.NamedTemporaryFile('w') as input_file2, \
tempfile.TemporaryDirectory() as output_dir:
b = self.batch()
input = b.read_input_group(in1=input_file1.name,
in2=input_file2.name)
b.write_output(input, output_dir + '/foo')
b.run()
self.assert_same_file(input_file1.name, output_dir + '/foo.in1')
self.assert_same_file(input_file2.name, output_dir + '/foo.in2')
def test_single_job(self):
with tempfile.NamedTemporaryFile('w') as output_file:
msg = 'hello world'
b = self.batch()
j = b.new_job()
j.command(f'echo "{msg}" > {j.ofile}')
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == msg
def test_single_job_with_shell(self):
with tempfile.NamedTemporaryFile('w') as output_file:
msg = 'hello world'
b = self.batch()
j = b.new_job(shell='/bin/bash')
j.command(f'echo "{msg}" > {j.ofile}')
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == msg
def test_single_job_with_nonsense_shell(self):
b = self.batch()
j = b.new_job(shell='/bin/ajdsfoijasidojf')
j.image(DOCKER_ROOT_IMAGE)
j.command(f'echo "hello"')
self.assertRaises(Exception, b.run)
b = self.batch()
j = b.new_job(shell='/bin/nonexistent')
j.command(f'echo "hello"')
self.assertRaises(Exception, b.run)
def test_single_job_with_intermediate_failure(self):
b = self.batch()
j = b.new_job()
j.command(f'echoddd "hello"')
j2 = b.new_job()
j2.command(f'echo "world"')
self.assertRaises(Exception, b.run)
def test_single_job_w_input(self):
with tempfile.NamedTemporaryFile('w') as input_file, \
tempfile.NamedTemporaryFile('w') as output_file:
msg = 'abc'
input_file.write(msg)
input_file.flush()
b = self.batch()
input = b.read_input(input_file.name)
j = b.new_job()
j.command(f'cat {input} > {j.ofile}')
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == msg
def test_single_job_w_input_group(self):
with tempfile.NamedTemporaryFile('w') as input_file1, \
tempfile.NamedTemporaryFile('w') as input_file2, \
tempfile.NamedTemporaryFile('w') as output_file:
msg1 = 'abc'
msg2 = '123'
input_file1.write(msg1)
input_file2.write(msg2)
input_file1.flush()
input_file2.flush()
b = self.batch()
input = b.read_input_group(in1=input_file1.name,
in2=input_file2.name)
j = b.new_job()
j.command(f'cat {input.in1} {input.in2} > {j.ofile}')
j.command(f'cat {input}.in1 {input}.in2')
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == msg1 + msg2
def test_single_job_bad_command(self):
b = self.batch()
j = b.new_job()
j.command("foo") # this should fail!
with self.assertRaises(sp.CalledProcessError):
b.run()
def test_declare_resource_group(self):
with tempfile.NamedTemporaryFile('w') as output_file:
msg = 'hello world'
b = self.batch()
j = b.new_job()
j.declare_resource_group(ofile={'log': "{root}.txt"})
assert isinstance(j.ofile, ResourceGroup)
j.command(f'echo "{msg}" > {j.ofile.log}')
b.write_output(j.ofile.log, output_file.name)
b.run()
assert self.read(output_file.name) == msg
def test_resource_group_get_all_inputs(self):
b = self.batch()
input = b.read_input_group(fasta="foo",
idx="bar")
j = b.new_job()
j.command(f"cat {input.fasta}")
assert input.fasta in j._inputs
assert input.idx in j._inputs
def test_resource_group_get_all_mentioned(self):
b = self.batch()
j = b.new_job()
j.declare_resource_group(foo={'bed': '{root}.bed', 'bim': '{root}.bim'})
assert isinstance(j.foo, ResourceGroup)
j.command(f"cat {j.foo.bed}")
assert j.foo.bed in j._mentioned
assert j.foo.bim not in j._mentioned
def test_resource_group_get_all_mentioned_dependent_jobs(self):
b = self.batch()
j = b.new_job()
j.declare_resource_group(foo={'bed': '{root}.bed', 'bim': '{root}.bim'})
j.command(f"cat")
j2 = b.new_job()
j2.command(f"cat {j.foo}")
def test_resource_group_get_all_outputs(self):
b = self.batch()
j1 = b.new_job()
j1.declare_resource_group(foo={'bed': '{root}.bed', 'bim': '{root}.bim'})
assert isinstance(j1.foo, ResourceGroup)
j1.command(f"cat {j1.foo.bed}")
j2 = b.new_job()
j2.command(f"cat {j1.foo.bed}")
for r in [j1.foo.bed, j1.foo.bim]:
assert r in j1._internal_outputs
assert r in j2._inputs
assert j1.foo.bed in j1._mentioned
assert j1.foo.bim not in j1._mentioned
assert j1.foo.bed in j2._mentioned
assert j1.foo.bim not in j2._mentioned
assert j1.foo not in j1._mentioned
def test_multiple_isolated_jobs(self):
b = self.batch()
output_files = []
try:
output_files = [tempfile.NamedTemporaryFile('w') for _ in range(5)]
for i, ofile in enumerate(output_files):
msg = f'hello world {i}'
j = b.new_job()
j.command(f'echo "{msg}" > {j.ofile}')
b.write_output(j.ofile, ofile.name)
b.run()
for i, ofile in enumerate(output_files):
msg = f'hello world {i}'
assert self.read(ofile.name) == msg
finally:
[ofile.close() for ofile in output_files]
def test_multiple_dependent_jobs(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
j = b.new_job()
j.command(f'echo "0" >> {j.ofile}')
for i in range(1, 3):
j2 = b.new_job()
j2.command(f'echo "{i}" > {j2.tmp1}')
j2.command(f'cat {j.ofile} {j2.tmp1} > {j2.ofile}')
j = j2
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == "0\n1\n2"
def test_select_jobs(self):
b = self.batch()
for i in range(3):
b.new_job(name=f'foo{i}')
self.assertTrue(len(b.select_jobs('foo')) == 3)
def test_scatter_gather(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
for i in range(3):
j = b.new_job(name=f'foo{i}')
j.command(f'echo "{i}" > {j.ofile}')
merger = b.new_job()
merger.command('cat {files} > {ofile}'.format(files=' '.join([j.ofile for j in sorted(b.select_jobs('foo'),
key=lambda x: x.name, # type: ignore
reverse=True)]),
ofile=merger.ofile))
b.write_output(merger.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == '2\n1\n0'
def test_add_extension_job_resource_file(self):
b = self.batch()
j = b.new_job()
j.command(f'echo "hello" > {j.ofile}')
assert isinstance(j.ofile, JobResourceFile)
j.ofile.add_extension('.txt.bgz')
assert j.ofile._value
assert j.ofile._value.endswith('.txt.bgz')
def test_add_extension_input_resource_file(self):
input_file1 = '/tmp/data/example1.txt.bgz.foo'
b = self.batch()
in1 = b.read_input(input_file1)
assert in1._value
assert in1._value.endswith('.txt.bgz.foo')
def test_file_name_space(self):
with tempfile.NamedTemporaryFile('w', prefix="some file name with (foo) spaces") as input_file, \
tempfile.NamedTemporaryFile('w', prefix="another file name with (foo) spaces") as output_file:
input_file.write('abc')
input_file.flush()
b = self.batch()
input = b.read_input(input_file.name)
j = b.new_job()
j.command(f'cat {input} > {j.ofile}')
b.write_output(j.ofile, output_file.name)
b.run()
self.assert_same_file(input_file.name, output_file.name)
def test_resource_group_mentioned(self):
b = self.batch()
j = b.new_job()
j.declare_resource_group(foo={'bed': '{root}.bed'})
assert isinstance(j.foo, ResourceGroup)
j.command(f'echo "hello" > {j.foo}')
t2 = b.new_job()
t2.command(f'echo "hello" >> {j.foo.bed}')
b.run()
def test_envvar(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
j = b.new_job()
j.env('SOME_VARIABLE', '123abcdef')
j.command(f'echo $SOME_VARIABLE > {j.ofile}')
b.write_output(j.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == '123abcdef'
def test_concatenate(self):
b = self.batch()
files = []
for _ in range(10):
j = b.new_job()
j.command(f'touch {j.ofile}')
files.append(j.ofile)
concatenate(b, files, branching_factor=2)
assert len(b._jobs) == 10 + (5 + 3 + 2 + 1)
b.run()
def test_python_job(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
head = b.new_job()
head.command(f'echo "5" > {head.r5}')
head.command(f'echo "3" > {head.r3}')
def read(path):
with open(path, 'r') as f:
i = f.read()
return int(i)
def multiply(x, y):
return x * y
def reformat(x, y):
return {'x': x, 'y': y}
middle = b.new_python_job()
r3 = middle.call(read, head.r3)
r5 = middle.call(read, head.r5)
r_mult = middle.call(multiply, r3, r5)
middle2 = b.new_python_job()
r_mult = middle2.call(multiply, r_mult, 2)
r_dict = middle2.call(reformat, r3, r5)
tail = b.new_job()
tail.command(f'cat {r3.as_str()} {r5.as_repr()} {r_mult.as_str()} {r_dict.as_json()} > {tail.ofile}')
b.write_output(tail.ofile, output_file.name)
b.run()
assert self.read(output_file.name) == '3\n5\n30\n{\"x\": 3, \"y\": 5}'
def test_backend_context_manager(self):
with LocalBackend() as backend:
b = Batch(backend=backend)
b.run()
def test_failed_jobs_dont_stop_non_dependent_jobs(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
head = b.new_job()
head.command(f'echo 1 > {head.ofile}')
head2 = b.new_job()
head2.command('false')
tail = b.new_job()
tail.command(f'cat {head.ofile} > {tail.ofile}')
b.write_output(tail.ofile, output_file.name)
self.assertRaises(Exception, b.run)
assert self.read(output_file.name) == '1'
def test_failed_jobs_stop_child_jobs(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
head = b.new_job()
head.command(f'echo 1 > {head.ofile}')
head.command('false')
head2 = b.new_job()
head2.command(f'echo 2 > {head2.ofile}')
tail = b.new_job()
tail.command(f'cat {head.ofile} > {tail.ofile}')
b.write_output(head2.ofile, output_file.name)
b.write_output(tail.ofile, output_file.name)
self.assertRaises(Exception, b.run)
assert self.read(output_file.name) == '2'
def test_failed_jobs_stop_grandchild_jobs(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
head = b.new_job()
head.command(f'echo 1 > {head.ofile}')
head.command('false')
head2 = b.new_job()
head2.command(f'echo 2 > {head2.ofile}')
tail = b.new_job()
tail.command(f'cat {head.ofile} > {tail.ofile}')
tail2 = b.new_job()
tail2.depends_on(tail)
tail2.command(f'echo foo > {tail2.ofile}')
b.write_output(head2.ofile, output_file.name)
b.write_output(tail2.ofile, output_file.name)
self.assertRaises(Exception, b.run)
assert self.read(output_file.name) == '2'
def test_failed_jobs_dont_stop_always_run_jobs(self):
with tempfile.NamedTemporaryFile('w') as output_file:
b = self.batch()
head = b.new_job()
head.command(f'echo 1 > {head.ofile}')
head.command('false')
tail = b.new_job()
tail.command(f'cat {head.ofile} > {tail.ofile}')
tail.always_run()
b.write_output(tail.ofile, output_file.name)
self.assertRaises(Exception, b.run)
assert self.read(output_file.name) == '1'
class ServiceTests(unittest.TestCase):
def setUp(self):
self.backend = ServiceBackend()
remote_tmpdir = get_remote_tmpdir('hailtop_test_batch_service_tests')
if not remote_tmpdir.endswith('/'):
remote_tmpdir += '/'
self.remote_tmpdir = remote_tmpdir + str(uuid.uuid4()) + '/'
if remote_tmpdir.startswith('gs://'):
match = re.fullmatch('gs://(?P<bucket_name>[^/]+).*', remote_tmpdir)
assert match
self.bucket = match.groupdict()['bucket_name']
else:
assert remote_tmpdir.startswith('hail-az://')
if remote_tmpdir.startswith('hail-az://'):
match = re.fullmatch('hail-az://(?P<storage_account>[^/]+)/(?P<container_name>[^/]+).*', remote_tmpdir)
assert match
storage_account, container_name = match.groups()
else:
assert remote_tmpdir.startswith('https://')
match = re.fullmatch('https://(?P<storage_account>[^/]+).blob.core.windows.net/(?P<container_name>[^/]+).*', remote_tmpdir)
assert match
storage_account, container_name = match.groups()
self.bucket = f'{storage_account}/{container_name}'
self.cloud_input_dir = f'{self.remote_tmpdir}batch-tests/resources'
token = uuid.uuid4()
self.cloud_output_path = f'/batch-tests/{token}'
self.cloud_output_dir = f'{self.remote_tmpdir}{self.cloud_output_path}'
in_cluster_key_file = '/test-gsa-key/key.json'
if not os.path.exists(in_cluster_key_file):
in_cluster_key_file = None
self.router_fs = RouterAsyncFS(gcs_kwargs={'gcs_requester_pays_configuration': 'hail-vdc', 'credentials_file': in_cluster_key_file},
azure_kwargs={'credential_file': in_cluster_key_file})
if not self.sync_exists(f'{self.remote_tmpdir}batch-tests/resources/hello.txt'):
self.sync_write(f'{self.remote_tmpdir}batch-tests/resources/hello.txt', b'hello world')
if not self.sync_exists(f'{self.remote_tmpdir}batch-tests/resources/hello spaces.txt'):
self.sync_write(f'{self.remote_tmpdir}batch-tests/resources/hello spaces.txt', b'hello')
if not self.sync_exists(f'{self.remote_tmpdir}batch-tests/resources/hello (foo) spaces.txt'):
self.sync_write(f'{self.remote_tmpdir}batch-tests/resources/hello (foo) spaces.txt', b'hello')
def tearDown(self):
self.backend.close()
def sync_exists(self, url):
return async_to_blocking(self.router_fs.exists(url))
def sync_write(self, url, data):
return async_to_blocking(self.router_fs.write(url, data))
def batch(self, **kwargs):
name_of_test_method = inspect.stack()[1][3]
return Batch(name=name_of_test_method,
backend=self.backend,
default_image=DOCKER_ROOT_IMAGE,
attributes={'foo': 'a', 'bar': 'b'},
**kwargs)
def test_single_task_no_io(self):
b = self.batch()
j = b.new_job()
j.command('echo hello')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_input(self):
b = self.batch()
input = b.read_input(f'{self.cloud_input_dir}/hello.txt')
j = b.new_job()
j.command(f'cat {input}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_input_resource_group(self):
b = self.batch()
input = b.read_input_group(foo=f'{self.cloud_input_dir}/hello.txt')
j = b.new_job()
j.storage('10Gi')
j.command(f'cat {input.foo}')
j.command(f'cat {input}.foo')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_output(self):
b = self.batch()
j = b.new_job(attributes={'a': 'bar', 'b': 'foo'})
j.command(f'echo hello > {j.ofile}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_write_output(self):
b = self.batch()
j = b.new_job()
j.command(f'echo hello > {j.ofile}')
b.write_output(j.ofile, f'{self.cloud_output_dir}/test_single_task_output.txt')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_resource_group(self):
b = self.batch()
j = b.new_job()
j.declare_resource_group(output={'foo': '{root}.foo'})
assert isinstance(j.output, ResourceGroup)
j.command(f'echo "hello" > {j.output.foo}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_task_write_resource_group(self):
b = self.batch()
j = b.new_job()
j.declare_resource_group(output={'foo': '{root}.foo'})
assert isinstance(j.output, ResourceGroup)
j.command(f'echo "hello" > {j.output.foo}')
b.write_output(j.output, f'{self.cloud_output_dir}/test_single_task_write_resource_group')
b.write_output(j.output.foo, f'{self.cloud_output_dir}/test_single_task_write_resource_group_file.txt')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_multiple_dependent_tasks(self):
output_file = f'{self.cloud_output_dir}/test_multiple_dependent_tasks.txt'
b = self.batch()
j = b.new_job()
j.command(f'echo "0" >> {j.ofile}')
for i in range(1, 3):
j2 = b.new_job()
j2.command(f'echo "{i}" > {j2.tmp1}')
j2.command(f'cat {j.ofile} {j2.tmp1} > {j2.ofile}')
j = j2
b.write_output(j.ofile, output_file)
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_specify_cpu(self):
b = self.batch()
j = b.new_job()
j.cpu('0.5')
j.command(f'echo "hello" > {j.ofile}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_specify_memory(self):
b = self.batch()
j = b.new_job()
j.memory('100M')
j.command(f'echo "hello" > {j.ofile}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_scatter_gather(self):
b = self.batch()
for i in range(3):
j = b.new_job(name=f'foo{i}')
j.command(f'echo "{i}" > {j.ofile}')
merger = b.new_job()
merger.command('cat {files} > {ofile}'.format(files=' '.join([j.ofile for j in sorted(b.select_jobs('foo'),
key=lambda x: x.name, # type: ignore
reverse=True)]),
ofile=merger.ofile))
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_file_name_space(self):
b = self.batch()
input = b.read_input(f'{self.cloud_input_dir}/hello (foo) spaces.txt')
j = b.new_job()
j.command(f'cat {input} > {j.ofile}')
b.write_output(j.ofile, f'{self.cloud_output_dir}/hello (foo) spaces.txt')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_local_paths_error(self):
b = self.batch()
b.new_job()
for input in ["hi.txt", "~/hello.csv", "./hey.tsv", "/sup.json", "file://yo.yaml"]:
with pytest.raises(ValueError) as e:
b.read_input(input)
assert str(e.value).startswith("Local filepath detected")
def test_dry_run(self):
b = self.batch()
j = b.new_job()
j.command(f'echo hello > {j.ofile}')
b.write_output(j.ofile, f'{self.cloud_output_dir}/test_single_job_output.txt')
b.run(dry_run=True)
def test_verbose(self):
b = self.batch()
input = b.read_input(f'{self.cloud_input_dir}/hello.txt')
j = b.new_job()
j.command(f'cat {input}')
b.write_output(input, f'{self.cloud_output_dir}/hello.txt')
res = b.run(verbose=True)
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_cloudfuse_fails_with_read_write_mount_option(self):
assert self.bucket
path = f'/{self.bucket}{self.cloud_output_path}'
b = self.batch()
j = b.new_job()
j.command(f'mkdir -p {path}; echo head > {path}/cloudfuse_test_1')
j.cloudfuse(self.bucket, f'/{self.bucket}', read_only=False)
try:
b.run()
except ClientResponseError as e:
assert 'Only read-only cloudfuse requests are supported' in e.body, e.body
else:
assert False
def test_cloudfuse_fails_with_io_mount_point(self):
assert self.bucket
path = f'/{self.bucket}{self.cloud_output_path}'
b = self.batch()
j = b.new_job()
j.command(f'mkdir -p {path}; echo head > {path}/cloudfuse_test_1')
j.cloudfuse(self.bucket, f'/io', read_only=True)
try:
b.run()
except ClientResponseError as e:
assert 'Cloudfuse requests with mount_path=/io are not supported' in e.body, e.body
else:
assert False
def test_cloudfuse_read_only(self):
assert self.bucket
path = f'/{self.bucket}{self.cloud_output_path}'
b = self.batch()
j = b.new_job()
j.command(f'mkdir -p {path}; echo head > {path}/cloudfuse_test_1')
j.cloudfuse(self.bucket, f'/{self.bucket}', read_only=True)
res = b.run()
res_status = res.status()
assert res_status['state'] == 'failure', str((res_status, res.debug_info()))
def test_cloudfuse_implicit_dirs(self):
assert self.bucket
path = self.router_fs.parse_url(f'{self.remote_tmpdir}batch-tests/resources/hello.txt').path
b = self.batch()
j = b.new_job()
j.command(f'cat /cloudfuse/{path}')
j.cloudfuse(self.bucket, f'/cloudfuse', read_only=True)
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_cloudfuse_empty_string_bucket_fails(self):
assert self.bucket
b = self.batch()
j = b.new_job()
with self.assertRaises(BatchException):
j.cloudfuse('', '/empty_bucket')
with self.assertRaises(BatchException):
j.cloudfuse(self.bucket, '')
def test_cloudfuse_submount_in_io_doesnt_rm_bucket(self):
assert self.bucket
b = self.batch()
j = b.new_job()
j.cloudfuse(self.bucket, '/io/cloudfuse')
j.command(f'ls /io/cloudfuse/')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
assert self.sync_exists(f'{self.remote_tmpdir}batch-tests/resources/hello.txt')
@skip_in_azure
def test_fuse_requester_pays(self):
b = self.batch(requester_pays_project='hail-vdc')
j = b.new_job()
j.cloudfuse('hail-test-requester-pays-fds32', '/fuse-bucket')
j.command('cat /fuse-bucket/hello')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
@skip_in_azure
def test_fuse_non_requester_pays_bucket_when_requester_pays_project_specified(self):
assert self.bucket
b = self.batch(requester_pays_project='hail-vdc')
j = b.new_job()
j.command(f'ls /fuse-bucket')
j.cloudfuse(self.bucket, f'/fuse-bucket', read_only=True)
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
@skip_in_azure
def test_requester_pays(self):
b = self.batch(requester_pays_project='hail-vdc')
input = b.read_input('gs://hail-test-requester-pays-fds32/hello')
j = b.new_job()
j.command(f'cat {input}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_benchmark_lookalike_workflow(self):
b = self.batch()
setup_jobs = []
for i in range(10):
j = b.new_job(f'setup_{i}').cpu(0.25)
j.command(f'echo "foo" > {j.ofile}')
setup_jobs.append(j)
jobs = []
for i in range(500):
j = b.new_job(f'create_file_{i}').cpu(0.25)
j.command(f'echo {setup_jobs[i % len(setup_jobs)].ofile} > {j.ofile}')
j.command(f'echo "bar" >> {j.ofile}')
jobs.append(j)
combine = b.new_job(f'combine_output').cpu(0.25)
for _ in grouped(arg_max(), jobs):
combine.command(f'cat {" ".join(shq(j.ofile) for j in jobs)} >> {combine.ofile}')
b.write_output(combine.ofile, f'{self.cloud_output_dir}/pipeline_benchmark_test.txt')
# too slow
# assert b.run().status()['state'] == 'success'
def test_envvar(self):
b = self.batch()
j = b.new_job()
j.env('SOME_VARIABLE', '123abcdef')
j.command('[ $SOME_VARIABLE = "123abcdef" ]')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_job_with_shell(self):
msg = 'hello world'
b = self.batch()
j = b.new_job(shell='/bin/sh')
j.command(f'echo "{msg}"')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_single_job_with_nonsense_shell(self):
b = self.batch()
j = b.new_job(shell='/bin/ajdsfoijasidojf')
j.command(f'echo "hello"')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'failure', str((res_status, res.debug_info()))
def test_single_job_with_intermediate_failure(self):
b = self.batch()
j = b.new_job()
j.command(f'echoddd "hello"')
j2 = b.new_job()
j2.command(f'echo "world"')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'failure', str((res_status, res.debug_info()))
def test_input_directory(self):
b = self.batch()
input1 = b.read_input(self.cloud_input_dir)
input2 = b.read_input(self.cloud_input_dir.rstrip('/') + '/')
j = b.new_job()
j.command(f'ls {input1}/hello.txt')
j.command(f'ls {input2}/hello.txt')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
def test_python_job(self):
b = self.batch(default_python_image=PYTHON_DILL_IMAGE)
head = b.new_job()
head.command(f'echo "5" > {head.r5}')
head.command(f'echo "3" > {head.r3}')
def read(path):
with open(path, 'r') as f:
i = f.read()
return int(i)
def multiply(x, y):
return x * y
def reformat(x, y):
return {'x': x, 'y': y}
middle = b.new_python_job()
r3 = middle.call(read, head.r3)
r5 = middle.call(read, head.r5)
r_mult = middle.call(multiply, r3, r5)
middle2 = b.new_python_job()
r_mult = middle2.call(multiply, r_mult, 2)
r_dict = middle2.call(reformat, r3, r5)
tail = b.new_job()
tail.command(f'cat {r3.as_str()} {r5.as_repr()} {r_mult.as_str()} {r_dict.as_json()}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
assert res.get_job_log(4)['main'] == "3\n5\n30\n{\"x\": 3, \"y\": 5}\n", str(res.debug_info())
def test_python_job_w_resource_group_unpack_individually(self):
b = self.batch(default_python_image=PYTHON_DILL_IMAGE)
head = b.new_job()
head.declare_resource_group(count={'r5': '{root}.r5',
'r3': '{root}.r3'})
assert isinstance(head.count, ResourceGroup)
head.command(f'echo "5" > {head.count.r5}')
head.command(f'echo "3" > {head.count.r3}')
def read(path):
with open(path, 'r') as f:
r = int(f.read())
return r
def multiply(x, y):
return x * y
def reformat(x, y):
return {'x': x, 'y': y}
middle = b.new_python_job()
r3 = middle.call(read, head.count.r3)
r5 = middle.call(read, head.count.r5)
r_mult = middle.call(multiply, r3, r5)
middle2 = b.new_python_job()
r_mult = middle2.call(multiply, r_mult, 2)
r_dict = middle2.call(reformat, r3, r5)
tail = b.new_job()
tail.command(f'cat {r3.as_str()} {r5.as_repr()} {r_mult.as_str()} {r_dict.as_json()}')
res = b.run()
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
assert res.get_job_log(4)['main'] == "3\n5\n30\n{\"x\": 3, \"y\": 5}\n", str(res.debug_info())
def test_python_job_can_write_to_resource_path(self):
b = self.batch(default_python_image=PYTHON_DILL_IMAGE)
def write(path):
with open(path, 'w') as f:
f.write('foo')
head = b.new_python_job()
head.call(write, head.ofile)
tail = b.new_bash_job()
tail.command(f'cat {head.ofile}')
res = b.run()
assert res
assert tail._job_id
res_status = res.status()
assert res_status['state'] == 'success', str((res_status, res.debug_info()))
assert res.get_job_log(tail._job_id)['main'] == 'foo', str(res.debug_info())
def test_python_job_w_resource_group_unpack_jointly(self):
b = self.batch(default_python_image=PYTHON_DILL_IMAGE)
head = b.new_job()
head.declare_resource_group(count={'r5': '{root}.r5',
'r3': '{root}.r3'})
assert isinstance(head.count, ResourceGroup)
head.command(f'echo "5" > {head.count.r5}')
head.command(f'echo "3" > {head.count.r3}')
def read_rg(root):
with open(root['r3'], 'r') as f:
r3 = int(f.read())
with open(root['r5'], 'r') as f:
r5 = int(f.read())
return (r3, r5)
def multiply(r):
x, y = r
return x * y