Debian Codegraph @

tests/unit/job_runner_handlers/test_pbs.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.pbs import (
20 JOB_RUNNER_HANDLER,
21 PBSHandler
22 )
23 from cylc.flow.job_runner_mgr import JobRunnerManager
24
25
26 VERY_LONG_STR = 'x' * 240
27
28
29 @pytest.mark.parametrize(
30 'job_conf,lines',
31 [
32 pytest.param(
33 {
34 'directives': {},
35 'execution_time_limit': 180,
36 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
37 'workflow_name': 'chop',
38 'task_id': '1/axe',
39 'platform': {
40 'job runner': 'pbs',
41 'job name length maximum': 100,
42 },
43 },
44 [
45 '#PBS -N axe.1.chop',
46 '#PBS -o cylc-run/chop/log/job/1/axe/01/job.out',
47 '#PBS -e cylc-run/chop/log/job/1/axe/01/job.err',
48 '#PBS -l walltime=180',
49 ],
50 id='basic',
51 ),
52 pytest.param(
53 {
54 'directives': {},
55 'execution_time_limit': 180,
56 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
57 'workflow_name': 'chop',
58 'task_id': VERY_LONG_STR,
59 'platform': {
60 'job runner': 'pbs',
61 },
62 },
63 [
64 '#PBS -N '
65 f'None.{VERY_LONG_STR[: PBSHandler.JOB_NAME_LEN_MAX - 5]}',
66 '#PBS -o cylc-run/chop/log/job/1/axe/01/job.out',
67 '#PBS -e cylc-run/chop/log/job/1/axe/01/job.err',
68 '#PBS -l walltime=180',
69 ],
70 id='long-job-name',
71 ),
72 pytest.param(
73 {
74 'directives': {},
75 'execution_time_limit': 180,
76 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
77 'workflow_name': 'chop',
78 'task_id': '1/axe',
79 'platform': {
80 'job runner': 'pbs',
81 'job name length maximum': 6,
82 },
83 },
84 [
85 '#PBS -N axe.1.',
86 '#PBS -o cylc-run/chop/log/job/1/axe/01/job.out',
87 '#PBS -e cylc-run/chop/log/job/1/axe/01/job.err',
88 '#PBS -l walltime=180',
89 ],
90 id='truncate-job-name',
91 ),
92 pytest.param(
93 {
94 'directives': {
95 '-q': 'forever',
96 '-V': '',
97 '-l mem': '256gb',
98 },
99 'execution_time_limit': 180,
100 'job_file_path': 'cylc-run/chop/log/job/1/axe/01/job',
101 'workflow_name': 'chop',
102 'task_id': '1/axe',
103 'platform': {
104 'job runner': 'pbs',
105 'job name length maximum': 100,
106 },
107 },
108 [
109 '#PBS -N axe.1.chop',
110 '#PBS -o cylc-run/chop/log/job/1/axe/01/job.out',
111 '#PBS -e cylc-run/chop/log/job/1/axe/01/job.err',
112 '#PBS -l walltime=180',
113 '#PBS -q forever',
114 '#PBS -V',
115 '#PBS -l mem=256gb',
116 ],
117 id='custom-directives',
118 ),
119 ],
120 )
121 def test_format_directives(job_conf: dict, lines: list):
122 assert JOB_RUNNER_HANDLER.format_directives(job_conf) == lines
123
124
125 def test_filter_poll_many_output():
126 """It should strip trailing junk from job IDs.
127
128 Job IDs are assumed to be a series of numbers, optionally followed by a
129 full-stop and some other letters and numbers which are not needed for
130 job tracking purposes.
131
132 Job IDs are not expected to start with letters e.g. `abc.456` is not
133 supported.
134 """
135 assert JOB_RUNNER_HANDLER.filter_poll_many_output('''
136 Job id Name User Time Use S Queue
137 ---------------- ---------------- ---------------- -------- - -----
138 12345.foo.bar.baz test-pbs xxxxxxx 0 Q reomq
139 23456.foo test-pbs xxxxxxx 0 Q romeq
140 34567 test-pbs xxxxxxx 1 Q romeq
141 abc.456 test-pbs xxxxxxx 2 Q romeq
142 abcdef test-pbs xxxxxxx 2 Q romeq
143 ''') == ['12345', '23456', '34567']
144
145
146 def test_filter_submit_output(tmp_path):
147 """See notes for test_filter_poll_many_output."""
148 status_file = tmp_path / 'submit_out'
149 status_file.touch()
150
151 def test(out):
152 return JobRunnerManager._filter_submit_output(
153 status_file,
154 JOB_RUNNER_HANDLER,
155 out,
156 '',
157 )[2]
158
159 assert test(' 12345.foo.bar.baz') == '12345'
160 assert test(' 12345.foo') == '12345'
161 assert test(' 12345') == '12345'