Skip to content

Commit

Permalink
bootconfig: fix get_current_bcd_store for PyMI
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ader1990 committed Jun 12, 2024
1 parent 993282d commit 26d7cee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
18 changes: 10 additions & 8 deletions cloudbaseinit/tests/utils/windows/test_bootconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,25 @@ def test_get_boot_system_devices(self):
**expected_call_args)
self.assertEqual(res, [mock_id])

def _test_get_current_bcd_store(self, mock_success=True, mock_store=None):
def _test_get_current_bcd_store(self, mock_success=True):
conn = self._wmi_mock.WMI
store = self._wmi_mock._wmi_object
mock_store = mock.Mock()
mock_bcdstore = mock.MagicMock()
conn.return_value = mock_bcdstore
store.return_value = mock_store
mock_bcdstore.BcdStore.OpenStore.return_value = (mock_success,
mock_store)
mock_store_open_store = mock.MagicMock()
mock_store_open_object = mock.MagicMock()
mock_bcdstore.BcdStore.OpenStore.return_value = (
mock_store_open_store, None)
if not mock_success:
mock_bcdstore.BcdStore.OpenStore.return_value = (False, None)
self.assertRaises(
exception.CloudbaseInitException,
self.bootconfig._get_current_bcd_store)
else:
mock_store.OpenObject.return_value = [None, mock_success]
mock_store_open_store.OpenObject.return_value = [
mock_store_open_object,
None]
res_store = self.bootconfig._get_current_bcd_store()
self.assertEqual(res_store, mock_store)
self.assertEqual(res_store, mock_store_open_object)

def test_get_current_bcd_store(self):
self._test_get_current_bcd_store()
Expand Down
18 changes: 12 additions & 6 deletions cloudbaseinit/utils/windows/bootconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ def get_boot_system_devices():

def _get_current_bcd_store():
conn = wmi.WMI(moniker='//./root/wmi')
success, store = conn.BcdStore.OpenStore(File="")
if not success:
# Under PyMI, the following returns one element if the store is found,
# two elements otherwise.
#
# >>> conn.BcdStore.OpenStore(File="file not present")
# (False, None)
# >>> conn.BcdStore.OpenStore(File="")
# (<pymi_object: >,)
store = conn.BcdStore.OpenStore(File="")[0]
if not store:
raise exception.CloudbaseInitException("Cannot open BCD store")
store = wmi._wmi_object(store)
current_store, success = store.OpenObject(Id=STORE_CURRENT)
current_store = wmi._wmi_object(current_store)
if not success:

current_store = store.OpenObject(Id=STORE_CURRENT)[0]
if not current_store:
raise exception.CloudbaseInitException("Cannot open BCD current store")

return current_store
Expand Down

0 comments on commit 26d7cee

Please sign in to comment.