test_enabled.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.runtime import RUNTIME_VARS
  10. # Import Salt Libs
  11. import salt.utils.platform
  12. import salt.utils.files
  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 = ("printf '%s\n' first second third | wc -l ; "
  19. "export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
  20. "echo duh &> /dev/null")
  21. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  22. def test_shell_default_enabled(self):
  23. '''
  24. ensure that python_shell defaults to True for cmd.run
  25. '''
  26. enabled_ret = '3\nsaltines' # the result of running self.cmd in a shell
  27. ret = self.run_function('cmd.run', [self.cmd])
  28. self.assertEqual(ret.strip(), enabled_ret)
  29. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  30. def test_shell_disabled(self):
  31. '''
  32. test shell disabled output for cmd.run
  33. '''
  34. disabled_ret = ('first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines'
  35. '\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null')
  36. ret = self.run_function('cmd.run', [self.cmd], python_shell=False)
  37. self.assertEqual(ret, disabled_ret)
  38. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  39. def test_template_shell(self):
  40. '''
  41. Test cmd.shell works correctly when using a template.
  42. Note: This test used to test that python_shell defaulted to True for templates
  43. in releases before 2017.7.0. The cmd.run --> cmd.shell aliasing was removed in
  44. 2017.7.0. Templates should now be using cmd.shell.
  45. '''
  46. state_name = 'template_shell_enabled'
  47. state_filename = state_name + '.sls'
  48. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  49. enabled_ret = '3 saltines' # the result of running self.cmd in a shell
  50. ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(enabled_ret)
  51. try:
  52. with salt.utils.files.fopen(state_file, 'w') as fp_:
  53. fp_.write(textwrap.dedent('''\
  54. {{% set shell_enabled = salt['cmd.shell']("{0}").strip() %}}
  55. shell_enabled:
  56. test.configurable_test_state:
  57. - name: '{{{{ shell_enabled }}}}'
  58. '''.format(self.cmd)))
  59. ret = self.run_function('state.sls', [state_name])
  60. self.assertEqual(ret[ret_key]['name'], enabled_ret)
  61. finally:
  62. os.remove(state_file)
  63. @skipIf(salt.utils.platform.is_windows(), 'Skip on Windows OS')
  64. def test_template_default_disabled(self):
  65. '''
  66. test shell disabled output for templates (python_shell=False is the default
  67. beginning with the 2017.7.0 release).
  68. '''
  69. state_name = 'template_shell_disabled'
  70. state_filename = state_name + '.sls'
  71. state_file = os.path.join(RUNTIME_VARS.BASE_FILES, state_filename)
  72. # the result of running self.cmd not in a shell
  73. disabled_ret = ('first second third | wc -l ; export SALTY_VARIABLE=saltines '
  74. '&& echo $SALTY_VARIABLE ; echo duh &> /dev/null')
  75. ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(disabled_ret)
  76. try:
  77. with salt.utils.files.fopen(state_file, 'w') as fp_:
  78. fp_.write(textwrap.dedent('''\
  79. {{% set shell_disabled = salt['cmd.run']("{0}") %}}
  80. shell_enabled:
  81. test.configurable_test_state:
  82. - name: '{{{{ shell_disabled }}}}'
  83. '''.format(self.cmd)))
  84. ret = self.run_function('state.sls', [state_name])
  85. self.assertEqual(ret[ret_key]['name'], disabled_ret)
  86. finally:
  87. os.remove(state_file)