1
0

test_output.py 6.6 KB

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