test_enabled.py 4.4 KB

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