mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #48830 from garethgreenaway/48659_file_grep_glob_fix
[2018.3] Fixes to file.grep
This commit is contained in:
commit
e23ba01cd2
2 changed files with 117 additions and 2 deletions
|
@ -6213,6 +6213,16 @@ def grep(path,
|
|||
'''
|
||||
path = os.path.expanduser(path)
|
||||
|
||||
# Backup the path in case the glob returns nothing
|
||||
_path = path
|
||||
path = glob.glob(path)
|
||||
|
||||
# If the list is empty no files exist
|
||||
# so we revert back to the original path
|
||||
# so the result is an error.
|
||||
if not path:
|
||||
path = _path
|
||||
|
||||
split_opts = []
|
||||
for opt in opts:
|
||||
try:
|
||||
|
@ -6227,7 +6237,10 @@ def grep(path,
|
|||
)
|
||||
split_opts.extend(split)
|
||||
|
||||
cmd = ['grep'] + split_opts + [pattern, path]
|
||||
if isinstance(path, list):
|
||||
cmd = ['grep'] + split_opts + [pattern] + path
|
||||
else:
|
||||
cmd = ['grep'] + split_opts + [pattern, path]
|
||||
try:
|
||||
ret = __salt__['cmd.run_all'](cmd, python_shell=False)
|
||||
except (IOError, OSError) as exc:
|
||||
|
|
|
@ -30,7 +30,7 @@ import salt.utils.stringutils
|
|||
import salt.modules.file as filemod
|
||||
import salt.modules.config as configmod
|
||||
import salt.modules.cmdmod as cmdmod
|
||||
from salt.exceptions import CommandExecutionError
|
||||
from salt.exceptions import CommandExecutionError, SaltInvocationError
|
||||
from salt.utils.jinja import SaltCacheLoader
|
||||
|
||||
SED_CONTENT = '''test
|
||||
|
@ -523,6 +523,108 @@ class FileBlockReplaceTestCase(TestCase, LoaderModuleMockMixin):
|
|||
)
|
||||
|
||||
|
||||
class FileGrepTestCase(TestCase, LoaderModuleMockMixin):
|
||||
def setup_loader_modules(self):
|
||||
return {
|
||||
filemod: {
|
||||
'__salt__': {
|
||||
'config.manage_mode': configmod.manage_mode,
|
||||
'cmd.run': cmdmod.run,
|
||||
'cmd.run_all': cmdmod.run_all
|
||||
},
|
||||
'__opts__': {
|
||||
'test': False,
|
||||
'file_roots': {'base': 'tmp'},
|
||||
'pillar_roots': {'base': 'tmp'},
|
||||
'cachedir': 'tmp',
|
||||
'grains': {},
|
||||
},
|
||||
'__grains__': {'kernel': 'Linux'},
|
||||
'__utils__': {'files.is_text': MagicMock(return_value=True)},
|
||||
}
|
||||
}
|
||||
|
||||
MULTILINE_STRING = textwrap.dedent('''\
|
||||
Lorem ipsum dolor sit amet, consectetur
|
||||
adipiscing elit. Nam rhoncus enim ac
|
||||
bibendum vulputate.
|
||||
''')
|
||||
|
||||
MULTILINE_STRING = os.linesep.join(MULTILINE_STRING.splitlines())
|
||||
|
||||
def setUp(self):
|
||||
self.tfile = tempfile.NamedTemporaryFile(delete=False, mode='w+')
|
||||
self.tfile.write(self.MULTILINE_STRING)
|
||||
self.tfile.close()
|
||||
|
||||
def tearDown(self):
|
||||
os.remove(self.tfile.name)
|
||||
del self.tfile
|
||||
|
||||
def test_grep_query_exists(self):
|
||||
result = filemod.grep(self.tfile.name,
|
||||
'Lorem ipsum')
|
||||
|
||||
self.assertTrue(result, None)
|
||||
self.assertTrue(result['retcode'] == 0)
|
||||
self.assertTrue(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
|
||||
self.assertTrue(result['stderr'] == '')
|
||||
|
||||
def test_grep_query_not_exists(self):
|
||||
result = filemod.grep(self.tfile.name,
|
||||
'Lorem Lorem')
|
||||
|
||||
self.assertTrue(result['retcode'] == 1)
|
||||
self.assertTrue(result['stdout'] == '')
|
||||
self.assertTrue(result['stderr'] == '')
|
||||
|
||||
def test_grep_query_exists_with_opt(self):
|
||||
result = filemod.grep(self.tfile.name,
|
||||
'Lorem ipsum',
|
||||
'-i')
|
||||
|
||||
self.assertTrue(result, None)
|
||||
self.assertTrue(result['retcode'] == 0)
|
||||
self.assertTrue(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
|
||||
self.assertTrue(result['stderr'] == '')
|
||||
|
||||
def test_grep_query_not_exists_opt(self):
|
||||
result = filemod.grep(self.tfile.name,
|
||||
'Lorem Lorem',
|
||||
'-v')
|
||||
|
||||
self.assertTrue(result['retcode'] == 0)
|
||||
self.assertTrue(result['stdout'] == FileGrepTestCase.MULTILINE_STRING)
|
||||
self.assertTrue(result['stderr'] == '')
|
||||
|
||||
def test_grep_query_too_many_opts(self):
|
||||
with self.assertRaisesRegex(SaltInvocationError, '^Passing multiple command line arg') as cm:
|
||||
result = filemod.grep(self.tfile.name,
|
||||
'Lorem Lorem',
|
||||
'-i -b2')
|
||||
|
||||
def test_grep_query_exists_wildcard(self):
|
||||
_file = '{0}*'.format(self.tfile.name)
|
||||
result = filemod.grep(_file,
|
||||
'Lorem ipsum')
|
||||
|
||||
self.assertTrue(result, None)
|
||||
self.assertTrue(result['retcode'] == 0)
|
||||
self.assertTrue(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
|
||||
self.assertTrue(result['stderr'] == '')
|
||||
|
||||
def test_grep_file_not_exists_wildcard(self):
|
||||
_file = '{0}-junk*'.format(self.tfile.name)
|
||||
result = filemod.grep(_file,
|
||||
'Lorem ipsum')
|
||||
|
||||
self.assertTrue(result, None)
|
||||
self.assertFalse(result['retcode'] == 0)
|
||||
self.assertFalse(result['stdout'] == 'Lorem ipsum dolor sit amet, consectetur')
|
||||
_expected_stderr = 'grep: {0}-junk*: No such file or directory'.format(self.tfile.name)
|
||||
self.assertTrue(result['stderr'] == _expected_stderr)
|
||||
|
||||
|
||||
class FileModuleTestCase(TestCase, LoaderModuleMockMixin):
|
||||
def setup_loader_modules(self):
|
||||
return {
|
||||
|
|
Loading…
Add table
Reference in a new issue