test_win_wusa.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Libs
  5. import salt.states.win_wusa as wusa
  6. from salt.exceptions import SaltInvocationError
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.mock import MagicMock, patch
  10. from tests.support.unit import TestCase
  11. class WinWusaTestCase(TestCase, LoaderModuleMockMixin):
  12. """
  13. test the function in the win_wusa state module
  14. """
  15. kb = "KB123456"
  16. def setup_loader_modules(self):
  17. return {wusa: {"__opts__": {"test": False}, "__env__": "base"}}
  18. def test_installed_no_source(self):
  19. """
  20. test wusa.installed without passing source
  21. """
  22. with self.assertRaises(SaltInvocationError) as excinfo:
  23. wusa.installed(name="KB123456", source=None)
  24. self.assertEqual(
  25. excinfo.exception.strerror, 'Must specify a "source" file to install'
  26. )
  27. def test_installed_existing(self):
  28. """
  29. test wusa.installed when the kb is already installed
  30. """
  31. mock_installed = MagicMock(return_value=True)
  32. with patch.dict(wusa.__salt__, {"wusa.is_installed": mock_installed}):
  33. returned = wusa.installed(
  34. name=self.kb, source="salt://{0}.msu".format(self.kb)
  35. )
  36. expected = {
  37. "changes": {},
  38. "comment": "{0} already installed".format(self.kb),
  39. "name": self.kb,
  40. "result": True,
  41. }
  42. self.assertDictEqual(expected, returned)
  43. def test_installed_test_true(self):
  44. """
  45. test wusa.installed with test=True
  46. """
  47. mock_installed = MagicMock(return_value=False)
  48. with patch.dict(
  49. wusa.__salt__, {"wusa.is_installed": mock_installed}
  50. ), patch.dict(wusa.__opts__, {"test": True}):
  51. returned = wusa.installed(
  52. name=self.kb, source="salt://{0}.msu".format(self.kb)
  53. )
  54. expected = {
  55. "changes": {},
  56. "comment": "{0} would be installed".format(self.kb),
  57. "name": self.kb,
  58. "result": None,
  59. }
  60. self.assertDictEqual(expected, returned)
  61. def test_installed_cache_fail(self):
  62. """
  63. test wusa.install when it fails to cache the file
  64. """
  65. mock_installed = MagicMock(return_value=False)
  66. mock_cache = MagicMock(return_value="")
  67. with patch.dict(
  68. wusa.__salt__,
  69. {"wusa.is_installed": mock_installed, "cp.cache_file": mock_cache},
  70. ):
  71. returned = wusa.installed(
  72. name=self.kb, source="salt://{0}.msu".format(self.kb)
  73. )
  74. expected = {
  75. "changes": {},
  76. "comment": "Unable to cache salt://{0}.msu from "
  77. 'saltenv "base"'.format(self.kb),
  78. "name": self.kb,
  79. "result": False,
  80. }
  81. self.assertDictEqual(expected, returned)
  82. def test_installed(self):
  83. """
  84. test wusa.installed assuming success
  85. """
  86. mock_installed = MagicMock(side_effect=[False, True])
  87. mock_cache = MagicMock(return_value="C:\\{0}.msu".format(self.kb))
  88. with patch.dict(
  89. wusa.__salt__,
  90. {
  91. "wusa.is_installed": mock_installed,
  92. "cp.cache_file": mock_cache,
  93. "wusa.install": MagicMock(),
  94. },
  95. ):
  96. returned = wusa.installed(
  97. name=self.kb, source="salt://{0}.msu".format(self.kb)
  98. )
  99. expected = {
  100. "changes": {"new": True, "old": False},
  101. "comment": "{0} was installed. ".format(self.kb),
  102. "name": self.kb,
  103. "result": True,
  104. }
  105. self.assertDictEqual(expected, returned)
  106. def test_installed_failed(self):
  107. """
  108. test wusa.installed with a failure
  109. """
  110. mock_installed = MagicMock(side_effect=[False, False])
  111. mock_cache = MagicMock(return_value="C:\\{0}.msu".format(self.kb))
  112. with patch.dict(
  113. wusa.__salt__,
  114. {
  115. "wusa.is_installed": mock_installed,
  116. "cp.cache_file": mock_cache,
  117. "wusa.install": MagicMock(),
  118. },
  119. ):
  120. returned = wusa.installed(
  121. name=self.kb, source="salt://{0}.msu".format(self.kb)
  122. )
  123. expected = {
  124. "changes": {},
  125. "comment": "{0} failed to install. ".format(self.kb),
  126. "name": self.kb,
  127. "result": False,
  128. }
  129. self.assertDictEqual(expected, returned)
  130. def test_uninstalled_non_existing(self):
  131. """
  132. test wusa.uninstalled when the kb is not installed
  133. """
  134. mock_installed = MagicMock(return_value=False)
  135. with patch.dict(wusa.__salt__, {"wusa.is_installed": mock_installed}):
  136. returned = wusa.uninstalled(name=self.kb)
  137. expected = {
  138. "changes": {},
  139. "comment": "{0} already uninstalled".format(self.kb),
  140. "name": self.kb,
  141. "result": True,
  142. }
  143. self.assertDictEqual(expected, returned)
  144. def test_uninstalled_test_true(self):
  145. """
  146. test wusa.uninstalled with test=True
  147. """
  148. mock_installed = MagicMock(return_value=True)
  149. with patch.dict(
  150. wusa.__salt__, {"wusa.is_installed": mock_installed}
  151. ), patch.dict(wusa.__opts__, {"test": True}):
  152. returned = wusa.uninstalled(name=self.kb)
  153. expected = {
  154. "changes": {},
  155. "comment": "{0} would be uninstalled".format(self.kb),
  156. "name": self.kb,
  157. "result": None,
  158. }
  159. self.assertDictEqual(expected, returned)
  160. def test_uninstalled(self):
  161. """
  162. test wusa.uninstalled assuming success
  163. """
  164. mock_installed = MagicMock(side_effect=[True, False])
  165. with patch.dict(
  166. wusa.__salt__,
  167. {"wusa.is_installed": mock_installed, "wusa.uninstall": MagicMock()},
  168. ):
  169. returned = wusa.uninstalled(name=self.kb)
  170. expected = {
  171. "changes": {"new": False, "old": True},
  172. "comment": "{0} was uninstalled".format(self.kb),
  173. "name": self.kb,
  174. "result": True,
  175. }
  176. self.assertDictEqual(expected, returned)
  177. def test_uninstalled_failed(self):
  178. """
  179. test wusa.uninstalled with a failure
  180. """
  181. mock_installed = MagicMock(side_effect=[True, True])
  182. with patch.dict(
  183. wusa.__salt__,
  184. {"wusa.is_installed": mock_installed, "wusa.uninstall": MagicMock()},
  185. ):
  186. returned = wusa.uninstalled(name=self.kb)
  187. expected = {
  188. "changes": {},
  189. "comment": "{0} failed to uninstall".format(self.kb),
  190. "name": self.kb,
  191. "result": False,
  192. }
  193. self.assertDictEqual(expected, returned)