test_win_servermanager.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  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.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.win_servermanager as win_servermanager
  16. class WinServermanagerTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the win_servermanager state
  19. '''
  20. def setup_loader_modules(self):
  21. return {win_servermanager: {}}
  22. def test_installed(self):
  23. '''
  24. Test to install the windows feature
  25. '''
  26. mock_list = MagicMock(
  27. side_effect=[{'spongebob': 'squarepants'},
  28. {'squidward': 'patrick'},
  29. {'spongebob': 'squarepants'},
  30. {'spongebob': 'squarepants',
  31. 'squidward': 'patrick'}])
  32. mock_install = MagicMock(
  33. return_value={'Success': True,
  34. 'Restarted': False,
  35. 'RestartNeeded': False,
  36. 'ExitCode': 1234,
  37. 'Features': {
  38. 'squidward': {
  39. 'DisplayName': 'Squidward',
  40. 'Message': '',
  41. 'RestartNeeded': True,
  42. 'SkipReason': 0,
  43. 'Success': True
  44. }
  45. }})
  46. with patch.dict(win_servermanager.__salt__,
  47. {"win_servermanager.list_installed": mock_list,
  48. "win_servermanager.install": mock_install}):
  49. ret = {'name': 'spongebob',
  50. 'changes': {},
  51. 'result': True,
  52. 'comment': 'The following features are already installed:\n'
  53. '- spongebob'}
  54. self.assertDictEqual(win_servermanager.installed('spongebob'), ret)
  55. with patch.dict(win_servermanager.__opts__, {"test": True}):
  56. ret = {'name': 'spongebob',
  57. 'result': None,
  58. 'comment': '',
  59. 'changes': {
  60. 'spongebob': 'Will be installed recurse=False'}}
  61. self.assertDictEqual(
  62. win_servermanager.installed('spongebob'), ret)
  63. with patch.dict(win_servermanager.__opts__, {"test": False}):
  64. ret = {'name': 'squidward',
  65. 'result': True,
  66. 'comment': 'Installed the following:\n- squidward',
  67. 'changes': {
  68. 'squidward': {'new': 'patrick', 'old': ''}}}
  69. self.assertDictEqual(
  70. win_servermanager.installed('squidward'), ret)
  71. def test_removed(self):
  72. '''
  73. Test to remove the windows feature
  74. '''
  75. mock_list = MagicMock(
  76. side_effect=[{'spongebob': 'squarepants'},
  77. {'squidward': 'patrick'},
  78. {'spongebob': 'squarepants',
  79. 'squidward': 'patrick'},
  80. {'spongebob': 'squarepants'}])
  81. mock_remove = MagicMock(
  82. return_value={'Success': True,
  83. 'RestartNeeded': False,
  84. 'Restarted': False,
  85. 'ExitCode': 1234,
  86. 'Features': {
  87. 'squidward': {
  88. 'DisplayName': 'Squidward',
  89. 'Message': '',
  90. 'RestartNeeded': True,
  91. 'SkipReason': 0,
  92. 'Success': True
  93. }
  94. }})
  95. with patch.dict(win_servermanager.__salt__,
  96. {"win_servermanager.list_installed": mock_list,
  97. "win_servermanager.remove": mock_remove}):
  98. ret = {'name': 'squidward',
  99. 'changes': {},
  100. 'result': True,
  101. 'comment': 'The following features are not installed:\n'
  102. '- squidward'}
  103. self.assertDictEqual(
  104. win_servermanager.removed('squidward'), ret)
  105. with patch.dict(win_servermanager.__opts__, {"test": True}):
  106. ret = {'name': 'squidward',
  107. 'result': None,
  108. 'comment': '',
  109. 'changes': {'squidward': 'Will be removed'}}
  110. self.assertDictEqual(
  111. win_servermanager.removed('squidward'), ret)
  112. with patch.dict(win_servermanager.__opts__, {"test": False}):
  113. ret = {'name': 'squidward',
  114. 'result': True,
  115. 'comment': 'Removed the following:\n- squidward',
  116. 'changes': {
  117. 'squidward': {'new': '', 'old': 'patrick'}}}
  118. self.assertDictEqual(
  119. win_servermanager.removed('squidward'), ret)