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

feat: Add SlurmOpsError to slurm_ops #13

Merged
merged 1 commit into from
Jul 10, 2024
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
13 changes: 11 additions & 2 deletions lib/charms/hpc_libs/v0/slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def _on_install(self, _) -> None:
_kebabize = re.compile(r"(?<=[a-z0-9])(?=[A-Z])")


class SlurmOpsError(Exception):
"""Exception raised when a slurm operation failed."""

@property
def message(self) -> str:
"""Return message passed as argument to exception."""
return self.args[0]


def format_key(key: str) -> str:
"""Format Slurm configuration keys from SlurmCASe into kebab case.

Expand Down Expand Up @@ -129,7 +138,7 @@ def _call(cmd: str, *args: str, stdin: Optional[str] = None) -> str:
"""Call a command with logging.

Raises:
subprocess.CalledProcessError: Raised if the command fails.
SlurmOpsError: Raised if the command fails.
"""
cmd = [cmd, *args]
_logger.debug(f"Executing command {cmd}")
Expand All @@ -138,7 +147,7 @@ def _call(cmd: str, *args: str, stdin: Optional[str] = None) -> str:
except subprocess.CalledProcessError as e:
_logger.error(f"`{' '.join(cmd)}` failed")
_logger.error(f"stderr: {e.stderr.decode()}")
raise
raise SlurmOpsError(f"command {cmd[0]} failed. Reason:\n{e.stderr.decode()}")


def _snap(*args) -> str:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_version(self, subcmd) -> None:
def test_call_error(self, subcmd) -> None:
"""Test that `slurm_ops` propagates errors when a command fails."""
subcmd.side_effect = subprocess.CalledProcessError(-1, cmd=[""], stderr=b"error")
with self.assertRaises(subprocess.CalledProcessError):
with self.assertRaises(slurm.SlurmOpsError):
slurm.install()


Expand Down