From 2fbb65b9074ca2bd2789d39221150e1fcbcbe705 Mon Sep 17 00:00:00 2001 From: Sudeep Bhandari <28506885+sudeephb@users.noreply.github.com> Date: Wed, 12 Jun 2024 05:34:19 +0545 Subject: [PATCH] Re-render smartctl service file on config changed (#262) --- src/service.py | 15 ++++++++++++++- tests/unit/test_service.py | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/service.py b/src/service.py index 7c721f6b..f93971c0 100644 --- a/src/service.py +++ b/src/service.py @@ -136,7 +136,7 @@ def _render_service(self, params: Dict[str, str]) -> bool: return write_to_file(self.exporter_service_path, content) def render_config(self) -> bool: - """Render exporter config file..""" + """Render exporter config file.""" if self.exporter_config_path is not None: content = self._render_config_content() return write_to_file(self.exporter_config_path, content, mode=0o600) @@ -287,6 +287,19 @@ def render_service(self) -> bool: ) return service_rendered + def render_config(self) -> bool: + """Override base render_config to render the service file. + + This is because smartctl_exporter doesn't support providing config file. + The config options need to be provided as flags while exectuting + smartctl_exporter. So, the service file must be re-rendered when a config + value is changed. + """ + service_rendered = self.render_service() + if service_rendered: + systemd.daemon_reload() + return service_rendered + @staticmethod def hw_tools() -> List[HWTool]: """Return list hardware tools to watch.""" diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 216d7566..2fecfb11 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -585,6 +585,10 @@ class TestSmartMetricExporter(unittest.TestCase): def setUp(self) -> None: """Set up harness for each test case.""" + systemd_lib_patcher = mock.patch.object(service, "systemd") + self.mock_systemd = systemd_lib_patcher.start() + self.addCleanup(systemd_lib_patcher.stop) + search_path = pathlib.Path(f"{__file__}/../../..").resolve() self.mock_config = { "smartctl-exporter-port": 10201, @@ -608,6 +612,20 @@ def test_render_service(self): } ) + @parameterized.expand( + [ + (True,), + (False,), + ] + ) + def test_render_config(self, service_render_success): + """Test render config.""" + self.exporter.render_service = mock.MagicMock() + self.exporter.render_service.return_value = service_render_success + + result = self.exporter.render_config() + self.assertEqual(result, service_render_success) + def test_hw_tools(self): self.assertEqual(self.exporter.hw_tools(), [HWTool.SMARTCTL])