Fix win_wua to handle empty CDispatch objects

This commit is contained in:
Shane Lee 2024-07-23 12:02:10 -06:00 committed by Daniel Wozniak
parent 3cd7a62f3f
commit 0d78611767
3 changed files with 50 additions and 6 deletions

2
changelog/66718.fixed.md Normal file
View file

@ -0,0 +1,2 @@
Fixed ``win_wua.available`` when some of the update objects are empty CDispatch
objects. The ``available`` function no longer crashes

View file

@ -528,14 +528,18 @@ class WindowsUpdateAgent:
found = updates.updates
for update in self._updates:
# Some update objects seem to be empty or undefined. Those will be
# exposed here if they are missing these attributes
try:
if salt.utils.data.is_true(update.IsHidden) and skip_hidden:
continue
if salt.utils.data.is_true(update.IsHidden) and skip_hidden:
continue
if salt.utils.data.is_true(update.IsInstalled) and skip_installed:
continue
if salt.utils.data.is_true(update.IsInstalled) and skip_installed:
continue
if salt.utils.data.is_true(update.IsMandatory) and skip_mandatory:
if salt.utils.data.is_true(update.IsMandatory) and skip_mandatory:
continue
except AttributeError:
continue
# Windows 10 build 2004 introduced some problems with the

View file

@ -1,14 +1,52 @@
import pytest
try:
import win32com.client
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
import salt.utils.win_update as win_update
from tests.support.mock import MagicMock, patch
pytestmark = [
pytest.mark.windows_whitelisted,
pytest.mark.skip_unless_on_windows,
pytest.mark.skipif(not HAS_WIN32, reason="Requires Win32 libraries"),
]
def test_available_no_updates():
"""
Test installed when there are no updates on the system
"""
with patch("salt.utils.winapi.Com", autospec=True), patch(
"win32com.client.Dispatch", autospec=True
), patch.object(win_update.WindowsUpdateAgent, "refresh", autospec=True):
wua = win_update.WindowsUpdateAgent(online=False)
wua._updates = []
available_updates = wua.available()
assert available_updates.updates.Add.call_count == 0
def test_available_no_updates_empty_objects():
"""
Test installed when there are no updates on the system
"""
with patch("salt.utils.winapi.Com", autospec=True), patch(
"win32com.client.Dispatch", autospec=True
), patch.object(win_update.WindowsUpdateAgent, "refresh", autospec=True):
wua = win_update.WindowsUpdateAgent(online=False)
wua._updates = [win32com.client.CDispatch, win32com.client.CDispatch]
available_updates = wua.available()
assert available_updates.updates.Add.call_count == 0
def test_installed_no_updates():
"""
Test installed when there are no updates on the system