5f431382d110add6a4c9c5f87ab992b191128ee8
[logging-analytics.git] / pylog / tests / test_log_watchdog.py
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
6 #
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.
10
11 import os
12 import sys
13 import unittest
14 from collections import namedtuple
15 from tempfile import NamedTemporaryFile
16
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
21
22 import pytest
23 import yaml
24
25 from onaplogging.logWatchDog import FileEventHandlers, _yaml2Dict, _yamlConfig
26
27
28 TestEvent = namedtuple("TestEvent", ["src_path"])
29
30
31 class TestLogWatchdog(unittest.TestCase):
32
33     TEST_DICT = {
34         "A": {
35             "B": "C"
36         }
37     }
38
39     def setUp(self):
40         super(TestLogWatchdog, self).setUp()
41     
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()
45
46     def tearDown(self):
47         super(TestLogWatchdog, self).tearDown()
48
49         os.unlink(self.temp_file.name)
50
51     def test_yaml2dict(self):
52         with pytest.raises(TypeError):
53             _yaml2Dict(None)
54         
55         self.assertDictEqual(self.TEST_DICT, _yaml2Dict(self.temp_file.name))
56
57     def test_file_event_handler(self):
58
59         with patch("onaplogging.logWatchDog.config.dictConfig") as mock_config:
60             mock_config.side_effect = Exception
61
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)
66
67         with patch("onaplogging.logWatchDog.config"):
68
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)
73
74     def test_patch_yaml_config(self):
75
76         with pytest.raises(TypeError):
77             _yamlConfig(filepath=None)
78
79         with pytest.raises(OSError):
80             _yamlConfig(filepath="invalid path")
81
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)
85
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()