From ba28f10b25959166e71028dfd995693d82b4991f Mon Sep 17 00:00:00 2001 From: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> Date: Fri, 14 Jan 2022 22:42:53 +0800 Subject: [PATCH] Fix build issue: cannot import name FW_AUTO_ERR_UKNOWN- required module not found (#9764) --- .../sonic_platform/component.py | 59 ++++++++++--------- .../mlnx-platform-api/tests/test_firmware.py | 15 +++-- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/component.py b/platform/mellanox/mlnx-platform-api/sonic_platform/component.py index 4a252db72d72..d32bb3ecfcd5 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/component.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/component.py @@ -37,8 +37,13 @@ from sonic_platform_base.component_base import ComponentBase, \ FW_AUTO_INSTALLED, \ FW_AUTO_ERR_BOOT_TYPE, \ - FW_AUTO_ERR_IMAGE, \ - FW_AUTO_ERR_UKNOWN + FW_AUTO_ERR_IMAGE + + # Temp workaround to fix build issue, shall be refactor once sonic-platform-common submodule pointer is updated + try: + from sonic_platform_base.component_base import FW_AUTO_ERR_UNKNOWN + except ImportError as e: + from sonic_platform_base.component_base import FW_AUTO_ERR_UKNOWN as FW_AUTO_ERR_UNKNOWN except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -138,9 +143,9 @@ def __mount_onie_fs(self): self.__umount_onie_fs() cmd = "fdisk -l | grep 'ONIE boot' | awk '{print $1}'" - fs_path = subprocess.check_output(cmd, - stderr=subprocess.STDOUT, - shell=True, + fs_path = subprocess.check_output(cmd, + stderr=subprocess.STDOUT, + shell=True, universal_newlines=True).rstrip('\n') os.mkdir(fs_mountpoint) @@ -194,8 +199,8 @@ def __is_update_staged(self, image_path): cmd = self.ONIE_FW_UPDATE_CMD_SHOW_PENDING try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get pending firmware updates: {}".format(str(e))) @@ -281,8 +286,8 @@ def get_onie_firmware_info(self, image_path): cmd = self.ONIE_IMAGE_INFO_COMMAND.format(image_path) try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get ONIE firmware info: {}".format(str(e))) @@ -303,8 +308,8 @@ def update_firmware(self, image_path): cmd = self.ONIE_FW_UPDATE_CMD_SHOW_PENDING try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get pending firmware updates: {}".format(str(e))) @@ -371,7 +376,7 @@ def auto_update_firmware(self, image_path, boot_action): # Successful update return FW_AUTO_INSTALLED # Failed update (unknown reason) - return FW_AUTO_ERR_UKNOWN + return FW_AUTO_ERR_UNKNOWN # boot_type did not match (skip) return FW_AUTO_ERR_BOOT_TYPE @@ -395,10 +400,10 @@ def _read_generic_file(filename, len, ignore_errors=False): @staticmethod def _get_command_result(cmdline): try: - proc = subprocess.Popen(cmdline, - stdout=subprocess.PIPE, - shell=True, - stderr=subprocess.STDOUT, + proc = subprocess.Popen(cmdline, + stdout=subprocess.PIPE, + shell=True, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] rc = proc.wait() @@ -510,7 +515,7 @@ def __install_firmware(self, image_path): def auto_update_firmware(self, image_path, boot_action): """ Handling of attempted automatic update for a SSD of a Mellanox switch. - Will first check the image_path to determine if a post-install reboot is required, + Will first check the image_path to determine if a post-install reboot is required, then compares it against boot_action to determine whether to proceed with install. """ @@ -529,14 +534,14 @@ def auto_update_firmware(self, image_path, boot_action): supported_boot += ['warm', 'fast', 'none', 'any'] except RuntimeError: # Unknown error from firmware probe - return FW_AUTO_ERR_UKNOWN + return FW_AUTO_ERR_UNKNOWN if boot_action in supported_boot: if self.update_firmware(image_path): # Successful update return FW_AUTO_INSTALLED # Failed update (unknown reason) - return FW_AUTO_ERR_UKNOWN + return FW_AUTO_ERR_UNKNOWN # boot_type did not match (skip) return FW_AUTO_ERR_BOOT_TYPE @@ -545,8 +550,8 @@ def get_firmware_version(self): cmd = self.SSD_INFO_COMMAND try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get {} info: {}".format(self.name, str(e))) @@ -561,8 +566,8 @@ def get_available_firmware_version(self, image_path): cmd = self.SSD_FIRMWARE_INFO_COMMAND.format(image_path) try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get {} firmware info: {}".format(self.name, str(e))) @@ -596,8 +601,8 @@ def get_firmware_update_notification(self, image_path): cmd = self.SSD_FIRMWARE_INFO_COMMAND.format(image_path) try: - output = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + output = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get {} firmware info: {}".format(self.name, str(e))) @@ -670,8 +675,8 @@ def get_firmware_version(self): cmd = self.BIOS_VERSION_COMMAND try: - version = subprocess.check_output(cmd.split(), - stderr=subprocess.STDOUT, + version = subprocess.check_output(cmd.split(), + stderr=subprocess.STDOUT, universal_newlines=True).rstrip('\n') except subprocess.CalledProcessError as e: raise RuntimeError("Failed to get {} version: {}".format(self.name, str(e))) diff --git a/platform/mellanox/mlnx-platform-api/tests/test_firmware.py b/platform/mellanox/mlnx-platform-api/tests/test_firmware.py index 6e24bdf07d24..3bc2d0510619 100644 --- a/platform/mellanox/mlnx-platform-api/tests/test_firmware.py +++ b/platform/mellanox/mlnx-platform-api/tests/test_firmware.py @@ -29,8 +29,13 @@ from sonic_platform_base.component_base import ComponentBase, \ FW_AUTO_INSTALLED, \ FW_AUTO_ERR_BOOT_TYPE, \ - FW_AUTO_ERR_IMAGE, \ - FW_AUTO_ERR_UKNOWN + FW_AUTO_ERR_IMAGE +# Temp workaround to fix build issue, shall be refactor once sonic-platform-common submodule pointer is updated +try: + from sonic_platform_base.component_base import FW_AUTO_ERR_UNKNOWN +except ImportError as e: + from sonic_platform_base.component_base import FW_AUTO_ERR_UKNOWN as FW_AUTO_ERR_UNKNOWN + def mock_update_firmware_success(image_path): return True @@ -50,14 +55,14 @@ def mock_update_notification_error(image_path): test_data_default = [ (None, False, None, FW_AUTO_ERR_IMAGE), (None, True, 'warm', FW_AUTO_ERR_BOOT_TYPE), - (mock_update_firmware_fail, True, 'cold', FW_AUTO_ERR_UKNOWN), + (mock_update_firmware_fail, True, 'cold', FW_AUTO_ERR_UNKNOWN), (mock_update_firmware_success, True, 'cold', FW_AUTO_INSTALLED) ] test_data_ssd = [ (None, None, False, None, FW_AUTO_ERR_IMAGE), - (None, mock_update_notification_error, True, None, FW_AUTO_ERR_UKNOWN), - (mock_update_firmware_fail, mock_update_notification_cold_boot, True, 'cold', FW_AUTO_ERR_UKNOWN), + (None, mock_update_notification_error, True, None, FW_AUTO_ERR_UNKNOWN), + (mock_update_firmware_fail, mock_update_notification_cold_boot, True, 'cold', FW_AUTO_ERR_UNKNOWN), (mock_update_firmware_success, mock_update_notification_cold_boot, True, 'warm', FW_AUTO_ERR_BOOT_TYPE), (mock_update_firmware_success, mock_update_notification_cold_boot, True, 'cold', FW_AUTO_INSTALLED), (mock_update_firmware_success, mock_update_notification_warm_boot, True, 'warm', FW_AUTO_INSTALLED),