Merge pull request #38487 from gtmanfred/2016.3

Fix crontab issues with spaces
This commit is contained in:
Mike Place 2017-01-01 13:33:29 -07:00 committed by GitHub
commit ec60f9c721
3 changed files with 40 additions and 12 deletions

View file

@ -336,9 +336,16 @@ def list_tab(user):
comment = comment_line
else:
comment += '\n' + comment_line
elif len(line.split()) > 5:
elif line.find('=') > 0 and (' ' not in line or line.index('=') < line.index(' ')):
# Appears to be a ENV setup line
comps = line.split('=', 1)
dat = {}
dat['name'] = comps[0]
dat['value'] = comps[1]
ret['env'].append(dat)
elif len(line.split(' ')) > 5:
# Appears to be a standard cron line
comps = line.split()
comps = line.split(' ')
dat = {'minute': comps[0],
'hour': comps[1],
'daymonth': comps[2],
@ -354,13 +361,6 @@ def list_tab(user):
identifier = None
comment = None
commented_cron_job = False
elif line.find('=') > 0:
# Appears to be a ENV setup line
comps = line.split('=')
dat = {}
dat['name'] = comps[0]
dat['value'] = ' '.join(comps[1:])
ret['env'].append(dat)
else:
ret['pre'].append(line)
return ret

View file

@ -294,7 +294,7 @@ def present(name,
.. versionadded:: 2016.3.0
'''
name = ' '.join(name.strip().split())
name = name.strip()
if identifier is False:
identifier = name
ret = {'changes': {},
@ -381,7 +381,7 @@ def absent(name,
### cannot be removed from the function definition, otherwise the use
### of unsupported arguments will result in a traceback.
name = ' '.join(name.strip().split())
name = name.strip()
if identifier is False:
identifier = name
ret = {'name': name,
@ -703,7 +703,7 @@ def env_absent(name,
the root user
'''
name = ' '.join(name.strip().split())
name = name.strip()
ret = {'name': name,
'result': True,
'changes': {},

View file

@ -28,6 +28,12 @@ STUB_CRON_TIMESTAMP = {'minute': '1',
STUB_SIMPLE_RAW_CRON = '5 0 * * * /tmp/no_script.sh'
STUB_SIMPLE_CRON_DICT = {'pre': ['5 0 * * * /tmp/no_script.sh'], 'crons': [], 'env': [], 'special': []}
STUB_CRON_SPACES = """
# Lines below here are managed by Salt, do not edit
TEST_VAR="a string with plenty of spaces"
# SALT_CRON_IDENTIFIER:echo "must be double spaced"
11 * * * * echo "must be double spaced"
"""
__grains__ = {}
L = '# Lines below here are managed by Salt, do not edit\n'
@ -528,6 +534,28 @@ class CronTestCase(TestCase):
'pre': ['# An unmanaged commented cron job', '#0 * * * * /bin/true'],
'special': []})
@patch('salt.modules.cron.raw_cron', new=MagicMock(return_value=STUB_CRON_SPACES))
def test_cron_extra_spaces(self):
'''
Issue #38449
'''
self.maxDiff = None
with patch.dict(cron.__grains__, {'os': None}):
ret = cron.list_tab('root')
eret = {'crons': [{'cmd': 'echo "must be double spaced"',
'comment': '',
'commented': False,
'daymonth': '*',
'dayweek': '*',
'hour': '*',
'identifier': 'echo "must be double spaced"',
'minute': '11',
'month': '*'}],
'env': [{'name': 'TEST_VAR', 'value': '"a string with plenty of spaces"'}],
'pre': [''],
'special': []}
self.assertEqual(eret, ret)
@patch('salt.modules.cron.raw_cron',
new=MagicMock(side_effect=[
(L + '\n'),