Read list type of environment variables
[ric-plt/sdlpy.git] / ricsdl-package / tests / backend / test_redis.py
index c5e1b03..744f927 100755 (executable)
@@ -1,5 +1,5 @@
 # Copyright (c) 2019 AT&T Intellectual Property.
-# Copyright (c) 2018-2019 Nokia.
+# Copyright (c) 2018-2022 Nokia.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
 #
 
 
-from unittest.mock import patch, Mock
+from unittest.mock import patch, Mock, MagicMock, call, ANY
 import pytest
 from redis import exceptions as redis_exceptions
 import ricsdl.backend
@@ -28,9 +28,34 @@ from ricsdl.configuration import _Configuration
 from ricsdl.configuration import DbBackendType
 import ricsdl.exceptions
 
+EVENT_SEPARATOR = "___"
+
+def get_test_sdl_standby_config():
+    return _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt',
+                                 db_ports=['6379'],
+                                 db_sentinel_ports=[],
+                                 db_sentinel_master_names=[],
+                                 db_cluster_addrs=['service-ricplt-dbaas-tcp-cluster-0.ricplt'],
+                                 db_type=DbBackendType.REDIS)
+
+def get_test_sdl_sentinel_config():
+    return _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt',
+                                 db_ports=['6379'],
+                                 db_sentinel_ports=['26379'],
+                                 db_sentinel_master_names=['dbaasmaster'],
+                                 db_cluster_addrs=['service-ricplt-dbaas-tcp-cluster-0.ricplt'],
+                                 db_type=DbBackendType.REDIS)
+
+def get_test_sdl_sentinel_cluster_config():
+    return _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt',
+                                 db_ports=['6379','6380'],
+                                 db_sentinel_ports=['26379','26380'],
+                                 db_sentinel_master_names=['dbaasmaster-cluster-0','dbaasmaster-cluster-1'],
+                                 db_cluster_addrs=['service-ricplt-dbaas-tcp-cluster-0.ricplt','service-ricplt-dbaas-tcp-cluster-1.ricplt'],
+                                 db_type=DbBackendType.REDIS)
 
 @pytest.fixture()
-def redis_backend_fixture(request):
+def redis_backend_common_fixture(request):
     request.cls.ns = 'some-ns'
     request.cls.dl_redis = [b'1', b'2']
     request.cls.dm = {'a': b'1', 'b': b'2'}
@@ -55,23 +80,91 @@ def redis_backend_fixture(request):
     request.cls.group_redis = '{some-ns},some-group'
     request.cls.groupmembers = set([b'm1', b'm2'])
     request.cls.groupmember = b'm1'
-    request.cls.channels = ['abs', 'gma']
-    request.cls.channels_and_events = {'abs': ['cbn']}
-    request.cls.channels_and_events_redis = ['{some-ns},abs', 'cbn']
+    request.cls.channels = ['ch1', 'ch2']
+    request.cls.channels_and_events = {'ch1': ['ev1'], 'ch2': ['ev2', 'ev3']}
+    request.cls.channels_and_events_redis = ['{some-ns},ch1', 'ev1',
+                                             '{some-ns},ch2', 'ev2' + EVENT_SEPARATOR + 'ev3']
 
+    yield
+
+@pytest.fixture(params=['standalone', 'sentinel', 'sentinel_cluster'])
+def redis_backend_fixture(request, redis_backend_common_fixture):
     request.cls.configuration = Mock()
-    mock_conf_params = _Configuration.Params(db_host=None,
-                                             db_port=None,
-                                             db_sentinel_port=None,
-                                             db_sentinel_master_name=None,
-                                             db_type=DbBackendType.REDIS)
-    request.cls.configuration.get_params.return_value = mock_conf_params
-    with patch('ricsdl.backend.redis.Redis') as mock_redis, patch(
-            'ricsdl.backend.redis.PubSub') as mock_pubsub:
-        db = ricsdl.backend.get_backend_instance(request.cls.configuration)
-        request.cls.mock_redis = mock_redis.return_value
-        request.cls.mock_pubsub = mock_pubsub.return_value
-    request.cls.db = db
+    request.cls.configuration.get_event_separator.return_value = EVENT_SEPARATOR
+    request.cls.test_backend_type = request.param
+    if request.param == 'standalone':
+        cfg = get_test_sdl_standby_config()
+        request.cls.configuration.get_params.return_value = cfg
+        with patch('ricsdl.backend.redis.Redis') as mock_redis, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub, patch(
+                   'threading.Thread') as mock_thread:
+            db = ricsdl.backend.get_backend_instance(request.cls.configuration)
+            request.cls.mock_redis = mock_redis.return_value
+            request.cls.mock_pubsub = mock_pubsub.return_value
+            request.cls.mock_pubsub_thread = mock_thread.return_value
+            request.cls.mock_pubsub_thread.is_alive.return_value = False
+        request.cls.db = db
+
+        mock_redis.assert_called_once_with(db=0, host=cfg.db_host, max_connections=20, port=cfg.db_ports[0])
+        mock_pubsub.assert_called_once_with(EVENT_SEPARATOR, request.cls.mock_redis.connection_pool,
+                                            ignore_subscribe_messages=True)
+        assert request.cls.mock_redis.set_response_callback.call_count == 2
+        assert request.cls.mock_redis.set_response_callback.call_args_list == [call('SETIE', ANY), call('DELIE', ANY)]
+
+    elif request.param == 'sentinel':
+        cfg = get_test_sdl_sentinel_config()
+        request.cls.configuration.get_params.return_value = cfg
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub, patch(
+                   'threading.Thread') as mock_thread:
+            db = ricsdl.backend.get_backend_instance(request.cls.configuration)
+            request.cls.mock_redis = mock_sentinel.return_value.master_for.return_value
+            request.cls.mock_pubsub = mock_pubsub.return_value
+            request.cls.mock_pubsub_thread = mock_thread.return_value
+            request.cls.mock_pubsub_thread.is_alive.return_value = False
+        request.cls.db = db
+
+        mock_sentinel.assert_called_once_with([(cfg.db_host, cfg.db_sentinel_ports[0])])
+        mock_sentinel.master_for.called_once_with(cfg.db_sentinel_master_names[0])
+        mock_pubsub.assert_called_once_with(EVENT_SEPARATOR, request.cls.mock_redis.connection_pool,
+                                            ignore_subscribe_messages=True)
+        assert request.cls.mock_redis.set_response_callback.call_count == 2
+        assert request.cls.mock_redis.set_response_callback.call_args_list == [call('SETIE', ANY), call('DELIE', ANY)]
+
+    elif request.param == 'sentinel_cluster':
+        cfg = get_test_sdl_sentinel_cluster_config()
+        request.cls.configuration.get_params.return_value = cfg
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub, patch(
+                   'threading.Thread') as mock_thread:
+            db = ricsdl.backend.get_backend_instance(request.cls.configuration)
+            request.cls.mock_redis = mock_sentinel.return_value.master_for.return_value
+            request.cls.mock_pubsub = mock_pubsub.return_value
+            request.cls.mock_pubsub_thread = mock_thread.return_value
+            request.cls.mock_pubsub_thread.is_alive.return_value = False
+        request.cls.db = db
+
+        assert mock_sentinel.call_count == 2
+        mock_sentinel.assert_has_calls([
+            call([('service-ricplt-dbaas-tcp-cluster-0.ricplt', '26379')]),
+            call([('service-ricplt-dbaas-tcp-cluster-1.ricplt', '26380')]),
+        ], any_order=True)
+        assert mock_sentinel.return_value.master_for.call_count == 2
+        mock_sentinel.return_value.master_for.assert_has_calls(
+            [call('dbaasmaster-cluster-0'), call('dbaasmaster-cluster-1')], any_order=True,
+        )
+        assert mock_pubsub.call_count == 2
+        mock_pubsub.assert_has_calls([
+            call(EVENT_SEPARATOR, request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True),
+            call(EVENT_SEPARATOR, request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True),
+        ])
+        assert request.cls.mock_redis.set_response_callback.call_count == 4
+        assert request.cls.mock_redis.set_response_callback.call_args_list == [
+            call('SETIE', ANY), call('DELIE', ANY),
+            call('SETIE', ANY), call('DELIE', ANY),
+        ]
+    else:
+        raise NotImplementedError
 
     yield
 
@@ -81,7 +174,10 @@ class TestRedisBackend:
     def test_is_connected_function_success(self):
         self.mock_redis.ping.return_value = True
         ret = self.db.is_connected()
-        self.mock_redis.ping.assert_called_once()
+        if self.test_backend_type == 'sentinel_cluster':
+            assert self.mock_redis.ping.call_count == 2
+        else:
+            assert self.mock_redis.ping.call_count == 1
         assert ret is True
 
     def test_is_connected_function_returns_false_if_ping_fails(self):
@@ -437,9 +533,11 @@ class TestRedisBackend:
 
     def test_subscribe_channel_with_thread_success(self):
         cb = Mock()
-        self.db.pubsub_thread.is_alive = Mock()
-        self.db.pubsub_thread.is_alive.return_value = False
-        self.db._run_in_thread = True
+        # Call first start_event_listener() to enable run_in_thread flag. When subscribe_channel()
+        # is called thread is started if the flag is enabled. In real-life scenario it's highly
+        # advisable at first to subscribe to some events by calling subscribe_channel() and only
+        # after it start threads by calling start_event_listener().
+        self.db.start_event_listener()
         self.db.subscribe_channel(self.ns, cb, self.channels)
         self.mock_pubsub.run_in_thread.assert_called_once_with(daemon=True, sleep_time=0.001)
 
@@ -450,53 +548,144 @@ class TestRedisBackend:
 
     def test_unsubscribe_channel_success(self):
         self.db.unsubscribe_channel(self.ns, [self.channels[0]])
-        self.mock_pubsub.unsubscribe.assert_called_with('{some-ns},abs')
+        self.mock_pubsub.unsubscribe.assert_called_with('{some-ns},ch1')
 
     def test_unsubscribe_channel_can_map_redis_exception_to_sdl_exeception(self):
         self.mock_pubsub.unsubscribe.side_effect = redis_exceptions.ResponseError('redis error!')
         with pytest.raises(ricsdl.exceptions.RejectedByBackend):
             self.db.unsubscribe_channel(self.ns, [self.channels[0]])
 
-    def test_start_event_listener_success(self):
-        self.db.start_event_listener()
-        assert self.db._run_in_thread
-
-    def test_start_event_listener_subscribe_first(self):
-        self.mock_pubsub.run_in_thread.return_value = Mock()
-        self.mock_redis.pubsub_channels.return_value = [b'{some-ns},abs']
+    def test_subscribe_and_start_event_listener(self):
+        self.mock_redis.pubsub_channels.return_value = self.channels_and_events_redis
         self.db.subscribe_channel(self.ns, Mock(), self.channels)
         self.db.start_event_listener()
-        self.mock_pubsub.run_in_thread.assert_called_once_with(daemon=True, sleep_time=0.001)
+
+        if self.test_backend_type == 'sentinel_cluster':
+            assert self.mock_redis.pubsub_channels.call_count == 2
+            assert self.mock_pubsub.run_in_thread.call_count == 2
+            self.mock_pubsub.run_in_thread.assert_has_calls([
+                call(daemon=True, sleep_time=0.001),
+                call(daemon=True, sleep_time=0.001),
+            ])
+        else:
+            self.mock_redis.pubsub_channels.assert_called_once()
+            self.mock_pubsub.run_in_thread.assert_called_once_with(daemon=True, sleep_time=0.001)
 
     def test_start_event_listener_fail(self):
-        self.db.pubsub_thread.is_alive = Mock()
-        self.db.pubsub_thread.is_alive.return_value = True
+        self.mock_pubsub_thread.is_alive.return_value = True
         with pytest.raises(ricsdl.exceptions.RejectedByBackend):
             self.db.start_event_listener()
 
     def test_handle_events_success(self):
         self.db.handle_events()
-        self.mock_pubsub.get_message.assert_called_once_with(ignore_subscribe_messages=True)
+        self.db.handle_events()
+        self.db.handle_events()
+        self.db.handle_events()
+        assert self.mock_pubsub.get_message.call_count == 4
 
-    def test_handle_events_fail_already_started(self):
-        self.db.pubsub_thread.is_alive = Mock()
-        self.db.pubsub_thread.is_alive.return_value = True
+    def test_handle_events_fail_if_subsub_thread_alive(self):
+        self.mock_pubsub_thread.is_alive.return_value = True
         with pytest.raises(ricsdl.exceptions.RejectedByBackend):
             self.db.handle_events()
 
-    def test_handle_events_fail_already_set(self):
-        self.db._run_in_thread = True
+    def test_handle_events_fail_if_event_listener_already_running(self):
+        self.db.start_event_listener()
         with pytest.raises(ricsdl.exceptions.RejectedByBackend):
             self.db.handle_events()
 
+    def test_handle_events_ignores_message_handling_redis_runtime_exception(self):
+         self.mock_pubsub.get_message.side_effect = RuntimeError()
+         self.db.handle_events()
+         self.mock_pubsub.get_message.assert_called_once()
+
     def test_get_redis_connection_function_success(self):
-        ret = self.db.get_redis_connection()
+        ret = self.db.get_redis_connection(self.ns)
         assert ret is self.mock_redis
 
     def test_redis_backend_object_string_representation(self):
         str_out = str(self.db)
         assert str_out is not None
 
+    def test_namespace_hash_algorithm_stays_unaltered(self):
+        ret_hash = self.db._RedisBackend__get_hash('sdltoolns')
+        assert ret_hash == 2897969051
+
+def test_standalone_redis_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_standby_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Redis') as mock_redis:
+            mock_redis.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_standalone_pubsub_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_standby_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Redis') as mock_redis, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub:
+            mock_pubsub.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_redis_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel:
+            mock_sentinel.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_pubsub_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub:
+            mock_pubsub.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_master_for_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub:
+            mock_sentinel.return_value.master_for.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_cluster_redis_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_cluster_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel:
+            mock_sentinel.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_cluster_pubsub_init_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_cluster_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub:
+            mock_pubsub.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
+
+def test_sentinel_cluster_master_for_exception_is_mapped_to_sdl_exeception():
+    mock_cfg = Mock()
+    cfg_params = get_test_sdl_sentinel_cluster_config()
+    mock_cfg.get_params.return_value = cfg_params
+    with pytest.raises(ricsdl.exceptions.RejectedByBackend):
+        with patch('ricsdl.backend.redis.Sentinel') as mock_sentinel, patch(
+                   'ricsdl.backend.redis.PubSub') as mock_pubsub:
+            mock_sentinel.return_value.master_for.side_effect = redis_exceptions.ResponseError('redis error!')
+            ricsdl.backend.get_backend_instance(mock_cfg)
 
 class MockRedisLock:
     def __init__(self, redis, name, timeout=None, sleep=0.1,
@@ -539,9 +728,10 @@ def redis_backend_lock_fixture(request, mock_redis_lock):
 
     request.cls.configuration = Mock()
     mock_conf_params = _Configuration.Params(db_host=None,
-                                             db_port=None,
-                                             db_sentinel_port=None,
-                                             db_sentinel_master_name=None,
+                                             db_ports=None,
+                                             db_sentinel_ports=None,
+                                             db_sentinel_master_names=None,
+                                             db_cluster_addrs=None,
                                              db_type=DbBackendType.REDIS)
     request.cls.configuration.get_params.return_value = mock_conf_params
 
@@ -674,9 +864,9 @@ def test_system_error_exceptions_are_not_mapped_to_any_sdl_exception():
 class TestRedisClient:
     @classmethod
     def setup_class(cls):
-        cls.pubsub = ricsdl.backend.redis.PubSub(Mock())
-        cls.pubsub.channels = {b'{some-ns},abs': Mock()}
+        cls.pubsub = ricsdl.backend.redis.PubSub(EVENT_SEPARATOR, Mock())
+        cls.pubsub.channels = {b'{some-ns},ch1': Mock()}
 
     def test_handle_pubsub_message(self):
-        assert self.pubsub.handle_message([b'message', b'{some-ns},abs', b'cbn']) == ('abs', 'cbn')
-        self.pubsub.channels.get(b'{some-ns},abs').assert_called_once_with('abs', 'cbn')
+        assert self.pubsub.handle_message([b'message', b'{some-ns},ch1', b'cbn']) == ('ch1', ['cbn'])
+        self.pubsub.channels.get(b'{some-ns},ch1').assert_called_once_with('ch1', ['cbn'])