test_enabled.py 4.1 KB

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