Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodong-Yang committed Jun 19, 2024
1 parent 3a65ef1 commit 51c4895
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/otaclient/app/boot_control/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/boot_control/_grub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/boot_control/_jetson_cboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/boot_control/_rpi_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/otaclient/app/ota_client_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 15 additions & 17 deletions tests/test_otaclient/test_boot_control/test_grub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ------ #
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
33 changes: 17 additions & 16 deletions tests/test_otaclient/test_boot_control/test_rpi_boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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 ------ #
Expand Down

0 comments on commit 51c4895

Please sign in to comment.