2012-03-27 23:05:38 +00:00
|
|
|
import os
|
|
|
|
import integration
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
class CMDModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the cmd module
|
|
|
|
'''
|
|
|
|
def test_run(self):
|
|
|
|
'''
|
|
|
|
cmd.run
|
|
|
|
'''
|
2012-04-27 22:13:33 +00:00
|
|
|
shell = os.environ['SHELL']
|
2012-03-27 23:05:38 +00:00
|
|
|
self.assertTrue(self.run_function('cmd.run', ['echo $SHELL']))
|
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('cmd.run',
|
2012-05-30 21:55:45 +00:00
|
|
|
['echo $SHELL', 'shell={0}'.format(shell)]).rstrip(),
|
|
|
|
shell)
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_stdout(self):
|
|
|
|
'''
|
|
|
|
cmd.run_stdout
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('cmd.run_stdout',
|
2012-05-30 21:55:45 +00:00
|
|
|
['echo "cheese"']).rstrip(),
|
|
|
|
'cheese')
|
2012-03-27 23:05:38 +00:00
|
|
|
|
|
|
|
def test_stderr(self):
|
|
|
|
'''
|
|
|
|
cmd.run_stderr
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
|
|
|
self.run_function('cmd.run_stderr',
|
2012-05-30 21:55:45 +00:00
|
|
|
['echo "cheese" 1>&2']).rstrip(),
|
|
|
|
'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
|
2012-03-27 23:05:38 +00:00
|
|
|
ret = self.run_function('cmd.run_all', ['echo "cheese" 1>&2'])
|
|
|
|
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
|
|
|
|
'''
|
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['true']), 0)
|
|
|
|
self.assertEqual(self.run_function('cmd.retcode', ['false']), 1)
|
|
|
|
|
|
|
|
def test_which(self):
|
|
|
|
'''
|
|
|
|
cmd.which
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
2012-06-19 00:17:55 +00:00
|
|
|
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']))
|
|
|
|
self.assertFalse(self.run_function(
|
|
|
|
'cmd.has_exec',
|
|
|
|
['alllfsdfnwieulrrh9123857ygf']
|
|
|
|
))
|
|
|
|
|
|
|
|
def test_exec_code(self):
|
|
|
|
'''
|
|
|
|
cmd.exec_code
|
|
|
|
'''
|
|
|
|
code = '''
|
|
|
|
import sys
|
|
|
|
sys.stdout.write('cheese')
|
|
|
|
'''
|
|
|
|
self.assertEqual(
|
2012-05-30 21:55:45 +00:00
|
|
|
self.run_function('cmd.exec_code', ['python', code]).rstrip(),
|
2012-03-27 23:05:38 +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)
|
|
|
|
|
|
|
|
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"'
|
|
|
|
result = self.run_function('cmd.run_stdout', [cmd],
|
|
|
|
runas=os.getlogin()).strip()
|
|
|
|
self.assertEqual(result, expected_result)
|
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(CMDModuleTest)
|