From c6e10ab462f870fcd8bb36ec190ded0761f77c34 Mon Sep 17 00:00:00 2001 From: Adrian Vladu Date: Thu, 13 Jun 2024 15:36:13 +0300 Subject: [PATCH] bootconfig: fix BCDStore SetBooleanElement order SetBooleanElement seems to not respect the method contract defined in the document: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/bcd/setbooleanelement-bcdobject#syntax Instead the method contract has the two parameters in the inverse order: ``` boolean SetBooleanElement( [in] boolean Boolean, [in] uint32 Type ); ``` Change-Id: I8938cd0333b21a89971de88da47559fa929162dc Signed-off-by: Adrian Vladu --- .../tests/utils/windows/test_bootconfig.py | 11 ++++++++--- cloudbaseinit/utils/windows/bootconfig.py | 16 +++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cloudbaseinit/tests/utils/windows/test_bootconfig.py b/cloudbaseinit/tests/utils/windows/test_bootconfig.py index 02db3968..12db56eb 100644 --- a/cloudbaseinit/tests/utils/windows/test_bootconfig.py +++ b/cloudbaseinit/tests/utils/windows/test_bootconfig.py @@ -152,10 +152,15 @@ def _test_enable_auto_recovery(self, mock_get_current_bcd_store, mock_success=True, mock_enable=True): mock_store = mock.Mock() mock_get_current_bcd_store.return_value = mock_store - mock_store.SetBooleanElement.side_effect = ((mock_success,),) + mock_store.SetBooleanElement.return_value = () + + mock_get_element = mock.MagicMock() + mock_get_element.Boolean = mock_success + mock_store.GetElement.return_value = [mock_get_element] + expected_call = ( - self.bootconfig.BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED, - mock_enable) + mock_enable, + self.bootconfig.BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED) if not mock_success: self.assertRaises(exception.CloudbaseInitException, self.bootconfig.enable_auto_recovery, diff --git a/cloudbaseinit/utils/windows/bootconfig.py b/cloudbaseinit/utils/windows/bootconfig.py index 1b8b0d8d..802f0116 100644 --- a/cloudbaseinit/utils/windows/bootconfig.py +++ b/cloudbaseinit/utils/windows/bootconfig.py @@ -96,9 +96,15 @@ def set_current_bcd_device_to_boot_partition(): def enable_auto_recovery(enable): current_store = _get_current_bcd_store() - success, = current_store.SetBooleanElement( - BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED, enable) - if not success: + current_store.SetBooleanElement( + enable, + BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED) + + current_state = current_store.GetElement( + BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED)[0].Boolean + + if current_state != enable: raise exception.CloudbaseInitException( - "Cannot set boolean element: %s" % - BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED) + "Cannot set boolean element: '%s'. " + "Current state '%s' != desired state '%s'." % + (BCDLIBRARY_BOOLEAN_AUTO_RECOVERY_ENABLED, current_state, enable))