From 51c48958e3f2a3566d80e0679781d5910e78feb3 Mon Sep 17 00:00:00 2001 From: "bodong.yang" Date: Wed, 19 Jun 2024 15:00:40 +0000 Subject: [PATCH] fixes --- src/otaclient/app/boot_control/_common.py | 2 +- src/otaclient/app/boot_control/_grub.py | 2 +- .../app/boot_control/_jetson_cboot.py | 2 +- src/otaclient/app/boot_control/_rpi_boot.py | 2 +- src/otaclient/app/ota_client_stub.py | 2 +- .../test_boot_control/test_grub.py | 32 +++++++++--------- .../test_boot_control/test_rpi_boot.py | 33 ++++++++++--------- 7 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/otaclient/app/boot_control/_common.py b/src/otaclient/app/boot_control/_common.py index 468f338df..691f6aafc 100644 --- a/src/otaclient/app/boot_control/_common.py +++ b/src/otaclient/app/boot_control/_common.py @@ -22,9 +22,9 @@ from pathlib import Path from typing import Callable, Literal, Optional, Union -from otaclient_common import cmdhelper from otaclient.app.configs import config as cfg from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from otaclient_common.common import read_str_from_file, write_str_to_file_sync logger = logging.getLogger(__name__) diff --git a/src/otaclient/app/boot_control/_grub.py b/src/otaclient/app/boot_control/_grub.py index 76ebf8649..959bad25b 100644 --- a/src/otaclient/app/boot_control/_grub.py +++ b/src/otaclient/app/boot_control/_grub.py @@ -42,7 +42,6 @@ from subprocess import CalledProcessError from typing import ClassVar, Dict, Generator, List, Optional, Tuple -from otaclient_common import cmdhelper from otaclient.app import errors as ota_errors from otaclient.app.boot_control._common import ( OTAStatusFilesControl, @@ -52,6 +51,7 @@ from otaclient.app.boot_control.configs import grub_cfg as cfg from otaclient.app.boot_control.protocol import BootControllerProtocol from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from otaclient_common.common import ( re_symlink_atomic, read_str_from_file, diff --git a/src/otaclient/app/boot_control/_jetson_cboot.py b/src/otaclient/app/boot_control/_jetson_cboot.py index 1e1012ecc..a5a19b1de 100644 --- a/src/otaclient/app/boot_control/_jetson_cboot.py +++ b/src/otaclient/app/boot_control/_jetson_cboot.py @@ -25,7 +25,6 @@ from pathlib import Path from typing import Generator, Optional -from otaclient_common import cmdhelper from otaclient.app import errors as ota_errors from otaclient.app.boot_control._common import OTAStatusFilesControl, SlotMountHelper from otaclient.app.boot_control._jetson_common import ( @@ -42,6 +41,7 @@ from otaclient.app.boot_control.protocol import BootControllerProtocol from otaclient.app.configs import config as cfg from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from otaclient_common.common import subprocess_run_wrapper logger = logging.getLogger(__name__) diff --git a/src/otaclient/app/boot_control/_rpi_boot.py b/src/otaclient/app/boot_control/_rpi_boot.py index 79eb26ff2..258dd37ed 100644 --- a/src/otaclient/app/boot_control/_rpi_boot.py +++ b/src/otaclient/app/boot_control/_rpi_boot.py @@ -27,7 +27,6 @@ from typing_extensions import Self import otaclient.app.errors as ota_errors -from otaclient_common import cmdhelper from otaclient.app.boot_control._common import ( OTAStatusFilesControl, SlotMountHelper, @@ -36,6 +35,7 @@ from otaclient.app.boot_control.configs import rpi_boot_cfg as cfg from otaclient.app.boot_control.protocol import BootControllerProtocol from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from otaclient_common.common import replace_atomic from otaclient_common.linux import subprocess_run_wrapper from otaclient_common.typing import StrOrPath diff --git a/src/otaclient/app/ota_client_stub.py b/src/otaclient/app/ota_client_stub.py index b4b828a55..d627ad77a 100644 --- a/src/otaclient/app/ota_client_stub.py +++ b/src/otaclient/app/ota_client_stub.py @@ -35,9 +35,9 @@ from otaclient.configs.ecu_info import ECUContact from otaclient_api.v2 import types as api_types from otaclient_api.v2.api_caller import ECUNoResponse, OTAClientCall +from otaclient_common import cmdhelper from otaclient_common.common import ensure_otaproxy_start -from ...otaclient_common import cmdhelper from .configs import config as cfg from .configs import ecu_info, proxy_info, server_cfg from .ota_client import OTAClientControlFlags, OTAServicer diff --git a/tests/test_otaclient/test_boot_control/test_grub.py b/tests/test_otaclient/test_boot_control/test_grub.py index 8a3cd1401..ff347b9b7 100644 --- a/tests/test_otaclient/test_boot_control/test_grub.py +++ b/tests/test_otaclient/test_boot_control/test_grub.py @@ -23,6 +23,7 @@ import pytest_mock from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from tests.conftest import TestConfiguration as cfg from tests.utils import SlotMeta @@ -219,7 +220,7 @@ def mock_setup( mocker: pytest_mock.MockerFixture, grub_ab_slot, ): - from otaclient.app.boot_control._common import CMDHelperFuncs, SlotMountHelper + from otaclient.app.boot_control._common import SlotMountHelper from otaclient.app.boot_control._grub import GrubABPartitionDetector # ------ start fsm ------ # @@ -266,16 +267,15 @@ def mock_setup( # bind to test instance self._grub_reboot_mock = _grub_reboot_mock - ###### mocking CMDHelperFuncs ###### - _CMDHelper_mock = typing.cast( - CMDHelperFuncs, mocker.MagicMock(spec=CMDHelperFuncs) - ) - _CMDHelper_mock.reboot.side_effect = self._fsm.switch_boot - _CMDHelper_mock.get_attrs_by_dev = mocker.MagicMock( + ###### mocking cmdhelper ###### + _cmdhelper_mock = typing.cast(cmdhelper, mocker.MagicMock(spec=cmdhelper)) + + _cmdhelper_mock.reboot.side_effect = self._fsm.switch_boot + _cmdhelper_mock.get_attrs_by_dev = mocker.MagicMock( wraps=self._fsm.get_attrs_by_dev ) # bind the mocker to the test instance - self._CMDHelper_mock = _CMDHelper_mock + self._cmdhelper_mock = _cmdhelper_mock ###### mock GrubHelper ###### _grub_mkconfig_path = f"{cfg.GRUB_MODULE_PATH}.GrubHelper.grub_mkconfig" @@ -285,14 +285,12 @@ def mock_setup( ) ###### patching ###### - # patch CMDHelper - # NOTE: also remember to patch CMDHelperFuncs in common - _CMDHelper_at_common_path = ( - f"{cfg.BOOT_CONTROL_COMMON_MODULE_PATH}.CMDHelperFuncs" - ) - _CMDHelper_at_grub_path = f"{cfg.GRUB_MODULE_PATH}.CMDHelperFuncs" - mocker.patch(_CMDHelper_at_common_path, _CMDHelper_mock) - mocker.patch(_CMDHelper_at_grub_path, _CMDHelper_mock) + # patch cmdhelper + # NOTE: also remember to patch cmdhelper in common + _cmdhelper_at_common_path = f"{cfg.BOOT_CONTROL_COMMON_MODULE_PATH}.cmdhelper" + _cmdhelper_at_grub_path = f"{cfg.GRUB_MODULE_PATH}.cmdhelper" + mocker.patch(_cmdhelper_at_common_path, _cmdhelper_mock) + mocker.patch(_cmdhelper_at_grub_path, _cmdhelper_mock) # patch _GrubABPartitionDetector _GrubABPartitionDetector_path = ( f"{cfg.GRUB_MODULE_PATH}.GrubABPartitionDetector" @@ -366,7 +364,7 @@ def test_grub_normal_update(self, mocker: pytest_mock.MockerFixture): ).read_text().strip() == GrubMkConfigFSM.GRUB_CFG_SLOT_A_UPDATED.strip() # NOTE: check grub.cfg_slot_a_post_update, the target entry is 0 self._grub_reboot_mock.assert_called_once_with(0) - self._CMDHelper_mock.reboot.assert_called_once() + self._cmdhelper_mock.reboot.assert_called_once() ###### stage 2 ###### # test init after first reboot diff --git a/tests/test_otaclient/test_boot_control/test_rpi_boot.py b/tests/test_otaclient/test_boot_control/test_rpi_boot.py index b8e99d9dc..26161f24a 100644 --- a/tests/test_otaclient/test_boot_control/test_rpi_boot.py +++ b/tests/test_otaclient/test_boot_control/test_rpi_boot.py @@ -9,9 +9,10 @@ import pytest_mock from otaclient.app.boot_control import _rpi_boot -from otaclient.app.boot_control._common import CMDHelperFuncs, SlotMountHelper +from otaclient.app.boot_control._common import SlotMountHelper from otaclient.app.boot_control.configs import rpi_boot_cfg from otaclient_api.v2 import types as api_types +from otaclient_common import cmdhelper from tests.conftest import TestConfiguration as cfg from tests.utils import SlotMeta @@ -33,9 +34,9 @@ # module path RPI_BOOT_MODULE_PATH = "otaclient.app.boot_control._rpi_boot" rpi_boot__RPIBootControl_MODULE = f"{RPI_BOOT_MODULE_PATH}._RPIBootControl" -rpi_boot_RPIBoot_CMDHelperFuncs_MODULE = f"{RPI_BOOT_MODULE_PATH}.CMDHelperFuncs" -boot_control_common_CMDHelperFuncs_MODULE = ( - f"{cfg.BOOT_CONTROL_COMMON_MODULE_PATH}.CMDHelperFuncs" +rpi_boot_RPIBoot_cmdhelper_MODULE = f"{RPI_BOOT_MODULE_PATH}.cmdhelper" +boot_control_common_cmdhelper_MODULE = ( + f"{cfg.BOOT_CONTROL_COMMON_MODULE_PATH}.cmdhelper" ) # image version @@ -157,24 +158,24 @@ def mock_setup(self, mocker: pytest_mock.MockerFixture, rpi_boot_ab_slot): # start the test FSM self.fsm = fsm = RPIBootABPartitionFSM() - # ------ patch CMDHelperFuncs ------ # - # NOTE: also remember to patch CMDHelperFuncs in common - self.CMDHelper_mock = CMDHelper_mock = typing.cast( - CMDHelperFuncs, mocker.MagicMock(spec=CMDHelperFuncs) + # ------ patch cmdhelper ------ # + # NOTE: also remember to patch cmdhelper in common + self.cmdhelper_mock = cmdhelper_mock = typing.cast( + cmdhelper, mocker.MagicMock(spec=cmdhelper) ) # NOTE: this is for system-boot mount check in _RPIBootControl; - CMDHelper_mock.is_target_mounted = mocker.Mock(return_value=True) - CMDHelper_mock.get_current_rootfs_dev = mocker.Mock( + cmdhelper_mock.is_target_mounted = mocker.Mock(return_value=True) + cmdhelper_mock.get_current_rootfs_dev = mocker.Mock( wraps=fsm.get_current_rootfs_dev ) - CMDHelper_mock.get_parent_dev = mocker.Mock(wraps=fsm.get_parent_dev) - CMDHelper_mock.get_device_tree = mocker.Mock(wraps=fsm.get_device_tree) - CMDHelper_mock.get_attrs_by_dev = mocker.Mock(wraps=fsm.get_attrs_by_dev) + cmdhelper_mock.get_parent_dev = mocker.Mock(wraps=fsm.get_parent_dev) + cmdhelper_mock.get_device_tree = mocker.Mock(wraps=fsm.get_device_tree) + cmdhelper_mock.get_attrs_by_dev = mocker.Mock(wraps=fsm.get_attrs_by_dev) - mocker.patch(rpi_boot_RPIBoot_CMDHelperFuncs_MODULE, self.CMDHelper_mock) + mocker.patch(rpi_boot_RPIBoot_cmdhelper_MODULE, self.cmdhelper_mock) mocker.patch( - boot_control_common_CMDHelperFuncs_MODULE, - self.CMDHelper_mock, + boot_control_common_cmdhelper_MODULE, + self.cmdhelper_mock, ) # ------ patch _RPIBootControl ------ #