def match_engine(): print("SETUP") os.environ["VERBOSE"] = "1" # Monkey Patch the SonicV2Connector Object from ...mock_tables import dbconnector db = SonicV2Connector() # popualate the db with mock data db_names = list(dedicated_dbs.keys()) try: populate_mock(db, db_names, dedicated_dbs) except Exception as e: assert False, "Mock initialization failed: " + str(e) # Initialize connection pool conn_pool = ConnectionPool() DEF_NS = '' # Default Namespace conn_pool.cache = {DEF_NS: {'conn': db, 'connected_to': set(db_names)}} # Initialize match_engine match_engine = MatchEngine(conn_pool) yield match_engine print("TEARDOWN") os.environ["VERBOSE"] = "0"
def match_engine(): print("SETUP") os.environ["VERBOSE"] = "1" # Monkey Patch the SonicV2Connector Object from ...mock_tables import dbconnector db = SonicV2Connector() # popualate the db with mock data db_names = list(dedicated_dbs.keys()) try: populate_mock(db, db_names, dedicated_dbs) except Exception as e: assert False, "Mock initialization failed: " + str(e) # Initialize connection pool conn_pool = ConnectionPool() conn_pool.fill(DEFAULT_NAMESPACE, db, db_names) # Initialize match_engine match_engine = MatchEngine(conn_pool) yield match_engine print("TEARDOWN") os.environ["VERBOSE"] = "0"
def match_engine(): print("SETUP") os.environ["VERBOSE"] = "1" dump_port_input = os.path.join(os.path.dirname(__file__), "../dump_input/dump/default") dedicated_dbs = {} dedicated_dbs['CONFIG_DB'] = os.path.join(dump_port_input, "config_db.json") dedicated_dbs['APPL_DB'] = os.path.join(dump_port_input, "appl_db.json") dedicated_dbs['STATE_DB'] = os.path.join(dump_port_input, "state_db.json") dedicated_dbs['ASIC_DB'] = os.path.join(dump_port_input, "asic_db.json") conn = SonicV2Connector() # popualate the db ,with mock data db_names = list(dedicated_dbs.keys()) try: populate_mock(conn, db_names, dedicated_dbs) except Exception as e: assert False, "Mock initialization failed: " + str(e) conn_pool = ConnectionPool() conn_pool.fill(DEFAULT_NAMESPACE, conn, db_names) match_engine = MatchEngine(conn_pool) yield match_engine print("TEARDOWN")
def test_missing_field(self): rv = { "COPP_GROUP|queue4_group2": { "trap_action": "copy", "trap_priority": "4", "queue": "4", "meter_type": "packets", "mode": "sr_tcm", "cir": "600", "cbs": "600", "red_action": "drop" } } template = {"error": "", "keys": [rv], "return_values": {}} m_engine = MatchEngine() m_engine.fetch = MagicMock(return_value=template) m_engine_optim = MatchRequestOptimizer(m_engine) req = MatchRequest(db="CONFIG_DB", table="COPP_GROUP", key_pattern="queue4*", field="red_action", value="drop", return_fields=["whatever"]) ret = m_engine_optim.fetch(req) assert ret["error"] == "" assert len(ret["keys"]) == 1 assert "COPP_GROUP|queue4_group2" in ret["keys"] # missing filed should not cause an excpetion in the optimizer assert "whatever" in ret["return_values"]["COPP_GROUP|queue4_group2"] assert not ret["return_values"]["COPP_GROUP|queue4_group2"]["whatever"]
def get_port_acl_binding(db_wrap, port, ns): """ Verify if the port is not bound to any ACL Table Args: db_wrap: utilities_common.Db() object port: Iface name ns: namespace Returns: list: ACL_TABLE names if found, otherwise empty """ ACL = "ACL_TABLE" # Table to look for port bindings if not isinstance(db_wrap, Db): raise Exception("db_wrap object is not of type utilities_common.Db") conn_pool = ConnectionPool() conn_pool.fill(ns, db_wrap.db_clients[ns], db_wrap.db_list) m_engine = MatchEngine(conn_pool) req = MatchRequest(db="CONFIG_DB", table=ACL, key_pattern="*", field="ports@", value=port, ns=ns, match_entire_list=False) ret = m_engine.fetch(req) acl_tables, _ = get_matched_keys(ret) return acl_tables
def test_namespace_asic1(self, match_engine): req = MatchRequest(db="CONFIG_DB", table="PORT", key_pattern="Ethernet-BP256", ns="asic1") match_engine = MatchEngine() ret = match_engine.fetch(req) assert ret["error"] == "" assert len(ret["keys"]) == 1 assert "PORT|Ethernet-BP256" in ret["keys"]
def test_namespace_asic0(self, match_engine): req = MatchRequest(db="CONFIG_DB", table="PORT", key_pattern="*", field="asic_port_name", value="Eth0-ASIC0", ns="asic0") match_engine = MatchEngine() ret = match_engine.fetch(req) assert ret["error"] == "" assert len(ret["keys"]) == 1 assert "PORT|Ethernet0" in ret["keys"]
def match_engine_masic(): print("SETUP") os.environ["VERBOSE"] = "1" from ..mock_tables import mock_multi_asic reload(mock_multi_asic) from ..mock_tables import dbconnector dbconnector.load_namespace_config() dump_input = os.path.join(os.path.dirname(__file__), "../dump_input/") dedicated_dbs = {} conn = SonicV2Connector() # popualate the db ,with mock data db_names = list(dedicated_dbs.keys()) try: populate_mock(conn, db_names, dedicated_dbs) except Exception as e: assert False, "Mock initialization failed: " + str(e) conn_pool = ConnectionPool() dedicated_dbs['CONFIG_DB'] = os.path.join(dump_input, "dump/default/config_db.json") dedicated_dbs['APPL_DB'] = os.path.join(dump_input, "dump/default/appl_db.json") dedicated_dbs['STATE_DB'] = os.path.join(dump_input, "dump/default/state_db.json") dedicated_dbs['ASIC_DB'] = os.path.join(dump_input, "dump/default/asic_db.json") conn_pool.fill(DEFAULT_NAMESPACE, conn_pool.initialize_connector(DEFAULT_NAMESPACE), list(dedicated_dbs.keys())) populate_mock(conn_pool.cache[DEFAULT_NAMESPACE][CONN], list(dedicated_dbs.keys()), dedicated_dbs) dedicated_dbs['CONFIG_DB'] = os.path.join(dump_input, "dump/asic0/config_db.json") dedicated_dbs['APPL_DB'] = os.path.join(dump_input, "dump/asic0/appl_db.json") dedicated_dbs['STATE_DB'] = os.path.join(dump_input, "dump/asic0/state_db.json") dedicated_dbs['ASIC_DB'] = os.path.join(dump_input, "dump/asic0/asic_db.json") conn_pool.fill("asic0", conn_pool.initialize_connector("asic0"), list(dedicated_dbs.keys())) populate_mock(conn_pool.cache["asic0"][CONN], list(dedicated_dbs.keys()), dedicated_dbs) dedicated_dbs['CONFIG_DB'] = os.path.join(dump_input, "dump/asic1/config_db.json") dedicated_dbs['APPL_DB'] = os.path.join(dump_input, "dump/asic1/appl_db.json") dedicated_dbs['STATE_DB'] = os.path.join(dump_input, "dump/asic1/state_db.json") dedicated_dbs['ASIC_DB'] = os.path.join(dump_input, "dump/asic1/asic_db.json") conn_pool.fill("asic1", conn_pool.initialize_connector("asic1"), list(dedicated_dbs.keys())) populate_mock(conn_pool.cache["asic1"][CONN], list(dedicated_dbs.keys()), dedicated_dbs) match_engine = MatchEngine(conn_pool) yield match_engine print("TEARDOWN")
def test_caching(self): rv = { "COPP_GROUP|queue4_group2": { "trap_action": "copy", "trap_priority": "4", "queue": "4", "meter_type": "packets", "mode": "sr_tcm", "cir": "600", "cbs": "600", "red_action": "drop" } } template = {"error": "", "keys": [rv], "return_values": {}} m_engine = MatchEngine() m_engine.fetch = MagicMock(return_value=template) m_engine_optim = MatchRequestOptimizer(m_engine) req = MatchRequest(db="CONFIG_DB", table="COPP_GROUP", key_pattern="queue4*", field="red_action", value="drop") ret = m_engine_optim.fetch(req) assert ret["error"] == "" assert len(ret["keys"]) == 1 assert "COPP_GROUP|queue4_group2" in ret["keys"] req = MatchRequest(db="CONFIG_DB", table="COPP_GROUP", key_pattern="queue4_group2") ret = m_engine_optim.fetch(req) assert ret["error"] == "" assert len(ret["keys"]) == 1 assert "COPP_GROUP|queue4_group2" in ret["keys"] assert m_engine.fetch.call_count == 1
def dump(ctx): ctx.obj = MatchEngine()
def __init__(self, match_engine=None): if not isinstance(match_engine, MatchEngine): self.match_engine = MatchEngine(None) else: self.match_engine = match_engine
def __init__(self, *args, **kwargs): super(TestMatchRequestValidation, self).__init__(*args, **kwargs) self.match_engine = MatchEngine()
def __init__(self, *args, **kwargs): super(TestMatchEngine, self).__init__(*args, **kwargs) self.match_engine = MatchEngine()