Skip to content

Commit 1233e5d

Browse files
Merge pull request #13 from larsbuntemeyer/parser-tests
Parser tests
2 parents dd1aac7 + ca7e904 commit 1233e5d

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

enjoy_slurm/slurm.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from .utils import (
44
kwargs_to_list,
5+
args_to_list,
56
parse_sacct,
67
execute,
78
create_scontrol_func,
@@ -31,13 +32,20 @@ def sbatch(jobscript=None, *args, **kwargs):
3132
jobid : int
3233
Slurm jobid.
3334
35+
3436
"""
3537
if jobscript is None:
3638
jobscript = []
3739
else:
3840
jobscript = [jobscript]
3941

40-
command = ["sbatch", "--parsable"] + list(args) + kwargs_to_list(kwargs) + jobscript
42+
command = (
43+
["sbatch", "--parsable"]
44+
+ args_to_list(args)
45+
+ kwargs_to_list(kwargs)
46+
+ jobscript
47+
)
48+
4149
jobid = int(execute(command))
4250

4351
return jobid

enjoy_slurm/utils.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,26 @@ def parse_slurm_arg(a):
4343
"""parse slurm arguments to str or list of str with colons"""
4444
if isinstance(a, list):
4545
return ":".join([str(x) for x in a])
46+
if a is True:
47+
return []
4648
return [str(a)]
4749

4850

51+
def args_to_list(args):
52+
"""parse sbatch arguments to list"""
53+
args_list = []
54+
for a in args:
55+
if isinstance(a, list):
56+
args_list += a
57+
elif "=" in a:
58+
args_list += a.split("=")
59+
elif " " in a:
60+
args_list += a.split()
61+
else:
62+
args_list.append(a)
63+
return args_list
64+
65+
4966
def kwargs_to_list(d):
5067
"""parse arguments to command line arguments for sbatch"""
5168
r = []
@@ -54,12 +71,21 @@ def kwargs_to_list(d):
5471
if k in own_kwargs:
5572
continue
5673
flag = "--" + k.replace("_", "-")
57-
r += [flag]
74+
5875
if k == "dependency" and v is not None:
59-
# flag += "=" + parse_dependency(v, d.get("how", None))
76+
r += [flag]
6077
r += parse_dependency(v, d.get("how", None))
61-
elif v is not True:
78+
continue
79+
if k == "kill_on_invalid_dep" and not isinstance(v, str):
80+
r += [flag]
81+
r += ["no"] if v is False else ["yes"]
82+
continue
83+
if v:
84+
r += [flag]
6285
r += parse_slurm_arg(v)
86+
continue
87+
if v is False:
88+
continue
6389
# r += [flag]
6490
return r
6591

tests/test_parser.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import pytest
22

3-
from enjoy_slurm.utils import parse_dependency, kwargs_to_list, handle_sacct_format
3+
from enjoy_slurm.utils import (
4+
parse_dependency,
5+
kwargs_to_list,
6+
handle_sacct_format,
7+
args_to_list,
8+
)
49
from enjoy_slurm.config import default_sacct_format
510

611

@@ -12,6 +17,10 @@ def test_parse_dependency():
1217

1318

1419
def test_kwargs_to_list():
20+
assert kwargs_to_list({"hello": True}) == ["--hello"]
21+
assert kwargs_to_list({"hello": False}) == []
22+
assert kwargs_to_list({"hello": 1}) == ["--hello", "1"]
23+
1524
kwargs = {"partition": "test", "dependency": [1, 2, 3]}
1625
assert kwargs_to_list(kwargs) == [
1726
"--partition",
@@ -26,6 +35,41 @@ def test_kwargs_to_list():
2635
"--dependency",
2736
"afterany:1:2:3",
2837
]
38+
kwargs = {
39+
"partition": "test",
40+
"dependency": [1, 2, 3],
41+
"how": "afterany",
42+
"kill_on_invalid_dep": True,
43+
}
44+
assert kwargs_to_list(kwargs) == [
45+
"--partition",
46+
"test",
47+
"--dependency",
48+
"afterany:1:2:3",
49+
"--kill-on-invalid-dep",
50+
"yes",
51+
]
52+
kwargs = {"kill_on_invalid_dep": False}
53+
assert kwargs_to_list(kwargs) == ["--kill-on-invalid-dep", "no"]
54+
kwargs = {"kill_on_invalid_dep": "yes"}
55+
assert kwargs_to_list(kwargs) == ["--kill-on-invalid-dep", "yes"]
56+
57+
58+
def test_args_to_list():
59+
assert args_to_list(("--partition=shared",)) == ["--partition", "shared"]
60+
assert args_to_list(("--partition shared",)) == ["--partition", "shared"]
61+
assert args_to_list(("--partition shared", "--dependency=afterok:1:2:3")) == [
62+
"--partition",
63+
"shared",
64+
"--dependency",
65+
"afterok:1:2:3",
66+
]
67+
assert args_to_list(("--partition shared", ["--dependency", "afterok:1:2:3"])) == [
68+
"--partition",
69+
"shared",
70+
"--dependency",
71+
"afterok:1:2:3",
72+
]
2973

3074

3175
def test_sacct_format():

tests/test_with_slurm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
print(hostname)
1212

13+
if on_levante:
14+
kwargs = {"partition": "shared", "account": "ch0636"}
15+
else:
16+
kwargs = {}
17+
1318

1419
@requires_slurm
1520
def test_sbatch():
@@ -30,3 +35,11 @@ def test_partitions():
3035
for p in partitions:
3136
pdict = slurm.scontrol.show(partition=p)
3237
assert p in pdict
38+
39+
40+
@requires_slurm
41+
def test_sacct():
42+
jobid = slurm.sbatch(wrap="echo Hello World", **kwargs)
43+
acct = slurm.sacct(jobid=jobid)
44+
scon = slurm.scontrol.show(jobid=jobid)
45+
assert str(jobid) in scon

0 commit comments

Comments
 (0)