Skip to content

Commit

Permalink
feat: add method to check if a service is active
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jul 12, 2024
1 parent bacb4d3 commit cd3a3e9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/charms/hpc_libs/v0/slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def _on_install(self, _) -> None:
"ConfigurationManager",
"ServiceType",
"SlurmManagerBase",
"SlurmOpsError",
]

import json
Expand Down Expand Up @@ -208,6 +209,20 @@ def restart(self) -> None:
"""Restart service."""
_snap("restart", f"slurm.{self._service.value}")

def active(self) -> bool:
"""Return True if the service is active."""
info = yaml.safe_load(_snap("info", "slurm"))
if (services := info.get("services")) is None:
raise SlurmOpsError("unable to retrive snap info. Ensure slurm is correctly installed")

if (state := services.get(f"slurm.{self._service.value}")) is None:
raise SlurmOpsError(f"could not find unknown service `slurm.{self._service.value}`")

# We could also parse this into type, startup and status, but it's not worth the additional
# complexity.
# We don't do `"active" in state` because the word "active" is also part of "inactive" :)
return "inactive" not in state


class ConfigurationManager:
"""Control configuration of a Slurm component."""
Expand Down
10 changes: 8 additions & 2 deletions tests/integration/slurm_ops/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def slurmctld() -> SlurmManagerBase:
def test_install(slurmctld: SlurmManagerBase) -> None:
"""Install Slurm using the manager."""
slurm.install()
slurmctld.enable()
slurmctld.munge.generate_key()

with open("/var/snap/slurm/common/etc/munge/munge.key", "rb") as f:
Expand All @@ -40,7 +39,7 @@ def test_rotate_key(slurmctld: SlurmManagerBase) -> None:
@pytest.mark.order(3)
def test_slurm_config(slurmctld: SlurmManagerBase) -> None:
"""Test that the slurm config can be changed."""
slurmctld.config.set({"cluster-name": "test-cluster"})
slurmctld.config.set({"slurmctld-host": "test-slurm-ops", "cluster-name": "test-cluster"})
value = slurmctld.config.get("cluster-name")
assert value == "test-cluster"

Expand All @@ -57,6 +56,13 @@ def test_slurm_config(slurmctld: SlurmManagerBase) -> None:


@pytest.mark.order(4)
def test_enable_service(slurmctld: SlurmManagerBase) -> None:
"""Test that the slurmctl daemon can be enabled."""
slurmctld.enable()
assert slurmctld.active()


@pytest.mark.order(5)
def test_version() -> None:
"""Test that the Slurm manager can report its version."""
version = slurm.version()
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
- slurm.command1
- slurm.command2
services:
slurm.logrotate: oneshot, enabled, inactive
slurm.munged: simple, enabled, active
slurm.slurm-prometheus-exporter: simple, disabled, inactive
slurm.slurmctld: simple, disabled, active
slurm.slurmd: simple, enabled, active
slurm.slurmdbd: simple, disabled, active
slurm.slurmrestd: simple, disabled, active
channels:
latest/stable: –
latest/candidate: 23.11.7 2024-06-26 (460) 114MB classic
Expand Down Expand Up @@ -101,6 +106,11 @@ def test_restart(self, subcmd, *_) -> None:
args = subcmd.call_args[0][0]
self.assertEqual(args, ["snap", "restart", f"slurm.{self.manager._service.value}"])

def test_active(self, subcmd, *_) -> None:
"""Test that the manager can detect that a service is active."""
subcmd.return_value = SLURM_INFO.encode()
self.assertTrue(self.manager.active())

def test_get_options(self, subcmd) -> None:
"""Test that the manager correctly collects all requested configuration options."""
subcmd.return_value = '{"%(name)s.key1": "value1", "%(name)s.key2": "value2"}' % {
Expand Down

0 comments on commit cd3a3e9

Please sign in to comment.