mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Added unit tests reproducing issue
This commit is contained in:
parent
3ffa3921b7
commit
110f74f3ba
1 changed files with 60 additions and 0 deletions
|
@ -1526,6 +1526,37 @@ class FilemodLineTests(TestCase, LoaderModuleMockMixin):
|
|||
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):
|
||||
'''
|
||||
|
@ -1600,6 +1631,35 @@ class FilemodLineTests(TestCase, LoaderModuleMockMixin):
|
|||
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):
|
||||
'''
|
||||
|
|
Loading…
Add table
Reference in a new issue