From cf4c6af787a9de5dca830cb6211ea148d1f980e8 Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Sun, 30 Oct 2022 16:19:02 +0800 Subject: [PATCH] CmisApi::get_application_advertisement catch AttributeError as well (#316) - Description Catch both TypeError and AttributeError in CmisApi::get_application_advertisement because an AttributeError will be thrown when updating a dict with None. - Motivation and Context Fix issue found during automation tests - How Has This Been Tested? Manually test Added new unit test Signed-off-by: Stephen Sun --- sonic_platform_base/sonic_xcvr/api/public/cmis.py | 2 +- tests/sonic_xcvr/test_cmis.py | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/sonic_platform_base/sonic_xcvr/api/public/cmis.py b/sonic_platform_base/sonic_xcvr/api/public/cmis.py index 4bfcac7b4f4d..0190802fb8a4 100644 --- a/sonic_platform_base/sonic_xcvr/api/public/cmis.py +++ b/sonic_platform_base/sonic_xcvr/api/public/cmis.py @@ -1889,7 +1889,7 @@ def get_application_advertisement(self): # Read the application advertisement in page01 try: dic.update(self.xcvr_eeprom.read(consts.APPLS_ADVT_FIELD_PAGE01)) - except TypeError as e: + except (TypeError, AttributeError) as e: logger.error('Failed to read APPLS_ADVT_FIELD_PAGE01: ' + str(e)) return ret diff --git a/tests/sonic_xcvr/test_cmis.py b/tests/sonic_xcvr/test_cmis.py index e78e91c9e128..3993eb608d20 100644 --- a/tests/sonic_xcvr/test_cmis.py +++ b/tests/sonic_xcvr/test_cmis.py @@ -2019,6 +2019,12 @@ def test_get_application_advertisement(self): assert result[1]['media_lane_count'] == 4 assert result[1]['host_lane_assignment_options'] == 0x01 + def test_get_application_advertisement_non_support(self): + self.api.xcvr_eeprom.read = MagicMock(return_value = None) + self.api.is_flat_memory = MagicMock(return_value = False) + result = self.api.get_application_advertisement() + assert result == {} + def test_get_application(self): self.api.xcvr_eeprom.read = MagicMock() self.api.xcvr_eeprom.read.return_value = 0x20