test_win_system.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_system as win_system
  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 WinSystemTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Validate the win_system state
  16. """
  17. def setup_loader_modules(self):
  18. return {win_system: {}}
  19. def test_computer_desc(self):
  20. """
  21. Test to manage the computer's description field
  22. """
  23. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  24. mock = MagicMock(side_effect=["salt", "stack", "stack"])
  25. with patch.dict(win_system.__salt__, {"system.get_computer_desc": mock}):
  26. ret.update({"comment": "Computer description" " already set to 'salt'"})
  27. self.assertDictEqual(win_system.computer_desc("salt"), ret)
  28. with patch.dict(win_system.__opts__, {"test": True}):
  29. ret.update(
  30. {
  31. "result": None,
  32. "comment": "Computer description" " will be changed to 'salt'",
  33. }
  34. )
  35. self.assertDictEqual(win_system.computer_desc("salt"), ret)
  36. with patch.dict(win_system.__opts__, {"test": False}):
  37. mock = MagicMock(return_value={"Computer Description": "nfs"})
  38. with patch.dict(
  39. win_system.__salt__, {"system.set_computer_desc": mock}
  40. ):
  41. ret.update(
  42. {
  43. "result": False,
  44. "comment": "Unable to set"
  45. " computer description to 'salt'",
  46. }
  47. )
  48. self.assertDictEqual(win_system.computer_desc("salt"), ret)
  49. def test_computer_name(self):
  50. """
  51. Test to manage the computer's name
  52. """
  53. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  54. mock = MagicMock(return_value="salt")
  55. with patch.dict(win_system.__salt__, {"system.get_computer_name": mock}):
  56. mock = MagicMock(side_effect=[None, "SALT", "Stack", "stack"])
  57. with patch.dict(
  58. win_system.__salt__, {"system.get_pending_computer_name": mock}
  59. ):
  60. ret.update({"comment": "Computer name already set to 'salt'"})
  61. self.assertDictEqual(win_system.computer_name("salt"), ret)
  62. ret.update(
  63. {
  64. "comment": "The current computer name"
  65. " is 'salt', but will be changed to 'salt' on"
  66. " the next reboot"
  67. }
  68. )
  69. self.assertDictEqual(win_system.computer_name("salt"), ret)
  70. with patch.dict(win_system.__opts__, {"test": True}):
  71. ret.update(
  72. {
  73. "result": None,
  74. "comment": "Computer name will" " be changed to 'salt'",
  75. }
  76. )
  77. self.assertDictEqual(win_system.computer_name("salt"), ret)
  78. with patch.dict(win_system.__opts__, {"test": False}):
  79. mock = MagicMock(return_value=False)
  80. with patch.dict(
  81. win_system.__salt__, {"system.set_computer_name": mock}
  82. ):
  83. ret.update(
  84. {
  85. "comment": "Unable to set computer name" " to 'salt'",
  86. "result": False,
  87. }
  88. )
  89. self.assertDictEqual(win_system.computer_name("salt"), ret)
  90. def test_hostname(self):
  91. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  92. mock = MagicMock(return_value="minion")
  93. with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
  94. mock = MagicMock(return_value=True)
  95. with patch.dict(win_system.__salt__, {"system.set_hostname": mock}):
  96. ret.update(
  97. {
  98. "comment": "The current hostname is 'minion', "
  99. "but will be changed to 'salt' on the next reboot",
  100. "changes": {"hostname": "salt"},
  101. }
  102. )
  103. self.assertDictEqual(win_system.hostname("salt"), ret)
  104. mock = MagicMock(return_value=False)
  105. with patch.dict(win_system.__salt__, {"system.set_hostname": mock}):
  106. ret.update(
  107. {
  108. "comment": "Unable to set hostname",
  109. "changes": {},
  110. "result": False,
  111. }
  112. )
  113. self.assertDictEqual(win_system.hostname("salt"), ret)
  114. mock = MagicMock(return_value="salt")
  115. with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
  116. ret.update(
  117. {
  118. "comment": "Hostname is already set to 'salt'",
  119. "changes": {},
  120. "result": True,
  121. }
  122. )
  123. self.assertDictEqual(win_system.hostname("salt"), ret)
  124. mock = MagicMock(return_value="salt")
  125. with patch.dict(win_system.__salt__, {"system.get_hostname": mock}):
  126. ret.update(
  127. {
  128. "name": "SALT",
  129. "comment": "Hostname is already set to 'SALT'",
  130. "changes": {},
  131. "result": True,
  132. }
  133. )
  134. self.assertDictEqual(win_system.hostname("SALT"), ret)