Skip to content

Commit

Permalink
Merge pull request #54411 from axa-group/fix-wusa-3010-error
Browse files Browse the repository at this point in the history
Manage wusa 3010 return code
  • Loading branch information
dwoz authored Dec 28, 2019
2 parents e2d74e0 + a997386 commit 5f9b611
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
5 changes: 3 additions & 2 deletions salt/modules/win_wusa.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def install(path, restart=False):
# Check the ret_code
file_name = os.path.basename(path)
errors = {2359302: '{0} is already installed'.format(file_name),
3010: '{0} correctly installed but server reboot is needed to complete installation'.format(file_name),
87: 'Unknown error'}
if ret_code in errors:
raise CommandExecutionError(errors[ret_code])
raise CommandExecutionError(errors[ret_code], ret_code)
elif ret_code:
raise CommandExecutionError('Unknown error: {0}'.format(ret_code))

Expand Down Expand Up @@ -199,7 +200,7 @@ def uninstall(path, restart=False):
2359303: '{0} not installed'.format(kb),
87: 'Unknown error. Try specifying an .msu file'}
if ret_code in errors:
raise CommandExecutionError(errors[ret_code])
raise CommandExecutionError(errors[ret_code], ret_code)
elif ret_code:
raise CommandExecutionError('Unknown error: {0}'.format(ret_code))

Expand Down
13 changes: 10 additions & 3 deletions salt/states/win_wusa.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import salt.utils.platform
import salt.utils.url
from salt.exceptions import SaltInvocationError
from salt.exceptions import CommandExecutionError

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -87,15 +88,21 @@ def installed(name, source):
return ret

# Install the KB
__salt__['wusa.install'](cached_source_path)

additional_comment = ''

try:
__salt__['wusa.install'](cached_source_path)
except CommandExecutionError as exc:
additional_comment = exc.message

# Verify successful install
if __salt__['wusa.is_installed'](name):
ret['comment'] = '{0} was installed'.format(name)
ret['comment'] = '{0} was installed. {1}'.format(name, additional_comment)
ret['changes'] = {'old': False, 'new': True}
ret['result'] = True
else:
ret['comment'] = '{0} failed to install'.format(name)
ret['comment'] = '{0} failed to install. {1}'.format(name, additional_comment)

return ret

Expand Down
32 changes: 26 additions & 6 deletions tests/unit/modules/test_win_wusa.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,48 @@ def test_install_already_installed(self):
'''
test install function when KB already installed
'''
mock_retcode = MagicMock(return_value=2359302)
retcode = 2359302
mock_retcode = MagicMock(return_value=retcode)
path = 'C:\\KB123456.msu'
name = 'KB123456.msu'
with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
with self.assertRaises(CommandExecutionError) as excinfo:
win_wusa.install(path)
mock_retcode.assert_called_once_with(
['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
self.assertEqual('{0} is already installed'.format(name),
self.assertEqual('{0} is already installed. Additional info follows:\n\n{1}'.format(name, retcode),
excinfo.exception.strerror)

def test_install_reboot_needed(self):
'''
test install function when KB need a reboot
'''
retcode = 3010
mock_retcode = MagicMock(return_value=retcode)
path = 'C:\\KB123456.msu'
name = 'KB123456.msu'
with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
with self.assertRaises(CommandExecutionError) as excinfo:
win_wusa.install(path)
mock_retcode.assert_called_once_with(
['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
self.assertEqual('{0} correctly installed but server reboot is needed to complete installation. Additional info follows:\n\n{1}'.format(name, retcode),
excinfo.exception.strerror)

def test_install_error_87(self):
'''
test install function when error 87 returned
'''
mock_retcode = MagicMock(return_value=87)
retcode = 87
mock_retcode = MagicMock(return_value=retcode)
path = 'C:\\KB123456.msu'
with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
with self.assertRaises(CommandExecutionError) as excinfo:
win_wusa.install(path)
mock_retcode.assert_called_once_with(
['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
self.assertEqual('Unknown error', excinfo.exception.strerror)
self.assertEqual('Unknown error. Additional info follows:\n\n{0}'.format(retcode),
excinfo.exception.strerror)

def test_install_error_other(self):
'''
Expand Down Expand Up @@ -162,15 +181,16 @@ def test_uninstall_already_uninstalled(self):
'''
test uninstall function when KB already uninstalled
'''
mock_retcode = MagicMock(return_value=2359303)
retcode = 2359303
mock_retcode = MagicMock(return_value=retcode)
kb = 'KB123456'
with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
with self.assertRaises(CommandExecutionError) as excinfo:
win_wusa.uninstall(kb)
mock_retcode.assert_called_once_with(
['wusa.exe', '/uninstall', '/quiet', '/kb:{0}'.format(kb[2:]), '/norestart'],
ignore_retcode=True)
self.assertEqual('{0} not installed'.format(kb),
self.assertEqual('{0} not installed. Additional info follows:\n\n{1}'.format(kb, retcode),
excinfo.exception.strerror)

def test_uninstall_path_error_other(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/states/test_win_wusa.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_installed(self):
returned = wusa.installed(name=self.kb,
source='salt://{0}.msu'.format(self.kb))
expected = {'changes': {'new': True, 'old': False},
'comment': '{0} was installed'.format(self.kb),
'comment': '{0} was installed. '.format(self.kb),
'name': self.kb,
'result': True}
self.assertDictEqual(expected, returned)
Expand All @@ -108,7 +108,7 @@ def test_installed_failed(self):
returned = wusa.installed(name=self.kb,
source='salt://{0}.msu'.format(self.kb))
expected = {'changes': {},
'comment': '{0} failed to install'.format(self.kb),
'comment': '{0} failed to install. '.format(self.kb),
'name': self.kb,
'result': False}
self.assertDictEqual(expected, returned)
Expand Down

0 comments on commit 5f9b611

Please sign in to comment.