2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2013-06-27 10:51:22 +00:00
|
|
|
# Import python libs
|
2012-03-27 23:05:38 +00:00
|
|
|
import os
|
2013-04-10 12:24:41 +00:00
|
|
|
import sys
|
2013-06-24 19:06:49 +00:00
|
|
|
import tempfile
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2013-06-24 19:06:49 +00:00
|
|
|
from salttesting import skipIf
|
2013-06-27 10:51:22 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2013-08-26 10:32:39 +00:00
|
|
|
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch
|
2013-06-27 10:51:22 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-03-27 23:05:38 +00:00
|
|
|
|
2013-08-26 10:32:39 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2012-03-27 23:05:38 +00:00
|
|
|
class CMDModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
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',
|
|
|
|
'shell={0}'.format(shell)]).rstrip(),
|
|
|
|
shell)
|
2012-03-27 23:05:38 +00:00
|
|
|
|
2013-03-11 21:54:10 +00:00
|
|
|
@patch('pwd.getpwnam')
|
|
|
|
@patch('subprocess.Popen')
|
|
|
|
@patch('json.loads')
|
|
|
|
def test_os_environment_remains_intact(self, *mocks):
|
2013-03-03 06:55:19 +00:00
|
|
|
'''
|
2013-03-21 22:22:29 +00:00
|
|
|
Make sure the OS environment is not tainted after running a command
|
|
|
|
that specifies runas.
|
2013-03-03 06:55:19 +00:00
|
|
|
'''
|
|
|
|
environment = os.environ.copy()
|
2013-03-21 22:22:29 +00:00
|
|
|
loads_mock, popen_mock, getpwnam_mock = mocks
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-03-11 21:54:10 +00:00
|
|
|
popen_mock.return_value = Mock(
|
2013-04-10 12:24:41 +00:00
|
|
|
communicate=lambda *args, **kwags: ['{}', None],
|
2013-03-11 21:54:10 +00:00
|
|
|
pid=lambda: 1,
|
|
|
|
retcode=0
|
|
|
|
)
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-03-11 21:54:10 +00:00
|
|
|
loads_mock.return_value = {'data': {'USER': 'foo'}}
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-08-20 22:44:03 +00:00
|
|
|
from salt.modules import cmdmod
|
2013-03-03 06:55:19 +00:00
|
|
|
|
2013-08-20 22:44:03 +00:00
|
|
|
cmdmod.__grains__ = {'os': 'darwin'}
|
2013-04-10 12:24:41 +00:00
|
|
|
if sys.platform.startswith('freebsd'):
|
|
|
|
shell = '/bin/sh'
|
|
|
|
else:
|
|
|
|
shell = '/bin/bash'
|
|
|
|
|
2013-03-21 22:22:29 +00:00
|
|
|
try:
|
2013-08-20 22:44:03 +00:00
|
|
|
cmdmod._run('ls',
|
2013-03-21 22:22:29 +00:00
|
|
|
cwd=tempfile.gettempdir(),
|
|
|
|
runas='foobar',
|
2013-04-10 12:24:41 +00:00
|
|
|
shell=shell)
|
2013-03-21 22:15:26 +00:00
|
|
|
|
2013-03-21 22:22:29 +00:00
|
|
|
environment2 = os.environ.copy()
|
2013-03-21 22:15:26 +00:00
|
|
|
|
2013-10-24 09:42:52 +00:00
|
|
|
self.assertEqual(environment, environment2)
|
2013-03-21 22:15:26 +00:00
|
|
|
|
2013-03-21 22:22:29 +00:00
|
|
|
getpwnam_mock.assert_called_with('foobar')
|
2013-04-10 12:24:41 +00:00
|
|
|
loads_mock.assert_called_with('{}')
|
2013-03-21 22:22:29 +00:00
|
|
|
finally:
|
2013-08-20 22:44:03 +00:00
|
|
|
delattr(cmdmod, '__grains__')
|
2013-03-03 06:55:19 +00:00
|
|
|
|
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
|
|
|
|
'''
|
2013-04-10 12:24:41 +00:00
|
|
|
if sys.platform.startswith('freebsd'):
|
|
|
|
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',
|
|
|
|
'shell={0}'.format(shell)]
|
|
|
|
).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
|
|
|
|
'''
|
2012-06-04 22:40:34 +00:00
|
|
|
from salt._compat import string_types
|
2013-04-10 12:24:41 +00:00
|
|
|
|
|
|
|
if sys.platform.startswith('freebsd'):
|
|
|
|
shell = '/bin/sh'
|
|
|
|
else:
|
|
|
|
shell = '/bin/bash'
|
|
|
|
|
|
|
|
ret = self.run_function('cmd.run_all', ['echo "cheese" 1>&2',
|
|
|
|
'shell={0}'.format(shell)])
|
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))
|
2012-06-04 22:40:34 +00:00
|
|
|
self.assertTrue(isinstance(ret.get('stdout'), string_types))
|
|
|
|
self.assertTrue(isinstance(ret.get('stderr'), 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
|
|
|
|
'''
|
2013-06-19 05:51:44 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['exit 0']), 0)
|
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['exit 1']), 1)
|
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
|
|
|
|
|
|
|
def test_has_exec(self):
|
|
|
|
'''
|
|
|
|
cmd.has_exec
|
|
|
|
'''
|
|
|
|
self.assertTrue(self.run_function('cmd.has_exec', ['python']))
|
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
|
|
|
|
'''
|
|
|
|
code = '''
|
|
|
|
import sys
|
|
|
|
sys.stdout.write('cheese')
|
|
|
|
'''
|
2013-03-21 22:22:29 +00:00
|
|
|
self.assertEqual(self.run_function('cmd.exec_code',
|
|
|
|
['python', code]).rstrip(),
|
|
|
|
'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)
|
|
|
|
|
2013-04-30 17:32:21 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
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
|
|
|
|
'''
|
2013-06-24 22:53:59 +00:00
|
|
|
self.assertTrue(
|
|
|
|
'Timed out' in self.run_function(
|
|
|
|
'cmd.run', ['sleep 2 && echo hello', 'timeout=1']))
|
2013-06-11 18:41:01 +00:00
|
|
|
|
|
|
|
def test_timeout_success(self):
|
|
|
|
'''
|
|
|
|
cmd.run sufficient timeout to succeed
|
|
|
|
'''
|
2013-06-24 22:53:59 +00:00
|
|
|
self.assertTrue(
|
|
|
|
'hello' == self.run_function(
|
|
|
|
'cmd.run', ['sleep 1 && echo hello', 'timeout=2']))
|
|
|
|
|
2013-09-11 02:57:41 +00:00
|
|
|
def test_run_cwd_doesnt_exist_issue_7154(self):
|
|
|
|
'''
|
2013-09-11 03:03:35 +00:00
|
|
|
cmd.run should fail and raise
|
|
|
|
salt.exceptions.CommandExecutionError if the cwd dir does not
|
2013-09-11 02:57:41 +00:00
|
|
|
exist
|
|
|
|
'''
|
|
|
|
from salt.exceptions import CommandExecutionError
|
|
|
|
import salt.modules.cmdmod as cmdmod
|
|
|
|
cmd = 'echo OHAI'
|
|
|
|
cwd = '/path/to/nowhere'
|
|
|
|
try:
|
|
|
|
cmdmod.run_all(cmd, cwd=cwd)
|
|
|
|
except CommandExecutionError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise RuntimeError
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(CMDModuleTest)
|