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: enable bulk getting and unsetting of configuration options #12

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 12 additions & 10 deletions lib/charms/hpc_libs/v0/slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def _on_install(self, _) -> None:
"format_key",
"install",
"version",
"ConfigurationManager",
"ServiceType",
"SlurmManagerBase",
]
Expand Down Expand Up @@ -202,8 +203,8 @@ def restart(self) -> None:
class ConfigurationManager:
"""Control configuration of a Slurm component."""

def __init__(self, service: ServiceType) -> None:
self._service = service
def __init__(self, name: str) -> None:
self._name = name

def get_options(self, *keys: str) -> Mapping[str, Any]:
"""Get given configurations values for Slurm component."""
Expand All @@ -215,29 +216,30 @@ def get_options(self, *keys: str) -> Mapping[str, Any]:

return configs

def get(self, key: str) -> Any:
def get(self, key: Optional[str] = None) -> Any:
"""Get specific configuration value for Slurm component."""
key = f"{self._service.config_name}.{key}"
key = f"{self._name}.{key}" if key else self._name
config = json.loads(_snap("get", "-d", "slurm", key))
return config[key]

def set(self, config: Mapping[str, Any]) -> None:
"""Set configuration for Slurm component."""
args = [f"{self._service.config_name}.{k}={json.dumps(v)}" for k, v in config.items()]
args = [f"{self._name}.{k}={json.dumps(v)}" for k, v in config.items()]
_snap("set", "slurm", *args)

def unset(self, *keys) -> None:
def unset(self, *keys: str) -> None:
"""Unset configuration for Slurm component."""
args = [f"{self._service.config_name}.{k}!" for k in keys]
args = [f"{self._name}.{k}" for k in keys] if len(keys) > 0 else [self._name]
_snap("unset", "slurm", *args)


class MungeManager(ServiceManager):
"""Manage `munged` service operations."""

def __init__(self) -> None:
self._service = ServiceType.MUNGED
self.config = ConfigurationManager(ServiceType.MUNGED)
service = ServiceType.MUNGED
self._service = service
self.config = ConfigurationManager(service.config_name)

def get_key(self) -> str:
"""Get the current munge key.
Expand Down Expand Up @@ -265,5 +267,5 @@ class SlurmManagerBase(ServiceManager):

def __init__(self, service: ServiceType) -> None:
self._service = service
self.config = ConfigurationManager(service)
self.config = ConfigurationManager(service.config_name)
self.munge = MungeManager()
24 changes: 19 additions & 5 deletions tests/unit/test_slurm_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ def test_config_name(self, *_) -> None:
def test_enable(self, subcmd, *_) -> None:
"""Test that the manager calls the correct enable command."""
self.manager.enable()
calls = [args[0][0] for args in subcmd.call_args_list]

args = subcmd.call_args[0][0]
self.assertEqual(
calls[0], ["snap", "start", "--enable", f"slurm.{self.manager._service.value}"]
args, ["snap", "start", "--enable", f"slurm.{self.manager._service.value}"]
)

def test_disable(self, subcmd, *_) -> None:
"""Test that the manager calls the correct disable command."""
self.manager.disable()
calls = [args[0][0] for args in subcmd.call_args_list]

args = subcmd.call_args[0][0]
self.assertEqual(
calls[0], ["snap", "stop", "--disable", f"slurm.{self.manager._service.value}"]
args, ["snap", "stop", "--disable", f"slurm.{self.manager._service.value}"]
)

def test_restart(self, subcmd, *_) -> None:
Expand Down Expand Up @@ -120,6 +120,14 @@ def test_get_config(self, subcmd, *_) -> None:
self.assertEqual(args, ["snap", "get", "-d", "slurm", f"{self.config_name}.key"])
self.assertEqual(value, "value")

def test_get_config_all(self, subcmd) -> None:
"""Test that manager calls the correct `snap get ...` with no arguments given."""
subcmd.return_value = '{"%s": "value"}' % self.config_name
value = self.manager.config.get()
args = subcmd.call_args[0][0]
self.assertEqual(args, ["snap", "get", "-d", "slurm", self.config_name])
self.assertEqual(value, "value")

def test_set_config(self, subcmd, *_) -> None:
"""Test that the manager calls the correct `snap set ...` command."""
self.manager.config.set({"key": "value"})
Expand All @@ -130,7 +138,13 @@ def test_unset_config(self, subcmd) -> None:
"""Test that the manager calls the correct `snap unset ...` command."""
self.manager.config.unset("key")
args = subcmd.call_args[0][0]
self.assertEqual(args, ["snap", "unset", "slurm", f"{self.config_name}.key!"])
self.assertEqual(args, ["snap", "unset", "slurm", f"{self.config_name}.key"])

def test_unset_config_all(self, subcmd) -> None:
"""Test the manager calls the correct `snap unset ...` with no arguments given."""
self.manager.config.unset()
args = subcmd.call_args[0][0]
self.assertEqual(args, ["snap", "unset", "slurm", self.config_name])

def test_generate_munge_key(self, subcmd, *_) -> None:
"""Test that the manager calls the correct `mungectl` command."""
Expand Down