test_script.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. '''
  3. unit tests for the script engine
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import patch
  11. # Import Salt Libs
  12. import salt.config
  13. import salt.engines.script as script
  14. from salt.exceptions import CommandExecutionError
  15. class EngineScriptTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.engine.script
  18. '''
  19. def setup_loader_modules(self):
  20. opts = salt.config.DEFAULT_MASTER_OPTS
  21. return {
  22. script: {
  23. '__opts__': opts
  24. }
  25. }
  26. def test__get_serializer(self):
  27. '''
  28. Test known serializer is returned or exception is raised
  29. if unknown serializer
  30. '''
  31. for serializers in ('json', 'yaml', 'msgpack'):
  32. self.assertTrue(script._get_serializer(serializers))
  33. with self.assertRaises(CommandExecutionError):
  34. script._get_serializer('bad')
  35. def test__read_stdout(self):
  36. '''
  37. Test we can yield stdout
  38. '''
  39. with patch('subprocess.Popen') as popen_mock:
  40. popen_mock.stdout.readline.return_value = 'test'
  41. self.assertEqual(next(script._read_stdout(popen_mock)), 'test')