test_output.py 6.9 KB

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