Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising error for reserved keywords under function parameter options in get_allocation #325

Merged
merged 25 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions smartsim/error/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@
SSInternalError,
SSUnsupportedError,
UserStrategyError,
SSReservedKeywordError,

)
4 changes: 4 additions & 0 deletions smartsim/error/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def create_message(file_path: str, read: bool) -> str:
return msg


class SSReservedKeywordError(SmartSimError):
"""Raised when a Reserved Keyword is used incorrectly"""


# Internal Exceptions


Expand Down
34 changes: 22 additions & 12 deletions smartsim/wlm/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
from .._core.launcher.slurm.slurmParser import parse_salloc, parse_salloc_error
from .._core.launcher.util.launcherUtil import ComputeNode, Partition
from .._core.utils.helpers import init_default
from ..error import AllocationError, LauncherError, SmartSimError
from ..error import (
AllocationError,
LauncherError,
SmartSimError,
SSReservedKeywordError,
)
from ..log import get_logger

logger = get_logger(__name__)
Expand Down Expand Up @@ -241,24 +246,29 @@ def _get_alloc_cmd(
"-J",
"SmartSim",
]
# TODO check format here
if time:
salloc_args.extend(["-t", time])
if account:
salloc_args.extend(["-A", str(account)])

arguments = set(options.keys() if options is not None else {})
invalid = {"t", "time", "N", "nodes", "A", "account"}

if valid := arguments.intersection(invalid):
raise SSReservedKeywordError(
f"Expecting time, nodes, account as an argument. Also received: {valid}"
)

for opt, val in (options or {}).items():
if opt not in ["t", "time", "N", "nodes", "A", "account"]:
short_arg = bool(len(str(opt)) == 1)
prefix = "-" if short_arg else "--"
if not val:
salloc_args += [prefix + opt]
short_arg = bool(len(str(opt)) == 1)
prefix = "-" if short_arg else "--"
if not val:
salloc_args += [prefix + opt]
else:
juliaputko marked this conversation as resolved.
Show resolved Hide resolved
if short_arg:
salloc_args += [prefix + opt, str(val)]
else:
if short_arg:
salloc_args += [prefix + opt, str(val)]
else:
salloc_args += ["=".join((prefix + opt, str(val)))]

salloc_args += ["=".join((prefix + opt, str(val)))]
return salloc_args


Expand Down
24 changes: 22 additions & 2 deletions tests/full_wlm/test_slurm_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import pytest

from smartsim.error import AllocationError
from smartsim.error import AllocationError, SSReservedKeywordError
from smartsim.wlm import slurm

# retrieved from pytest fixtures
Expand All @@ -48,11 +48,31 @@ def test_get_release_allocation_w_options(wlmutils):
"""test slurm interface for obtaining allocations"""
options = {"ntasks-per-node": 1}
account = wlmutils.get_test_account()
alloc = slurm.get_allocation(nodes=1, time="00:05:00", options=options, account=account)
alloc = slurm.get_allocation(
nodes=1, time="00:05:00", options=options, account=account
)
time.sleep(5) # give slurm a rest
slurm.release_allocation(alloc)


@pytest.mark.parametrize(
"func_parameters,test_parameters",
[
("t", "T"),
("time", "TIME"),
("nodes", "NODES"),
("N", "N"),
("account", "ACCOUNT"),
("A", "A"),
],
)
def test_get_allocation_bad_params(func_parameters, test_parameters):
"""test get_allocation with reserved keywords as option"""
with pytest.raises(SSReservedKeywordError) as e:
alloc = slurm.get_allocation(options={func_parameters: test_parameters})
assert "Expecting time, nodes, account as an argument" in e.value.args[0]


# --------- Error handling ----------------------------


Expand Down
21 changes: 0 additions & 21 deletions tests/test_slurm_get_alloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,3 @@ def test_get_alloc_format():
"--ntasks-per-node=5",
]
assert alloc_cmd == result


def test_get_alloc_format_overlap():
"""Test get alloc with collision between arguments and options"""
time = "10:00:00"
nodes = 5
account = "A35311"
options = {"N": 10, "time": "15", "account": "S1242"}
alloc_cmd = _get_alloc_cmd(nodes, time, account, options)
result = [
"--no-shell",
"-N",
"5",
"-J",
"SmartSim",
"-t",
"10:00:00",
"-A",
"A35311",
]
assert result == alloc_cmd
Loading