1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
# -*- coding: utf-8 -*-
# Import python libs
import os
import sys
import tempfile
# Import Salt Testing libs
from salttesting import skipIf
from salttesting.helpers import ensure_in_syspath
from salttesting.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch
ensure_in_syspath('../../')
try:
from salttesting.helpers import skip_if_binaries_missing
except ImportError:
from integration import skip_if_binaries_missing
# Import salt libs
import integration
import salt.utils
AVAILABLE_PYTHON_EXECUTABLE = salt.utils.which_bin([
'python',
'python2',
'python2.6',
'python2.7'
])
@skipIf(NO_MOCK, NO_MOCK_REASON)
class CMDModuleTest(integration.ModuleCase):
'''
Validate the cmd module
'''
def test_run(self):
'''
cmd.run
'''
shell = os.environ.get('SHELL')
if shell is None:
# Failed to get the SHELL var, don't run
self.skipTest('Unable to get the SHELL environment variable')
self.assertTrue(self.run_function('cmd.run', ['echo $SHELL']))
self.assertEqual(
self.run_function('cmd.run',
['echo $SHELL',
'shell={0}'.format(shell)]).rstrip(),
shell)
@patch('pwd.getpwnam')
@patch('subprocess.Popen')
@patch('json.loads')
def test_os_environment_remains_intact(self,
loads_mock,
popen_mock,
getpwnam_mock):
'''
Make sure the OS environment is not tainted after running a command
that specifies runas.
'''
environment = os.environ.copy()
popen_mock.return_value = Mock(
communicate=lambda *args, **kwags: ['{}', None],
pid=lambda: 1,
retcode=0
)
loads_mock.return_value = {'data': {'USER': 'foo'}}
from salt.modules import cmdmod
cmdmod.__grains__ = {'os': 'darwin'}
if sys.platform.startswith('freebsd'):
shell = '/bin/sh'
else:
shell = '/bin/bash'
try:
cmdmod._run('ls',
cwd=tempfile.gettempdir(),
runas='foobar',
shell=shell)
environment2 = os.environ.copy()
self.assertEqual(environment, environment2)
getpwnam_mock.assert_called_with('foobar')
loads_mock.assert_called_with('{}')
finally:
delattr(cmdmod, '__grains__')
def test_stdout(self):
'''
cmd.run_stdout
'''
self.assertEqual(self.run_function('cmd.run_stdout',
['echo "cheese"']).rstrip(),
'cheese')
def test_stderr(self):
'''
cmd.run_stderr
'''
if sys.platform.startswith('freebsd'):
shell = '/bin/sh'
else:
shell = '/bin/bash'
self.assertEqual(self.run_function('cmd.run_stderr',
['echo "cheese" 1>&2',
'shell={0}'.format(shell)]
).rstrip(),
'cheese')
def test_run_all(self):
'''
cmd.run_all
'''
from salt._compat import string_types
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)])
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))
self.assertTrue(isinstance(ret.get('stdout'), string_types))
self.assertTrue(isinstance(ret.get('stderr'), string_types))
self.assertEqual(ret.get('stderr').rstrip(), 'cheese')
def test_retcode(self):
'''
cmd.retcode
'''
self.assertEqual(self.run_function('cmd.retcode', ['exit 0']), 0)
self.assertEqual(self.run_function('cmd.retcode', ['exit 1']), 1)
@skip_if_binaries_missing(['which'])
def test_which(self):
'''
cmd.which
'''
self.assertEqual(self.run_function('cmd.which', ['cat']).rstrip(),
self.run_function('cmd.run', ['which cat']).rstrip())
def test_has_exec(self):
'''
cmd.has_exec
'''
self.assertTrue(self.run_function('cmd.has_exec',
[AVAILABLE_PYTHON_EXECUTABLE]))
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(self.run_function('cmd.exec_code',
[AVAILABLE_PYTHON_EXECUTABLE,
code]).rstrip(),
'cheese')
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)
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
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"'
try:
runas = os.getlogin()
except: # pylint: disable=W0702
# On some distros (notably Gentoo) os.getlogin() fails
import pwd
runas = pwd.getpwuid(os.getuid())[0]
result = self.run_function('cmd.run_stdout', [cmd],
runas=runas).strip()
self.assertEqual(result, expected_result)
def test_timeout(self):
'''
cmd.run trigger timeout
'''
self.assertTrue(
'Timed out' in self.run_function(
'cmd.run', ['sleep 2 && echo hello', 'timeout=1']))
def test_timeout_success(self):
'''
cmd.run sufficient timeout to succeed
'''
self.assertTrue(
'hello' == self.run_function(
'cmd.run', ['sleep 1 && echo hello', 'timeout=2']))
def test_run_cwd_doesnt_exist_issue_7154(self):
'''
cmd.run should fail and raise
salt.exceptions.CommandExecutionError if the cwd dir does not
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
if __name__ == '__main__':
from integration import run_tests
run_tests(CMDModuleTest)
|