1
0

test_jobs.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. """
  3. unit tests for the jobs runner
  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.runners.jobs as jobs
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import patch
  13. from tests.support.unit import TestCase
  14. class JobsTest(TestCase, LoaderModuleMockMixin):
  15. """
  16. Validate the jobs runner
  17. """
  18. def setup_loader_modules(self):
  19. return {
  20. jobs: {
  21. "__opts__": {"ext_job_cache": None, "master_job_cache": "local_cache"}
  22. }
  23. }
  24. def test_list_jobs_with_search_target(self):
  25. """
  26. test jobs.list_jobs runner with search_target args
  27. """
  28. mock_jobs_cache = {
  29. "20160524035503086853": {
  30. "Arguments": [],
  31. "Function": "test.ping",
  32. "StartTime": "2016, May 24 03:55:03.086853",
  33. "Target": "node-1-1.com",
  34. "Target-type": "glob",
  35. "User": "root",
  36. },
  37. "20160524035524895387": {
  38. "Arguments": [],
  39. "Function": "test.ping",
  40. "StartTime": "2016, May 24 03:55:24.895387",
  41. "Target": ["node-1-2.com", "node-1-1.com"],
  42. "Target-type": "list",
  43. "User": "sudo_ubuntu",
  44. },
  45. }
  46. def return_mock_jobs():
  47. return mock_jobs_cache
  48. class MockMasterMinion(object):
  49. returners = {"local_cache.get_jids": return_mock_jobs}
  50. def __init__(self, *args, **kwargs):
  51. pass
  52. returns = {
  53. "all": mock_jobs_cache,
  54. "node-1-1.com": mock_jobs_cache,
  55. "node-1-2.com": {
  56. "20160524035524895387": mock_jobs_cache["20160524035524895387"]
  57. },
  58. "non-existant": {},
  59. }
  60. with patch.object(salt.minion, "MasterMinion", MockMasterMinion):
  61. self.assertEqual(jobs.list_jobs(), returns["all"])
  62. self.assertEqual(
  63. jobs.list_jobs(search_target=["node-1-1*", "node-1-2*"]), returns["all"]
  64. )
  65. self.assertEqual(
  66. jobs.list_jobs(search_target="node-1-1.com"), returns["node-1-1.com"]
  67. )
  68. self.assertEqual(
  69. jobs.list_jobs(search_target="node-1-2.com"), returns["node-1-2.com"]
  70. )
  71. self.assertEqual(
  72. jobs.list_jobs(search_target="non-existant"), returns["non-existant"]
  73. )