test_win_system.py 5.5 KB

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