Skip to content

Commit

Permalink
Re-render smartctl service file on config changed (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
sudeephb authored Jun 11, 2024
1 parent 19c4dfe commit 2fbb65b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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."""
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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])

Expand Down

0 comments on commit 2fbb65b

Please sign in to comment.