| 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 oslo_config import fixture as fixture_config |
| 19 |
|
| 20 |
from masakarimonitors.hostmonitor.consul_check import consul_helper |
| 21 |
|
| 22 |
|
| 23 |
class FakerAgentMembers(object): |
| 24 |
|
| 25 |
def __init__(self): |
| 26 |
self.agent_members = [] |
| 27 |
|
| 28 |
def create_agent(self, name, status=1): |
| 29 |
agent = { |
| 30 |
'Name': name, |
| 31 |
'Status': status, |
| 32 |
'Port': 'agent_lan_port', |
| 33 |
'Addr': 'agent_ip', |
| 34 |
'Tags': { |
| 35 |
'dc': 'storage', |
| 36 |
'role': 'consul', |
| 37 |
'port': 'agent_server_port', |
| 38 |
'wan_join_port': 'agent_wan_port', |
| 39 |
'expect': '3', |
| 40 |
'id': 'agent_id', |
| 41 |
'vsn_max': '3', |
| 42 |
'vsn_min': '2', |
| 43 |
'vsn': '2', |
| 44 |
'raft_vsn': '2', |
| 45 |
}, |
| 46 |
'ProtocolMax': 5, |
| 47 |
'ProtocolMin': 1, |
| 48 |
'ProtocolCur': 2, |
| 49 |
'DelegateMax': 5, |
| 50 |
'DelegateMin': 2, |
| 51 |
'DelegateCur': 4, |
| 52 |
} |
| 53 |
|
| 54 |
self.agent_members.append(agent) |
| 55 |
|
| 56 |
def generate_agent_members(self): |
| 57 |
return self.agent_members |
| 58 |
|
| 59 |
|
| 60 |
class TestConsulManager(testtools.TestCase): |
| 61 |
|
| 62 |
def setUp(self): |
| 63 |
super(TestConsulManager, self).setUp() |
| 64 |
self.CONF = self.useFixture(fixture_config.Config()).conf |
| 65 |
self.consul_manager = consul_helper.ConsulManager(self.CONF) |
| 66 |
self.consul_manager.agents = { |
| 67 |
'manage': consul_helper.ConsulAgent('manage'), |
| 68 |
'tenant': consul_helper.ConsulAgent('tenant'), |
| 69 |
'storage': consul_helper.ConsulAgent('storage'), |
| 70 |
} |
| 71 |
|
| 72 |
@mock.patch('consul.Consul') |
| 73 |
def test_init_agents_no_agent(self, mock_consul): |
| 74 |
mgr = consul_helper.ConsulManager(self.CONF) |
| 75 |
self.assertEqual({}, mgr.agents) |
| 76 |
mock_consul.assert_not_called() |
| 77 |
|
| 78 |
@mock.patch('consul.Consul') |
| 79 |
def test_init_agents_ipv4(self, mock_consul): |
| 80 |
self.CONF.set_override('agent_manage', '192.0.2.10:1000', 'consul') |
| 81 |
self.CONF.set_override('agent_tenant', '192.0.2.11:1001', 'consul') |
| 82 |
self.CONF.set_override('agent_storage', '192.0.2.12', 'consul') |
| 83 |
mgr = consul_helper.ConsulManager(self.CONF) |
| 84 |
self.assertEqual('manage', mgr.agents['manage'].name) |
| 85 |
self.assertEqual('192.0.2.10', mgr.agents['manage'].addr) |
| 86 |
self.assertEqual(1000, mgr.agents['manage'].port) |
| 87 |
self.assertEqual(mock.call(host='192.0.2.10', port=1000), |
| 88 |
mock_consul.mock_calls[0]) |
| 89 |
self.assertEqual('tenant', mgr.agents['tenant'].name) |
| 90 |
self.assertEqual('192.0.2.11', mgr.agents['tenant'].addr) |
| 91 |
self.assertEqual(1001, mgr.agents['tenant'].port) |
| 92 |
self.assertEqual(mock.call(host='192.0.2.11', port=1001), |
| 93 |
mock_consul.mock_calls[1]) |
| 94 |
self.assertEqual('storage', mgr.agents['storage'].name) |
| 95 |
self.assertEqual('192.0.2.12', mgr.agents['storage'].addr) |
| 96 |
self.assertEqual(8500, mgr.agents['storage'].port) |
| 97 |
self.assertEqual(mock.call(host='192.0.2.12', port=8500), |
| 98 |
mock_consul.mock_calls[2]) |
| 99 |
self.assertEqual(3, mock_consul.call_count) |
| 100 |
|
| 101 |
@mock.patch('consul.Consul') |
| 102 |
def test_init_agents_ipv6(self, mock_consul): |
| 103 |
self.CONF.set_override( |
| 104 |
'agent_manage', '[2001:db8::10]:1000', 'consul') |
| 105 |
self.CONF.set_override( |
| 106 |
'agent_tenant', '[2001:db8::11]:1001', 'consul') |
| 107 |
self.CONF.set_override( |
| 108 |
'agent_storage', '2001:db8::12', 'consul') |
| 109 |
mgr = consul_helper.ConsulManager(self.CONF) |
| 110 |
self.assertEqual('manage', mgr.agents['manage'].name) |
| 111 |
self.assertEqual('2001:db8::10', mgr.agents['manage'].addr) |
| 112 |
self.assertEqual(1000, mgr.agents['manage'].port) |
| 113 |
self.assertEqual(mock.call(host='2001:db8::10', port=1000), |
| 114 |
mock_consul.mock_calls[0]) |
| 115 |
self.assertEqual('tenant', mgr.agents['tenant'].name) |
| 116 |
self.assertEqual('2001:db8::11', mgr.agents['tenant'].addr) |
| 117 |
self.assertEqual(1001, mgr.agents['tenant'].port) |
| 118 |
self.assertEqual(mock.call(host='2001:db8::11', port=1001), |
| 119 |
mock_consul.mock_calls[1]) |
| 120 |
self.assertEqual('storage', mgr.agents['storage'].name) |
| 121 |
self.assertEqual('2001:db8::12', mgr.agents['storage'].addr) |
| 122 |
self.assertEqual(8500, mgr.agents['storage'].port) |
| 123 |
self.assertEqual(mock.call(host='2001:db8::12', port=8500), |
| 124 |
mock_consul.mock_calls[2]) |
| 125 |
self.assertEqual(3, mock_consul.call_count) |
| 126 |
|
| 127 |
def test_get_health(self): |
| 128 |
fake_manage_agents = FakerAgentMembers() |
| 129 |
fake_manage_agents.create_agent('node01', status=1) |
| 130 |
fake_manage_agents.create_agent('node02', status=1) |
| 131 |
fake_manage_agents.create_agent('node03', status=1) |
| 132 |
agent_manage_members = fake_manage_agents.generate_agent_members() |
| 133 |
|
| 134 |
fake_tenant_agents = FakerAgentMembers() |
| 135 |
fake_tenant_agents.create_agent('node01', status=1) |
| 136 |
fake_tenant_agents.create_agent('node02', status=1) |
| 137 |
fake_tenant_agents.create_agent('node03', status=1) |
| 138 |
agent_tenant_members = fake_tenant_agents.generate_agent_members() |
| 139 |
|
| 140 |
fake_storage_agents = FakerAgentMembers() |
| 141 |
fake_storage_agents.create_agent('node01', status=1) |
| 142 |
fake_storage_agents.create_agent('node02', status=1) |
| 143 |
fake_storage_agents.create_agent('node03', status=3) |
| 144 |
agent_storage_members = fake_storage_agents.generate_agent_members() |
| 145 |
|
| 146 |
with mock.patch.object(self.consul_manager.agents['manage'], |
| 147 |
'get_agents', return_value=agent_manage_members): |
| 148 |
with mock.patch.object(self.consul_manager.agents['tenant'], |
| 149 |
'get_agents', return_value=agent_tenant_members): |
| 150 |
with mock.patch.object(self.consul_manager.agents['storage'], |
| 151 |
'get_agents', return_value=agent_storage_members): |
| 152 |
excepted_health = { |
| 153 |
"node01": ['up', 'up', 'up'], |
| 154 |
"node02": ['up', 'up', 'up'], |
| 155 |
"node03": ['up', 'up', 'down'], |
| 156 |
} |
| 157 |
sequence = ['manage', 'tenant', 'storage'] |
| 158 |
agents_health = self.consul_manager.get_health(sequence) |
| 159 |
self.assertEqual(excepted_health, agents_health) |
| 160 |
|
| 161 |
|
| 162 |
class TestConsulAgent(testtools.TestCase): |
| 163 |
|
| 164 |
def setUp(self): |
| 165 |
super(TestConsulAgent, self).setUp() |
| 166 |
self.consul_agent = consul_helper.ConsulAgent('test') |
| 167 |
|
| 168 |
def test_get_health(self): |
| 169 |
fake_agents = FakerAgentMembers() |
| 170 |
fake_agents.create_agent('node01', status=1) |
| 171 |
fake_agents.create_agent('node02', status=1) |
| 172 |
fake_agents.create_agent('node03', status=3) |
| 173 |
agent_members = fake_agents.generate_agent_members() |
| 174 |
|
| 175 |
with mock.patch.object(self.consul_agent, 'get_agents', |
| 176 |
return_value=agent_members): |
| 177 |
excepted_health = { |
| 178 |
"node01": 'up', |
| 179 |
"node02": 'up', |
| 180 |
"node03": 'down', |
| 181 |
} |
| 182 |
agents_health = self.consul_agent.get_health() |
| 183 |
self.assertEqual(excepted_health, agents_health) |