-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathrun-tests.py
executable file
·380 lines (342 loc) · 12.9 KB
/
run-tests.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
#!/usr/bin/env python3
# pylint: disable=broad-except,consider-using-f-string,duplicate-code
# pylint: disable=invalid-name,redefined-outer-name
#
# This file is part of Slurm-Mail.
#
# Slurm-Mail is a drop in replacement for Slurm's e-mails to give users
# much more information about their jobs compared to the standard Slurm
# e-mails.
#
# Copyright (C) 2018-2024 Neil Munday ([email protected])
#
# Slurm-Mail is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# Slurm-Mail is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Slurm-Mail. If not, see <http://www.gnu.org/licenses/>.
#
"""
run-tests.py
Author: Neil Munday
Script to automatically run tests for Slurm-Mail.
"""
import argparse
import configparser
import logging
import pathlib
import os
import re
import sys
import time
import yaml # type: ignore
import slurmmail
from slurmmail.common import check_file, check_dir, die, run_command
def echo_log(log_file: pathlib.Path):
"""
Helper function to display the contents of the given file.
"""
if log_file.is_file():
with open(log_file, mode="r", encoding="utf-8") as f:
log_output = f.read().split("\n")
lines = ""
for line in log_output:
lines += "---> {0}\n".format(line)
logging.error("%s contents:\n%s", log_file, lines)
else:
logging.warning("%s does not exist", log_file)
def remove_logs():
"""
Delete Slurm-Mail log files
"""
if spool_log is not None and spool_log.is_file():
logging.debug("deleting: %s", spool_log)
os.unlink(spool_log)
else:
logging.debug("%s does not exist", spool_log)
if send_log is not None and send_log.is_file():
logging.debug("deleting: %s", send_log)
os.unlink(send_log)
else:
logging.debug("%s does not exist", send_log)
def wait_for_job():
"""
Wait for all jobs to complete.
"""
i = 0
limit = 120
for i in range(0, limit):
rtn, stdout, _ = run_command("squeue --noheader")
if rtn != 0:
die("failed to run squeue")
if stdout != "":
logging.debug("waiting for jobs to finish")
time.sleep(1)
else:
break
if i == limit:
die("jobs still running after {0}s".format(limit))
if __name__ == "__main__":
SLURM_SEND_MAIL_EXE = pathlib.Path("/usr/bin/slurm-send-mail")
MAIL_LOG = pathlib.Path("/var/log/supervisor/mailserver.log")
SLURMCTLD_LOG = pathlib.Path("/var/log/slurm/slurmctld.log")
parser = argparse.ArgumentParser(
description="Perform tests of Slurm-Mail", add_help=True
)
parser.add_argument(
"-i", "--input", help="Test input file (YAML)",
dest="input", required=True,
)
parser.add_argument(
"-k", "--keep-logs", help="Keep log files - can only be used when running a single test",
dest="keep_logs",
action="store_true"
)
parser.add_argument(
"-m", "--mail-log", help="Display mail log", dest="mail_log",
action="store_true"
)
parser.add_argument(
"-o", "--output", help="Output directory", dest="output",
required=True
)
parser.add_argument(
"-t", "--test", help="Run a particular test", dest="test"
)
parser.add_argument(
"-v", "--verbose", help="Turn on debug messages", dest="verbose",
action="store_true"
)
args = parser.parse_args()
if args.test is None and args.keep_logs:
die("--keep-logs can only be used when running one test")
log_date = "%Y/%m/%d %H:%M:%S"
log_format = "%(asctime)s:%(levelname)s: %(message)s"
log_level = logging.INFO
if args.verbose:
log_level = logging.DEBUG
logging.basicConfig(
format=log_format, datefmt=log_date, level=log_level
)
input_file = pathlib.Path(args.input)
output_dir = pathlib.Path(args.output)
spool_dir = pathlib.Path("/var/spool/slurm-mail")
logging.info("using tests defined in: %s", input_file)
check_file(SLURM_SEND_MAIL_EXE)
check_file(input_file)
check_dir(output_dir)
check_dir(spool_dir)
check_file(slurmmail.conf_file)
spool_log = None
send_log = None
try:
config = configparser.RawConfigParser()
config.read(str(slurmmail.conf_file))
send_log = pathlib.Path(config.get("slurm-send-mail", "logFile"))
spool_log = pathlib.Path(config.get("slurm-spool-mail", "logFile"))
except Exception as e:
die("Error: {0}".format(e))
with open(input_file, mode="r", encoding="utf-8") as stream:
dictionary = yaml.safe_load(stream)
if "tests" not in dictionary:
die("invalid YAML: could not find \"tests\" definition")
# check Slurm-Mail cron file is not present
if pathlib.Path("/etc/cron.d/slurm-mail").exists():
die("/etc/cron.d/slurm-mail exists!")
# check that slurm is up
logging.info("waiting for Slurm...")
nodes_found = -1
for i in range(0, 30):
rtn, stdout, _ = run_command("sinfo -h -r -N")
if rtn == 0:
nodes_found = len(stdout.strip().split("\n"))
break
time.sleep(1)
if nodes_found == -1:
die("no response from Slurm")
max_nodes = 1
for test, fields in dictionary["tests"].items():
if "options" in fields and "nodes" in fields["options"] and int(fields["options"]["nodes"]) > max_nodes:
max_nodes = fields["options"]["nodes"]
logging.info("Slurm is responding: %s nodes found", nodes_found)
if nodes_found < max_nodes:
die(f"needed {max_nodes} but only found {nodes_found} available")
error_re = re.compile(r":ERROR:")
send_email_re = re.compile(r":INFO: Sending e-mail to: root using root for job")
passed = 0
total = 0
for test, fields in dictionary["tests"].items():
if args.test and args.test != test:
logging.info("skipping test: %s", test)
continue
total += 1
logging.info("running: %s: %s", test, fields["description"])
logging.info("creating JCF...")
jcf_path = output_dir / "{0}.jcf".format(test)
with open(jcf_path, mode="w", encoding="utf-8") as jcf_file:
jcf_file.write("#!/bin/bash\n")
jcf_file.write("#SBATCH -J {0}\n".format(test))
jcf_file.write("#SBATCH -o {0}/%j.out\n".format(output_dir))
if "options" in fields:
if fields["hetjob"]:
slurm_options = []
for sbatch_option, sbatch_value in fields["options"].items():
slurm_options.append("--{0}={1}".format(
sbatch_option,
sbatch_value
))
sbatch_str = "#SBATCH {0}".format(
" ".join(slurm_options)
)
jcf_file.write("{0}\n".format(sbatch_str))
jcf_file.write("#SBATCH hetjob\n")
jcf_file.write("{0}\n".format(sbatch_str))
else:
for sbatch_option, sbatch_value in fields["options"].items():
jcf_file.write(
"#SBATCH --{0}={1}\n".format(
sbatch_option,
sbatch_value
)
)
jcf_file.write(fields["commands"])
# display generated JCF
with open(jcf_path, mode="r", encoding="utf-8") as jcf_file:
logging.debug("\n%s", jcf_file.read())
logging.info("submitting job...")
rtn, stdout, stderr = run_command("sbatch {0}".format(jcf_path))
if rtn != 0:
logging.error(
"%s failed: could not submit job\nstdout:\n%s\nstderr:\n%s",
test,
stdout,
stderr
)
dictionary["tests"][test]["pass"] = False
continue
# are there any post submit commands?
if "post_submit" in fields:
logging.info("%s running post submit commands", test)
for cmd in fields["post_submit"].split("\n"):
cmd = cmd.strip()
if cmd != "":
rtn, stdout, stderr = run_command(cmd)
if rtn != 0:
logging.error(
"%s post_submit failed: %s\nstdout:\n%s\nstderr:\n%s",
test,
cmd,
stdout,
stderr
)
dictionary["tests"][test]["pass"] = False
continue
logging.info("waiting for job to finish...")
wait_for_job()
# Although the job has finished there is a chance
# that slurmctld hasn't triggered slurm-spool-mail
# just yet.
logging.info(
"waiting for %s spool files...",
fields["spool_file_total"]
)
spoolOk = False
WAIT_FOR = 25
spool_file_total = 0
for i in range(0, WAIT_FOR):
spool_file_total = len(list(spool_dir.glob("*.mail")))
if spool_file_total == fields["spool_file_total"]: # noqa
spoolOk = True
break
time.sleep(1)
if spoolOk:
logging.info("%s: spool files created ok", test)
else:
logging.error(
"%s failed: found %s spool files, expected %s after %ss",
spool_file_total, fields["spool_file_total"], test, WAIT_FOR
)
dictionary["tests"][test]["pass"] = False
if spool_log.is_file():
echo_log(spool_log)
if SLURMCTLD_LOG.is_file():
echo_log(SLURMCTLD_LOG)
remove_logs()
continue
rtn, stdout, stderr = run_command(str(SLURM_SEND_MAIL_EXE))
if rtn != 0:
logging.error(
"failed to run %s\nsdtout:\n%s\nstderr:\n%s",
SLURM_SEND_MAIL_EXE,
stdout,
stderr
)
if len(list(spool_dir.glob("*.mail"))) != 0:
logging.error(
"%s failed: spool files still present - deleting for next test", # noqa
test
)
echo_log(send_log)
dictionary["tests"][test]["pass"] = False
for spool_file in spool_dir.glob("*.mail"):
logging.debug("deleting: %s", spool_file)
os.remove(str(spool_file))
continue
logging.info("spool files gone, checking log files")
if not fields["send_errors"]:
send_log_output = None
send_log_ok = True
send_email_found = False
with open(send_log, mode="r", encoding="utf-8") as f:
send_log_output = f.read().split("\n")
for line in send_log_output:
match = error_re.search(line)
if match:
send_log_ok = False
break
match = send_email_re.search(line)
if match:
send_email_found = True
if not send_log_ok or not send_email_found:
lines = ""
for line in send_log_output:
lines += "---> {0}\n".format(line)
if not send_log_ok:
logging.error(
"%s failed: errors present in %s:\n%s",
test,
send_log,
lines
)
elif not send_email_found:
logging.error(
"%s failed: no evidence of e-mail delivery in %s:\n%s",
test,
send_log,
lines
)
dictionary["tests"][test]["pass"] = False
if not args.keep_logs:
remove_logs()
continue
dictionary["tests"][test]["pass"] = True
passed += 1
logging.info("%s passed: OK", test)
if not args.keep_logs:
remove_logs()
if args.mail_log:
logging.info("Mail log:")
echo_log(MAIL_LOG)
# display test results
failed = total - passed
logging.info("passed: %d, failed: %d", passed, failed)
if failed > 0:
sys.exit(1)
sys.exit(0)