Skip to content

Commit caf4ebc

Browse files
authored
[client] Add default period when missing a specific config (#75)
Signed-off-by: Antoine MAZEAS <[email protected]>
1 parent b41ab06 commit caf4ebc

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pyobas/daemons/collector_daemon.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pyobas.daemons import BaseDaemon
55
from pyobas.utils import PingAlive
66

7+
DEFAULT_PERIOD_SECONDS = 60
8+
79

810
class CollectorDaemon(BaseDaemon):
911
"""Implementation of a daemon of Collector type. Note that it requires
@@ -16,6 +18,8 @@ class CollectorDaemon(BaseDaemon):
1618
"""
1719

1820
def _setup(self):
21+
if self._configuration.get("collector_period") is None:
22+
self._configuration.set("collector_period", DEFAULT_PERIOD_SECONDS)
1923
icon_path = self._configuration.get("collector_icon_filepath")
2024
icon_name = self._configuration.get("collector_id") + ".png"
2125
with open(icon_path, "rb") as icon_file_handle:
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
from unittest.mock import mock_open, patch
3+
4+
from pyobas.configuration import Configuration
5+
from pyobas.daemons import CollectorDaemon
6+
from pyobas.daemons.collector_daemon import DEFAULT_PERIOD_SECONDS
7+
8+
9+
class TestCollectorDaemon(unittest.TestCase):
10+
@patch("pyobas.apis.DocumentManager.upsert")
11+
@patch("pyobas.apis.CollectorManager.create")
12+
@patch("builtins.open", new_callable=mock_open, read_data="data")
13+
@patch("pyobas.utils.PingAlive.start")
14+
def test_when_no_period_config_provided_set_default_period(
15+
self,
16+
mock_ping_alive,
17+
mock_open_local,
18+
mock_collector_create,
19+
mock_document_upsert,
20+
):
21+
mock_ping_alive.return_value = None
22+
mock_collector_create.return_value = {}
23+
mock_document_upsert.return_value = {}
24+
config = Configuration(
25+
config_hints={
26+
"openbas_url": {"data": "fake"},
27+
"openbas_token": {"data": "fake"},
28+
"collector_id": {"data": "fake id"},
29+
}
30+
)
31+
collector = CollectorDaemon(config)
32+
33+
collector._setup()
34+
35+
self.assertEqual(config.get("collector_period"), DEFAULT_PERIOD_SECONDS)
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

0 commit comments

Comments
 (0)