Skip to content

Commit

Permalink
Move DomInfoUpdateTask class to a separate file (sonic-net#552)
Browse files Browse the repository at this point in the history
* Move DomInfoUpdateTask class to a separate file

Signed-off-by: Mihir Patel <[email protected]>

* Improved code coverage

* Improved code coverage

---------

Signed-off-by: Mihir Patel <[email protected]>
  • Loading branch information
mihirpat1 authored and mssonicbld committed Oct 25, 2024
1 parent 0dc97ce commit 8f506ed
Show file tree
Hide file tree
Showing 3 changed files with 443 additions and 369 deletions.
103 changes: 80 additions & 23 deletions sonic-xcvrd/tests/test_xcvrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def test_DomInfoUpdateTask_task_run_with_exception(self):
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager)
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
exception_received = None
trace = None
try:
Expand All @@ -315,7 +315,7 @@ def test_DomInfoUpdateTask_task_run_with_exception(self):
assert not dom_info_update.is_alive()
assert(type(exception_received) == NotImplementedError)
assert("NotImplementedError" in str(trace) and "effect" in str(trace))
assert("sonic-xcvrd/xcvrd/xcvrd.py" in str(trace))
assert("sonic-xcvrd/xcvrd/dom_mgr.py" in str(trace))
assert("subscribe_port_config_change" in str(trace))

@patch('xcvrd.xcvrd.SfpStateUpdateTask.init', MagicMock())
Expand Down Expand Up @@ -414,9 +414,10 @@ def test_is_npu_si_settings_update_required(self):
assert xcvr_table_helper.is_npu_si_settings_update_required("Ethernet0", port_mapping)
assert not xcvr_table_helper.is_npu_si_settings_update_required("Ethernet0", port_mapping)

@patch('xcvrd.xcvrd._wrapper_is_flat_memory')
@patch('xcvrd.xcvrd._wrapper_get_presence')
@patch('xcvrd.xcvrd._wrapper_get_sfp_type')
@patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0]))
@patch('xcvrd.xcvrd._wrapper_get_presence', MagicMock(return_value=True))
@patch('xcvrd.xcvrd._wrapper_get_transceiver_dom_info', MagicMock(return_value={'temperature': '22.75',
'voltage': '0.5',
'rx1power': '0.7',
Expand All @@ -443,36 +444,60 @@ def test_is_npu_si_settings_update_required(self):
'tx6power': '0.7',
'tx7power': '0.7',
'tx8power': '0.7', }))
def test_post_port_dom_info_to_db(self, mock_get_sfp_type):
def test_post_port_dom_info_to_db(self, mock_get_sfp_type, mock_get_presence, mock_is_flat_memory):
logical_port_name = "Ethernet0"
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
dom_tbl = Table("STATE_DB", TRANSCEIVER_DOM_SENSOR_TABLE)
post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
stop_event.set()
dom_info_update.post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
assert dom_tbl.get_size() == 0
stop_event.clear()
mock_get_presence.return_value = False
dom_info_update.post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
assert dom_tbl.get_size() == 0
mock_get_presence.return_value = True
mock_is_flat_memory.return_value = True
dom_info_update.post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
assert dom_tbl.get_size() == 0
mock_is_flat_memory.return_value = False
dom_info_update.post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
mock_get_sfp_type.return_value = 'QSFP_DD'
post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)
dom_info_update.post_port_dom_info_to_db(logical_port_name, port_mapping, dom_tbl, stop_event)

@patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0]))
@patch('xcvrd.xcvrd._wrapper_get_presence', MagicMock(return_value=True))
@patch('xcvrd.xcvrd._wrapper_get_transceiver_firmware_info', MagicMock(return_value={'active_firmware': '2.1.1',
'inactive_firmware': '1.2.4'}))
def test_post_port_sfp_firmware_info_to_db(self):
@patch('xcvrd.xcvrd._wrapper_is_flat_memory')
@patch('xcvrd.xcvrd._wrapper_get_presence')
def test_post_port_sfp_firmware_info_to_db(self, mock_get_presence, mock_is_flat_memory):
logical_port_name = "Ethernet0"
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
firmware_info_tbl = Table("STATE_DB", TRANSCEIVER_FIRMWARE_INFO_TABLE)
stop_event.set()
dom_info_update.post_port_sfp_firmware_info_to_db(logical_port_name, port_mapping, firmware_info_tbl, stop_event)
assert firmware_info_tbl.get_size() == 0
stop_event.clear()
mock_get_presence.return_value = False
dom_info_update.post_port_sfp_firmware_info_to_db(logical_port_name, port_mapping, firmware_info_tbl, stop_event)
assert firmware_info_tbl.get_size() == 0
post_port_sfp_firmware_info_to_db(logical_port_name, port_mapping, firmware_info_tbl, stop_event)
mock_get_presence.return_value = True
dom_info_update.post_port_sfp_firmware_info_to_db(logical_port_name, port_mapping, firmware_info_tbl, stop_event)
assert firmware_info_tbl.get_size_for_key(logical_port_name) == 2

def test_post_port_dom_threshold_info_to_db(self, mock_get_sfp_type):
logical_port_name = "Ethernet0"
port_mapping = PortMapping()
stop_event = threading.Event()
dom_threshold_tbl = Table("STATE_DB", TRANSCEIVER_DOM_THRESHOLD_TABLE)
post_port_dom_info_to_db(logical_port_name, port_mapping, dom_threshold_tbl, stop_event)
post_port_dom_threshold_info_to_db(logical_port_name, port_mapping, dom_threshold_tbl, stop_event)
mock_get_sfp_type.return_value = 'QSFP_DD'
post_port_dom_info_to_db(logical_port_name, port_mapping, dom_threshold_tbl, stop_event)
post_port_dom_threshold_info_to_db(logical_port_name, port_mapping, dom_threshold_tbl, stop_event)

@patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0]))
@patch('xcvrd.xcvrd._wrapper_get_presence', MagicMock(return_value=True))
Expand All @@ -486,9 +511,11 @@ def test_post_port_pm_info_to_db(self):
logical_port_name = "Ethernet0"
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
pm_tbl = Table("STATE_DB", TRANSCEIVER_PM_TABLE)
assert pm_tbl.get_size() == 0
post_port_pm_info_to_db(logical_port_name, port_mapping, pm_tbl, stop_event)
dom_info_update.post_port_pm_info_to_db(logical_port_name, port_mapping, pm_tbl, stop_event)
assert pm_tbl.get_size_for_key(logical_port_name) == 6

@patch('xcvrd.xcvrd_utilities.port_event_helper.PortMapping.logical_port_name_to_physical_port_list', MagicMock(return_value=[0]))
Expand Down Expand Up @@ -524,9 +551,11 @@ def test_update_port_transceiver_status_table_hw(self):
logical_port_name = "Ethernet0"
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
dom_info_update = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
status_tbl = Table("STATE_DB", TRANSCEIVER_STATUS_TABLE)
assert status_tbl.get_size() == 0
update_port_transceiver_status_table_hw(logical_port_name, port_mapping, status_tbl, stop_event)
dom_info_update.update_port_transceiver_status_table_hw(logical_port_name, port_mapping, status_tbl, stop_event)
assert status_tbl.get_size_for_key(logical_port_name) == 5

@patch('xcvrd.xcvrd.get_physical_port_name_dict', MagicMock(return_value={0: 'Ethernet0'}))
Expand Down Expand Up @@ -2547,7 +2576,7 @@ def mock_get(key):
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager)
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
task.xcvr_table_helper = XcvrTableHelper(DEFAULT_NAMESPACE)
task.port_mapping.handle_port_change_event(PortChangeEvent('Ethernet4', 1, 0, PortChangeEvent.PORT_ADD))
task.port_mapping.handle_port_change_event(PortChangeEvent('Ethernet12', 1, 0, PortChangeEvent.PORT_ADD))
Expand All @@ -2573,21 +2602,49 @@ def test_DomInfoUpdateTask_is_port_in_cmis_initialization_process(self, mock_get
lport = 'Ethernet0'
port_change_event = PortChangeEvent(lport, 1, 0, PortChangeEvent.PORT_ADD)
stop_event = threading.Event()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, skip_cmis_manager)
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, skip_cmis_manager, helper_logger)
task.xcvr_table_helper = XcvrTableHelper(DEFAULT_NAMESPACE)
task.on_port_config_change(port_change_event)
mock_get_cmis_state_from_state_db.return_value = mock_cmis_state
if is_asic_index_none:
lport='INVALID_PORT'
assert task.is_port_in_cmis_initialization_process(lport) == expected_result

def test_beautify_dom_info_dict(self):
dom_info_dict = {
'temperature': '0C',
'eSNR' : 1.1,
}
expected_dom_info_dict = {
'temperature': '0',
'eSNR' : '1.1',
}
port_mapping = PortMapping()
stop_event = threading.Event()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, MagicMock(), helper_logger)
task.beautify_dom_info_dict(dom_info_dict, None)
assert dom_info_dict == expected_dom_info_dict

def test_beautify_info_dict(self):
dom_info_dict = {
'eSNR' : 1.1,
}
expected_dom_info_dict = {
'eSNR' : '1.1',
}
port_mapping = PortMapping()
stop_event = threading.Event()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, MagicMock(), helper_logger)
task.beautify_info_dict(dom_info_dict)
assert dom_info_dict == expected_dom_info_dict

@patch('xcvrd.xcvrd.XcvrTableHelper', MagicMock())
@patch('xcvrd.xcvrd.delete_port_from_status_table_hw')
def test_DomInfoUpdateTask_handle_port_change_event(self, mock_del_status_tbl_hw):
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager)
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
task.xcvr_table_helper = XcvrTableHelper(DEFAULT_NAMESPACE)
port_change_event = PortChangeEvent('Ethernet0', 1, 0, PortChangeEvent.PORT_ADD)
task.on_port_config_change(port_change_event)
Expand All @@ -2611,20 +2668,20 @@ def test_DomInfoUpdateTask_task_run_stop(self):
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager)
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
task.start()
task.join()
assert not task.is_alive()

@patch('xcvrd.xcvrd.XcvrTableHelper', MagicMock())
@patch('xcvrd.xcvrd_utilities.sfp_status_helper.detect_port_in_error_status')
@patch('xcvrd.xcvrd.post_port_sfp_firmware_info_to_db')
@patch('xcvrd.xcvrd.post_port_dom_info_to_db')
@patch('xcvrd.dom_mgr.DomInfoUpdateTask.post_port_sfp_firmware_info_to_db')
@patch('xcvrd.dom_mgr.DomInfoUpdateTask.post_port_dom_info_to_db')
@patch('swsscommon.swsscommon.Select.addSelectable', MagicMock())
@patch('swsscommon.swsscommon.SubscriberStateTable')
@patch('swsscommon.swsscommon.Select.select')
@patch('xcvrd.xcvrd.update_port_transceiver_status_table_hw')
@patch('xcvrd.xcvrd.post_port_pm_info_to_db')
@patch('xcvrd.dom_mgr.DomInfoUpdateTask.update_port_transceiver_status_table_hw')
@patch('xcvrd.dom_mgr.DomInfoUpdateTask.post_port_pm_info_to_db')
def test_DomInfoUpdateTask_task_worker(self, mock_post_pm_info, mock_update_status_hw,
mock_select, mock_sub_table,
mock_post_dom_info, mock_post_firmware_info, mock_detect_error):
Expand All @@ -2637,7 +2694,7 @@ def test_DomInfoUpdateTask_task_worker(self, mock_post_pm_info, mock_update_stat
port_mapping = PortMapping()
stop_event = threading.Event()
mock_cmis_manager = MagicMock()
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager)
task = DomInfoUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, mock_cmis_manager, helper_logger)
task.xcvr_table_helper = XcvrTableHelper(DEFAULT_NAMESPACE)
task.task_stopping_event.wait = MagicMock(side_effect=[False, True])
task.get_dom_polling_from_config_db = MagicMock(return_value='enabled')
Expand Down Expand Up @@ -2791,7 +2848,7 @@ def test_SfpStateUpdateTask_mapping_event_from_change_event(self):
@patch('xcvrd.xcvrd._wrapper_get_transceiver_change_event')
@patch('xcvrd.xcvrd.del_port_sfp_dom_info_from_db')
@patch('xcvrd.xcvrd_utilities.media_settings_parser.notify_media_setting')
@patch('xcvrd.xcvrd.post_port_sfp_firmware_info_to_db')
@patch('xcvrd.dom_mgr.DomInfoUpdateTask.post_port_sfp_firmware_info_to_db')
@patch('xcvrd.xcvrd.post_port_dom_threshold_info_to_db')
@patch('xcvrd.xcvrd.post_port_sfp_info_to_db')
@patch('xcvrd.xcvrd.update_port_transceiver_status_table_sw')
Expand Down
Loading

0 comments on commit 8f506ed

Please sign in to comment.