From c51e6bf0a1dbefae03dddc8f1404647d71c4b823 Mon Sep 17 00:00:00 2001 From: Yona Cohen Date: Thu, 23 Feb 2023 08:52:18 +0200 Subject: [PATCH 1/3] Added try catch for verify_image_sign Added try-catch wrapper for verify_image_sign to fix method not implemented in non-grub bootloaders. --- sonic_installer/main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sonic_installer/main.py b/sonic_installer/main.py index d78259317e..fef32d9ef5 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -579,11 +579,16 @@ def install(url, force, skip_platform_check=False, skip_migration=False, skip_pa # Calling verification script by default - signature will be checked if enabled in bios echo_and_log("Verifing image {} signature...".format(binary_image_version)) - if not bootloader.verify_image_sign(image_path): - echo_and_log('Error: Failed verify image signature', LOG_ERR) - raise click.Abort() - else: - echo_and_log('Verification successful') + try: + if not bootloader.verify_image_sign(image_path): + echo_and_log('Error: Failed verify image signature', LOG_ERR) + raise click.Abort() + else: + echo_and_log('Verification successful') + except AttributeError: + echo_and_log("Skip Verifing image {} signature,".format(binary_image_version) + + "method not implemented for the current bootloader type: {}".format(bootloader.__class__.__name__)) + pass echo_and_log("Installing image {} and setting it as default...".format(binary_image_version)) with SWAPAllocator(not skip_setup_swap, swap_mem_size, total_mem_threshold, available_mem_threshold): From 21693d5122499ce74b92b2c54ec795e71897abd9 Mon Sep 17 00:00:00 2001 From: Yona Cohen Date: Thu, 23 Feb 2023 10:44:34 +0200 Subject: [PATCH 2/3] added sonic installer test --- tests/test_sonic_installer.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_sonic_installer.py b/tests/test_sonic_installer.py index 0f8fcdb8ca..3604fa16f7 100644 --- a/tests/test_sonic_installer.py +++ b/tests/test_sonic_installer.py @@ -53,6 +53,13 @@ def rootfs_path_mock(path): print(result.output) assert result.exit_code != 0 + mock_bootloader_verify_image_sign_not_implemented = mock_bootloader + mock_bootloader_verify_image_sign_not_implemented.verify_image_sign = Mock(side_effect=AttributeError) + get_bootloader.return_value=mock_bootloader_verify_image_sign_not_implemented + result = runner.invoke(sonic_installer.commands["install"], [sonic_image_filename, "-y"]) + print(result.output) + + assert result.exit_code == 0 # Assert bootloader install API was called mock_bootloader.install_image.assert_called_with(f"./{sonic_image_filename}") # Assert all below commands were called, so we ensure that From ed0078e46fdcb433798b5c75838e3a2eac28b2aa Mon Sep 17 00:00:00 2001 From: Yona Cohen Date: Thu, 23 Feb 2023 14:06:03 +0200 Subject: [PATCH 3/3] test fix --- sonic_installer/main.py | 6 +++--- tests/test_sonic_installer.py | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sonic_installer/main.py b/sonic_installer/main.py index fef32d9ef5..54f465aee2 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -586,9 +586,9 @@ def install(url, force, skip_platform_check=False, skip_migration=False, skip_pa else: echo_and_log('Verification successful') except AttributeError: - echo_and_log("Skip Verifing image {} signature,".format(binary_image_version) + - "method not implemented for the current bootloader type: {}".format(bootloader.__class__.__name__)) - pass + echo_and_log("Skip Verifing image {} signature,".format(binary_image_version) + + " method not implemented for the current bootloader type: {}".format(bootloader.__class__.__name__)) + pass echo_and_log("Installing image {} and setting it as default...".format(binary_image_version)) with SWAPAllocator(not skip_setup_swap, swap_mem_size, total_mem_threshold, available_mem_threshold): diff --git a/tests/test_sonic_installer.py b/tests/test_sonic_installer.py index 3604fa16f7..a8b862b878 100644 --- a/tests/test_sonic_installer.py +++ b/tests/test_sonic_installer.py @@ -59,7 +59,7 @@ def rootfs_path_mock(path): result = runner.invoke(sonic_installer.commands["install"], [sonic_image_filename, "-y"]) print(result.output) - assert result.exit_code == 0 + assert result.exit_code != 0 # Assert bootloader install API was called mock_bootloader.install_image.assert_called_with(f"./{sonic_image_filename}") # Assert all below commands were called, so we ensure that @@ -96,6 +96,11 @@ def rootfs_path_mock(path): call(["umount", "-f", "-R", mounted_image_folder], raise_exception=False), call(["umount", "-r", "-f", mounted_image_folder], raise_exception=False), call(["rm", "-rf", mounted_image_folder], raise_exception=False), + call(['mkdir', '-p', mounted_image_folder]), + call(["mount", "-t", "squashfs", mounted_image_folder, mounted_image_folder]), + call(["sonic-cfggen", "-d", "-y", f"{mounted_image_folder}/etc/sonic/sonic_version.yml", "-t", f"{mounted_image_folder}/usr/share/sonic/templates/sonic-environment.j2"]), + call(["umount", "-r", "-f", mounted_image_folder], raise_exception=True), + call(["rm", "-rf", mounted_image_folder], raise_exception=True), ] assert run_command_or_raise.call_args_list == expected_call_list