test_nested.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. """
  3. Unit tests for the Nested outputter
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. # Import Salt Libs
  8. import salt.output.nested as nested
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. class NestedOutputterTestCase(TestCase, LoaderModuleMockMixin):
  13. """
  14. Test cases for salt.output.nested
  15. """
  16. def setup_loader_modules(self):
  17. return {nested: {"__opts__": {"extension_modules": "", "color": True}}}
  18. def setUp(self):
  19. # The example from the documentation for the test.arg execution function
  20. # Same function from the highstate outputter
  21. self.data = {
  22. "local": {
  23. "args": (1, "two", 3.1),
  24. "kwargs": {
  25. u"__pub_pid": 25938,
  26. "wow": {"a": 1, "b": "hello"},
  27. u"__pub_fun": "test.arg",
  28. u"__pub_jid": "20171207105927331329",
  29. u"__pub_tgt": "salt-call",
  30. "txt": "hello",
  31. },
  32. }
  33. }
  34. self.addCleanup(delattr, self, "data")
  35. def test_output_with_colors(self):
  36. # Should look exacly like that, with the default color scheme:
  37. #
  38. # local:
  39. # ----------
  40. # args:
  41. # - 1
  42. # - two
  43. # - 3.1
  44. # kwargs:
  45. # ----------
  46. # __pub_fun:
  47. # test.arg
  48. # __pub_jid:
  49. # 20171207105927331329
  50. # __pub_pid:
  51. # 25938
  52. # __pub_tgt:
  53. # salt-call
  54. # txt:
  55. # hello
  56. # wow:
  57. # ----------
  58. # a:
  59. # 1
  60. # b:
  61. # hello
  62. expected_output_str = (
  63. "\x1b[0;36mlocal\x1b[0;0m:\n \x1b[0;36m----------\x1b[0;0m\n \x1b[0;36margs\x1b[0;0m:\n"
  64. " \x1b[0;1;33m- 1\x1b[0;0m\n \x1b[0;32m- two\x1b[0;0m\n \x1b[0;1;33m- 3.1\x1b[0;0m\n"
  65. " \x1b[0;36mkwargs\x1b[0;0m:\n \x1b[0;36m----------\x1b[0;0m\n"
  66. " \x1b[0;36m__pub_fun\x1b[0;0m:\n \x1b[0;32mtest.arg\x1b[0;0m\n"
  67. " \x1b[0;36m__pub_jid\x1b[0;0m:\n \x1b[0;32m20171207105927331329\x1b[0;0m\n"
  68. " \x1b[0;36m__pub_pid\x1b[0;0m:\n \x1b[0;1;33m25938\x1b[0;0m\n"
  69. " \x1b[0;36m__pub_tgt\x1b[0;0m:\n \x1b[0;32msalt-call\x1b[0;0m\n"
  70. " \x1b[0;36mtxt\x1b[0;0m:\n \x1b[0;32mhello\x1b[0;0m\n \x1b[0;36mwow\x1b[0;0m:\n"
  71. " \x1b[0;36m----------\x1b[0;0m\n \x1b[0;36ma\x1b[0;0m:\n"
  72. " \x1b[0;1;33m1\x1b[0;0m\n \x1b[0;36mb\x1b[0;0m:\n"
  73. " \x1b[0;32mhello\x1b[0;0m"
  74. )
  75. ret = nested.output(self.data)
  76. self.assertEqual(ret, expected_output_str)
  77. def test_output_with_retcode(self):
  78. # Non-zero retcode should change the colors
  79. # Same output format as above, just different colors
  80. expected_output_str = (
  81. "\x1b[0;31mlocal\x1b[0;0m:\n \x1b[0;31m----------\x1b[0;0m\n \x1b[0;31margs\x1b[0;0m:\n"
  82. " \x1b[0;1;33m- 1\x1b[0;0m\n \x1b[0;32m- two\x1b[0;0m\n \x1b[0;1;33m- 3.1\x1b[0;0m\n"
  83. " \x1b[0;31mkwargs\x1b[0;0m:\n \x1b[0;31m----------\x1b[0;0m\n"
  84. " \x1b[0;31m__pub_fun\x1b[0;0m:\n \x1b[0;32mtest.arg\x1b[0;0m\n"
  85. " \x1b[0;31m__pub_jid\x1b[0;0m:\n \x1b[0;32m20171207105927331329\x1b[0;0m\n"
  86. " \x1b[0;31m__pub_pid\x1b[0;0m:\n \x1b[0;1;33m25938\x1b[0;0m\n"
  87. " \x1b[0;31m__pub_tgt\x1b[0;0m:\n \x1b[0;32msalt-call\x1b[0;0m\n"
  88. " \x1b[0;31mtxt\x1b[0;0m:\n \x1b[0;32mhello\x1b[0;0m\n \x1b[0;31mwow\x1b[0;0m:\n"
  89. " \x1b[0;31m----------\x1b[0;0m\n \x1b[0;31ma\x1b[0;0m:\n"
  90. " \x1b[0;1;33m1\x1b[0;0m\n \x1b[0;31mb\x1b[0;0m:\n"
  91. " \x1b[0;32mhello\x1b[0;0m"
  92. )
  93. # You can notice that in test_output_with_colors the color code is \x1b[0;36m, i.e., GREEN,
  94. # while here the color code is \x1b[0;31m, i.e., RED (failure)
  95. ret = nested.output(self.data, _retcode=1)
  96. self.assertEqual(ret, expected_output_str)
  97. def test_output_with_indent(self):
  98. # Everything must be indented by exactly two spaces
  99. # (using nested_indent=2 sent to nested.output as kwarg)
  100. expected_output_str = (
  101. " \x1b[0;36m----------\x1b[0;0m\n \x1b[0;36mlocal\x1b[0;0m:\n \x1b[0;36m----------\x1b[0;0m\n"
  102. " \x1b[0;36margs\x1b[0;0m:\n \x1b[0;1;33m- 1\x1b[0;0m\n \x1b[0;32m- two\x1b[0;0m\n"
  103. " \x1b[0;1;33m- 3.1\x1b[0;0m\n \x1b[0;36mkwargs\x1b[0;0m:\n"
  104. " \x1b[0;36m----------\x1b[0;0m\n \x1b[0;36m__pub_fun\x1b[0;0m:\n"
  105. " \x1b[0;32mtest.arg\x1b[0;0m\n \x1b[0;36m__pub_jid\x1b[0;0m:\n"
  106. " \x1b[0;32m20171207105927331329\x1b[0;0m\n \x1b[0;36m__pub_pid\x1b[0;0m:\n"
  107. " \x1b[0;1;33m25938\x1b[0;0m\n \x1b[0;36m__pub_tgt\x1b[0;0m:\n"
  108. " \x1b[0;32msalt-call\x1b[0;0m\n \x1b[0;36mtxt\x1b[0;0m:\n"
  109. " \x1b[0;32mhello\x1b[0;0m\n \x1b[0;36mwow\x1b[0;0m:\n"
  110. " \x1b[0;36m----------\x1b[0;0m\n \x1b[0;36ma\x1b[0;0m:\n"
  111. " \x1b[0;1;33m1\x1b[0;0m\n \x1b[0;36mb\x1b[0;0m:\n"
  112. " \x1b[0;32mhello\x1b[0;0m"
  113. )
  114. ret = nested.output(self.data, nested_indent=2)
  115. self.assertEqual(ret, expected_output_str)