test_win_pkg.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the win_pkg 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 MagicMock, patch
  10. from tests.support.unit import TestCase
  11. # Import Salt Libs
  12. import salt.modules.pkg_resource as pkg_resource
  13. import salt.modules.win_pkg as win_pkg
  14. class WinPkgInstallTestCase(TestCase, LoaderModuleMockMixin):
  15. '''
  16. Test cases for salt.modules.win_pkg
  17. '''
  18. def setup_loader_modules(self):
  19. pkg_info = {
  20. '3.03': {
  21. 'full_name': 'Nullsoft Install System',
  22. 'installer': 'http://download.sourceforge.net/project/nsis/NSIS%203/3.03/nsis-3.03-setup.exe',
  23. 'install_flags': '/S',
  24. 'uninstaller': '%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe',
  25. 'uninstall_flags': '/S',
  26. 'msiexec': False,
  27. 'reboot': False
  28. },
  29. '3.02': {
  30. 'full_name': 'Nullsoft Install System',
  31. 'installer': 'http://download.sourceforge.net/project/nsis/NSIS%203/3.02/nsis-3.02-setup.exe',
  32. 'install_flags': '/S',
  33. 'uninstaller': '%PROGRAMFILES(x86)%\\NSIS\\uninst-nsis.exe',
  34. 'uninstall_flags': '/S',
  35. 'msiexec': False,
  36. 'reboot': False
  37. }
  38. }
  39. return{
  40. win_pkg: {
  41. '_get_latest_package_version': MagicMock(return_value='3.03'),
  42. '_get_package_info': MagicMock(return_value=pkg_info),
  43. '__salt__': {
  44. 'pkg_resource.add_pkg': pkg_resource.add_pkg,
  45. 'pkg_resource.parse_targets': pkg_resource.parse_targets,
  46. 'pkg_resource.sort_pkglist': pkg_resource.sort_pkglist,
  47. 'pkg_resource.stringify': pkg_resource.stringify,
  48. },
  49. },
  50. pkg_resource: {
  51. '__grains__': {
  52. 'os': 'Windows'
  53. }
  54. },
  55. }
  56. def test_pkg_install_not_found(self):
  57. '''
  58. Test pkg.install when the Version is NOT FOUND in the Software
  59. Definition
  60. '''
  61. ret_reg = {'Nullsoft Install System': '3.03'}
  62. # The 2nd time it's run with stringify
  63. se_list_pkgs = {'nsis': ['3.03']}
  64. with patch.object(win_pkg, 'list_pkgs', return_value=se_list_pkgs), \
  65. patch.object(win_pkg, '_get_reg_software', return_value=ret_reg):
  66. expected = {'nsis': {'not found': '3.01'}}
  67. result = win_pkg.install(name='nsis', version='3.01')
  68. self.assertDictEqual(expected, result)
  69. def test_pkg_install_rollback(self):
  70. '''
  71. test pkg.install rolling back to a previous version
  72. '''
  73. ret_reg = {'Nullsoft Install System': '3.03'}
  74. # The 2nd time it's run, pkg.list_pkgs uses with stringify
  75. se_list_pkgs = [{'nsis': ['3.03']},
  76. {'nsis': '3.02'}]
  77. with patch.object(win_pkg, 'list_pkgs', side_effect=se_list_pkgs), \
  78. patch.object(
  79. win_pkg, '_get_reg_software', return_value=ret_reg), \
  80. patch.dict(
  81. win_pkg.__salt__,
  82. {'cp.is_cached': MagicMock(return_value=False)}), \
  83. patch.dict(
  84. win_pkg.__salt__,
  85. {'cp.cache_file':
  86. MagicMock(return_value='C:\\fake\\path.exe')}), \
  87. patch.dict(
  88. win_pkg.__salt__,
  89. {'cmd.run_all':
  90. MagicMock(return_value={'retcode': 0})}):
  91. expected = {'nsis': {'new': '3.02', 'old': '3.03'}}
  92. result = win_pkg.install(name='nsis', version='3.02')
  93. self.assertDictEqual(expected, result)
  94. def test_pkg_install_existing(self):
  95. '''
  96. test pkg.install when the package is already installed
  97. no version passed
  98. '''
  99. ret_reg = {'Nullsoft Install System': '3.03'}
  100. # The 2nd time it's run, pkg.list_pkgs uses with stringify
  101. se_list_pkgs = {'nsis': ['3.03']}
  102. with patch.object(win_pkg, 'list_pkgs', return_value=se_list_pkgs), \
  103. patch.object(
  104. win_pkg, '_get_reg_software', return_value=ret_reg), \
  105. patch.dict(
  106. win_pkg.__salt__,
  107. {'cp.is_cached': MagicMock(return_value=False)}), \
  108. patch.dict(
  109. win_pkg.__salt__,
  110. {'cp.cache_file':
  111. MagicMock(return_value='C:\\fake\\path.exe')}), \
  112. patch.dict(
  113. win_pkg.__salt__,
  114. {'cmd.run_all':
  115. MagicMock(return_value={'retcode': 0})}):
  116. expected = {}
  117. result = win_pkg.install(name='nsis')
  118. self.assertDictEqual(expected, result)
  119. def test_pkg_install_existing_with_version(self):
  120. '''
  121. test pkg.install when the package is already installed
  122. A version is passed
  123. '''
  124. ret_reg = {'Nullsoft Install System': '3.03'}
  125. # The 2nd time it's run, pkg.list_pkgs uses with stringify
  126. se_list_pkgs = {'nsis': ['3.03']}
  127. with patch.object(win_pkg, 'list_pkgs', return_value=se_list_pkgs), \
  128. patch.object(
  129. win_pkg, '_get_reg_software', return_value=ret_reg), \
  130. patch.dict(
  131. win_pkg.__salt__,
  132. {'cp.is_cached': MagicMock(return_value=False)}), \
  133. patch.dict(
  134. win_pkg.__salt__,
  135. {'cp.cache_file':
  136. MagicMock(return_value='C:\\fake\\path.exe')}), \
  137. patch.dict(
  138. win_pkg.__salt__,
  139. {'cmd.run_all':
  140. MagicMock(return_value={'retcode': 0})}):
  141. expected = {}
  142. result = win_pkg.install(name='nsis', version='3.03')
  143. self.assertDictEqual(expected, result)