Write a unit test demonstrating stack trace in #31135

Here is the stack trace that happens when running file.line with
mode=replace on a file that exists, but is empty, as described in
the bug report:

unit.modules.file_test.FileModuleTestCase.test_replace_line_in_empty_file  .................................................
   Traceback (most recent call last):
     File "/root/SaltStack/salt/tests/unit/modules/file_test.py", line 593, in test_replace_line_in_empty_file
       mode='replace'))
     File "/root/SaltStack/salt/salt/modules/file.py", line 1523, in line
       for line in body.split(os.linesep)])
   TypeError: expected a character buffer object
This commit is contained in:
rallytime 2016-11-03 14:57:10 -06:00
parent 80a99c4cc5
commit 94a00c66eb

View file

@ -571,6 +571,32 @@ class FileModuleTestCase(TestCase):
saltenv='base')
self.assertEqual(ret, 'This is a templated file.')
def test_replace_line_in_empty_file(self):
'''
Tests that when calling file.line with ``mode=replace``,
the function doesn't stack trace if the file is empty.
Should return ``False``.
See Issue #31135.
'''
# Create an empty temporary named file
empty_file = tempfile.NamedTemporaryFile(delete=False,
mode='w+')
# Assert that the file was created and is empty
self.assertEqual(os.stat(empty_file.name).st_size, 0)
# Now call the function on the empty file and assert
# the return is False instead of stack-tracing
self.assertFalse(filemod.line(empty_file.name,
content='foo',
match='bar',
mode='replace'))
# Close and remove the file
empty_file.close()
os.remove(empty_file.name)
if __name__ == '__main__':
from integration import run_tests