forked from snakemake/snakemake-executor-plugin-slurm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
1046 lines (880 loc) · 39.5 KB
/
Copy pathtests.py
File metadata and controls
1046 lines (880 loc) · 39.5 KB
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 os
import re
import inspect
from pathlib import Path
from typing import Optional
import snakemake.common.tests
from snakemake.settings.types import ConfigSettings
import snakemake.settings.types as settings
from snakemake import api
from snakemake_interface_executor_plugins.settings import ExecutorSettingsBase
from unittest.mock import MagicMock, patch
import pytest
from snakemake_executor_plugin_slurm import ExecutorSettings
from snakemake_executor_plugin_slurm.utils import set_gres_string
from snakemake_executor_plugin_slurm.submit_string import get_submit_command
from snakemake_executor_plugin_slurm.validation import (
validate_slurm_extra,
validate_or_get_slurm_job_id,
)
from snakemake_interface_common.exceptions import WorkflowError
class TestWorkflows(snakemake.common.tests.TestWorkflowsLocalStorageBase):
__test__ = True
def get_executor(self) -> str:
return "slurm"
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
return ExecutorSettings(
init_seconds_before_status_checks=2,
# seconds_between_status_checks=5,
)
def get_config_settings(self) -> Optional[ConfigSettings]:
"""Override to provide config settings for the workflow under test.
Returns None by default, which uses the workflow's default config.
This dummy function is needed to inject config setting ast he
baseclass does not provide this functionality."
"""
return None
class TestPassCommandAsScript(snakemake.common.tests.TestWorkflowsLocalStorageBase):
"""Integration-style test that runs the real workflow on the Slurm test cluster
and verifies the plugin can submit the job by passing the command as a script
via stdin (pass_command_as_script=True).
"""
__test__ = True
def get_executor(self) -> str:
return "slurm"
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
# Use the real ExecutorSettings and enable the flag under test.
return ExecutorSettings(
pass_command_as_script=True,
init_seconds_before_status_checks=2,
)
class TestWrapExecJobQuoteEscaping(
snakemake.common.tests.TestWorkflowsLocalStorageBase
):
"""Regression test for issue #29: ensure double quotes in config values are
properly escaped when passed to --wrap="{exec_job}".
Runs the real workflow on the Slurm test cluster and verifies the plugin can
submit the wrapped exec_job string with escaped double quotes.
"""
__test__ = True
def get_executor(self) -> str:
return "slurm"
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
# Use the real ExecutorSettings and enable the flag under test.
return ExecutorSettings(
init_seconds_before_status_checks=2,
)
def get_config_settings(self) -> Optional[ConfigSettings]:
"""The config that triggered the error"""
return ConfigSettings(config_args={"path": "{'x': '/path/to/file'}"})
def run_workflow(self, test_name, tmp_path, deployment_method=frozenset()):
test_path = (
Path(
inspect.getfile(snakemake.common.tests.TestWorkflowsLocalStorageBase)
).parent
/ "testcases"
/ test_name
)
if self.omit_tmp:
tmp_path = test_path
else:
tmp_path = Path(tmp_path) / test_name
self._copy_test_files(test_path, tmp_path)
resource_settings = self.get_resource_settings()
if self._common_settings().local_exec:
resource_settings.cores = 3
resource_settings.nodes = None
else:
resource_settings.cores = 1
resource_settings.nodes = 3
with api.SnakemakeApi(
settings.OutputSettings(
verbose=True,
show_failed_logs=True,
),
) as snakemake_api:
workflow_api = snakemake_api.workflow(
config_settings=self.get_config_settings(),
resource_settings=resource_settings,
storage_settings=settings.StorageSettings(
default_storage_provider=self.get_default_storage_provider(),
default_storage_prefix=self.get_default_storage_prefix(),
shared_fs_usage=(
settings.SharedFSUsage.all()
if self.get_assume_shared_fs()
else frozenset()
),
),
deployment_settings=self.get_deployment_settings(deployment_method),
storage_provider_settings=self.get_default_storage_provider_settings(),
workdir=Path(tmp_path),
snakefile=tmp_path / "Snakefile",
)
dag_api = workflow_api.dag()
if self.create_report:
dag_api.create_report(
reporter=self.get_reporter(),
report_settings=self.get_report_settings(),
)
else:
dag_api.execute_workflow(
executor=self.get_executor(),
executor_settings=self.get_executor_settings(),
execution_settings=settings.ExecutionSettings(
latency_wait=self.latency_wait,
),
remote_execution_settings=self.get_remote_execution_settings(),
)
class TestEfficiencyReport(snakemake.common.tests.TestWorkflowsLocalStorageBase):
__test__ = True
def get_executor(self) -> str:
return "slurm"
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
self.REPORT_PATH = Path.cwd() / "efficiency_report_test"
return ExecutorSettings(
efficiency_report=True,
init_seconds_before_status_checks=5,
efficiency_report_path=self.REPORT_PATH,
# seconds_between_status_checks=5,
)
def test_simple_workflow(self, tmp_path):
self.run_workflow("simple", tmp_path)
# The efficiency report is created in the
# current working directory
pattern = re.compile(r"efficiency_report_[\w-]+\.csv")
report_found = False
report_path = None
# using a short handle for the expected path
expected_path = self.REPORT_PATH
# Check if the efficiency report file exists - based on the regex pattern
for fname in os.listdir(expected_path):
if pattern.match(fname):
report_found = True
report_path = os.path.join(expected_path, fname)
# Verify it's not empty
assert (
os.stat(report_path).st_size > 0
), f"Efficiency report {report_path} is empty"
break
assert report_found, "Efficiency report file not found"
class TestWorkflowsRequeue(TestWorkflows):
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
return ExecutorSettings(requeue=True, init_seconds_before_status_checks=1)
class TestGresString:
"""Test cases for the set_gres_string function."""
@pytest.fixture
def mock_job(self):
"""Create a mock job with configurable resources."""
def _create_job(**resources):
mock_resources = MagicMock()
# Configure get method to return values from resources dict
mock_resources.get.side_effect = lambda key, default=None: resources.get(
key, default
)
# Add direct attribute access for certain resources
for key, value in resources.items():
setattr(mock_resources, key, value)
mock_job = MagicMock()
mock_job.resources = mock_resources
mock_job.name = "test_job"
mock_job.wildcards = {}
mock_job.is_group.return_value = False
mock_job.jobid = 1
return mock_job
return _create_job
def test_no_gres_or_gpu(self, mock_job):
"""Test with no GPU or GRES resources specified."""
job = mock_job()
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == ""
def test_valid_gres_simple(self, mock_job):
"""Test with valid GRES format (simple)."""
job = mock_job(gres="gpu:1")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gres=gpu:1"
def test_valid_gres_with_model(self, mock_job):
"""Test with valid GRES format including GPU model."""
job = mock_job(gres="gpu:tesla:2")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gres=gpu:tesla:2"
def test_invalid_gres_format(self, mock_job):
"""Test with invalid GRES format."""
job = mock_job(gres="gpu")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
with pytest.raises(WorkflowError, match="Invalid GRES format"):
set_gres_string(job)
def test_invalid_gres_format_missing_count(self, mock_job):
"""Test with invalid GRES format (missing count)."""
job = mock_job(gres="gpu:tesla:")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
with pytest.raises(WorkflowError, match="Invalid GRES format"):
set_gres_string(job)
def test_valid_gpu_number(self, mock_job):
"""Test with valid GPU number."""
job = mock_job(gpu="2")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gpus=2"
def test_valid_gpu_with_name(self, mock_job):
"""Test with valid GPU name and number."""
job = mock_job(gpu="tesla:2")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gpus=tesla:2"
def test_gpu_with_model(self, mock_job):
"""Test GPU with model specification."""
job = mock_job(gpu="2", gpu_model="tesla")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gpus=tesla:2"
def test_invalid_gpu_model_format(self, mock_job):
"""Test with invalid GPU model format."""
job = mock_job(gpu="2", gpu_model="invalid:model")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
with pytest.raises(WorkflowError, match="Invalid GPU model format"):
set_gres_string(job)
def test_gpu_model_without_gpu(self, mock_job):
"""Test GPU model without GPU number."""
job = mock_job(gpu_model="tesla")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# test whether the resource setting raises the correct error
with pytest.raises(
WorkflowError, match="GPU model is set, but no GPU number is given"
):
set_gres_string(job)
def test_tmpspace_gres_10G(self, mock_job):
"""Test with valid GRES format (simple)."""
job = mock_job(gres="tmpspace:10G")
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert set_gres_string(job) == " --gres=tmpspace:10G"
def test_both_gres_and_gpu_set(self, mock_job):
"""Test error case when both GRES and GPU are specified."""
job = mock_job(gres="gpu:1", gpu="2")
# Patch subprocess.Popen to simulate job submission
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to simulate successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Ensure the error is raised when both GRES and GPU are set
with pytest.raises(
WorkflowError, match="GRES and GPU are set. Please only set one"
):
set_gres_string(job)
def test_nested_string_raise(self, mock_job):
"""Test error case when gres is a nested string."""
job = mock_job(gres="'gpu:1'")
# Patch subprocess.Popen to simulate job submission
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to simulate successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Ensure the error is raised when both GRES and GPU are set
with pytest.raises(
WorkflowError,
match="GRES format should not be a nested string",
):
set_gres_string(job)
def test_gpu_model_nested_string_raise(self, mock_job):
"""Test error case when gpu_model is a nested string."""
job = mock_job(gpu_model="'tesla'", gpu="2")
# Patch subprocess.Popen to simulate job submission
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to simulate successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Ensure the error is raised when both GRES and GPU are set
with pytest.raises(
WorkflowError,
match="GPU model format should not be a nested string",
):
set_gres_string(job)
class TestSLURMResources(TestWorkflows):
"""
Test workflows using job resources passed as part of the job configuration.
This test suite uses the `get_submit_command` function to generate the
sbatch command and validates the inclusion of resources.
"""
@pytest.fixture
def mock_job(self):
"""Create a mock job with configurable resources."""
def _create_job(**resources):
mock_resources = MagicMock()
# Configure get method to return values from resources dict
mock_resources.get.side_effect = lambda key, default=None: resources.get(
key, default
)
# Add direct attribute access for certain resources
for key, value in resources.items():
setattr(mock_resources, key, value)
mock_job = MagicMock()
mock_job.resources = mock_resources
mock_job.name = "test_job"
mock_job.wildcards = {}
mock_job.is_group.return_value = False
mock_job.jobid = 1
return mock_job
return _create_job
def test_constraint_resource(self, mock_job):
"""
Test that the constraint resource is correctly
added to the sbatch command.
"""
# Create a job with a constraint resource
job = mock_job(constraint="haswell")
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"constraint": "haswell",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert " -C haswell" in get_submit_command(job, params)
def test_qos_resource(self, mock_job):
"""Test that the qos resource is correctly added to the sbatch command."""
# Create a job with a qos resource
job = mock_job(qos="normal")
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"qos": "normal",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert " --qos=normal" in get_submit_command(job, params)
def test_both_constraint_and_qos(self, mock_job):
"""Test that both constraint and qos resources can be used together."""
# Create a job with both constraint and qos resources
job = mock_job(constraint="haswell", qos="high")
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"constraint": "haswell",
"qos": "high",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Assert both resources are correctly included
sbatch_command = get_submit_command(job, params)
assert " --qos=high" in sbatch_command
assert " -C haswell" in sbatch_command
def test_no_resources(self, mock_job):
"""
Test that no constraint or qos flags are added
when resources are not specified.
"""
# Create a job without constraint or qos resources
job = mock_job()
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Assert neither resource is included
sbatch_command = get_submit_command(job, params)
assert "-C " not in sbatch_command
assert "--qos " not in sbatch_command
def test_empty_constraint(self, mock_job):
"""Test that an empty constraint is still included in the command."""
# Create a job with an empty constraint
job = mock_job(constraint="")
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"constraint": "",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Assert the constraint is included (even if empty)
assert "-C ''" in get_submit_command(job, params)
def test_empty_qos(self, mock_job):
"""Test that an empty qos is still included in the command."""
# Create a job with an empty qos
job = mock_job(qos="")
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"qos": "",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
# Assert the qos is included (even if empty)
assert "--qos=''" in get_submit_command(job, params)
def test_taks(self, mock_job):
"""Test that tasks are correctly included in the sbatch command."""
# Create a job with tasks
job = mock_job(tasks=4)
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"tasks": 4,
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert "--ntasks=4" in get_submit_command(job, params)
def test_gpu_tasks(self, mock_job):
"""Test that GPU tasks are correctly included in the sbatch command."""
# Create a job with GPU tasks
job = mock_job(gpu=1, tasks_per_gpu=2)
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"tasks_per_gpu": 2,
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert "--ntasks-per-gpu=2" in get_submit_command(job, params)
def test_no_gpu_task(self, mock_job):
"""Test that no GPU tasks are included when not specified."""
# Create a job without GPU tasks
job = mock_job(gpu=1, tasks_per_gpu=-1)
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
"tasks_per_gpu": -1,
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert "--ntasks-per-gpu" not in get_submit_command(job, params)
def test_task_set_for_unset_tasks(self, mock_job):
"""Test that tasks are set to 1 when unset."""
# Create a job without tasks
job = mock_job(tasks=None)
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert "--ntasks=1" in get_submit_command(job, params)
def test_gpu_tasks_set_for_unset_tasks(self, mock_job):
"""Test that GPU tasks are set to 1 when unset."""
# Create a job without GPU tasks
job = mock_job(gpu=1)
params = {
"run_uuid": "test_run",
"slurm_logfile": "test_logfile",
"comment_str": "test_comment",
"account": None,
"partition": None,
"workdir": ".",
}
# Patch subprocess.Popen to capture the sbatch command
with patch("subprocess.Popen") as mock_popen:
# Configure the mock to return successful submission
process_mock = MagicMock()
process_mock.communicate.return_value = ("123", "")
process_mock.returncode = 0
mock_popen.return_value = process_mock
assert "--ntasks-per-gpu=1" in get_submit_command(job, params)
class TestWildcardsWithSlashes(snakemake.common.tests.TestWorkflowsLocalStorageBase):
"""
Test handling of wildcards with slashes to ensure log directories are
correctly constructed.
"""
__test__ = True
def get_executor(self) -> str:
return "slurm"
def get_executor_settings(self) -> Optional[ExecutorSettingsBase]:
return ExecutorSettings(
logdir="test_logdir", init_seconds_before_status_checks=1
)
def test_wildcard_slash_replacement(self):
"""
Test that slashes in wildcards are correctly replaced with
underscores in log directory paths.
"""
# Just test the wildcard sanitization logic directly
wildcards = ["/leading_slash", "middle/slash", "trailing/"]
# This is the actual logic from the Executor.run_job method
wildcard_str = "_".join(wildcards).replace("/", "_") if wildcards else ""
# Assert that slashes are correctly replaced with underscores
assert wildcard_str == "_leading_slash_middle_slash_trailing_"
# Verify no slashes remain in the wildcard string
assert "/" not in wildcard_str
class TestSlurmExtraValidation:
"""Test cases for the validate_slurm_extra function."""
@pytest.fixture
def mock_job(self):
"""Create a mock job with configurable slurm_extra resource."""
def _create_job(**resources):
mock_resources = MagicMock()
# Configure get method to return values from resources dict
mock_resources.get.side_effect = lambda key, default=None: resources.get(
key, default
)
# Add direct attribute access for certain resources
for key, value in resources.items():
setattr(mock_resources, key, value)
mock_job = MagicMock()
mock_job.resources = mock_resources
mock_job.name = "test_job"
mock_job.wildcards = {}
mock_job.is_group.return_value = False
mock_job.jobid = 1
return mock_job
return _create_job
def test_valid_slurm_extra(self, mock_job):
"""Test that validation passes with allowed SLURM options."""
job = mock_job(slurm_extra="--mail-type=END --mail-user=user@example.com")
# Should not raise any exception
validate_slurm_extra(job)
def test_forbidden_job_name_long_form(self, mock_job):
"""Test that --job-name is rejected."""
job = mock_job(slurm_extra="--job-name=my-job --mail-type=END")
with pytest.raises(WorkflowError, match=r"job-name.*not allowed"):
validate_slurm_extra(job)
def test_forbidden_job_name_short_form(self, mock_job):
"""Test that -J is rejected."""
job = mock_job(slurm_extra="-J my-job --mail-type=END")
with pytest.raises(WorkflowError, match=r"job-name.*not allowed"):
validate_slurm_extra(job)
def test_forbidden_account_long_form(self, mock_job):
"""Test that --account is rejected."""
job = mock_job(slurm_extra="--account=myaccount --mail-type=END")
with pytest.raises(WorkflowError, match=r"account.*not allowed"):
validate_slurm_extra(job)
def test_forbidden_account_short_form(self, mock_job):
"""Test that -A is rejected."""
job = mock_job(slurm_extra="-A myaccount --mail-type=END")
with pytest.raises(WorkflowError, match=r"account.*not allowed"):
validate_slurm_extra(job)
def test_forbidden_comment(self, mock_job):
"""Test that --comment is rejected."""
job = mock_job(slurm_extra="--comment='my comment' --mail-type=END")
with pytest.raises(WorkflowError, match=r"job-comment.*not allowed"):
validate_slurm_extra(job)
def test_forbidden_gres(self, mock_job):
"""Test that --gres is rejected."""
job = mock_job(slurm_extra="--gres=gpu:1 --mail-type=END")
with pytest.raises(WorkflowError, match=r"generic-resources.*not allowed"):
validate_slurm_extra(job)
def test_multiple_forbidden_options(self, mock_job):
"""Test that the first forbidden option found is reported."""
job = mock_job(slurm_extra="--job-name=test --account=myaccount")
# Should raise error for job-name (first one encountered)
with pytest.raises(WorkflowError, match=r"job-name.*not allowed"):
validate_slurm_extra(job)
class TestSlurmJobIdValidation:
"""Test cases for the validate_or_get_slurm_job_id function."""
def test_parsable_format_simple(self):
"""Test parsable format with just job ID."""
output = "12345"
result = validate_or_get_slurm_job_id("12345", output)
assert result == "12345"
def test_parsable_format_with_cluster(self):
"""Test parsable format with cluster name (jobid;clustername)."""
output = "54321;mycluster"
result = validate_or_get_slurm_job_id("54321", output)
assert result == "54321"
def test_convoluted_output_with_percentages(self):
"""Test extraction from output containing percentages."""
output = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Maecenas quis risus porttitor: 25%
pretium enim volutpat: 23.3%
Submitted batch job 88888
some other text"""
result = validate_or_get_slurm_job_id("88888", output)
assert result == "88888"
def test_convoluted_output_with_units(self):
"""Test extraction from output containing memory/size units."""
output = """System information:
Memory available: 256 GiB
CPU usage: 12 cores
Storage: 500 G
Job ID: 77777
Files processed: 1500 files"""
result = validate_or_get_slurm_job_id("77777", output)
assert result == "77777"
def test_convoluted_output_mixed(self):
"""Test extraction with percentages, decimals, and units mixed."""
output = """Cluster status report
Queue utilization: 75.5%
Memory allocated: 128 KiB per node
Disk usage: 23.3 % of quota
Allocated space: 50 G
Processing 3000 files
Your job 123456 has been submitted
"""
result = validate_or_get_slurm_job_id("123456", output)
assert result == "123456"
def test_job_id_at_beginning(self):
"""Test extraction when job ID appears at the start."""
output = """999888 submitted successfully
Memory: 64 GiB
Nodes: 4"""
result = validate_or_get_slurm_job_id("999888", output)
assert result == "999888"
def test_job_id_in_middle(self):
"""Test extraction when job ID is in the middle of output."""
output = """Configuration loaded: 100%
Job 444555 queued
Estimated wait time: 5.5 minutes"""
result = validate_or_get_slurm_job_id("444555", output)
assert result == "444555"
def test_output_with_lowercase_units(self):
"""Test that lowercase units are properly excluded."""
output = """Memory: 32 m
Storage: 100 k files
Job: 333222"""
result = validate_or_get_slurm_job_id("333222", output)
assert result == "333222"
def test_output_with_cores_and_cpus(self):
"""Test that numbers followed by 'cores' or 'cpus' are excluded."""
output = """System resources:
Allocated: 16 cores
Available CPUs: 32
Using 8 cpus
Active cpu: 1
Job ID: 555666"""
result = validate_or_get_slurm_job_id("555666", output)
assert result == "555666"
def test_output_with_mixed_case_units(self):
"""Test mixed case memory units (MiB, GiB, etc.)."""
output = """Allocated: 512 MiB
Reserved: 2 GiB
Cache: 128 KiB
Job ID is 111222"""
result = validate_or_get_slurm_job_id("111222", output)
assert result == "111222"
def test_decimal_numbers_excluded(self):
"""Test that decimal numbers are not matched as job IDs."""
output = """Performance: 23.3 MB/s
Efficiency: 99.9%
Job: 666777
Load: 1.5"""
result = validate_or_get_slurm_job_id("666777", output)
assert result == "666777"
def test_percentage_with_space(self):
"""Test percentages with space before % sign."""
output = """Completion: 45 %
Progress: 78.5 %
Job ID: 555444"""
result = validate_or_get_slurm_job_id("555444", output)
assert result == "555444"
def test_units_with_hyphen(self):
"""Test units followed by hyphen."""
output = """Memory: 256M-512M range
Job: 888999"""
result = validate_or_get_slurm_job_id("888999", output)
assert result == "888999"
def test_units_with_period(self):
"""Test units followed by period."""
output = """Allocated 128G. Starting job 777888."""
result = validate_or_get_slurm_job_id("777888", output)
assert result == "777888"
def test_multiple_job_ids_error(self):
"""Test that multiple possible job IDs raise an error."""
output = """Previous job: 11111
New job: 22222
Both are active"""
with pytest.raises(
WorkflowError, match=r"Multiple possible SLURM job IDs found"
):
validate_or_get_slurm_job_id("invalid", output)
def test_no_valid_job_id_error(self):
"""Test that output with no valid job ID raises an error."""
output = """Error: 23.3%
Memory: 128 GiB
Status: 99.9% complete"""
with pytest.raises(WorkflowError, match=r"No valid SLURM job ID found"):
validate_or_get_slurm_job_id("invalid", output)
def test_complex_multiline_output(self):