salt/tests/integration/shell/test_enabled.py

103 lines
3.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-04-17 18:00:01 +00:00
# Import Python Libs
from __future__ import absolute_import
import os
import textwrap
# Import Salt Testing libs
from tests.support.case import ModuleCase
from tests.support.paths import FILES
2015-04-17 18:00:01 +00:00
# Import Salt Libs
import salt.utils
STATE_DIR = os.path.join(FILES, 'file', 'base')
class EnabledTest(ModuleCase):
'''
validate the use of shell processing for cmd.run on the salt command line
and in templating
'''
cmd = ("printf '%s\n' first second third | wc -l ; "
"export SALTY_VARIABLE='saltines' && echo $SALTY_VARIABLE ; "
"echo duh &> /dev/null")
def test_shell_default_enabled(self):
'''
ensure that python_shell defaults to True for cmd.run
'''
enabled_ret = '3\nsaltines' # the result of running self.cmd in a shell
ret = self.run_function('cmd.run', [self.cmd])
self.assertEqual(ret.strip(), enabled_ret)
def test_shell_disabled(self):
'''
test shell disabled output for cmd.run
'''
disabled_ret = ('first\nsecond\nthird\n|\nwc\n-l\n;\nexport\nSALTY_VARIABLE=saltines'
'\n&&\necho\n$SALTY_VARIABLE\n;\necho\nduh\n&>\n/dev/null')
ret = self.run_function('cmd.run', [self.cmd], python_shell=False)
self.assertEqual(ret, disabled_ret)
def test_template_shell(self):
'''
Test cmd.shell works correctly when using a template.
Note: This test used to test that python_shell defaulted to True for templates
in releases before 2017.7.0. The cmd.run --> cmd.shell aliasing was removed in
2017.7.0. Templates should now be using cmd.shell.
'''
state_name = 'template_shell_enabled'
state_filename = state_name + '.sls'
state_file = os.path.join(STATE_DIR, state_filename)
enabled_ret = '3 saltines' # the result of running self.cmd in a shell
ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(enabled_ret)
try:
Clean up open filehandles (#35359) * salt/crypt.py: clean up open filehandles * salt/fileclient.py: clean up open filehandles * salt/grains/core.py: clean up open filehandles * salt/modules/cp.py: clean up open filehandles * salt/modules/data.py: clean up open filehandles * salt/modules/dnsutil.py: clean up open filehandles * salt/modules/dockerng.py: clean up open filehandles * salt/modules/inspectlib/collector.py: clean up open filehandles * salt/modules/file.py: clean up open filehandles * salt/modules/hosts.py: clean up open filehandles * salt/modules/incron.py: clean up open filehandles * salt/modules/dpkg.py: clean up open filehandles * salt/modules/linux_sysctl.py: clean up open filehandles * salt/modules/netbsd_sysctl.py: clean up open filehandles * salt/modules/network.py: clean up open filehandles * salt/modules/nftables.py: clean up open filehandles * salt/modules/openbsd_sysctl.py: clean up open filehandles * salt/modules/rh_ip.py: clean up open filehandles * salt/modules/portage_config.py: clean up open filehandles * salt/modules/status.py: clean up open filehandles * salt/modules/tls.py: clean up open filehandles * salt/modules/xapi.py: clean up open filehandles * salt/modules/x509.py: clean up open filehandles * salt/modules/virt.py: clean up open filehandles * salt/modules/zcbuildout.py: clean up open filehandles * salt/returners/local_cache.py: clean up open filehandles * salt/utils/cloud.py: clean up open filehandles * salt/states/pkgrepo.py: clean up open filehandles * salt/states/x509.py: clean up open filehandles * salt/transport/mixins/auth.py: clean up open filehandles * salt/utils/__init__.py: clean up open filehandles * salt/states/pkg.py: clean up open filehandles * salt/utils/minion.py: clean up open filehandles * salt/utils/openstack/nova.py: clean up open filehandles * salt/utils/openstack/swift.py: clean up open filehandles * salt/utils/process.py: clean up open filehandles * salt/utils/templates.py: clean up open filehandles * salt/utils/virt.py: clean up open filehandles * tests/integration/__init__.py: clean up open filehandles * tests/integration/cli/grains.py: clean up open filehandles * tests/integration/client/standard.py: clean up open filehandles * tests/integration/modules/hosts.py: clean up open filehandles * tests/unit/utils/vt_test.py: clean up open filehandles * tests/integration/shell/enabled.py: clean up open filehandles * tests/integration/states/cmd.py: clean up open filehandles * tests/integration/states/file.py: clean up open filehandles * tests/integration/states/match.py: clean up open filehandles * tests/unit/config_test.py: clean up open filehandles * tests/unit/templates/jinja_test.py: clean up open filehandles * tests/unit/utils/find_test.py: clean up open filehandles * tests/integration/modules/state.py: clean up open filehandles * Update dnsutil_test to reflect changes in fopen usage
2016-08-11 16:45:24 +00:00
with salt.utils.fopen(state_file, 'w') as fp_:
fp_.write(textwrap.dedent('''\
{{% set shell_enabled = salt['cmd.shell']("{0}").strip() %}}
shell_enabled:
test.configurable_test_state:
- name: '{{{{ shell_enabled }}}}'
'''.format(self.cmd)))
ret = self.run_function('state.sls', [state_name])
self.assertEqual(ret[ret_key]['name'], enabled_ret)
finally:
os.remove(state_file)
def test_template_default_disabled(self):
'''
test shell disabled output for templates (python_shell=False is the default
beginning with the 2017.7.0 release).
'''
state_name = 'template_shell_disabled'
state_filename = state_name + '.sls'
state_file = os.path.join(STATE_DIR, state_filename)
# the result of running self.cmd not in a shell
disabled_ret = ('first second third | wc -l ; export SALTY_VARIABLE=saltines '
'&& echo $SALTY_VARIABLE ; echo duh &> /dev/null')
ret_key = 'test_|-shell_enabled_|-{0}_|-configurable_test_state'.format(disabled_ret)
try:
Clean up open filehandles (#35359) * salt/crypt.py: clean up open filehandles * salt/fileclient.py: clean up open filehandles * salt/grains/core.py: clean up open filehandles * salt/modules/cp.py: clean up open filehandles * salt/modules/data.py: clean up open filehandles * salt/modules/dnsutil.py: clean up open filehandles * salt/modules/dockerng.py: clean up open filehandles * salt/modules/inspectlib/collector.py: clean up open filehandles * salt/modules/file.py: clean up open filehandles * salt/modules/hosts.py: clean up open filehandles * salt/modules/incron.py: clean up open filehandles * salt/modules/dpkg.py: clean up open filehandles * salt/modules/linux_sysctl.py: clean up open filehandles * salt/modules/netbsd_sysctl.py: clean up open filehandles * salt/modules/network.py: clean up open filehandles * salt/modules/nftables.py: clean up open filehandles * salt/modules/openbsd_sysctl.py: clean up open filehandles * salt/modules/rh_ip.py: clean up open filehandles * salt/modules/portage_config.py: clean up open filehandles * salt/modules/status.py: clean up open filehandles * salt/modules/tls.py: clean up open filehandles * salt/modules/xapi.py: clean up open filehandles * salt/modules/x509.py: clean up open filehandles * salt/modules/virt.py: clean up open filehandles * salt/modules/zcbuildout.py: clean up open filehandles * salt/returners/local_cache.py: clean up open filehandles * salt/utils/cloud.py: clean up open filehandles * salt/states/pkgrepo.py: clean up open filehandles * salt/states/x509.py: clean up open filehandles * salt/transport/mixins/auth.py: clean up open filehandles * salt/utils/__init__.py: clean up open filehandles * salt/states/pkg.py: clean up open filehandles * salt/utils/minion.py: clean up open filehandles * salt/utils/openstack/nova.py: clean up open filehandles * salt/utils/openstack/swift.py: clean up open filehandles * salt/utils/process.py: clean up open filehandles * salt/utils/templates.py: clean up open filehandles * salt/utils/virt.py: clean up open filehandles * tests/integration/__init__.py: clean up open filehandles * tests/integration/cli/grains.py: clean up open filehandles * tests/integration/client/standard.py: clean up open filehandles * tests/integration/modules/hosts.py: clean up open filehandles * tests/unit/utils/vt_test.py: clean up open filehandles * tests/integration/shell/enabled.py: clean up open filehandles * tests/integration/states/cmd.py: clean up open filehandles * tests/integration/states/file.py: clean up open filehandles * tests/integration/states/match.py: clean up open filehandles * tests/unit/config_test.py: clean up open filehandles * tests/unit/templates/jinja_test.py: clean up open filehandles * tests/unit/utils/find_test.py: clean up open filehandles * tests/integration/modules/state.py: clean up open filehandles * Update dnsutil_test to reflect changes in fopen usage
2016-08-11 16:45:24 +00:00
with salt.utils.fopen(state_file, 'w') as fp_:
fp_.write(textwrap.dedent('''\
{{% set shell_disabled = salt['cmd.run']("{0}") %}}
shell_enabled:
test.configurable_test_state:
- name: '{{{{ shell_disabled }}}}'
'''.format(self.cmd)))
ret = self.run_function('state.sls', [state_name])
self.assertEqual(ret[ret_key]['name'], disabled_ret)
finally:
os.remove(state_file)