test_jobs.py 2.7 KB

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