Read list type of environment variables
[ric-plt/sdlpy.git] / ricsdl-package / tests / backend / test_redis.py
index 1d79752..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.
@@ -28,29 +28,30 @@ 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_port=6379,
-                                 db_sentinel_port=None,
-                                 db_sentinel_master_name=None,
-                                 db_cluster_addr_list=None,
+                                 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_port=6379,
-                                 db_sentinel_port=26379,
-                                 db_sentinel_master_name='dbaasmaster',
-                                 db_cluster_addr_list=None,
+                                 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_port=6379,
-                                 db_sentinel_port=26379,
-                                 db_sentinel_master_name='dbaasmaster',
-                                 db_cluster_addr_list='service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.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()
@@ -79,15 +80,17 @@ def redis_backend_common_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'], 'gma': ['jkl']}
-    request.cls.channels_and_events_redis = ['{some-ns},abs', 'cbn', '{some-ns},gma', 'jkl']
+    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()
+    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()
@@ -102,8 +105,9 @@ def redis_backend_fixture(request, redis_backend_common_fixture):
             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_port)
-        mock_pubsub.assert_called_once_with(request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True)
+        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)]
 
@@ -120,9 +124,10 @@ def redis_backend_fixture(request, redis_backend_common_fixture):
             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_port)])
-        mock_sentinel.master_for.called_once_with(cfg.db_sentinel_master_name)
-        mock_pubsub.assert_called_once_with(request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True)
+        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)]
 
@@ -141,17 +146,17 @@ def redis_backend_fixture(request, redis_backend_common_fixture):
 
         assert mock_sentinel.call_count == 2
         mock_sentinel.assert_has_calls([
-            call([('service-ricplt-dbaas-tcp-cluster-0.ricplt', cfg.db_sentinel_port)]),
-            call([('service-ricplt-dbaas-tcp-cluster-1.ricplt', cfg.db_sentinel_port)]),
+            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(cfg.db_sentinel_master_name), call(cfg.db_sentinel_master_name)], any_order=True,
+            [call('dbaasmaster-cluster-0'), call('dbaasmaster-cluster-1')], any_order=True,
         )
         assert mock_pubsub.call_count == 2
         mock_pubsub.assert_has_calls([
-            call(request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True),
-            call(request.cls.mock_redis.connection_pool, ignore_subscribe_messages=True),
+            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 == [
@@ -543,7 +548,7 @@ 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!')
@@ -723,10 +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_cluster_addr_list=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
 
@@ -859,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'])