test_saltutil.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the saltutil state
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import inspect
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch
  14. )
  15. # Import Salt Libs
  16. import salt.states.saltutil as saltutil_state
  17. import salt.modules.saltutil as saltutil_module
  18. class Saltutil(TestCase, LoaderModuleMockMixin):
  19. '''
  20. Test cases for salt.states.saltutil
  21. '''
  22. def setup_loader_modules(self):
  23. return {saltutil_state: {'__opts__': {'test': False}}}
  24. def test_saltutil_sync_all_nochange(self):
  25. sync_output = {
  26. 'clouds': [],
  27. 'engines': [],
  28. 'executors': [],
  29. 'grains': [],
  30. 'beacons': [],
  31. 'utils': [],
  32. 'returners': [],
  33. 'modules': [],
  34. 'renderers': [],
  35. 'log_handlers': [],
  36. 'thorium': [],
  37. 'states': [],
  38. 'sdb': [],
  39. 'proxymodules': [],
  40. 'output': [],
  41. 'pillar': [],
  42. 'matchers': [],
  43. 'serializers': [],
  44. }
  45. state_id = 'somename'
  46. state_result = {'changes': {},
  47. 'comment': 'No updates to sync',
  48. 'name': 'somename',
  49. 'result': True
  50. }
  51. mock_moduleout = MagicMock(return_value=sync_output)
  52. with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
  53. result = saltutil_state.sync_all(state_id, refresh=True)
  54. self.assertEqual(result, state_result)
  55. def test_saltutil_sync_all_test(self):
  56. sync_output = {
  57. 'clouds': [],
  58. 'engines': [],
  59. 'executors': [],
  60. 'grains': [],
  61. 'beacons': [],
  62. 'utils': [],
  63. 'returners': [],
  64. 'modules': [],
  65. 'renderers': [],
  66. 'log_handlers': [],
  67. 'thorium': [],
  68. 'states': [],
  69. 'sdb': [],
  70. 'proxymodules': [],
  71. 'output': [],
  72. 'pillar': [],
  73. 'matchers': [],
  74. 'serializers': [],
  75. }
  76. state_id = 'somename'
  77. state_result = {'changes': {},
  78. 'comment': 'saltutil.sync_all would have been run',
  79. 'name': 'somename',
  80. 'result': None
  81. }
  82. mock_moduleout = MagicMock(return_value=sync_output)
  83. with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
  84. with patch.dict(saltutil_state.__opts__, {'test': True}):
  85. result = saltutil_state.sync_all(state_id, refresh=True)
  86. self.assertEqual(result, state_result)
  87. def test_saltutil_sync_all_change(self):
  88. sync_output = {
  89. 'clouds': [],
  90. 'engines': [],
  91. 'executors': [],
  92. 'grains': [],
  93. 'beacons': [],
  94. 'utils': [],
  95. 'returners': [],
  96. 'modules': ['modules.file'],
  97. 'renderers': [],
  98. 'log_handlers': [],
  99. 'thorium': [],
  100. 'states': ['states.saltutil', 'states.ssh_auth'],
  101. 'sdb': [],
  102. 'proxymodules': [],
  103. 'output': [],
  104. 'pillar': [],
  105. 'matchers': [],
  106. 'serializers': [],
  107. }
  108. state_id = 'somename'
  109. state_result = {'changes': {'modules': ['modules.file'],
  110. 'states': ['states.saltutil', 'states.ssh_auth']},
  111. 'comment': 'Sync performed',
  112. 'name': 'somename',
  113. 'result': True
  114. }
  115. mock_moduleout = MagicMock(return_value=sync_output)
  116. with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
  117. result = saltutil_state.sync_all(state_id, refresh=True)
  118. self.assertEqual(result, state_result)
  119. def test_saltutil_sync_states_should_match_saltutil_module(self):
  120. module_functions = [
  121. f[0] for f in inspect.getmembers(saltutil_module, inspect.isfunction)
  122. if f[0].startswith('sync_')
  123. ]
  124. state_functions = [
  125. f[0] for f in inspect.getmembers(saltutil_state, inspect.isfunction)
  126. if f[0].startswith('sync_')
  127. ]
  128. for fn in module_functions:
  129. self.assertIn(
  130. fn,
  131. state_functions,
  132. msg='modules.saltutil.{} has no matching state in states.saltutil'.format(fn)
  133. )