test_win_servermanager.py 5.1 KB

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