test_win_wusa.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  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.unit import TestCase
  10. from tests.support.mock import MagicMock, patch
  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},
  18. '__env__': 'base'}}
  19. def test_installed_no_source(self):
  20. '''
  21. test wusa.installed without passing source
  22. '''
  23. with self.assertRaises(SaltInvocationError) as excinfo:
  24. wusa.installed(name='KB123456', source=None)
  25. self.assertEqual(excinfo.exception.strerror,
  26. 'Must specify a "source" file to install')
  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(name=self.kb,
  34. source='salt://{0}.msu'.format(self.kb))
  35. expected = {'changes': {},
  36. 'comment': '{0} already installed'.format(self.kb),
  37. 'name': self.kb,
  38. 'result': True}
  39. self.assertDictEqual(expected, returned)
  40. def test_installed_test_true(self):
  41. '''
  42. test wusa.installed with test=True
  43. '''
  44. mock_installed = MagicMock(return_value=False)
  45. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed}), \
  46. patch.dict(wusa.__opts__, {'test': True}):
  47. returned = wusa.installed(name=self.kb,
  48. source='salt://{0}.msu'.format(self.kb))
  49. expected = {'changes': {},
  50. 'comment': '{0} would be installed'.format(self.kb),
  51. 'name': self.kb,
  52. 'result': None}
  53. self.assertDictEqual(expected, returned)
  54. def test_installed_cache_fail(self):
  55. '''
  56. test wusa.install when it fails to cache the file
  57. '''
  58. mock_installed = MagicMock(return_value=False)
  59. mock_cache = MagicMock(return_value='')
  60. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed,
  61. 'cp.cache_file': mock_cache}):
  62. returned = wusa.installed(name=self.kb,
  63. source='salt://{0}.msu'.format(self.kb))
  64. expected = {'changes': {},
  65. 'comment': 'Unable to cache salt://{0}.msu from '
  66. 'saltenv "base"'.format(self.kb),
  67. 'name': self.kb,
  68. 'result': False}
  69. self.assertDictEqual(expected, returned)
  70. def test_installed(self):
  71. '''
  72. test wusa.installed assuming success
  73. '''
  74. mock_installed = MagicMock(side_effect=[False, True])
  75. mock_cache = MagicMock(return_value='C:\\{0}.msu'.format(self.kb))
  76. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed,
  77. 'cp.cache_file': mock_cache,
  78. 'wusa.install': MagicMock()}):
  79. returned = wusa.installed(name=self.kb,
  80. source='salt://{0}.msu'.format(self.kb))
  81. expected = {'changes': {'new': True, 'old': False},
  82. 'comment': '{0} was installed. '.format(self.kb),
  83. 'name': self.kb,
  84. 'result': True}
  85. self.assertDictEqual(expected, returned)
  86. def test_installed_failed(self):
  87. '''
  88. test wusa.installed with a failure
  89. '''
  90. mock_installed = MagicMock(side_effect=[False, False])
  91. mock_cache = MagicMock(return_value='C:\\{0}.msu'.format(self.kb))
  92. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed,
  93. 'cp.cache_file': mock_cache,
  94. 'wusa.install': MagicMock()}):
  95. returned = wusa.installed(name=self.kb,
  96. source='salt://{0}.msu'.format(self.kb))
  97. expected = {'changes': {},
  98. 'comment': '{0} failed to install. '.format(self.kb),
  99. 'name': self.kb,
  100. 'result': False}
  101. self.assertDictEqual(expected, returned)
  102. def test_uninstalled_non_existing(self):
  103. '''
  104. test wusa.uninstalled when the kb is not installed
  105. '''
  106. mock_installed = MagicMock(return_value=False)
  107. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed}):
  108. returned = wusa.uninstalled(name=self.kb)
  109. expected = {'changes': {},
  110. 'comment': '{0} already uninstalled'.format(self.kb),
  111. 'name': self.kb,
  112. 'result': True}
  113. self.assertDictEqual(expected, returned)
  114. def test_uninstalled_test_true(self):
  115. '''
  116. test wusa.uninstalled with test=True
  117. '''
  118. mock_installed = MagicMock(return_value=True)
  119. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed}), \
  120. patch.dict(wusa.__opts__, {'test': True}):
  121. returned = wusa.uninstalled(name=self.kb)
  122. expected = {'changes': {},
  123. 'comment': '{0} would be uninstalled'.format(self.kb),
  124. 'name': self.kb,
  125. 'result': None}
  126. self.assertDictEqual(expected, returned)
  127. def test_uninstalled(self):
  128. '''
  129. test wusa.uninstalled assuming success
  130. '''
  131. mock_installed = MagicMock(side_effect=[True, False])
  132. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed,
  133. 'wusa.uninstall': MagicMock()}):
  134. returned = wusa.uninstalled(name=self.kb)
  135. expected = {'changes': {'new': False, 'old': True},
  136. 'comment': '{0} was uninstalled'.format(self.kb),
  137. 'name': self.kb,
  138. 'result': True}
  139. self.assertDictEqual(expected, returned)
  140. def test_uninstalled_failed(self):
  141. '''
  142. test wusa.uninstalled with a failure
  143. '''
  144. mock_installed = MagicMock(side_effect=[True, True])
  145. with patch.dict(wusa.__salt__, {'wusa.is_installed': mock_installed,
  146. 'wusa.uninstall': MagicMock()}):
  147. returned = wusa.uninstalled(name=self.kb)
  148. expected = {'changes': {},
  149. 'comment': '{0} failed to uninstall'.format(self.kb),
  150. 'name': self.kb,
  151. 'result': False}
  152. self.assertDictEqual(expected, returned)