diff --git a/tests/dump_tests/module_tests/mock_sonicv2connector.py b/tests/dump_tests/module_tests/mock_sonicv2connector.py deleted file mode 100644 index f6ba18981e..0000000000 --- a/tests/dump_tests/module_tests/mock_sonicv2connector.py +++ /dev/null @@ -1,72 +0,0 @@ -import json -import re -import os -from utilities_common.constants import DEFAULT_NAMESPACE - - -class MockSonicV2Connector(): - def __init__(self, dedicated_dbs, **kwargs): - if "namespace" in kwargs and kwargs["namespace"] != DEFAULT_NAMESPACE: - raise "This Mock doesn't support multi-asic configuration" - self.db = None - self.db_name_connect_to = None - self.dedicated_dbs = dedicated_dbs - db_config_path = os.path.join(os.path.dirname(__file__), "../../mock_tables/database_config.json") - with open(db_config_path) as f: - self.db_cfg = json.load(f) - - def connect(self, db_name, retry=False): - if db_name not in self.dedicated_dbs: - raise Exception("{} not found. Available db's: {}".format(db_name, self.dedicated_dbs.keys())) - try: - with open(self.dedicated_dbs[db_name]) as f: - self.db = json.load(f) - self.db_name_connect_to = db_name - except BaseException as e: - raise "Connection Failure" + str(e) - - def get_db_separator(self, db_name): - return self.db_cfg["DATABASES"][db_name]["separator"] - - def keys(self, db_name, pattern): - if not self.db: - raise "MockDB Not Connected" - if self.db_name_connect_to != db_name: - raise "Failed to find {} in the MockDB".format(db_name) - - pattern = re.escape(pattern) - pattern = pattern.replace("\\*", ".*") - filtered_keys = [] - all_keys = self.db.keys() - for key in all_keys: - if re.match(pattern, key): - filtered_keys.append(key) - return filtered_keys - - def get_all(self, db_name, key): - if not self.db: - raise "MockDB Not Connected" - if self.db_name_connect_to != db_name: - raise "Failed to find {} in the MockDB".format(db_name) - if key not in self.db: - return {} - return self.db[key] - - def get(self, db_name, key, field): - if not self.db: - raise "MockDB Not Connected" - if self.db_name_connect_to != db_name: - raise "Failed to find {} in the MockDB".format(db_name) - if key not in self.db or field not in self.db[key]: - return "" - return self.db[key][field] - - def hexists(self, db_name, key, field): - if not self.db: - raise "MockDB Not Connected" - if self.db_name_connect_to != db_name: - raise "Failed to find {} in the MockDB".format(db_name) - if key not in self.db or field not in self.db[key]: - return False - else: - return True diff --git a/tests/dump_tests/module_tests/port_test.py b/tests/dump_tests/module_tests/port_test.py index 9886df014a..ebed245e31 100644 --- a/tests/dump_tests/module_tests/port_test.py +++ b/tests/dump_tests/module_tests/port_test.py @@ -8,17 +8,17 @@ from mock import patch from dump.helper import create_template_dict, sort_lists from dump.plugins.port import Port +from dump.match_infra import MatchEngine, ConnectionPool +from swsscommon.swsscommon import SonicV2Connector -from .mock_sonicv2connector import MockSonicV2Connector - +# Location for dedicated db's used for UT module_tests_path = os.path.dirname(__file__) dump_tests_path = os.path.join(module_tests_path, "../") tests_path = os.path.join(dump_tests_path, "../") dump_test_input = os.path.join(tests_path, "dump_input") - -# Location for dedicated db's used for UT port_files_path = os.path.join(dump_test_input, "port") +# Define the mock files to read from dedicated_dbs = {} dedicated_dbs['CONFIG_DB'] = os.path.join(port_files_path, "config_db.json") dedicated_dbs['APPL_DB'] = os.path.join(port_files_path, "appl_db.json") @@ -26,27 +26,56 @@ dedicated_dbs['STATE_DB'] = os.path.join(port_files_path, "state_db.json") -def mock_connector(namespace, use_unix_socket_path=True): - return MockSonicV2Connector(dedicated_dbs, namespace=namespace, use_unix_socket_path=use_unix_socket_path) +def populate_mock(db, db_names): + for db_name in db_names: + db.connect(db_name) + # Delete any default data + db.delete_all_by_pattern(db_name, "*") + with open(dedicated_dbs[db_name]) as f: + mock_json = json.load(f) + for key in mock_json: + for field, value in mock_json[key].items(): + db.set(db_name, key, field, value) + +@pytest.fixture(scope="class", autouse=True) +def match_engine(): -@pytest.fixture(scope="module", autouse=True) -def verbosity_setup(): print("SETUP") os.environ["VERBOSE"] = "1" - yield + + # 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) + 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" -@patch("dump.match_infra.SonicV2Connector", mock_connector) -class TestPortModule(unittest.TestCase): - def test_working_state(self): +@pytest.mark.usefixtures("match_engine") +class TestPortModule: + def test_working_state(self, match_engine): """ Scenario: When the config is properly applied and propagated """ params = {Port.ARG_NAME: "Ethernet176", "namespace": ""} - m_port = Port() + m_port = Port(match_engine) returned = m_port.execute(params) expect = create_template_dict(dbs=["CONFIG_DB", "APPL_DB", "ASIC_DB", "STATE_DB"]) expect["CONFIG_DB"]["keys"].append("PORT|Ethernet176") @@ -57,12 +86,12 @@ def test_working_state(self): ddiff = DeepDiff(sort_lists(returned), sort_lists(expect), ignore_order=True) assert not ddiff, ddiff - def test_missing_asic_port(self): + def test_missing_asic_port(self, match_engine): """ Scenario: When the config was applied and just the SAI_OBJECT_TYPE_PORT is missing """ params = {Port.ARG_NAME: "Ethernet160", "namespace": ""} - m_port = Port() + m_port = Port(match_engine) returned = m_port.execute(params) expect = create_template_dict(dbs=["CONFIG_DB", "APPL_DB", "ASIC_DB", "STATE_DB"]) expect["CONFIG_DB"]["keys"].append("PORT|Ethernet160") @@ -73,12 +102,12 @@ def test_missing_asic_port(self): ddiff = DeepDiff(sort_lists(returned), sort_lists(expect), ignore_order=True) assert not ddiff, ddiff - def test_missing_asic_hostif(self): + def test_missing_asic_hostif(self, match_engine): """ Scenario: When the config was applied and it did not propagate to ASIC DB """ params = {Port.ARG_NAME: "Ethernet164", "namespace": ""} - m_port = Port() + m_port = Port(match_engine) returned = m_port.execute(params) expect = create_template_dict(dbs=["CONFIG_DB", "APPL_DB", "ASIC_DB", "STATE_DB"]) expect["CONFIG_DB"]["keys"].append("PORT|Ethernet164") @@ -89,12 +118,12 @@ def test_missing_asic_hostif(self): ddiff = DeepDiff(returned, expect, ignore_order=True) assert not ddiff, ddiff - def test_missing_state_and_appl(self): + def test_missing_state_and_appl(self, match_engine): """ Scenario: When the config was applied and it did not propagate to other db's """ params = {Port.ARG_NAME: "Ethernet156", "namespace": ""} - m_port = Port() + m_port = Port(match_engine) returned = m_port.execute(params) expect = create_template_dict(dbs=["CONFIG_DB", "APPL_DB", "ASIC_DB", "STATE_DB"]) expect["CONFIG_DB"]["keys"].append("PORT|Ethernet156") @@ -105,12 +134,12 @@ def test_missing_state_and_appl(self): ddiff = DeepDiff(returned, expect, ignore_order=True) assert not ddiff, ddiff - def test_no_port(self): + def test_no_port(self, match_engine): """ Scenario: When no entry for the port is present in any of the db's """ params = {Port.ARG_NAME: "Ethernet152", "namespace": ""} - m_port = Port() + m_port = Port(match_engine) returned = m_port.execute(params) expect = create_template_dict(dbs=["CONFIG_DB", "APPL_DB", "ASIC_DB", "STATE_DB"]) expect["CONFIG_DB"]["tables_not_found"].append("PORT") @@ -121,12 +150,12 @@ def test_no_port(self): ddiff = DeepDiff(returned, expect, ignore_order=True) assert not ddiff, ddiff - def test_all_args(self): + def test_all_args(self, match_engine): """ Scenario: Verify Whether the get_all_args method is working as expected """ params = {} - m_port = Port() + m_port = Port(match_engine) returned = m_port.get_all_args("") expect = ["Ethernet156", "Ethernet160", "Ethernet164", "Ethernet176"] ddiff = DeepDiff(expect, returned, ignore_order=True)