test_enabled.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import os
  4. import textwrap
  5. import pytest
  6. import salt.utils.files
  7. import salt.utils.platform
  8. from tests.support.case import ModuleCase
  9. from tests.support.helpers import slowTest
  10. from tests.support.runtests import RUNTIME_VARS
  11. from tests.support.unit import skipIf
  12. @pytest.mark.windows_whitelisted
  13. class EnabledTest(ModuleCase):
  14. """
  15. validate the use of shell processing for cmd.run on the salt command line
  16. and in templating
  17. """
  18. cmd = (
  19. "printf '%s\\n' first second third | wc -l ; "
  20. "export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
  21. "echo duh &> /dev/null"
  22. )
  23. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  24. def test_shell_default_enabled(self):
  25. """
  26. ensure that python_shell defaults to True for cmd.run
  27. """
  28. enabled_ret = "3\nsaltines" # the result of running self.cmd in a shell
  29. ret = self.run_function("cmd.run", [self.cmd])
  30. self.assertEqual(ret.strip(), enabled_ret)
  31. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  32. def test_shell_disabled(self):
  33. """
  34. test shell disabled output for cmd.run
  35. """
  36. disabled_ret = (
  37. "first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines"
  38. "\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null"
  39. )
  40. ret = self.run_function("cmd.run", [self.cmd], python_shell=False)
  41. self.assertEqual(ret, disabled_ret)
  42. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  43. def test_template_shell(self):
  44. """
  45. Test cmd.shell works correctly when using a template.
  46. Note: This test used to test that python_shell defaulted to True for templates
  47. in releases before 2017.7.0. The cmd.run --> cmd.shell aliasing was removed in
  48. 2017.7.0. Templates should now be using cmd.shell.
  49. """
  50. state_name = "template_shell_enabled"
  51. state_filename = state_name + ".sls"
  52. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  53. enabled_ret = "3 saltines" # the result of running self.cmd in a shell
  54. ret_key = "test_|-shell_enabled_|-{0}_|-configurable_test_state".format(
  55. enabled_ret
  56. )
  57. try:
  58. with salt.utils.files.fopen(state_file, "w") as fp_:
  59. fp_.write(
  60. textwrap.dedent(
  61. """\
  62. {{% set shell_enabled = salt['cmd.shell']("{0}").strip() %}}
  63. shell_enabled:
  64. test.configurable_test_state:
  65. - name: '{{{{ shell_enabled }}}}'
  66. """.format(
  67. self.cmd
  68. )
  69. )
  70. )
  71. ret = self.run_function("state.sls", [state_name])
  72. self.assertEqual(ret[ret_key]["name"], enabled_ret)
  73. finally:
  74. os.remove(state_file)
  75. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  76. @slowTest
  77. def test_template_default_disabled(self):
  78. """
  79. test shell disabled output for templates (python_shell=False is the default
  80. beginning with the 2017.7.0 release).
  81. """
  82. state_name = "template_shell_disabled"
  83. state_filename = state_name + ".sls"
  84. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  85. # the result of running self.cmd not in a shell
  86. disabled_ret = (
  87. "first second third | wc -l ; export SALTY_VARIABLE=saltines "
  88. "&& echo $SALTY_VARIABLE ; echo duh &> /dev/null"
  89. )
  90. ret_key = "test_|-shell_enabled_|-{0}_|-configurable_test_state".format(
  91. disabled_ret
  92. )
  93. try:
  94. with salt.utils.files.fopen(state_file, "w") as fp_:
  95. fp_.write(
  96. textwrap.dedent(
  97. """\
  98. {{% set shell_disabled = salt['cmd.run']("{0}") %}}
  99. shell_enabled:
  100. test.configurable_test_state:
  101. - name: '{{{{ shell_disabled }}}}'
  102. """.format(
  103. self.cmd
  104. )
  105. )
  106. )
  107. ret = self.run_function("state.sls", [state_name])
  108. self.assertEqual(ret[ret_key]["name"], disabled_ret)
  109. finally:
  110. os.remove(state_file)