Skip to content

Commit f29a378

Browse files
committed
Add unit tests to test against invalid and unsupported constraint expressions
1 parent fdf7b6d commit f29a378

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

reframe/core/schedulers/slurm.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,13 +678,21 @@ def is_down(self):
678678
return not self.is_avail()
679679

680680
def satisfies(self, slurm_constraint):
681-
# Convert the Slurm constraint to a Python expression and evaluate it
681+
# Convert the Slurm constraint to a Python expression and evaluate it,
682+
# but restrict our syntax to accept only AND or OR constraints and
683+
# their combinations
684+
if not re.match(r'^[\w\d\(\)\|\&]*$', slurm_constraint):
685+
return False
686+
682687
names = {grp[0]
683688
for grp in re.finditer(r'(\w(\w|\d)*)', slurm_constraint)}
684689
expr = slurm_constraint.replace('|', ' or ').replace('&', ' and ')
685690
vars = {n: True for n in self.active_features}
686691
vars.update({n: False for n in names - self.active_features})
687-
return eval(expr, {}, vars)
692+
try:
693+
return eval(expr, {}, vars)
694+
except BaseException:
695+
return False
688696

689697
@property
690698
def active_features(self):

unittests/test_schedulers.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,19 @@ def test_flex_alloc_enough_nodes_constraint_expr(make_flexible_job):
11831183
assert job.num_tasks == 8
11841184

11851185

1186+
def test_flex_alloc_nodes_unsupported_constraint(make_flexible_job):
1187+
job = make_flexible_job('all')
1188+
job.options = ['-C "[f1*2&f2*4]"']
1189+
with pytest.raises(JobError):
1190+
prepare_job(job)
1191+
1192+
def test_flex_alloc_nodes_invalid_constraint(make_flexible_job):
1193+
job = make_flexible_job('all')
1194+
job.options = ['-C "(f1|f2)&"']
1195+
with pytest.raises(JobError):
1196+
prepare_job(job)
1197+
1198+
11861199
def test_flex_alloc_not_enough_nodes_constraint_expr(make_flexible_job):
11871200
job = make_flexible_job('all')
11881201
job.options = ['-C "(f1|f2)&(f8|f9)"']

0 commit comments

Comments
 (0)