test_job.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for salt.utils.job
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import salt.minion
  8. # Import Salt Libs
  9. import salt.utils.job as job
  10. # Import 3rd-party libs
  11. from salt.ext import six
  12. # Import Salt Testing Libs
  13. from tests.support.mock import patch
  14. from tests.support.unit import TestCase, skipIf
  15. class MockMasterMinion(object):
  16. def return_mock_jobs(self):
  17. return self.mock_jobs_cache
  18. opts = {"job_cache": True, "ext_job_cache": None, "master_job_cache": "foo"}
  19. mock_jobs_cache = {}
  20. returners = {
  21. "foo.save_load": lambda *args, **kwargs: True,
  22. "foo.prep_jid": lambda *args, **kwargs: True,
  23. "foo.get_load": lambda *args, **kwargs: True,
  24. "foo.returner": lambda *args, **kwargs: True,
  25. }
  26. def __init__(self, *args, **kwargs):
  27. pass
  28. class JobTest(TestCase):
  29. """
  30. Validate salt.utils.job
  31. """
  32. @skipIf(not six.PY3, "Can only assertLogs in PY3")
  33. def test_store_job_exception_handled(self):
  34. """
  35. test store_job exception handling
  36. """
  37. for func in ["foo.save_load", "foo.prep_jid", "foo.returner"]:
  38. def raise_exception(*arg, **kwarg):
  39. raise Exception("expected")
  40. with patch.object(
  41. salt.minion, "MasterMinion", MockMasterMinion
  42. ), patch.dict(MockMasterMinion.returners, {func: raise_exception}), patch(
  43. "salt.utils.verify.valid_id", return_value=True
  44. ):
  45. with self.assertLogs("salt.utils.job", level="CRITICAL") as logged:
  46. job.store_job(
  47. MockMasterMinion.opts,
  48. {
  49. "jid": "20190618090114890985",
  50. "return": {"success": True},
  51. "id": "a",
  52. },
  53. )
  54. self.assertIn(
  55. "The specified 'foo' returner threw a stack trace",
  56. logged.output[0],
  57. )