test_win_wusa.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the win_wusa execution module
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import patch, MagicMock
  10. from tests.support.unit import TestCase, skipIf
  11. # Import Salt Libs
  12. import salt.utils.platform
  13. import salt.modules.win_wusa as win_wusa
  14. from salt.exceptions import CommandExecutionError
  15. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  16. class WinWusaTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. test the functions in the win_wusa execution module
  19. '''
  20. def setup_loader_modules(self):
  21. return {win_wusa: {}}
  22. def test_is_installed_false(self):
  23. '''
  24. test is_installed function when the KB is not installed
  25. '''
  26. mock_retcode = MagicMock(return_value=1)
  27. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  28. self.assertFalse(win_wusa.is_installed('KB123456'))
  29. def test_is_installed_true(self):
  30. '''
  31. test is_installed function when the KB is installed
  32. '''
  33. mock_retcode = MagicMock(return_value=0)
  34. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  35. self.assertTrue(win_wusa.is_installed('KB123456'))
  36. def test_list(self):
  37. '''
  38. test list function
  39. '''
  40. ret = {'pid': 1,
  41. 'retcode': 0,
  42. 'stderr': '',
  43. 'stdout': '[{"HotFixID": "KB123456"}, '
  44. '{"HotFixID": "KB123457"}]'}
  45. mock_all = MagicMock(return_value=ret)
  46. with patch.dict(win_wusa.__salt__, {'cmd.run_all': mock_all}):
  47. expected = ['KB123456', 'KB123457']
  48. returned = win_wusa.list()
  49. self.assertListEqual(expected, returned)
  50. def test_install(self):
  51. '''
  52. test install function
  53. '''
  54. mock_retcode = MagicMock(return_value=0)
  55. path = 'C:\\KB123456.msu'
  56. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  57. self.assertTrue(win_wusa.install(path))
  58. mock_retcode.assert_called_once_with(
  59. ['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
  60. def test_install_restart(self):
  61. '''
  62. test install function with restart=True
  63. '''
  64. mock_retcode = MagicMock(return_value=0)
  65. path = 'C:\\KB123456.msu'
  66. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  67. self.assertTrue(win_wusa.install(path, restart=True))
  68. mock_retcode.assert_called_once_with(
  69. ['wusa.exe', path, '/quiet', '/forcerestart'], ignore_retcode=True)
  70. def test_install_already_installed(self):
  71. '''
  72. test install function when KB already installed
  73. '''
  74. mock_retcode = MagicMock(return_value=2359302)
  75. path = 'C:\\KB123456.msu'
  76. name = 'KB123456.msu'
  77. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  78. with self.assertRaises(CommandExecutionError) as excinfo:
  79. win_wusa.install(path)
  80. mock_retcode.assert_called_once_with(
  81. ['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
  82. self.assertEqual('{0} is already installed'.format(name),
  83. excinfo.exception.strerror)
  84. def test_install_error_87(self):
  85. '''
  86. test install function when error 87 returned
  87. '''
  88. mock_retcode = MagicMock(return_value=87)
  89. path = 'C:\\KB123456.msu'
  90. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  91. with self.assertRaises(CommandExecutionError) as excinfo:
  92. win_wusa.install(path)
  93. mock_retcode.assert_called_once_with(
  94. ['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
  95. self.assertEqual('Unknown error', excinfo.exception.strerror)
  96. def test_install_error_other(self):
  97. '''
  98. test install function on other unknown error
  99. '''
  100. mock_retcode = MagicMock(return_value=1234)
  101. path = 'C:\\KB123456.msu'
  102. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  103. with self.assertRaises(CommandExecutionError) as excinfo:
  104. win_wusa.install(path)
  105. mock_retcode.assert_called_once_with(
  106. ['wusa.exe', path, '/quiet', '/norestart'], ignore_retcode=True)
  107. self.assertEqual('Unknown error: 1234', excinfo.exception.strerror)
  108. def test_uninstall_kb(self):
  109. '''
  110. test uninstall function passing kb name
  111. '''
  112. mock_retcode = MagicMock(return_value=0)
  113. kb = 'KB123456'
  114. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}), \
  115. patch("os.path.exists", MagicMock(return_value=False)):
  116. self.assertTrue(win_wusa.uninstall(kb))
  117. mock_retcode.assert_called_once_with(
  118. ['wusa.exe', '/uninstall', '/quiet', '/kb:{0}'.format(kb[2:]), '/norestart'],
  119. ignore_retcode=True)
  120. def test_uninstall_path(self):
  121. '''
  122. test uninstall function passing full path to .msu file
  123. '''
  124. mock_retcode = MagicMock(return_value=0)
  125. path = 'C:\\KB123456.msu'
  126. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}), \
  127. patch("os.path.exists", MagicMock(return_value=True)):
  128. self.assertTrue(win_wusa.uninstall(path))
  129. mock_retcode.assert_called_once_with(
  130. ['wusa.exe', '/uninstall', '/quiet', path, '/norestart'],
  131. ignore_retcode=True)
  132. def test_uninstall_path_restart(self):
  133. '''
  134. test uninstall function with full path and restart=True
  135. '''
  136. mock_retcode = MagicMock(return_value=0)
  137. path = 'C:\\KB123456.msu'
  138. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}), \
  139. patch("os.path.exists", MagicMock(return_value=True)):
  140. self.assertTrue(win_wusa.uninstall(path, restart=True))
  141. mock_retcode.assert_called_once_with(
  142. ['wusa.exe', '/uninstall', '/quiet', path, '/forcerestart'],
  143. ignore_retcode=True)
  144. def test_uninstall_already_uninstalled(self):
  145. '''
  146. test uninstall function when KB already uninstalled
  147. '''
  148. mock_retcode = MagicMock(return_value=2359303)
  149. kb = 'KB123456'
  150. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}):
  151. with self.assertRaises(CommandExecutionError) as excinfo:
  152. win_wusa.uninstall(kb)
  153. mock_retcode.assert_called_once_with(
  154. ['wusa.exe', '/uninstall', '/quiet', '/kb:{0}'.format(kb[2:]), '/norestart'],
  155. ignore_retcode=True)
  156. self.assertEqual('{0} not installed'.format(kb),
  157. excinfo.exception.strerror)
  158. def test_uninstall_path_error_other(self):
  159. '''
  160. test uninstall function with unknown error
  161. '''
  162. mock_retcode = MagicMock(return_value=1234)
  163. path = 'C:\\KB123456.msu'
  164. with patch.dict(win_wusa.__salt__, {'cmd.retcode': mock_retcode}), \
  165. patch("os.path.exists", MagicMock(return_value=True)), \
  166. self.assertRaises(CommandExecutionError) as excinfo:
  167. win_wusa.uninstall(path)
  168. mock_retcode.assert_called_once_with(
  169. ['wusa.exe', '/uninstall', '/quiet', path, '/norestart'],
  170. ignore_retcode=True)
  171. self.assertEqual('Unknown error: 1234', excinfo.exception.strerror)