1
0

test_enabled.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import textwrap
  3. import pytest
  4. import salt.utils.files
  5. import salt.utils.platform
  6. from tests.support.case import ModuleCase
  7. from tests.support.helpers import slowTest
  8. from tests.support.runtests import RUNTIME_VARS
  9. from tests.support.unit import skipIf
  10. @pytest.mark.windows_whitelisted
  11. class EnabledTest(ModuleCase):
  12. """
  13. validate the use of shell processing for cmd.run on the salt command line
  14. and in templating
  15. """
  16. cmd = (
  17. "printf '%s\\n' first second third | wc -l ; "
  18. "export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
  19. "echo duh &> /dev/null"
  20. )
  21. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  22. @skipIf(salt.utils.platform.is_freebsd(), "Skip on FreeBSD")
  23. def test_shell_default_enabled(self):
  24. """
  25. ensure that python_shell defaults to True for cmd.run
  26. """
  27. enabled_ret = "3\nsaltines" # the result of running self.cmd in a shell
  28. ret = self.run_function("cmd.run", [self.cmd])
  29. self.assertEqual(ret.strip(), enabled_ret)
  30. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  31. def test_shell_disabled(self):
  32. """
  33. test shell disabled output for cmd.run
  34. """
  35. disabled_ret = (
  36. "first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines"
  37. "\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null"
  38. )
  39. ret = self.run_function("cmd.run", [self.cmd], python_shell=False)
  40. self.assertEqual(ret, disabled_ret)
  41. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  42. @skipIf(salt.utils.platform.is_freebsd(), "Skip on FreeBSD")
  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_|-{}_|-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_|-{}_|-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)