Debian Codegraph @

tests/unit/job_runner_handlers/test_slurm.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import pytest
18
19 from cylc.flow.job_runner_handlers.slurm import JOB_RUNNER_HANDLER
20
21
22 @pytest.mark.parametrize(
23 'job_conf,lines',
24 [
25 ( # basic
26 {
27 'directives': {},
28 'execution_time_limit': 180,
29 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
30 'workflow_name': 'chop',
31 'task_id': '1/axe',
32 },
33 [
34 '#SBATCH --job-name=axe.1.chop',
35 (
36 '#SBATCH --output='
37 'cylc-run/chop/log/job/1/axe/01/job.out'
38 ),
39 (
40 '#SBATCH --error='
41 'cylc-run/chop/log/job/1/axe/01/job.err'
42 ),
43 '#SBATCH --time=3:00',
44 ],
45 ),
46 ( # task name with % character
47 {
48 'directives': {},
49 'execution_time_limit': 180,
50 'job_file_path': (
51 'cylc-run/chop/log/job/1/axe%40HEAD/01/job'
52 ),
53 'workflow_name': 'chop',
54 'task_id': '1/axe%40HEAD',
55 },
56 [
57 '#SBATCH --job-name=axe%40HEAD.1.chop',
58 (
59 '#SBATCH --output'
60 '=cylc-run/chop/log/job/1/axe%%40HEAD/01/job.out'
61 ),
62 (
63 '#SBATCH --error'
64 '=cylc-run/chop/log/job/1/axe%%40HEAD/01/job.err'
65 ),
66 '#SBATCH --time=3:00',
67 ],
68 ),
69 ( # some useful directives
70 {
71 'directives': {
72 '-p': 'middle',
73 '--no-requeue': '',
74 '--mem': '256gb',
75 },
76 'execution_time_limit': 200,
77 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
78 'workflow_name': 'chop',
79 'task_id': '1/axe',
80 },
81 [
82 '#SBATCH --job-name=axe.1.chop',
83 (
84 '#SBATCH --output='
85 'cylc-run/chop/log/job/1/axe/01/job.out'
86 ),
87 (
88 '#SBATCH --error='
89 'cylc-run/chop/log/job/1/axe/01/job.err'
90 ),
91 '#SBATCH --time=3:20',
92 '#SBATCH -p=middle',
93 '#SBATCH --no-requeue',
94 '#SBATCH --mem=256gb',
95 ],
96 ),
97 ( # heterogeneous job
98 {
99 'directives': {
100 '-p': 'middle',
101 'hetjob_0_--mem': '128gb',
102 'hetjob_1_--mem': '256gb',
103 },
104 'execution_time_limit': 200,
105 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
106 'workflow_name': 'chop',
107 'task_id': '1/axe',
108 },
109 [
110 '#SBATCH --job-name=axe.1.chop',
111 (
112 '#SBATCH --output='
113 'cylc-run/chop/log/job/1/axe/01/job.out'
114 ),
115 (
116 '#SBATCH --error='
117 'cylc-run/chop/log/job/1/axe/01/job.err'
118 ),
119 '#SBATCH --time=3:20',
120 '#SBATCH -p=middle',
121 '#SBATCH --mem=128gb',
122 '#SBATCH hetjob',
123 '#SBATCH --mem=256gb',
124 ],
125 ),
126
127 ],
128 )
129 def test_format_directives(job_conf: dict, lines: list):
130 assert JOB_RUNNER_HANDLER.format_directives(job_conf) == lines
131
132
133 @pytest.mark.parametrize(
134 'job_ids,cmd',
135 [
136 [['1234567'], ['squeue', '-h', '-j', '1234567']],
137 [
138 ['1234567', '709394', '30624700'],
139 ['squeue', '-h', '-j', '1234567,709394,30624700'],
140 ],
141 ],
142 )
143 def test_get_poll_many_cmd(job_ids: list, cmd: list):
144 assert JOB_RUNNER_HANDLER.get_poll_many_cmd(job_ids) == cmd
145
146
147 @pytest.mark.parametrize(
148 'out,job_ids',
149 [
150 [
151 """HEADING
152 1234567 JOB PROPERTIES
153 709394 JOB PROPERTIES
154 30624700 JOB PROPERTIES
155 """, ['1234567', '30624700', '709394'],
156 ],
157 [
158 """HEADING
159 1234567+0 JOB PROPERTIES (HETERO)
160 1234567+1 JOB PROPERTIES (HETERO)
161 709394 JOB PROPERTIES
162 30624700 JOB PROPERTIES
163 """, ['1234567', '30624700', '709394'],
164 ],
165 ],
166 )
167 def test_filter_poll_many_output(job_ids: list, out: str):
168 assert sorted(JOB_RUNNER_HANDLER.filter_poll_many_output(out)) == job_ids