test_output.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Nicole Thomas <nicole@saltstack.com>
  4. '''
  5. # Import Salt Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import traceback
  9. # Import Salt Testing Libs
  10. from tests.support.case import ShellCase
  11. from tests.support.mixins import RUNTIME_VARS
  12. # Import Salt libs
  13. import salt.config
  14. import salt.utils.yaml
  15. from salt.output import display_output
  16. from salt.ext import six
  17. class OutputReturnTest(ShellCase):
  18. '''
  19. Integration tests to ensure outputters return their expected format.
  20. Tests against situations where the loader might not be returning the
  21. right outputter even though it was explicitly requested.
  22. '''
  23. def test_output_json(self):
  24. '''
  25. Tests the return of json-formatted data
  26. '''
  27. ret = self.run_call('test.ping --out=json')
  28. self.assertIn('{', ret)
  29. self.assertIn('"local": true', ''.join(ret))
  30. self.assertIn('}', ''.join(ret))
  31. def test_output_nested(self):
  32. '''
  33. Tests the return of nested-formatted data
  34. '''
  35. expected = ['local:', ' True']
  36. ret = self.run_call('test.ping --out=nested')
  37. self.assertEqual(ret, expected)
  38. def test_output_quiet(self):
  39. '''
  40. Tests the return of an out=quiet query
  41. '''
  42. expected = []
  43. ret = self.run_call('test.ping --out=quiet')
  44. self.assertEqual(ret, expected)
  45. def test_output_pprint(self):
  46. '''
  47. Tests the return of pprint-formatted data
  48. '''
  49. expected = ["{u'local': True}"] if six.PY2 else ["{'local': True}"]
  50. ret = self.run_call('test.ping --out=pprint')
  51. self.assertEqual(ret, expected)
  52. def test_output_raw(self):
  53. '''
  54. Tests the return of raw-formatted data
  55. '''
  56. expected = ["{u'local': True}"] if six.PY2 else ["{'local': True}"]
  57. ret = self.run_call('test.ping --out=raw')
  58. self.assertEqual(ret, expected)
  59. def test_output_txt(self):
  60. '''
  61. Tests the return of txt-formatted data
  62. '''
  63. expected = ['local: True']
  64. ret = self.run_call('test.ping --out=txt')
  65. self.assertEqual(ret, expected)
  66. def test_output_yaml(self):
  67. '''
  68. Tests the return of yaml-formatted data
  69. '''
  70. expected = ['local: true']
  71. ret = self.run_call('test.ping --out=yaml')
  72. self.assertEqual(ret, expected)
  73. def test_output_yaml_namespaced_dict_wrapper(self):
  74. '''
  75. Tests the ability to dump a NamespacedDictWrapper instance, as used in
  76. magic dunders like __grains__ and __pillar__
  77. See https://github.com/saltstack/salt/issues/49269
  78. '''
  79. dumped_yaml = '\n'.join(self.run_call('grains.items --out=yaml'))
  80. loaded_yaml = salt.utils.yaml.safe_load(dumped_yaml)
  81. # We just want to check that the dumped YAML loades as a dict with a
  82. # single top-level key, we don't care about the real contents.
  83. assert isinstance(loaded_yaml, dict)
  84. assert list(loaded_yaml) == ['local']
  85. def test_output_unicodebad(self):
  86. '''
  87. Tests outputter reliability with utf8
  88. '''
  89. opts = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
  90. opts['output_file'] = os.path.join(
  91. RUNTIME_VARS.TMP,
  92. 'outputtest'
  93. )
  94. data = {'foo': {'result': False,
  95. 'aaa': 'azerzaeréééé',
  96. 'comment': u'ééééàààà'}}
  97. try:
  98. # this should not raises UnicodeEncodeError
  99. display_output(data, opts=opts)
  100. except Exception:
  101. # display trace in error message for debugging on jenkins
  102. trace = traceback.format_exc()
  103. sentinel = object()
  104. old_max_diff = getattr(self, 'maxDiff', sentinel)
  105. try:
  106. self.maxDiff = None
  107. self.assertEqual(trace, '')
  108. finally:
  109. if old_max_diff is sentinel:
  110. delattr(self, 'maxDiff')
  111. else:
  112. self.maxDiff = old_max_diff
  113. def test_output_highstate(self):
  114. '''
  115. Regression tests for the highstate outputter. Calls a basic state with various
  116. flags. Each comparison should be identical when successful.
  117. '''
  118. # Test basic highstate output. No frills.
  119. expected = ['minion:', ' ID: simple-ping', ' Function: module.run',
  120. ' Name: test.ping', ' Result: True',
  121. ' Comment: Module function test.ping executed',
  122. ' Changes: ', ' ret:', ' True',
  123. 'Summary for minion', 'Succeeded: 1 (changed=1)', 'Failed: 0',
  124. 'Total states run: 1']
  125. state_run = self.run_salt('"minion" state.sls simple-ping')
  126. for expected_item in expected:
  127. self.assertIn(expected_item, state_run)
  128. # Test highstate output while also passing --out=highstate.
  129. # This is a regression test for Issue #29796
  130. state_run = self.run_salt('"minion" state.sls simple-ping --out=highstate')
  131. for expected_item in expected:
  132. self.assertIn(expected_item, state_run)
  133. # Test highstate output when passing --static and running a state function.
  134. # See Issue #44556.
  135. state_run = self.run_salt('"minion" state.sls simple-ping --static')
  136. for expected_item in expected:
  137. self.assertIn(expected_item, state_run)
  138. # Test highstate output when passing --static and --out=highstate.
  139. # See Issue #44556.
  140. state_run = self.run_salt('"minion" state.sls simple-ping --static --out=highstate')
  141. for expected_item in expected:
  142. self.assertIn(expected_item, state_run)
  143. def test_output_highstate_falls_back_nested(self):
  144. '''
  145. Tests outputter when passing --out=highstate with a non-state call. This should
  146. fall back to "nested" output.
  147. '''
  148. expected = ['minion:', ' True']
  149. ret = self.run_salt('"minion" test.ping --out=highstate')
  150. self.assertEqual(ret, expected)
  151. def test_static_simple(self):
  152. '''
  153. Tests passing the --static option with a basic test.ping command. This
  154. should be the "nested" output.
  155. '''
  156. expected = ['minion:', ' True']
  157. ret = self.run_salt('"minion" test.ping --static')
  158. self.assertEqual(ret, expected)