Debian Codegraph @

masakarimonitors/tests/unit/hostmonitor/consul_check/test_manager.py

raw
1 # Copyright(c) 2021 Inspur
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import testtools
16 from unittest import mock
17
18 from collections import deque
19 import eventlet
20 from oslo_config import fixture as fixture_config
21
22 import masakarimonitors.conf
23 from masakarimonitors.ha import masakari
24 from masakarimonitors.hostmonitor.consul_check import consul_helper
25 from masakarimonitors.hostmonitor.consul_check import manager
26 from masakarimonitors.hostmonitor.consul_check import matrix_helper
27
28 eventlet.monkey_patch(os=False)
29
30 CONF = masakarimonitors.conf.CONF
31
32
33 class TestConsulCheck(testtools.TestCase):
34
35 def setUp(self):
36 super(TestConsulCheck, self).setUp()
37 self.CONF = self.useFixture(fixture_config.Config()).conf
38 self.host_monitor = manager.ConsulCheck()
39 self.host_monitor.matrix_manager = \
40 matrix_helper.MatrixManager(self.CONF)
41 self.host_monitor._matrix = matrix_helper.DEFAULT_MATRIX
42 self.host_monitor.consul_manager = \
43 consul_helper.ConsulManager(self.CONF)
44 self.host_monitor._sequence = ['manage', 'tenant', 'storage']
45 self.host_monitor.monitoring_data = {
46 "node01": deque([['up', 'up', 'up'],
47 ['up', 'up', 'up'],
48 ['up', 'up', 'up']],
49 maxlen=3),
50 "node02": deque([['up', 'up', 'up'],
51 ['up', 'up', 'up'],
52 ['up', 'up', 'down']],
53 maxlen=3),
54 "node03": deque([['up', 'up', 'up'],
55 ['down', 'up', 'up'],
56 ['down', 'up', 'up']],
57 maxlen=3),
58 }
59
60 def test_update_monitoring_data(self):
61 mock_health = {
62 'node01': ['up', 'up', 'up'],
63 'node02': ['up', 'up', 'up'],
64 'node03': ['up', 'up', 'up']
65 }
66
67 with mock.patch.object(self.host_monitor.consul_manager, 'get_health',
68 return_value=mock_health):
69 self.host_monitor.update_monitoring_data()
70 excepted_monitoring_data = {
71 "node01": deque([['up', 'up', 'up'],
72 ['up', 'up', 'up'],
73 ['up', 'up', 'up']],
74 maxlen=3),
75 "node02": deque([['up', 'up', 'up'],
76 ['up', 'up', 'down'],
77 ['up', 'up', 'up']],
78 maxlen=3),
79 "node03": deque([['down', 'up', 'up'],
80 ['down', 'up', 'up'],
81 ['up', 'up', 'up']],
82 maxlen=3),
83 }
84 self.assertEqual(excepted_monitoring_data,
85 self.host_monitor.monitoring_data)
86
87 def test_get_host_statistical_health(self):
88 self.assertEqual(['up', 'up', 'up'],
89 self.host_monitor.get_host_health('node01'))
90 self.assertEqual(['up', 'up', None],
91 self.host_monitor.get_host_health('node02'))
92 self.assertEqual([None, 'up', 'up'],
93 self.host_monitor.get_host_health('node03'))
94
95 def test_host_statistical_health_changed(self):
96 self.host_monitor.last_host_health = {
97 'node02': ['up', 'up', None],
98 'node03': ['up', 'up', 'down']
99 }
100
101 self.assertFalse(self.host_monitor._host_health_changed(
102 'node01', ['up', 'up', 'up']))
103 self.assertTrue(self.host_monitor._host_health_changed(
104 'node02', ['up', 'up', 'up']))
105 self.assertTrue(self.host_monitor._host_health_changed(
106 'node03', ['up', 'up', 'up']))
107 last_host_health = {
108 'node01': ['up', 'up', 'up'],
109 'node02': ['up', 'up', 'up'],
110 'node03': ['up', 'up', 'up']
111 }
112 self.assertEqual(self.host_monitor.last_host_health,
113 last_host_health)
114
115 def test_get_action_from_matrix_by_host_health(self):
116 self.assertEqual(
117 [],
118 self.host_monitor.get_action_from_matrix(['up', 'up', 'up']))
119 self.assertEqual(
120 ["recovery"],
121 self.host_monitor.get_action_from_matrix(['up', 'up', 'down']))
122 self.assertEqual(
123 [],
124 self.host_monitor.get_action_from_matrix(['down', 'up', 'up']))
125 self.assertEqual(
126 ["recovery"],
127 self.host_monitor.get_action_from_matrix(['down', 'down', 'down']))
128
129 @mock.patch.object(masakari.SendNotification, 'send_notification')
130 @mock.patch.object(manager.ConsulCheck, '_event')
131 def test_poll_hosts(self, mock_event, mock_send_notification):
132 self.host_monitor.monitoring_data = {
133 "node01": deque([['up', 'up', 'up'],
134 ['up', 'up', 'up'],
135 ['up', 'up', 'up']],
136 maxlen=3),
137 "node02": deque([['up', 'up', 'down'],
138 ['up', 'up', 'down'],
139 ['up', 'up', 'down']],
140 maxlen=3),
141 "node03": deque([['up', 'up', 'up'],
142 ['up', 'up', 'up'],
143 ['up', 'up', 'up']],
144 maxlen=3),
145 }
146
147 self.host_monitor.last_host_health = {
148 'node02': ['up', 'up', None],
149 'node03': ['up', 'up', 'up']
150 }
151
152 test_event = {'notification': 'test'}
153 mock_event.return_value = test_event
154
155 self.host_monitor.poll_hosts()
156 mock_send_notification.assert_called_once_with(
157 CONF.host.api_retry_max, CONF.host.api_retry_interval, test_event)