test_script.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 Libs
  8. import salt.config
  9. import salt.engines.script as script
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import patch
  14. from tests.support.unit import TestCase
  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 {script: {"__opts__": opts}}
  22. def test__get_serializer(self):
  23. """
  24. Test known serializer is returned or exception is raised
  25. if unknown serializer
  26. """
  27. for serializers in ("json", "yaml", "msgpack"):
  28. self.assertTrue(script._get_serializer(serializers))
  29. with self.assertRaises(CommandExecutionError):
  30. script._get_serializer("bad")
  31. def test__read_stdout(self):
  32. """
  33. Test we can yield stdout
  34. """
  35. with patch("subprocess.Popen") as popen_mock:
  36. popen_mock.stdout.readline.return_value = "test"
  37. self.assertEqual(next(script._read_stdout(popen_mock)), "test")