From ec9dcc92bda0e71ff5fb32289a07f137ee153d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Juli=C3=A1n=20Espina?= Date: Fri, 12 Jul 2024 13:33:10 -0600 Subject: [PATCH] feat: add method to get the host name --- lib/charms/hpc_libs/v0/slurm_ops.py | 8 +++++++- tests/unit/test_slurm_ops.py | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/charms/hpc_libs/v0/slurm_ops.py b/lib/charms/hpc_libs/v0/slurm_ops.py index d49a38a..e5dc001 100644 --- a/lib/charms/hpc_libs/v0/slurm_ops.py +++ b/lib/charms/hpc_libs/v0/slurm_ops.py @@ -67,6 +67,7 @@ def _on_install(self, _) -> None: import json import logging import re +import socket import subprocess from collections.abc import Mapping from enum import Enum @@ -82,7 +83,7 @@ def _on_install(self, _) -> None: # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 3 +LIBPATCH = 4 # Charm library dependencies to fetch during `charmcraft pack`. PYDEPS = ["pyyaml>=6.0.1"] @@ -278,3 +279,8 @@ def __init__(self, service: ServiceType) -> None: self._service = service self.config = ConfigurationManager(service.config_name) self.munge = MungeManager() + + @property + def hostname(self) -> str: + """The hostname where this manager is running.""" + return socket.gethostname().split(".")[0] diff --git a/tests/unit/test_slurm_ops.py b/tests/unit/test_slurm_ops.py index 2d4b1c5..36dcf51 100644 --- a/tests/unit/test_slurm_ops.py +++ b/tests/unit/test_slurm_ops.py @@ -173,6 +173,14 @@ def test_configure_munge(self, subcmd) -> None: args = subcmd.call_args[0][0] self.assertEqual(args, ["snap", "set", "slurm", "munge.max-thread-count=24"]) + @patch("charms.hpc_libs.v0.slurm_ops.socket.gethostname") + def test_hostname(self, gethostname, *_) -> None: + """Test that manager is able to correctly get the host name.""" + gethostname.return_value = "machine" + self.assertEqual(self.manager.hostname, "machine") + gethostname.return_value = "machine.domain.com" + self.assertEqual(self.manager.hostname, "machine") + parameters = [ (SlurmManagerBase(ServiceType.SLURMCTLD), "slurm"),