Skip to content

Commit

Permalink
Removed the entra mock and added a match_engine fixture
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy Karri <vkarri@nvidia.com>
  • Loading branch information
vivekrnv committed Aug 19, 2021
1 parent 663f46b commit 0082da2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 95 deletions.
72 changes: 0 additions & 72 deletions tests/dump_tests/module_tests/mock_sonicv2connector.py

This file was deleted.

75 changes: 52 additions & 23 deletions tests/dump_tests/module_tests/port_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,74 @@
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")
dedicated_dbs['ASIC_DB'] = os.path.join(port_files_path, "asic_db.json")
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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 0082da2

Please sign in to comment.