class Db(object): def __init__(self): self.cfgdb_clients = {} self.db_clients = {} self.cfgdb = ConfigDBConnector() self.cfgdb.connect() self.cfgdb_pipe = ConfigDBPipeConnector() self.cfgdb_pipe.connect() self.db = SonicV2Connector(host="127.0.0.1") # Skip connecting to chassis databases in line cards self.db_list = list(self.db.get_db_list()) if not device_info.is_supervisor(): try: self.db_list.remove('CHASSIS_APP_DB') self.db_list.remove('CHASSIS_STATE_DB') except Exception: pass for db_id in self.db_list: self.db.connect(db_id) self.cfgdb_clients[constants.DEFAULT_NAMESPACE] = self.cfgdb self.db_clients[constants.DEFAULT_NAMESPACE] = self.db if multi_asic.is_multi_asic(): self.ns_list = multi_asic_ns_choices() for ns in self.ns_list: self.cfgdb_clients[ns] = ( multi_asic.connect_config_db_for_ns(ns)) self.db_clients[ns] = multi_asic.connect_to_all_dbs_for_ns(ns) def get_data(self, table, key): data = self.cfgdb.get_table(table) return data[key] if key in data else None
def test_ConfigDBPipeConnector(): config_db = ConfigDBPipeConnector() config_db.connect(wait_for_init=False) config_db.get_redis_client(config_db.CONFIG_DB).flushdb() config_db.set_entry("TEST_PORT", "Ethernet112", {"alias": "etp1x"}) allconfig = config_db.get_config() assert allconfig["TEST_PORT"]["Ethernet112"]["alias"] == "etp1x" config_db.set_entry("TEST_PORT", "Ethernet112", {"mtu": "12345"}) allconfig = config_db.get_config() assert "alias" not in allconfig["TEST_PORT"]["Ethernet112"] assert allconfig["TEST_PORT"]["Ethernet112"]["mtu"] == "12345" config_db.mod_config(allconfig) allconfig["TEST_PORT"]["Ethernet113"] = None allconfig["TEST_VLAN"] = None config_db.mod_config(allconfig) allconfig.setdefault("ACL_TABLE", {}).setdefault( "EVERFLOW", {})["ports"] = ["Ethernet0", "Ethernet4", "Ethernet8"] config_db.mod_config(allconfig) allconfig = config_db.get_config() config_db.delete_table("TEST_PORT") config_db.delete_table("ACL_TABLE") allconfig = config_db.get_config() assert len(allconfig) == 0
def test_ConfigDBScan(): config_db = ConfigDBPipeConnector() config_db.connect(wait_for_init=False) n = 1000 for i in range(0, n): s = str(i) config_db.mod_entry("TEST_TYPE" + s, "Ethernet" + s, {"alias" + s: "etp" + s}) allconfig = config_db.get_config() assert len(allconfig) == n config_db = ConfigDBConnector() config_db.connect(wait_for_init=False) allconfig = config_db.get_config() assert len(allconfig) == n for i in range(0, n): s = str(i) config_db.delete_table("TEST_TYPE" + s)