test_enabled.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  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. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  33. def test_shell_disabled(self):
  34. """
  35. test shell disabled output for cmd.run
  36. """
  37. disabled_ret = (
  38. "first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines"
  39. "\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null"
  40. )
  41. ret = self.run_function("cmd.run", [self.cmd], python_shell=False)
  42. self.assertEqual(ret, disabled_ret)
  43. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  44. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  45. def test_template_shell(self):
  46. """
  47. Test cmd.shell works correctly when using a template.
  48. Note: This test used to test that python_shell defaulted to True for templates
  49. in releases before 2017.7.0. The cmd.run --> cmd.shell aliasing was removed in
  50. 2017.7.0. Templates should now be using cmd.shell.
  51. """
  52. state_name = "template_shell_enabled"
  53. state_filename = state_name + ".sls"
  54. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  55. enabled_ret = "3 saltines" # the result of running self.cmd in a shell
  56. ret_key = "test_|-shell_enabled_|-{0}_|-configurable_test_state".format(
  57. enabled_ret
  58. )
  59. try:
  60. with salt.utils.files.fopen(state_file, "w") as fp_:
  61. fp_.write(
  62. textwrap.dedent(
  63. """\
  64. {{% set shell_enabled = salt['cmd.shell']("{0}").strip() %}}
  65. shell_enabled:
  66. test.configurable_test_state:
  67. - name: '{{{{ shell_enabled }}}}'
  68. """.format(
  69. self.cmd
  70. )
  71. )
  72. )
  73. ret = self.run_function("state.sls", [state_name])
  74. self.assertEqual(ret[ret_key]["name"], enabled_ret)
  75. finally:
  76. os.remove(state_file)
  77. @skipIf(salt.utils.platform.is_windows(), "Skip on Windows OS")
  78. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  79. def test_template_default_disabled(self):
  80. """
  81. test shell disabled output for templates (python_shell=False is the default
  82. beginning with the 2017.7.0 release).
  83. """
  84. state_name = "template_shell_disabled"
  85. state_filename = state_name + ".sls"
  86. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  87. # the result of running self.cmd not in a shell
  88. disabled_ret = (
  89. "first second third | wc -l ; export SALTY_VARIABLE=saltines "
  90. "&& echo $SALTY_VARIABLE ; echo duh &> /dev/null"
  91. )
  92. ret_key = "test_|-shell_enabled_|-{0}_|-configurable_test_state".format(
  93. disabled_ret
  94. )
  95. try:
  96. with salt.utils.files.fopen(state_file, "w") as fp_:
  97. fp_.write(
  98. textwrap.dedent(
  99. """\
  100. {{% set shell_disabled = salt['cmd.run']("{0}") %}}
  101. shell_enabled:
  102. test.configurable_test_state:
  103. - name: '{{{{ shell_disabled }}}}'
  104. """.format(
  105. self.cmd
  106. )
  107. )
  108. )
  109. ret = self.run_function("state.sls", [state_name])
  110. self.assertEqual(ret[ret_key]["name"], disabled_ret)
  111. finally:
  112. os.remove(state_file)