Skip to content

Commit

Permalink
Merge pull request #50366 from jdsieci/2018.3-fix-issue50254
Browse files Browse the repository at this point in the history
2018.3 fix issue50254
  • Loading branch information
dwoz committed Nov 20, 2018
2 parents 735c9f4 + a9b9fa2 commit 60b4622
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions salt/modules/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,8 @@ def _to_words(txt):
if not src or not probe:
return no_match

src = src.rstrip('\n\r')
probe = probe.rstrip('\n\r')
if src == probe:
return equal

Expand Down Expand Up @@ -6001,6 +6003,7 @@ def list_backups(path, limit=None):
[files[x] for x in sorted(files, reverse=True)[:limit]]
)))


list_backup = salt.utils.functools.alias_function(list_backups, 'list_backup')


Expand Down Expand Up @@ -6173,6 +6176,7 @@ def delete_backup(path, backup_id):

return ret


remove_backup = salt.utils.functools.alias_function(delete_backup, 'remove_backup')


Expand Down
56 changes: 56 additions & 0 deletions tests/integration/modules/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,59 @@ def test_file_line_content(self):
with salt.utils.files.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)

def test_file_line_duplicate_insert_after(self):
"""
Test file.line duplicates line.
Issue #50254
"""
with salt.utils.files.fopen(self.myfile, 'a') as fp:
fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
self.minion_run('file.line', self.myfile, 'Goodbye',
mode='insert', after='Hello')
with salt.utils.files.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)

def test_file_line_duplicate_insert_before(self):
"""
Test file.line duplicates line.
Issue #50254
"""
with salt.utils.files.fopen(self.myfile, 'a') as fp:
fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
self.minion_run('file.line', self.myfile, 'Hello',
mode='insert', before='Goodbye')
with salt.utils.files.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)

def test_file_line_duplicate_ensure_after(self):
"""
Test file.line duplicates line.
Issue #50254
"""
with salt.utils.files.fopen(self.myfile, 'a') as fp:
fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
self.minion_run('file.line', self.myfile, 'Goodbye',
mode='ensure', after='Hello')
with salt.utils.files.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)

def test_file_line_duplicate_ensure_before(self):
"""
Test file.line duplicates line.
Issue #50254
"""
with salt.utils.files.fopen(self.myfile, 'a') as fp:
fp.write(salt.utils.stringutils.to_str('Goodbye' + os.linesep))
self.minion_run('file.line', self.myfile, 'Hello',
mode='ensure', before='Goodbye')
with salt.utils.files.fopen(self.myfile, 'r') as fp:
content = fp.read()
self.assertEqual(content, 'Hello' + os.linesep + 'Goodbye' + os.linesep)
60 changes: 60 additions & 0 deletions tests/unit/modules/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,37 @@ def test_line_insert_ensure_before(self, name):
expected = self._get_body(file_modified)
assert writelines_content[0] == expected, (writelines_content[0], expected)

@with_tempfile()
def test_line_insert_duplicate_ensure_before(self, name):
'''
Test for file.line for insertion ensuring the line is before
:return:
'''
cfg_content = '/etc/init.d/someservice restart'
file_content = os.linesep.join([
'#!/bin/bash',
'',
cfg_content,
'exit 0'
])
file_modified = os.linesep.join([
'#!/bin/bash',
'',
cfg_content,
'exit 0'
])

isfile_mock = MagicMock(side_effect=lambda x: True if x == name else DEFAULT)
with patch('os.path.isfile', isfile_mock), \
patch('os.stat', MagicMock(return_value=DummyStat())), \
patch('salt.utils.files.fopen',
mock_open(read_data=file_content)), \
patch('salt.utils.atomicfile.atomic_open',
mock_open()) as atomic_open_mock:
filemod.line(name, content=cfg_content, before='exit 0', mode='ensure')
# If file not modified no handlers in dict
assert atomic_open_mock.filehandles.get(name) is None

@with_tempfile()
def test_line_insert_ensure_before_first_line(self, name):
'''
Expand Down Expand Up @@ -1600,6 +1631,35 @@ def test_line_insert_ensure_after(self, name):
expected = self._get_body(file_modified)
assert writelines_content[0] == expected, (writelines_content[0], expected)

@with_tempfile()
def test_line_insert_duplicate_ensure_after(self, name):
'''
Test for file.line for insertion ensuring the line is after
:return:
'''
cfg_content = 'exit 0'
file_content = os.linesep.join([
'#!/bin/bash',
'/etc/init.d/someservice restart',
cfg_content
])
file_modified = os.linesep.join([
'#!/bin/bash',
'/etc/init.d/someservice restart',
cfg_content
])

isfile_mock = MagicMock(side_effect=lambda x: True if x == name else DEFAULT)
with patch('os.path.isfile', isfile_mock), \
patch('os.stat', MagicMock(return_value=DummyStat())), \
patch('salt.utils.files.fopen',
mock_open(read_data=file_content)), \
patch('salt.utils.atomicfile.atomic_open',
mock_open()) as atomic_open_mock:
filemod.line(name, content=cfg_content, after='/etc/init.d/someservice restart', mode='ensure')
# If file not modified no handlers in dict
assert atomic_open_mock.filehandles.get(name) is None

@with_tempfile()
def test_line_insert_ensure_beforeafter_twolines(self, name):
'''
Expand Down

0 comments on commit 60b4622

Please sign in to comment.