903 lines
40 KiB
Python
903 lines
40 KiB
Python
# Copyright 2024 LY Corp.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from datetime import datetime
|
|
from unittest import mock
|
|
|
|
import consul
|
|
import testscenarios
|
|
|
|
from oslo_config import cfg
|
|
from oslo_messaging._drivers.http_driver import consul_operator
|
|
from oslo_messaging.tests import utils as test_utils
|
|
|
|
load_tests = testscenarios.load_tests_apply_scenarios
|
|
|
|
# Sample raw data returned from Consul health API
|
|
# i.e. /health/service/:service
|
|
raw_services = [
|
|
{"Service": {
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"ID": "hostname1:3000",
|
|
"Name": "nova.scheduler",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Port": 3000,
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}
|
|
},
|
|
{"Service": {
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.5",
|
|
"Datacenter": "dc1",
|
|
"ID": "hostname2:4000",
|
|
"Name": "nova.scheduler",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Port": 4000,
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}}
|
|
]
|
|
# The raw data is filtered out by ConsulOperator when getting service list.
|
|
formatted_services = [service['Service'] for service in raw_services]
|
|
|
|
|
|
@mock.patch('consul.Consul')
|
|
class TestConsulOperator(test_utils.BaseTestCase):
|
|
|
|
def setUp(self):
|
|
self.conf = cfg.CONF
|
|
super(TestConsulOperator, self).setUp(conf=self.conf)
|
|
self.consul_svc = 'nova.scheduler'
|
|
self.server = 'hostname1'
|
|
consul_operator.register_consul_opts(self.conf)
|
|
|
|
def _init_consul_operator(self, cache_max_age=0, host=None, port=None):
|
|
consul = consul_operator.ConsulOperator(host=host, port=port)
|
|
consul.cache_max_age = cache_max_age
|
|
consul.check_interval = "60s"
|
|
consul.check_timeout = "5s"
|
|
consul.check_deregister = "600s"
|
|
return consul
|
|
|
|
def test_init_with_specified_token(self, m_consul):
|
|
self.conf.set_override('token', 'default_token', group='consul')
|
|
self._init_consul_operator()
|
|
m_consul.assert_called_once_with(
|
|
host='127.0.0.1', port=8500, token='default_token')
|
|
|
|
def test_init_specified_token_is_none(self, m_consul):
|
|
self.conf.set_override('token', None, group='consul')
|
|
self._init_consul_operator()
|
|
m_consul.assert_called_once_with(
|
|
host='127.0.0.1', port=8500, token=None)
|
|
|
|
def test_init_no_token_specified(self, m_consul):
|
|
self._init_consul_operator()
|
|
m_consul.assert_called_once_with(
|
|
host='127.0.0.1', port=8500, token=None)
|
|
|
|
def test__get_service_list_from_dc(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
|
|
dc = 'fake-dc'
|
|
m_consul.return_value.health.service.return_value = (
|
|
mock.MagicMock, raw_services)
|
|
operator._get_service_list_from_dc(self.consul_svc, dc, None)
|
|
# Check if the health.service is called correctly.
|
|
m_consul.return_value.health.service.assert_called_once_with(
|
|
'nova.scheduler', dc='fake-dc', tag=None, wait='50ms',
|
|
passing=True, token=None)
|
|
self.assertEqual(operator.dc_access_counter[dc], 1)
|
|
# Call the second time to check the counter.
|
|
operator._get_service_list_from_dc(self.consul_svc, dc, None)
|
|
self.assertEqual(operator.dc_access_counter[dc], 2)
|
|
|
|
other_dc = "other-dc"
|
|
operator._get_service_list_from_dc(self.consul_svc, other_dc, None)
|
|
self.assertEqual(operator.dc_access_counter[other_dc], 1)
|
|
|
|
def test__get_service_list_from_dc_with_error(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
dc = 'fake-dc'
|
|
m_consul.return_value.health.service.side_effect = [
|
|
Exception('fake consul error')]
|
|
|
|
# Check if ConsulAccessException is raised.
|
|
self.assertRaises(consul_operator.ConsulAccessException,
|
|
operator._get_service_list_from_dc,
|
|
self.consul_svc, dc, None)
|
|
# Check if the health.service is called correctly.
|
|
m_consul.return_value.health.service.assert_called_once_with(
|
|
'nova.scheduler', dc='fake-dc', tag=None, wait='50ms',
|
|
passing=True, token=None)
|
|
|
|
self.assertNotIn(dc, operator.dc_access_counter)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_with_local_dc(self, m__get_service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
m_consul.return_value.agent.self.return_value = {
|
|
'Config': {'Datacenter': 'local'}}
|
|
m__get_service_list_from_dc.return_value = formatted_services
|
|
|
|
operator._get_service_list(self.consul_svc, self.server)
|
|
# Check if the local DC is used.
|
|
m__get_service_list_from_dc.assert_called_once_with(
|
|
self.consul_svc, 'local', self.server
|
|
)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_with_other_dc_simple(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
m_consul.return_value.agent.self.return_value = {
|
|
'Config': {'Datacenter': 'local'}}
|
|
# Set the service list of the local DC to empty, so that we can
|
|
# access other DCs.
|
|
m__service_list_from_dc.return_value = []
|
|
m_consul.return_value.catalog.datacenters.return_value = ['local',
|
|
'tokyo-2',
|
|
'tokyo-3']
|
|
operator.dc_access_counter['tokyo-2'] = 1
|
|
operator._get_service_list(self.consul_svc, self.server)
|
|
# Local should always be accessed first.
|
|
calls = [mock.call(self.consul_svc, 'local', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-3', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-2', self.server), ]
|
|
m__service_list_from_dc.assert_has_calls(calls)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_with_other_dc_complex(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
m_consul.return_value.agent.self.return_value = {
|
|
'Config': {'Datacenter': 'local'}}
|
|
# Set the service list of the local DC to empty, so that we can
|
|
# access other DCs.
|
|
m__service_list_from_dc.return_value = []
|
|
m_consul.return_value.catalog.datacenters.return_value = ['local',
|
|
'tokyo-2',
|
|
'tokyo-3']
|
|
|
|
operator.dc_access_counter = {'tokyo-2': 2, 'tokyo-3': 5, 'local': 8}
|
|
operator._get_service_list(self.consul_svc, self.server)
|
|
# Local should always be accessed first.
|
|
# Ensure we access DCs in a correct order.
|
|
calls = [mock.call(self.consul_svc, 'local', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-2', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-3', self.server), ]
|
|
m__service_list_from_dc.assert_has_calls(calls)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_across_dc_with_no_dc(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Set the service list of any DC to be non-empty.
|
|
m__service_list_from_dc.return_value = ['service1']
|
|
m_consul.return_value.catalog.datacenters.return_value = []
|
|
|
|
result = operator._get_service_list_across_dc(self.consul_svc,
|
|
self.server)
|
|
m__service_list_from_dc.assert_not_called()
|
|
# The result should be empty as there is no DC.
|
|
self.assertEqual(result, [])
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_across_dc_with_one_dc(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Set the service list of any DC to be non-empty.
|
|
m__service_list_from_dc.return_value = ['service1']
|
|
m_consul.return_value.catalog.datacenters.return_value = ['tokyo-1']
|
|
|
|
result = operator._get_service_list_across_dc(self.consul_svc,
|
|
self.server)
|
|
calls = [mock.call(self.consul_svc, 'tokyo-1', self.server), ]
|
|
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
|
|
self.assertEqual(result, ['service1'])
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_across_dc_no_result(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Set the service list of any DC to be empty.
|
|
m__service_list_from_dc.return_value = []
|
|
m_consul.return_value.catalog.datacenters.return_value = ['local',
|
|
'tokyo-2',
|
|
'tokyo-3']
|
|
|
|
result = operator._get_service_list_across_dc(self.consul_svc,
|
|
self.server)
|
|
calls = [mock.call(self.consul_svc, 'local', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-2', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-3', self.server), ]
|
|
# The order doesn't matter here.
|
|
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
|
|
self.assertEqual(result, [])
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_from_dc')
|
|
def test__get_service_list_across_dc(self,
|
|
m__service_list_from_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
m__service_list_from_dc.return_value = ['service1']
|
|
m_consul.return_value.catalog.datacenters.return_value = ['tokyo-2',
|
|
'tokyo-1']
|
|
|
|
result = operator._get_service_list_across_dc(self.consul_svc,
|
|
self.server)
|
|
calls = [mock.call(self.consul_svc, 'tokyo-1', self.server),
|
|
mock.call(self.consul_svc, 'tokyo-2', self.server)]
|
|
# The order doesn't matter here.
|
|
m__service_list_from_dc.assert_has_calls(calls, any_order=True)
|
|
# This result should contain services from tokyo-1 and tokyo-2.
|
|
self.assertEqual(result, ['service1', 'service1'])
|
|
|
|
def test__update_cached_with_single_server(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
self.assertEqual(operator.cached_services, {})
|
|
|
|
expected_cache = {
|
|
"nova.scheduler": {
|
|
"hostname1": {
|
|
"ID": "hostname1:3000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 3000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
},
|
|
}
|
|
}
|
|
# First update with hostname1.
|
|
operator._update_cache(self.consul_svc, "hostname1",
|
|
[formatted_services[0]])
|
|
self.assertEqual(operator.cached_services, expected_cache)
|
|
|
|
# Second update with hostname2.
|
|
operator._update_cache(self.consul_svc, "hostname2",
|
|
[formatted_services[1]])
|
|
|
|
# The cache should contain information for both hostname1 and
|
|
# hostname2.
|
|
expected_cache = {
|
|
"nova.scheduler": {
|
|
"hostname1": {
|
|
"ID": "hostname1:3000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 3000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
},
|
|
"hostname2": {
|
|
"ID": "hostname2:4000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 4000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.5",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.assertEqual(operator.cached_services, expected_cache)
|
|
|
|
def test__update_cached_with_multiple_servers(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
self.assertEqual(operator.cached_services, {})
|
|
|
|
expected_cache = {
|
|
"nova.scheduler": {
|
|
"hostname1": {
|
|
"ID": "hostname1:3000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 3000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
},
|
|
"hostname2": {
|
|
"ID": "hostname2:4000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 4000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.5",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
self.assertEqual(operator.cached_services, expected_cache)
|
|
|
|
def test__update_cached_mix(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Update cache for nova.scheduler with all servers
|
|
# including hostname1 and hostname2.
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
|
|
new_service_info = [
|
|
{
|
|
"Node": "consul-self.server1",
|
|
"Address": "6.6.6.6",
|
|
"Datacenter": "dc1",
|
|
"ID": "hostname2:5000",
|
|
"Name": "nova.scheduler",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Port": 5000,
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}]
|
|
# Update cache for nova.scheduler with hostname2.
|
|
operator._update_cache(self.consul_svc, 'hostname2', new_service_info)
|
|
expected_cache = {
|
|
"nova.scheduler": {
|
|
"hostname1": {
|
|
"ID": "hostname1:3000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 3000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
},
|
|
"hostname2": {
|
|
"ID": "hostname2:5000",
|
|
"Name": "nova.scheduler",
|
|
"Port": 5000,
|
|
"Node": "consul-self.server1",
|
|
"Address": "6.6.6.6",
|
|
"Datacenter": "dc1",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.assertEqual(operator.cached_services, expected_cache)
|
|
|
|
def test__get_cached_service_list_without_server(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
result = operator._get_cached_service_list(self.consul_svc)
|
|
self.assertEqual(result, formatted_services)
|
|
|
|
# Check if an empty list is returned when a service name doesn't exist
|
|
# in cache.
|
|
result = operator._get_cached_service_list("other_service_name")
|
|
self.assertEqual(result, [])
|
|
|
|
def test__get_cached_service_list_with_server(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
result = operator._get_cached_service_list(self.consul_svc,
|
|
'hostname1')
|
|
expected_result = [
|
|
{
|
|
"ID": "hostname1:3000",
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.4",
|
|
"Datacenter": "dc1",
|
|
"Name": "nova.scheduler",
|
|
"Tags": [
|
|
"hostname1"
|
|
],
|
|
"Port": 3000,
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}, ]
|
|
self.assertEqual(result, expected_result)
|
|
|
|
result = operator._get_cached_service_list(self.consul_svc,
|
|
'hostname2')
|
|
expected_result = [
|
|
{
|
|
"ID": "hostname2:4000",
|
|
"Node": "consul-self.server1",
|
|
"Address": "172.25.0.5",
|
|
"Datacenter": "dc1",
|
|
"Name": "nova.scheduler",
|
|
"Tags": [
|
|
"hostname2"
|
|
],
|
|
"Port": 4000,
|
|
"Meta": {
|
|
"reverse_proxy_name": "",
|
|
"reverse_proxy_endpoint": ""
|
|
}
|
|
}, ]
|
|
self.assertEqual(result, expected_result)
|
|
|
|
result = operator._get_cached_service_list(self.consul_svc,
|
|
'non-exist')
|
|
self.assertEqual(result, [])
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_force_revalidate(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator.get_service_list(self.consul_svc, self.server, True)
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_zero_max_age(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
|
|
# Check if cache_max_age=0 is equivalent to force_revalidate=True
|
|
operator = self._init_consul_operator(cache_max_age=0)
|
|
operator.get_service_list(self.consul_svc, self.server, False)
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_first_access(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
|
|
operator = self._init_consul_operator(cache_max_age=100)
|
|
# Make sure the service list returned from Consul is not empty.
|
|
m__get_service_list.return_value = formatted_services
|
|
# Since we haven't access to Consul, last_requested should be None.
|
|
self.assertIsNone(operator.last_requested)
|
|
operator.get_service_list(self.consul_svc, self.server, False)
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
self.assertIsNotNone(operator.last_requested)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_first_access_with_no_result(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
|
|
operator = self._init_consul_operator(cache_max_age=100)
|
|
# Test when accessing Consul for the first time and the service
|
|
# list returned from Consul is empty.
|
|
m__get_service_list.return_value = []
|
|
# Since we haven't access to Consul, last_requested should be None.
|
|
self.assertIsNone(operator.last_requested)
|
|
svc_list = operator.get_service_list(self.consul_svc, self.server,
|
|
False)
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
# The returned svc_list should be also empty since no cache is
|
|
# available for the first time accessing Consul.
|
|
self.assertEqual(svc_list, [])
|
|
# Since we get an empty service list from Consul, last_requested should
|
|
# still be None.
|
|
self.assertIsNone(operator.last_requested)
|
|
|
|
@mock.patch('datetime.datetime')
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_cache_timeout(self,
|
|
m__get_service_list,
|
|
m_datetime,
|
|
m_consul):
|
|
operator = self._init_consul_operator(cache_max_age=2)
|
|
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 13)
|
|
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
|
|
|
|
operator.get_service_list(self.consul_svc, self.server, False)
|
|
# Do not use cache since cache expired.
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
|
|
@mock.patch('datetime.datetime')
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_cache_miss(self,
|
|
m__get_service_list,
|
|
m_datetime,
|
|
m_consul):
|
|
operator = self._init_consul_operator(cache_max_age=10)
|
|
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 13)
|
|
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
|
|
|
|
operator.get_service_list(self.consul_svc, self.server, False)
|
|
# Test if we fetch data from Consul due the cache miss.
|
|
m__get_service_list.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
|
|
@mock.patch('datetime.datetime')
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_cached(self,
|
|
m__get_service_list,
|
|
m_datetime,
|
|
m_consul):
|
|
operator = self._init_consul_operator(cache_max_age=2)
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
m_datetime.now.return_value = datetime(2022, 6, 29, 2, 22, 12)
|
|
operator.last_requested = datetime(2022, 6, 29, 2, 22, 10)
|
|
|
|
result = operator.get_service_list(self.consul_svc, None, False)
|
|
# _get_service_list should not be called since we're using cache.
|
|
self.assertEqual(0, m__get_service_list.call_count)
|
|
# The order of entries in the list might be different.
|
|
# Sort the lists for comparison purposes.
|
|
self.assertEqual(result, formatted_services)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_exception(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
m__get_service_list.side_effect = \
|
|
consul_operator.ConsulAccessException("some error")
|
|
result = operator.get_service_list(self.consul_svc, None, True)
|
|
# Check if cache is used ConsulAccessException happens.
|
|
self.assertEqual(result, formatted_services)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_exception_with_no_cache(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator.cached_services[self.consul_svc] = {}
|
|
m__get_service_list.side_effect = \
|
|
consul_operator.ConsulAccessException("some error")
|
|
# Check if ConsulAccessException is raised again when cache is empty.
|
|
self.assertRaises(consul_operator.ConsulAccessException,
|
|
operator.get_service_list,
|
|
self.consul_svc, None, True)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_empty(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator._update_cache(self.consul_svc, None, formatted_services)
|
|
m__get_service_list.return_value = []
|
|
result = operator.get_service_list(self.consul_svc, None, True)
|
|
# Check if cache is used when we get an empty service list (no result)
|
|
# from Consul.
|
|
self.assertEqual(result, formatted_services)
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list')
|
|
def test_get_service_list_empty_with_no_cache(self,
|
|
m__get_service_list,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator.cached_services[self.consul_svc] = {}
|
|
m__get_service_list.return_value = []
|
|
result = operator.get_service_list(self.consul_svc, None, True)
|
|
# Check if an empty result is returned when neither Consul nor cache
|
|
# has the service list we are looking for.
|
|
self.assertEqual(result, [])
|
|
|
|
@mock.patch.object(consul_operator.ConsulOperator,
|
|
'_get_service_list_across_dc')
|
|
def test_get_service_list_with_cross_dc_true(self,
|
|
m__get_service_list_across_dc,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator.get_service_list(self.consul_svc, self.server, cross_dc=True)
|
|
m__get_service_list_across_dc.assert_called_once_with(self.consul_svc,
|
|
self.server)
|
|
|
|
@mock.patch('consul.Check')
|
|
def test_register_service(self, m_check, m_consul):
|
|
operator = self._init_consul_operator()
|
|
target = mock.Mock()
|
|
target.server = 'server'
|
|
target.exchange = "nova"
|
|
target.topic = "scheduler"
|
|
# Mock the case where both service and check registrations succeed.
|
|
m_consul.return_value.agent.service.register.return_value = True
|
|
m_consul.return_value.agent.check.register.return_value = True
|
|
|
|
name = '{}.{}'.format(target.exchange, target.topic)
|
|
tags = [target.server]
|
|
proxy_conf = mock.Mock()
|
|
proxy_conf.reverse_proxy_name = 'test_reverse_proxy'
|
|
proxy_conf.reverse_proxy_endpoint = 'http://10.123.216.5:30900/'
|
|
proxy_conf.proxy_name = ''
|
|
proxy_conf.proxy_endpoint = ''
|
|
is_registered = operator.register_service(name, 'None:10000',
|
|
'127.0.0.1', 10000, tags,
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once_with(
|
|
name='nova.scheduler', service_id='None:10000',
|
|
address='127.0.0.1', port=10000,
|
|
tags=['server'], token=None,
|
|
meta={
|
|
'reverse_proxy_name': 'test_reverse_proxy',
|
|
'reverse_proxy_endpoint': 'http://10.123.216.5:30900/'
|
|
})
|
|
# Check if we attach a http check to this service.
|
|
expected_url = 'http://10.123.216.5:30900/healthcheck'
|
|
expected_header = {
|
|
'x-rpchost': ['127.0.0.1'],
|
|
'x-rpchost-port': ['10000']
|
|
}
|
|
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
|
|
expected_header,
|
|
tls_skip_verify=True)
|
|
m_consul.return_value.agent.check.register.assert_called_once()
|
|
|
|
# Check the final result.
|
|
self.assertIs(is_registered, True)
|
|
|
|
@mock.patch('consul.Check')
|
|
def test_register_service_without_reserve_proxy(self, m_check, m_consul):
|
|
operator = self._init_consul_operator()
|
|
target = mock.Mock()
|
|
target.server = 'server'
|
|
target.exchange = "nova"
|
|
target.topic = "scheduler"
|
|
# Mock the case where both service and check registrations succeed.
|
|
m_consul.return_value.agent.service.register.return_value = True
|
|
m_consul.return_value.agent.check.register.return_value = True
|
|
|
|
name = '{}.{}'.format(target.exchange, target.topic)
|
|
tags = [target.server]
|
|
proxy_conf = mock.Mock()
|
|
proxy_conf.reverse_proxy_name = ''
|
|
proxy_conf.reverse_proxy_endpoint = ''
|
|
proxy_conf.proxy_name = ''
|
|
proxy_conf.proxy_endpoint = ''
|
|
is_registered = operator.register_service(name, 'None:10000',
|
|
'127.0.0.1', 10000, tags,
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once_with(
|
|
name='nova.scheduler', service_id='None:10000',
|
|
address='127.0.0.1', port=10000,
|
|
tags=['server'],
|
|
token=None,
|
|
meta={
|
|
'reverse_proxy_name': '',
|
|
'reverse_proxy_endpoint': ''
|
|
})
|
|
# Check if we attach a http check to this service.
|
|
expected_url = 'http://127.0.0.1:10000/healthcheck'
|
|
expected_header = {}
|
|
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
|
|
expected_header,
|
|
tls_skip_verify=True)
|
|
m_consul.return_value.agent.check.register.assert_called_once()
|
|
|
|
# Check the final result.
|
|
self.assertIs(is_registered, True)
|
|
|
|
def test_register_service_failed_with_service(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Mock the case where service registrations fails.
|
|
m_consul.return_value.agent.service.register.return_value = False
|
|
proxy_conf = mock.Mock()
|
|
is_registered = operator.register_service('nova.compute', 'None:10000',
|
|
'127.0.0.1', 10000, [],
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once()
|
|
# Since the service registration failed (step 1), check registration
|
|
# shouldn't be called.
|
|
m_consul.return_value.agent.check.register.assert_not_called()
|
|
|
|
# Check the final result.
|
|
self.assertIs(is_registered, False)
|
|
|
|
@mock.patch('consul.Check')
|
|
def test_register_service_with_reserve_proxy_and_proxy(self, m_check,
|
|
m_consul):
|
|
operator = self._init_consul_operator()
|
|
target = mock.Mock()
|
|
target.server = 'server'
|
|
target.exchange = "nova"
|
|
target.topic = "scheduler"
|
|
# Mock the case where both service and check registrations succeed.
|
|
m_consul.return_value.agent.service.register.return_value = True
|
|
m_consul.return_value.agent.check.register.return_value = True
|
|
|
|
name = '{}.{}'.format(target.exchange, target.topic)
|
|
tags = [target.server]
|
|
proxy_conf = mock.Mock()
|
|
proxy_conf.reverse_proxy_name = 'test_reverse_proxy'
|
|
proxy_conf.reverse_proxy_endpoint = 'http://10.123.216.5:30900/'
|
|
proxy_conf.proxy_name = 'test_proxy'
|
|
proxy_conf.proxy_endpoint = 'http://10.123.216.8:30288/'
|
|
is_registered = operator.register_service(name, 'None:10000',
|
|
'127.0.0.1', 10000, tags,
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once_with(
|
|
name='nova.scheduler', service_id='None:10000',
|
|
address='127.0.0.1', port=10000,
|
|
tags=['server'],
|
|
token=None,
|
|
meta={
|
|
'reverse_proxy_name': 'test_reverse_proxy',
|
|
'reverse_proxy_endpoint': 'http://10.123.216.5:30900/'
|
|
})
|
|
# Check if we attach a http check to this service.
|
|
expected_url = 'http://127.0.0.1:10000/healthcheck'
|
|
# When reverse proxy and proxy co-exist, health check doesn't need to
|
|
# go though reverse-proxy.
|
|
expected_header = {}
|
|
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
|
|
expected_header,
|
|
tls_skip_verify=True)
|
|
m_consul.return_value.agent.check.register.assert_called_once()
|
|
|
|
# Check the final result.
|
|
self.assertIs(is_registered, True)
|
|
|
|
@mock.patch('consul.Check')
|
|
def test_register_service_with_proxy_only(self, m_check, m_consul):
|
|
operator = self._init_consul_operator()
|
|
target = mock.Mock()
|
|
target.server = 'server'
|
|
target.exchange = "nova"
|
|
target.topic = "scheduler"
|
|
# Mock the case where both service and check registrations succeed.
|
|
m_consul.return_value.agent.service.register.return_value = True
|
|
m_consul.return_value.agent.check.register.return_value = True
|
|
|
|
name = '{}.{}'.format(target.exchange, target.topic)
|
|
tags = [target.server]
|
|
proxy_conf = mock.Mock()
|
|
proxy_conf.reverse_proxy_name = ''
|
|
proxy_conf.reverse_proxy_endpoint = ''
|
|
proxy_conf.proxy_name = 'test_proxy'
|
|
proxy_conf.proxy_endpoint = 'http://10.123.216.8:30288/'
|
|
is_registered = operator.register_service(name, 'None:10000',
|
|
'127.0.0.1', 10000, tags,
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once_with(
|
|
name='nova.scheduler', service_id='None:10000',
|
|
address='127.0.0.1', port=10000,
|
|
tags=['server'],
|
|
token=None,
|
|
meta={
|
|
'reverse_proxy_name': '',
|
|
'reverse_proxy_endpoint': ''
|
|
})
|
|
# Check if we attach a http check to this service.
|
|
expected_url = 'http://127.0.0.1:10000/healthcheck'
|
|
expected_header = {}
|
|
m_check.http.assert_called_once_with(expected_url, '60s', '5s', '600s',
|
|
expected_header,
|
|
tls_skip_verify=True)
|
|
m_consul.return_value.agent.check.register.assert_called_once()
|
|
|
|
# Check the final result.
|
|
self.assertIs(is_registered, True)
|
|
|
|
def test_register_service_failed_with_check(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
# Mock the case where only check registrations fails.
|
|
m_consul.return_value.agent.service.register.return_value = True
|
|
m_consul.return_value.agent.check.register.return_value = False
|
|
proxy_conf = mock.Mock()
|
|
is_registered = operator.register_service('nova.compute', 'None:10000',
|
|
'127.0.0.1', 10000, [],
|
|
proxy_conf)
|
|
# Check if we register the service on Consul.
|
|
m_consul.return_value.agent.service.register.assert_called_once()
|
|
# Since the service registration succeed (step 1), check registration
|
|
# should proceed.
|
|
m_consul.return_value.agent.check.register.assert_called_once()
|
|
|
|
# The final result should be False because check registration failed.
|
|
self.assertIs(is_registered, False)
|
|
|
|
def test_deregister_service_normal(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
operator.deregister_service('fake-service-id')
|
|
m_consul.return_value.agent.service.deregister.assert_called_once_with(
|
|
'fake-service-id')
|
|
|
|
def test_deregister_service_raised_consul_exception(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
m_consul.return_value.agent.service.deregister.side_effect = [
|
|
consul.ConsulException('500 Unknown service "faile-service-id')]
|
|
operator.deregister_service('fake-service-id')
|
|
m_consul.return_value.agent.service.deregister.assert_called_once_with(
|
|
'fake-service-id')
|
|
|
|
def test_deregister_service_raised_exception(self, m_consul):
|
|
operator = self._init_consul_operator()
|
|
m_consul.return_value.agent.service.deregister.side_effect = [
|
|
Exception('Unknown exception')]
|
|
operator.deregister_service('fake-service-id')
|
|
m_consul.return_value.agent.service.deregister.assert_called_once_with(
|
|
'fake-service-id')
|