test_jobs.py 2.8 KB

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