1 # Copyright (c) 2020 Deutsche Telekom.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 from collections import namedtuple
15 from tempfile import NamedTemporaryFile
17 if sys.version_info[0] < 3:
18 from mock import patch
19 if sys.version_info[0] >= 3:
20 from unittest.mock import patch
25 from onaplogging.logWatchDog import FileEventHandlers, _yaml2Dict, _yamlConfig
28 TestEvent = namedtuple("TestEvent", ["src_path"])
31 class TestLogWatchdog(unittest.TestCase):
40 super(TestLogWatchdog, self).setUp()
42 self.temp_file = NamedTemporaryFile(mode="w+t", delete=False)
43 self.temp_file.write(yaml.dump(self.TEST_DICT))
44 self.temp_file.close()
47 super(TestLogWatchdog, self).tearDown()
49 os.unlink(self.temp_file.name)
51 def test_yaml2dict(self):
52 with pytest.raises(TypeError):
55 self.assertDictEqual(self.TEST_DICT, _yaml2Dict(self.temp_file.name))
57 def test_file_event_handler(self):
59 with patch("onaplogging.logWatchDog.config.dictConfig") as mock_config:
60 mock_config.side_effect = Exception
62 feh = FileEventHandlers(self.temp_file.name)
63 self.assertIsNone(feh.currentConfig)
64 feh.on_modified(TestEvent(src_path=self.temp_file.name))
65 self.assertIsNone(feh.currentConfig)
67 with patch("onaplogging.logWatchDog.config"):
69 feh = FileEventHandlers(self.temp_file.name)
70 self.assertIsNone(feh.currentConfig)
71 feh.on_modified(TestEvent(src_path=self.temp_file.name))
72 self.assertIsNotNone(feh.currentConfig)
74 def test_patch_yaml_config(self):
76 with pytest.raises(TypeError):
77 _yamlConfig(filepath=None)
79 with pytest.raises(OSError):
80 _yamlConfig(filepath="invalid path")
82 with patch("onaplogging.logWatchDog.config.dictConfig") as mock_config:
83 _yamlConfig(filepath=self.temp_file.name)
84 mock_config.assert_called_once_with(self.TEST_DICT)
86 with patch("onaplogging.logWatchDog.config.dictConfig") as mock_config:
87 with patch("onaplogging.logWatchDog.Observer.start") as mock_observer_start:
88 _yamlConfig(filepath=self.temp_file.name, watchDog=True)
89 mock_config.assert_called_once_with(self.TEST_DICT)
90 mock_observer_start.assert_called_once()