@@ -14,20 +14,42 @@ class TestCmis(object):
1414 eeprom = XcvrEeprom (reader , writer , mem_map )
1515 api = CmisTargetFWUpgradeAPI (eeprom )
1616
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 )
17+ @pytest .mark .parametrize ("target,write_results,accessible,exception,expected_result" , [
18+ (1 , [False , True , True , True ], True , None , False ), # Failed to set target mode
19+ (1 , [True , False , True , True ], True , None , False ), # Failed to set page select byte
20+ (1 , [True , True , True , True ], False , None , False ), # Remote target not accessible
21+ (1 , [True , True , True , True ], True , Exception ("Simulated exception" ), False ), # Exception occurred
22+ (1 , [True , True , True , True ], True , None , True ), # All operations successful
23+ (0 , [True , True , True , True ], True , None , True ), # Target is E0, all operations successful
24+ ])
25+ def test_set_firmware_download_target_end (self , target , write_results , accessible , exception , expected_result ):
26+ self .api .xcvr_eeprom .write = MagicMock ()
27+ self .api .xcvr_eeprom .write .side_effect = write_results
28+ with patch ('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._is_remote_target_accessible' , return_value = accessible ):
29+ with patch ('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._restore_target_to_E0' , return_value = False ):
30+ if exception is not None :
31+ self .api .xcvr_eeprom .write .side_effect = exception
32+
33+ result = self .api .set_firmware_download_target_end (target )
34+ assert result == expected_result
35+ if result :
36+ expected_call_count = 0
37+ else :
38+ expected_call_count = 1
39+ assert self .api ._restore_target_to_E0 .call_count == expected_call_count
40+
41+ @pytest .mark .parametrize ("set_firmware_result, exception_raised" , [
42+ (False , False ),
43+ (True , True )
2144 ])
2245 @patch ('sonic_platform_base.sonic_xcvr.api.public.cmis.CmisApi.get_transceiver_info_firmware_versions' , MagicMock (side_effect = ({}, Exception ('error' ), {})))
2346 @patch ('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._get_server_firmware_version' , MagicMock ())
2447 @patch ('traceback.format_exception' )
25- def test_get_transceiver_info_firmware_versions_failure (self , mock_format_exception , set_firmware_result , module_type , exception_raised ):
48+ def test_get_transceiver_info_firmware_versions_failure (self , mock_format_exception , set_firmware_result , exception_raised ):
2649 expected_output = {'active_firmware' : 'N/A' , 'inactive_firmware' : 'N/A' , 'e1_active_firmware' : 'N/A' ,\
2750 'e1_inactive_firmware' : 'N/A' , 'e2_active_firmware' : 'N/A' , 'e2_inactive_firmware' : 'N/A' ,\
2851 'e1_server_firmware' : 'N/A' , 'e2_server_firmware' : 'N/A' }
2952 self .api .set_firmware_download_target_end = MagicMock (return_value = set_firmware_result )
30- self .api .get_module_type = MagicMock (return_value = module_type )
3153
3254 result = self .api .get_transceiver_info_firmware_versions ()
3355 assert result == expected_output
@@ -58,12 +80,29 @@ def test_get_transceiver_info_firmware_versions_success(self, fw_info_dict, serv
5880 with patch ('sonic_platform_base.sonic_xcvr.api.public.cmis.CmisApi.get_transceiver_info_firmware_versions' , side_effect = fw_info_dict ):
5981 with patch ('sonic_platform_base.sonic_xcvr.api.public.cmisTargetFWUpgrade.CmisTargetFWUpgradeAPI._get_server_firmware_version' , side_effect = server_fw_info_dict ):
6082 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' )
6283
6384 result = self .api .get_transceiver_info_firmware_versions ()
6485 assert result == expected_output
6586 assert self .api .set_firmware_download_target_end .call_count == len (TARGET_LIST ) + 1
6687
88+ @pytest .mark .parametrize ("module_type, expected_result" , [
89+ ('Unknown' , False ),
90+ ('QSFP+ or later with CMIS' , True )
91+ ])
92+ def test_is_remote_target_accessible (self , module_type , expected_result ):
93+ # Mock the get_module_type method to return the parameterized module_type
94+ self .api .get_module_type = MagicMock (return_value = module_type )
95+
96+ # Call the method and check the result
97+ result = self .api ._is_remote_target_accessible ()
98+ assert result == expected_result
99+
100+ def test_restore_target_to_E0 (self ):
101+ self .api .xcvr_eeprom .write = MagicMock ()
102+ assert self .api ._restore_target_to_E0 () == False
103+ self .api .xcvr_eeprom .write .assert_called_once ()
104+
105+
67106 @pytest .mark .parametrize ("magic_byte, checksum, server_fw_version_byte_array, expected" , [
68107 (0 , 0 , (), {'server_firmware' : 'N/A' }),
69108 (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
0 commit comments