Skip to content

Commit 26d7cee

Browse files
committed
bootconfig: fix get_current_bcd_store for PyMI
When PyMI replaced the WMI implementation, the method contract for `conn.BcdStore.OpenStore` changed, leading to an error: ```bash ValueError: not enough values to unpack (expected 2, got 1) ``` Under PyMI, the following returns one element if the store is found, two elements otherwise: ```python >> conn.BcdStore.OpenStore(File="fake") (False, None) >> conn.BcdStore.OpenStore(File="") (<pymi_object: >,) ``` Fixes: #146 Change-Id: Ibb298e4c92c451946a3ac4cfd2e436b2b3c203cf Signed-off-by: Adrian Vladu <[email protected]>
1 parent 993282d commit 26d7cee

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

cloudbaseinit/tests/utils/windows/test_bootconfig.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,25 @@ def test_get_boot_system_devices(self):
8484
**expected_call_args)
8585
self.assertEqual(res, [mock_id])
8686

87-
def _test_get_current_bcd_store(self, mock_success=True, mock_store=None):
87+
def _test_get_current_bcd_store(self, mock_success=True):
8888
conn = self._wmi_mock.WMI
89-
store = self._wmi_mock._wmi_object
90-
mock_store = mock.Mock()
9189
mock_bcdstore = mock.MagicMock()
9290
conn.return_value = mock_bcdstore
93-
store.return_value = mock_store
94-
mock_bcdstore.BcdStore.OpenStore.return_value = (mock_success,
95-
mock_store)
91+
mock_store_open_store = mock.MagicMock()
92+
mock_store_open_object = mock.MagicMock()
93+
mock_bcdstore.BcdStore.OpenStore.return_value = (
94+
mock_store_open_store, None)
9695
if not mock_success:
96+
mock_bcdstore.BcdStore.OpenStore.return_value = (False, None)
9797
self.assertRaises(
9898
exception.CloudbaseInitException,
9999
self.bootconfig._get_current_bcd_store)
100100
else:
101-
mock_store.OpenObject.return_value = [None, mock_success]
101+
mock_store_open_store.OpenObject.return_value = [
102+
mock_store_open_object,
103+
None]
102104
res_store = self.bootconfig._get_current_bcd_store()
103-
self.assertEqual(res_store, mock_store)
105+
self.assertEqual(res_store, mock_store_open_object)
104106

105107
def test_get_current_bcd_store(self):
106108
self._test_get_current_bcd_store()

cloudbaseinit/utils/windows/bootconfig.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,19 @@ def get_boot_system_devices():
5656

5757
def _get_current_bcd_store():
5858
conn = wmi.WMI(moniker='//./root/wmi')
59-
success, store = conn.BcdStore.OpenStore(File="")
60-
if not success:
59+
# Under PyMI, the following returns one element if the store is found,
60+
# two elements otherwise.
61+
#
62+
# >>> conn.BcdStore.OpenStore(File="file not present")
63+
# (False, None)
64+
# >>> conn.BcdStore.OpenStore(File="")
65+
# (<pymi_object: >,)
66+
store = conn.BcdStore.OpenStore(File="")[0]
67+
if not store:
6168
raise exception.CloudbaseInitException("Cannot open BCD store")
62-
store = wmi._wmi_object(store)
63-
current_store, success = store.OpenObject(Id=STORE_CURRENT)
64-
current_store = wmi._wmi_object(current_store)
65-
if not success:
69+
70+
current_store = store.OpenObject(Id=STORE_CURRENT)[0]
71+
if not current_store:
6672
raise exception.CloudbaseInitException("Cannot open BCD current store")
6773

6874
return current_store

0 commit comments

Comments
 (0)