1
0

test_nested.py 6.1 KB

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