1
0

test_highstate.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. # -*- coding: utf-8 -*-
  2. '''
  3. unittests for highstate outputter
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. # Import Salt Libs
  11. import salt.utils.stringutils
  12. import salt.output.highstate as highstate
  13. # Import 3rd-party libs
  14. from salt.ext import six
  15. class JsonTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.output.highstate
  18. '''
  19. def setup_loader_modules(self):
  20. return {
  21. highstate: {
  22. '__opts__': {
  23. 'extension_modules': '',
  24. 'optimization_order': [0, 1, 2],
  25. 'color': False,
  26. }
  27. }
  28. }
  29. def setUp(self):
  30. self.data = {
  31. 'data': {
  32. 'master': {
  33. 'salt_|-call_sleep_state_|-call_sleep_state_|-state': {
  34. '__id__': 'call_sleep_state',
  35. '__jid__': '20170418153529810135',
  36. '__run_num__': 0,
  37. '__sls__': 'orch.simple',
  38. 'changes': {
  39. 'out': 'highstate',
  40. 'ret': {
  41. 'minion': {
  42. 'module_|-simple-ping_|-test.ping_|-run': {
  43. '__id__': 'simple-ping',
  44. '__run_num__': 0,
  45. '__sls__': 'simple-ping',
  46. 'changes': {'ret': True},
  47. 'comment': 'Module function test.ping executed',
  48. 'duration': 56.179,
  49. 'name': 'test.ping',
  50. 'result': True,
  51. 'start_time': '15:35:31.282099'
  52. }
  53. },
  54. 'sub_minion': {
  55. 'module_|-simple-ping_|-test.ping_|-run': {
  56. '__id__': 'simple-ping',
  57. '__run_num__': 0,
  58. '__sls__': 'simple-ping',
  59. 'changes': {'ret': True},
  60. 'comment': 'Module function test.ping executed',
  61. 'duration': 54.103,
  62. 'name': 'test.ping',
  63. 'result': True,
  64. 'start_time': '15:35:31.005606'
  65. }
  66. }
  67. }
  68. },
  69. 'comment': 'States ran successfully. Updating sub_minion, minion.',
  70. 'duration': 1638.047,
  71. 'name': 'call_sleep_state',
  72. 'result': True,
  73. 'start_time': '15:35:29.762657'
  74. }
  75. }
  76. },
  77. 'outputter': 'highstate',
  78. 'retcode': 0
  79. }
  80. self.addCleanup(delattr, self, 'data')
  81. def test_default_output(self):
  82. ret = highstate.output(self.data)
  83. self.assertIn('Succeeded: 1 (changed=1)', ret)
  84. self.assertIn('Failed: 0', ret)
  85. self.assertIn('Total states run: 1', ret)
  86. def test_output_comment_is_not_unicode(self):
  87. entry = None
  88. for key in ('data', 'master', 'salt_|-call_sleep_state_|-call_sleep_state_|-state',
  89. 'changes', 'ret', 'minion', 'module_|-simple-ping_|-test.ping_|-run'):
  90. if entry is None:
  91. entry = self.data[key]
  92. continue
  93. entry = entry[key]
  94. if six.PY2:
  95. entry['comment'] = salt.utils.stringutils.to_unicode(entry['comment'])
  96. else:
  97. entry['comment'] = salt.utils.stringutils.to_bytes(entry['comment'])
  98. ret = highstate.output(self.data)
  99. self.assertIn('Succeeded: 1 (changed=1)', ret)
  100. self.assertIn('Failed: 0', ret)
  101. self.assertIn('Total states run: 1', ret)
  102. # this should all pass the above tests
  103. class JsonNestedTestCase(TestCase, LoaderModuleMockMixin):
  104. '''
  105. Test cases for nested salt.output.highstate (ie orchestrations calling other orchs)
  106. '''
  107. def setup_loader_modules(self):
  108. return {
  109. highstate: {
  110. '__opts__': {
  111. 'extension_modules': '',
  112. 'color': False,
  113. 'optimization_order': [0, 1, 2],
  114. }
  115. }
  116. }
  117. def setUp(self):
  118. self.data = {
  119. 'outputter': 'highstate',
  120. 'data': {
  121. 'local_master': {
  122. 'salt_|-nested_|-state.orchestrate_|-runner': {
  123. 'comment': 'Runner function \'state.orchestrate\' executed.',
  124. 'name': 'state.orchestrate',
  125. '__orchestration__': True,
  126. 'start_time': '09:22:53.158742',
  127. 'result': True,
  128. 'duration': 980.694,
  129. '__run_num__': 0,
  130. '__jid__': '20180326092253538853',
  131. '__sls__': 'orch.test.nested',
  132. 'changes': {
  133. 'return': {
  134. 'outputter': 'highstate',
  135. 'data': {
  136. 'local_master': {
  137. 'test_|-always-passes-with-changes_|-oinaosf_|-succeed_with_changes': {
  138. 'comment': 'Success!',
  139. 'name': 'oinaosf',
  140. 'start_time': '09:22:54.128415',
  141. 'result': True,
  142. 'duration': 0.437,
  143. '__run_num__': 0,
  144. '__sls__': 'orch.test.changes',
  145. 'changes': {
  146. 'testing': {
  147. 'new': 'Something pretended to change',
  148. 'old': 'Unchanged'
  149. }
  150. },
  151. '__id__': 'always-passes-with-changes'
  152. },
  153. 'test_|-always-passes_|-fasdfasddfasdfoo_|-succeed_without_changes': {
  154. 'comment': 'Success!',
  155. 'name': 'fasdfasddfasdfoo',
  156. 'start_time': '09:22:54.128986',
  157. 'result': True,
  158. 'duration': 0.25,
  159. '__run_num__': 1,
  160. '__sls__': 'orch.test.changes',
  161. 'changes': {},
  162. '__id__': 'always-passes'
  163. }
  164. }
  165. },
  166. 'retcode': 0
  167. }
  168. },
  169. '__id__': 'nested'
  170. }
  171. }
  172. },
  173. 'retcode': 0
  174. }
  175. self.addCleanup(delattr, self, 'data')
  176. def test_nested_output(self):
  177. ret = highstate.output(self.data)
  178. self.assertIn('Succeeded: 1 (changed=1)', ret)
  179. self.assertIn('Failed: 0', ret)
  180. self.assertIn('Total states run: 1', ret)
  181. # the whitespace is relevant in this case, it is testing that it is nested
  182. self.assertIn(' ID: always-passes-with-changes', ret)
  183. self.assertIn(' Started: 09:22:54.128415', ret)
  184. self.assertIn(' Succeeded: 2 (changed=1)', ret)
  185. self.assertIn(' Failed: 0', ret)
  186. self.assertIn(' Total states run: 2', ret)