test_filebuffer.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Pedro Algarvio (pedro@algarvio.me)
  4. tests.unit.utils.filebuffer_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, unicode_literals, print_function
  9. import os
  10. # Import Salt Testing libs
  11. from tests.support.unit import TestCase
  12. from tests.support.paths import BASE_FILES
  13. # Import salt libs
  14. from salt.utils.filebuffer import BufferedReader, InvalidFileMode
  15. class TestFileBuffer(TestCase):
  16. def test_read_only_mode(self):
  17. with self.assertRaises(InvalidFileMode):
  18. BufferedReader('/tmp/foo', mode='a')
  19. with self.assertRaises(InvalidFileMode):
  20. BufferedReader('/tmp/foo', mode='ab')
  21. with self.assertRaises(InvalidFileMode):
  22. BufferedReader('/tmp/foo', mode='w')
  23. with self.assertRaises(InvalidFileMode):
  24. BufferedReader('/tmp/foo', mode='wb')
  25. def test_issue_51309(self):
  26. '''
  27. https://github.com/saltstack/salt/issues/51309
  28. '''
  29. file_name = os.path.join(BASE_FILES, 'grail', 'scene33')
  30. def find_value(text):
  31. stripped_text = text.strip()
  32. try:
  33. with BufferedReader(file_name) as breader:
  34. for chunk in breader:
  35. if stripped_text in chunk:
  36. return True
  37. return False
  38. except (IOError, OSError):
  39. return False
  40. self.assertTrue(find_value('We have the Holy Hand Grenade'))