2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-06-27 10:51:22 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2012-03-27 23:05:38 +00:00
|
|
|
import os
|
2013-04-10 12:24:41 +00:00
|
|
|
import sys
|
2015-03-02 02:54:22 +00:00
|
|
|
import textwrap
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.helpers import (
|
2015-03-02 02:54:22 +00:00
|
|
|
destructiveTest,
|
2017-04-04 17:57:27 +00:00
|
|
|
skip_if_binaries_missing,
|
|
|
|
skip_if_not_root
|
2015-03-02 02:54:22 +00:00
|
|
|
)
|
2013-06-27 10:51:22 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
2014-01-14 14:10:33 +00:00
|
|
|
import salt.utils
|
|
|
|
|
2017-03-06 13:53:19 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import salt.ext.six as six
|
|
|
|
|
2014-01-14 14:10:33 +00:00
|
|
|
|
|
|
|
AVAILABLE_PYTHON_EXECUTABLE = salt.utils.which_bin([
|
|
|
|
'python',
|
|
|
|
'python2',
|
|
|
|
'python2.6',
|
|
|
|
'python2.7'
|
|
|
|
|
|
|
|
])
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-03-27 23:05:38 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class CMDModuleTest(ModuleCase):
|
2012-03-27 23:05:38 +00:00
|
|
|
'''
|
|
|
|
Validate the cmd module
|
|
|
|
'''
|
|
|
|
def test_run(self):
|
|
|
|
'''
|
|
|
|
cmd.run
|
|
|
|
'''
|
2013-08-10 18:14:10 +00:00
|
|
|
shell = os.environ.get('SHELL')
|
|
|
|
if shell is None:
|
|
|
|
# Failed to get the SHELL var, don't run
|
2013-08-10 18:50:31 +00:00
|
|
|
self.skipTest('Unable to get the SHELL environment variable')
|
|
|
|
|
2012-03-27 23:05:38 +00:00
|
|
|
self.assertTrue(self.run_function('cmd.run', ['echo $SHELL']))
|
|
|
|
self.assertEqual(
|
2013-03-21 22:22:29 +00:00
|
|
|
self.run_function('cmd.run',
|
|
|
|
['echo $SHELL',
|
2015-02-23 22:04:43 +00:00
|
|
|
'shell={0}'.format(shell)],
|
|
|
|
python_shell=True).rstrip(), shell)
|
|
|
|
self.assertEqual(self.run_function('cmd.run',
|
|
|
|
['ls / | grep etc'],
|
|
|
|
python_shell=True), 'etc')
|
|
|
|
self.assertEqual(self.run_function('cmd.run',
|
|
|
|
['echo {{grains.id}} | awk "{print $1}"'],
|
|
|
|
template='jinja',
|
|
|
|
python_shell=True), 'minion')
|
|
|
|
self.assertEqual(self.run_function('cmd.run',
|
|
|
|
['grep f'],
|
|
|
|
stdin='one\ntwo\nthree\nfour\nfive\n'), 'four\nfive')
|
|
|
|
self.assertEqual(self.run_function('cmd.run',
|
|
|
|
['echo "a=b" | sed -e s/=/:/g'],
|
|
|
|
python_shell=True), 'a:b')
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_stdout(self):
|
|
|
|
'''
|
|
|
|
cmd.run_stdout
|
|
|
|
'''
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.run_stdout',
|
|
|
|
['echo "cheese"']).rstrip(),
|
|
|
|
'cheese')
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_stderr(self):
|
|
|
|
'''
|
|
|
|
cmd.run_stderr
|
|
|
|
'''
|
2014-09-04 08:23:59 +00:00
|
|
|
if sys.platform.startswith(('freebsd', 'openbsd')):
|
2013-04-10 12:24:41 +00:00
|
|
|
shell = '/bin/sh'
|
|
|
|
else:
|
|
|
|
shell = '/bin/bash'
|
|
|
|
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.run_stderr',
|
2013-04-10 12:24:41 +00:00
|
|
|
['echo "cheese" 1>&2',
|
2014-12-23 20:02:56 +00:00
|
|
|
'shell={0}'.format(shell)], python_shell=True
|
2013-04-10 12:24:41 +00:00
|
|
|
).rstrip(),
|
2013-03-21 22:22:29 +00:00
|
|
|
'cheese')
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_run_all(self):
|
|
|
|
'''
|
|
|
|
cmd.run_all
|
|
|
|
'''
|
2014-09-04 08:23:59 +00:00
|
|
|
if sys.platform.startswith(('freebsd', 'openbsd')):
|
2013-04-10 12:24:41 +00:00
|
|
|
shell = '/bin/sh'
|
|
|
|
else:
|
|
|
|
shell = '/bin/bash'
|
|
|
|
|
|
|
|
ret = self.run_function('cmd.run_all', ['echo "cheese" 1>&2',
|
2014-12-23 20:02:56 +00:00
|
|
|
'shell={0}'.format(shell)], python_shell=True)
|
2012-03-27 23:05:38 +00:00
|
|
|
self.assertTrue('pid' in ret)
|
|
|
|
self.assertTrue('retcode' in ret)
|
|
|
|
self.assertTrue('stdout' in ret)
|
|
|
|
self.assertTrue('stderr' in ret)
|
|
|
|
self.assertTrue(isinstance(ret.get('pid'), int))
|
|
|
|
self.assertTrue(isinstance(ret.get('retcode'), int))
|
2017-03-06 13:53:19 +00:00
|
|
|
self.assertTrue(isinstance(ret.get('stdout'), six.string_types))
|
|
|
|
self.assertTrue(isinstance(ret.get('stderr'), six.string_types))
|
2012-05-30 21:55:45 +00:00
|
|
|
self.assertEqual(ret.get('stderr').rstrip(), 'cheese')
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_retcode(self):
|
|
|
|
'''
|
|
|
|
cmd.retcode
|
|
|
|
'''
|
2014-12-23 20:02:56 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['exit 0'], python_shell=True), 0)
|
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['exit 1'], python_shell=True), 1)
|
2012-03-27 23:05:38 +00:00
|
|
|
|
2016-08-30 23:13:12 +00:00
|
|
|
def test_blacklist_glob(self):
|
|
|
|
'''
|
|
|
|
cmd_blacklist_glob
|
|
|
|
'''
|
|
|
|
self.assertEqual(self.run_function('cmd.run',
|
|
|
|
['bad_command --foo']).rstrip(),
|
|
|
|
'ERROR: This shell command is not permitted: "bad_command --foo"')
|
|
|
|
|
2015-03-02 02:54:22 +00:00
|
|
|
def test_script(self):
|
|
|
|
'''
|
|
|
|
cmd.script
|
|
|
|
'''
|
|
|
|
args = 'saltines crackers biscuits=yes'
|
|
|
|
script = 'salt://script.py'
|
|
|
|
ret = self.run_function('cmd.script', [script, args])
|
|
|
|
self.assertEqual(ret['stdout'], args)
|
|
|
|
|
|
|
|
def test_script_retcode(self):
|
|
|
|
'''
|
|
|
|
cmd.script_retcode
|
|
|
|
'''
|
|
|
|
script = 'salt://script.py'
|
|
|
|
ret = self.run_function('cmd.script_retcode', [script])
|
|
|
|
self.assertEqual(ret, 0)
|
|
|
|
|
|
|
|
@destructiveTest
|
|
|
|
def test_tty(self):
|
|
|
|
'''
|
|
|
|
cmd.tty
|
|
|
|
'''
|
|
|
|
for tty in ('tty0', 'pts3'):
|
|
|
|
if os.path.exists(os.path.join('/dev', tty)):
|
|
|
|
ret = self.run_function('cmd.tty', [tty, 'apply salt liberally'])
|
|
|
|
self.assertTrue('Success' in ret)
|
|
|
|
|
2014-01-14 12:30:18 +00:00
|
|
|
@skip_if_binaries_missing(['which'])
|
2012-03-27 23:05:38 +00:00
|
|
|
def test_which(self):
|
|
|
|
'''
|
|
|
|
cmd.which
|
|
|
|
'''
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.which', ['cat']).rstrip(),
|
|
|
|
self.run_function('cmd.run', ['which cat']).rstrip())
|
2012-03-27 23:05:38 +00:00
|
|
|
|
2015-03-02 02:54:22 +00:00
|
|
|
@skip_if_binaries_missing(['which'])
|
|
|
|
def test_which_bin(self):
|
|
|
|
'''
|
|
|
|
cmd.which_bin
|
|
|
|
'''
|
2017-03-17 03:07:52 +00:00
|
|
|
cmds = ['pip3', 'pip2', 'pip', 'pip-python']
|
2015-03-02 02:54:22 +00:00
|
|
|
ret = self.run_function('cmd.which_bin', [cmds])
|
|
|
|
self.assertTrue(os.path.split(ret)[1] in cmds)
|
|
|
|
|
2012-03-27 23:05:38 +00:00
|
|
|
def test_has_exec(self):
|
|
|
|
'''
|
|
|
|
cmd.has_exec
|
|
|
|
'''
|
2014-01-14 14:10:33 +00:00
|
|
|
self.assertTrue(self.run_function('cmd.has_exec',
|
|
|
|
[AVAILABLE_PYTHON_EXECUTABLE]))
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertFalse(self.run_function('cmd.has_exec',
|
|
|
|
['alllfsdfnwieulrrh9123857ygf']))
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_exec_code(self):
|
|
|
|
'''
|
|
|
|
cmd.exec_code
|
|
|
|
'''
|
2015-03-02 02:54:22 +00:00
|
|
|
code = textwrap.dedent('''\
|
|
|
|
import sys
|
|
|
|
sys.stdout.write('cheese')''')
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.exec_code',
|
2014-01-14 14:10:33 +00:00
|
|
|
[AVAILABLE_PYTHON_EXECUTABLE,
|
|
|
|
code]).rstrip(),
|
2013-03-21 22:22:29 +00:00
|
|
|
'cheese')
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-07-26 16:21:07 +00:00
|
|
|
def test_quotes(self):
|
|
|
|
'''
|
|
|
|
cmd.run with quoted command
|
|
|
|
'''
|
|
|
|
cmd = '''echo 'SELECT * FROM foo WHERE bar="baz"' '''
|
|
|
|
expected_result = 'SELECT * FROM foo WHERE bar="baz"'
|
|
|
|
result = self.run_function('cmd.run_stdout', [cmd]).strip()
|
|
|
|
self.assertEqual(result, expected_result)
|
|
|
|
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2012-07-26 16:21:07 +00:00
|
|
|
def test_quotes_runas(self):
|
|
|
|
'''
|
|
|
|
cmd.run with quoted command
|
|
|
|
'''
|
|
|
|
cmd = '''echo 'SELECT * FROM foo WHERE bar="baz"' '''
|
|
|
|
expected_result = 'SELECT * FROM foo WHERE bar="baz"'
|
2012-12-19 01:39:16 +00:00
|
|
|
|
|
|
|
try:
|
2013-03-21 22:22:29 +00:00
|
|
|
runas = os.getlogin()
|
2013-11-27 13:04:45 +00:00
|
|
|
except: # pylint: disable=W0702
|
2012-12-19 01:39:16 +00:00
|
|
|
# On some distros (notably Gentoo) os.getlogin() fails
|
|
|
|
import pwd
|
2013-03-21 22:22:29 +00:00
|
|
|
runas = pwd.getpwuid(os.getuid())[0]
|
2012-12-19 01:39:16 +00:00
|
|
|
|
2012-07-26 16:21:07 +00:00
|
|
|
result = self.run_function('cmd.run_stdout', [cmd],
|
2012-12-19 01:39:16 +00:00
|
|
|
runas=runas).strip()
|
2012-07-26 16:21:07 +00:00
|
|
|
self.assertEqual(result, expected_result)
|
|
|
|
|
2013-06-11 18:41:01 +00:00
|
|
|
def test_timeout(self):
|
|
|
|
'''
|
|
|
|
cmd.run trigger timeout
|
|
|
|
'''
|
2017-02-26 02:11:06 +00:00
|
|
|
out = self.run_function('cmd.run',
|
|
|
|
['sleep 2 && echo hello'],
|
|
|
|
f_timeout=1,
|
|
|
|
python_shell=True)
|
|
|
|
self.assertTrue('Timed out' in out)
|
2013-06-11 18:41:01 +00:00
|
|
|
|
|
|
|
def test_timeout_success(self):
|
|
|
|
'''
|
|
|
|
cmd.run sufficient timeout to succeed
|
|
|
|
'''
|
2017-02-26 02:11:06 +00:00
|
|
|
out = self.run_function('cmd.run',
|
|
|
|
['sleep 1 && echo hello'],
|
|
|
|
f_timeout=2,
|
|
|
|
python_shell=True)
|
|
|
|
self.assertEqual(out, 'hello')
|