|
| 1 | +from unittest.mock import patch |
| 2 | +from mock import MagicMock |
| 3 | +import pytest |
| 4 | +from sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade import TARGET_E0_VALUE, TARGET_LIST, CmisTargetFWUpgradeAPI |
| 5 | +from sonic_platform_base.sonic_xcvr.codes.public.cmisTargetFWUpgrade import CmisTargetFWUpgradeCodes |
| 6 | +from sonic_platform_base.sonic_xcvr.mem_maps.public.cmisTargetFWUpgrade import CmisTargetFWUpgradeMemMap |
| 7 | +from sonic_platform_base.sonic_xcvr.xcvr_eeprom import XcvrEeprom |
| 8 | + |
| 9 | +class TestCmis(object): |
| 10 | + codes = CmisTargetFWUpgradeCodes |
| 11 | + mem_map = CmisTargetFWUpgradeMemMap(codes) |
| 12 | + reader = MagicMock(return_value=None) |
| 13 | + writer = MagicMock() |
| 14 | + eeprom = XcvrEeprom(reader, writer, mem_map) |
| 15 | + api = CmisTargetFWUpgradeAPI(eeprom) |
| 16 | + |
| 17 | + @pytest.mark.parametrize("set_firmware_result, module_type, exception_raised", [ |
| 18 | + (False, 'QSFP+ or later with CMIS', False), |
| 19 | + (True, 'Unknown', False), |
| 20 | + (True, 'QSFP+ or later with CMIS', True) |
| 21 | + ]) |
| 22 | + @patch('sonic_platform_base.sonic_xcvr.api.public.cmis.CmisApi.get_transceiver_info_firmware_versions', MagicMock(side_effect=({}, Exception('error'), {}))) |
| 23 | + @patch('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._get_server_firmware_version', MagicMock()) |
| 24 | + @patch('traceback.format_exception') |
| 25 | + def test_get_transceiver_info_firmware_versions_failure(self, mock_format_exception, set_firmware_result, module_type, exception_raised): |
| 26 | + expected_output = {'active_firmware': 'N/A', 'inactive_firmware': 'N/A', 'e1_active_firmware': 'N/A',\ |
| 27 | + 'e1_inactive_firmware': 'N/A', 'e2_active_firmware': 'N/A', 'e2_inactive_firmware': 'N/A',\ |
| 28 | + 'e1_server_firmware': 'N/A', 'e2_server_firmware': 'N/A'} |
| 29 | + self.api.set_firmware_download_target_end = MagicMock(return_value=set_firmware_result) |
| 30 | + self.api.get_module_type = MagicMock(return_value=module_type) |
| 31 | + |
| 32 | + result = self.api.get_transceiver_info_firmware_versions() |
| 33 | + assert result == expected_output |
| 34 | + |
| 35 | + assert self.api.set_firmware_download_target_end.call_count == len(TARGET_LIST) + 1 |
| 36 | + # Ensure that FW version is read for all targets |
| 37 | + for index, call in enumerate(self.api.set_firmware_download_target_end.call_args_list): |
| 38 | + args, _ = call |
| 39 | + # Ensure target is restore to E0 after reading FW version from all targets |
| 40 | + if index == len(TARGET_LIST): |
| 41 | + assert args[0] == TARGET_E0_VALUE |
| 42 | + else: |
| 43 | + assert args[0] == TARGET_LIST[index] |
| 44 | + |
| 45 | + if exception_raised: |
| 46 | + assert mock_format_exception.call_count == 1 |
| 47 | + assert self.api._get_server_firmware_version.call_count == 1 |
| 48 | + else: |
| 49 | + self.api._get_server_firmware_version.assert_not_called() |
| 50 | + |
| 51 | + @pytest.mark.parametrize("fw_info_dict, server_fw_info_dict, expected_output", [ |
| 52 | + (({'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0'}, {'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0'}, {'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0'}), ({'server_firmware': '1.5.0.1421'}, {'server_firmware': '1.5.0.1421'}),\ |
| 53 | + {'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0', 'e1_active_firmware': '1.1.1', 'e1_inactive_firmware': '1.0.0', 'e2_active_firmware': '1.1.1', 'e2_inactive_firmware': '1.0.0', 'e1_server_firmware': '1.5.0.1421', 'e2_server_firmware': '1.5.0.1421'}), |
| 54 | + (({'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0'}, {'active_firmware': '2.1.1', 'inactive_firmware': '1.0.0'}, {'active_firmware': '1.1.1', 'inactive_firmware': '2.0.1'}), ({'server_firmware': '1223.6.0.739'}, {'server_firmware': '93.5.0.3431'}),\ |
| 55 | + {'active_firmware': '1.1.1', 'inactive_firmware': '1.0.0', 'e1_active_firmware': '2.1.1', 'e1_inactive_firmware': '1.0.0', 'e2_active_firmware': '1.1.1', 'e2_inactive_firmware': '2.0.1', 'e1_server_firmware': '1223.6.0.739', 'e2_server_firmware': '93.5.0.3431'}) |
| 56 | + ]) |
| 57 | + def test_get_transceiver_info_firmware_versions_success(self, fw_info_dict, server_fw_info_dict, expected_output): |
| 58 | + with patch('sonic_platform_base.sonic_xcvr.api.public.cmis.CmisApi.get_transceiver_info_firmware_versions', side_effect=fw_info_dict): |
| 59 | + with patch('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._get_server_firmware_version', side_effect=server_fw_info_dict): |
| 60 | + self.api.set_firmware_download_target_end = MagicMock(return_value=True) |
| 61 | + self.api.get_module_type = MagicMock(return_value='QSFP+ or later with CMIS') |
| 62 | + |
| 63 | + result = self.api.get_transceiver_info_firmware_versions() |
| 64 | + assert result == expected_output |
| 65 | + assert self.api.set_firmware_download_target_end.call_count == len(TARGET_LIST) + 1 |
| 66 | + |
| 67 | + @pytest.mark.parametrize("magic_byte, checksum, server_fw_version_byte_array, expected", [ |
| 68 | + (0, 0, (), {'server_firmware': 'N/A'}), |
| 69 | + (0, 0x98, [0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 0x8d], {'server_firmware': 'N/A'}), # Magic byte is 0 but other values are valid |
| 70 | + (0xAC, 0x98, ([0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 0x8d], "1.5.0.1421"), {'server_firmware': '1.5.0.1421'}), |
| 71 | + (0xff, 0xff, ([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], "N/A"), {'server_firmware': 'N/A'}), |
| 72 | + (0xAC, 0x98, ([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff], "N/A"), {'server_firmware': 'N/A'}) |
| 73 | + ]) |
| 74 | + def test_get_server_firmware_version(self, magic_byte, checksum, server_fw_version_byte_array, expected): |
| 75 | + self.api.xcvr_eeprom.read = MagicMock() |
| 76 | + self.api.xcvr_eeprom.read.side_effect = [magic_byte, checksum, server_fw_version_byte_array] |
| 77 | + |
| 78 | + result = self.api._get_server_firmware_version() |
| 79 | + assert result == expected |
0 commit comments