def test_ConfigDBSubscribe(): table_name = 'TEST_TABLE' test_key = 'key1' test_data = {'field1': 'value1'} global output_data global stop_listen_thread output_data = "" stop_listen_thread = False def test_handler(key, data): global output_data assert key == test_key assert data == test_data output_data = test_data['field1'] def thread_coming_entry(): # Note: use a local constructed ConfigDBConnector, and do not reuse Redis connection across threads config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) time.sleep(5) config_db.set_entry(table_name, test_key, test_data) config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) client = config_db.get_redis_client(config_db.CONFIG_DB) client.flushdb() config_db.subscribe(table_name, lambda table, key, data: test_handler(key, data)) assert table_name in config_db.handlers thread = Thread(target=thread_coming_entry) thread.start() pubsub = config_db.get_redis_client(config_db.db_name).pubsub() pubsub.psubscribe("__keyspace@{}__:*".format( config_db.get_dbid(config_db.db_name))) time.sleep(2) while True: if not thread.is_alive(): break item = pubsub.listen_message() if 'type' in item and item['type'] == 'pmessage': key = item['channel'].split(':', 1)[1] try: (table, row) = key.split(config_db.TABLE_NAME_SEPARATOR, 1) if table in config_db.handlers: client = config_db.get_redis_client(config_db.db_name) data = config_db.raw_to_typed(client.hgetall(key)) config_db._ConfigDBConnector__fire(table, row, data) except ValueError: pass # Ignore non table-formated redis entries thread.join() assert output_data == test_data['field1'] config_db.unsubscribe(table_name) assert table_name not in config_db.handlers
def test_ConfigDBConnect(): config_db = ConfigDBConnector() config_db.db_connect('CONFIG_DB') client = config_db.get_redis_client(config_db.CONFIG_DB) client.flushdb() allconfig = config_db.get_config() assert len(allconfig) == 0
def test_ConfigDBConnector(): config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) config_db.get_redis_client(config_db.CONFIG_DB).flushdb() config_db.set_entry("TEST_PORT", "Ethernet111", {"alias": "etp1x"}) allconfig = config_db.get_config() assert allconfig["TEST_PORT"]["Ethernet111"]["alias"] == "etp1x" config_db.set_entry("TEST_PORT", "Ethernet111", {"mtu": "12345"}) allconfig = config_db.get_config() assert "alias" not in allconfig["TEST_PORT"]["Ethernet111"] assert allconfig["TEST_PORT"]["Ethernet111"]["mtu"] == "12345" config_db.delete_table("TEST_PORT") allconfig = config_db.get_config() assert len(allconfig) == 0
def test_ConfigDBConnectorSeparator(): db = swsscommon.DBConnector("APPL_DB", 0, True) config_db = ConfigDBConnector() config_db.db_connect("APPL_DB", False, False) config_db.get_redis_client(config_db.APPL_DB).flushdb() config_db.set_entry("TEST_PORT", "Ethernet222", {"alias": "etp2x"}) db.set("ItemWithoutSeparator", "item11") allconfig = config_db.get_config() assert "TEST_PORT" in allconfig assert "ItemWithoutSeparator" not in allconfig alltable = config_db.get_table("*") assert "Ethernet222" in alltable config_db.delete_table("TEST_PORT") db.delete("ItemWithoutSeparator") allconfig = config_db.get_config() assert len(allconfig) == 0
def test_ConfigDBSubscribe(): table_name = 'TEST_TABLE' test_key = 'key1' test_data = {'field1': 'value1'} global output_data global stop_listen_thread output_data = "" stop_listen_thread = False def test_handler(key, data): global output_data assert key == test_key assert data == test_data output_data = test_data['field1'] # the config_db.listen() is a blocking function so could not use that in the tests # this function has similar logic with a way to exit the listen function def listen_thread_func(config_db): global stop_listen_thread pubsub = config_db.get_redis_client(config_db.db_name).pubsub() pubsub.psubscribe("__keyspace@{}__:*".format( config_db.get_dbid(config_db.db_name))) time.sleep(2) while True: if stop_listen_thread: break item = pubsub.listen_message() if 'type' in item and item['type'] == 'pmessage': key = item['channel'].split(':', 1)[1] try: (table, row) = key.split(config_db.TABLE_NAME_SEPARATOR, 1) if table in config_db.handlers: client = config_db.get_redis_client(config_db.db_name) data = config_db.raw_to_typed(client.hgetall(key)) config_db._ConfigDBConnector__fire(table, row, data) except ValueError: pass # Ignore non table-formated redis entries config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) client = config_db.get_redis_client(config_db.CONFIG_DB) client.flushdb() config_db.subscribe(table_name, lambda table, key, data: test_handler(key, data)) assert table_name in config_db.handlers thread = Thread(target=listen_thread_func, args=(config_db, )) thread.start() time.sleep(5) config_db.set_entry(table_name, test_key, test_data) stop_listen_thread = True thread.join() assert output_data == test_data['field1'] config_db.unsubscribe(table_name) assert table_name not in config_db.handlers
def test_ConfigDBFlush(): config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) config_db.set_entry("TEST_PORT", "Ethernet111", {"alias": "etp1x"}) client = config_db.get_redis_client(config_db.CONFIG_DB) assert ConfigDBConnector.INIT_INDICATOR == "CONFIG_DB_INITIALIZED" assert config_db.INIT_INDICATOR == "CONFIG_DB_INITIALIZED" suc = client.set(config_db.INIT_INDICATOR, 1) assert suc # TODO: redis.get is not yet supported # indicator = client.get(config_db.INIT_INDICATOR) # assert indicator == '1' client.flushdb() allconfig = config_db.get_config() assert len(allconfig) == 0