test_maxrunning.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import
  4. import copy
  5. import logging
  6. import os
  7. import dateutil.parser as dateutil_parser
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.mixins import SaltReturnAssertsMixin
  11. # Import Salt Testing Libs
  12. from tests.support.mock import MagicMock, patch
  13. import tests.integration as integration
  14. # Import Salt libs
  15. import salt.utils.schedule
  16. from salt.modules.test import ping as ping
  17. try:
  18. import croniter # pylint: disable=W0611
  19. HAS_CRONITER = True
  20. except ImportError:
  21. HAS_CRONITER = False
  22. log = logging.getLogger(__name__)
  23. ROOT_DIR = os.path.join(integration.TMP, 'schedule-unit-tests')
  24. SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
  25. DEFAULT_CONFIG = salt.config.minion_config(None)
  26. DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
  27. DEFAULT_CONFIG['root_dir'] = ROOT_DIR
  28. DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
  29. DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
  30. DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
  31. class SchedulerMaxRunningTest(ModuleCase, SaltReturnAssertsMixin):
  32. '''
  33. Validate the pkg module
  34. '''
  35. def setUp(self):
  36. with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
  37. functions = {'test.ping': ping}
  38. self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
  39. self.schedule.opts['loop_interval'] = 1
  40. self.schedule.opts['run_schedule_jobs_in_background'] = False
  41. def tearDown(self):
  42. self.schedule.reset()
  43. def test_maxrunning_minion(self):
  44. '''
  45. verify that scheduled job runs
  46. '''
  47. self.schedule.opts['__role'] = 'minion'
  48. job = {
  49. 'schedule': {
  50. 'maxrunning_minion': {
  51. 'function': 'test.ping',
  52. 'seconds': 10,
  53. 'maxrunning': 1
  54. }
  55. }
  56. }
  57. job_data = {'function': 'test.ping',
  58. 'run': True,
  59. 'name': 'maxrunning_minion',
  60. 'seconds': 10,
  61. '_seconds': 10,
  62. 'jid_include': True,
  63. 'maxrunning': 1}
  64. # Add the job to the scheduler
  65. self.schedule.opts.update(job)
  66. running_data = [{'fun_args': [],
  67. 'jid': '20181018165923360935',
  68. 'schedule': 'maxrunning_minion',
  69. 'pid': 15338,
  70. 'fun': 'test.ping',
  71. 'id': 'host'}]
  72. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  73. with patch('salt.utils.minion.running',
  74. MagicMock(return_value=running_data)):
  75. with patch('salt.utils.process.os_is_running',
  76. MagicMock(return_value=True)):
  77. ret = self.schedule._check_max_running('test.ping',
  78. job_data,
  79. self.schedule.opts,
  80. now=run_time)
  81. self.assertIn('_skip_reason', ret)
  82. self.assertEqual('maxrunning', ret['_skip_reason'])
  83. self.assertEqual(False, ret['run'])
  84. def test_maxrunning_master(self):
  85. '''
  86. verify that scheduled job runs
  87. '''
  88. self.schedule.opts['__role'] = 'master'
  89. job = {
  90. 'schedule': {
  91. 'maxrunning_master': {
  92. 'function': 'state.orch',
  93. 'args': ['test.orch_test'],
  94. 'minutes': 1,
  95. 'maxrunning': 1
  96. }
  97. }
  98. }
  99. job_data = {'function': 'state.orch',
  100. 'fun_args': ['test.orch_test'],
  101. 'run': True,
  102. 'name': 'maxrunning_master',
  103. 'minutes': 1,
  104. 'jid_include': True,
  105. 'maxrunning': 1}
  106. # Add the job to the scheduler
  107. self.schedule.opts.update(job)
  108. running_data = [{'fun_args': ['test.orch_test'],
  109. 'jid': '20181018165923360935',
  110. 'schedule': 'maxrunning_master',
  111. 'pid': 15338,
  112. 'fun': 'state.orch',
  113. 'id': 'host'}]
  114. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  115. with patch('salt.utils.master.get_running_jobs',
  116. MagicMock(return_value=running_data)):
  117. with patch('salt.utils.process.os_is_running',
  118. MagicMock(return_value=True)):
  119. ret = self.schedule._check_max_running('state.orch',
  120. job_data,
  121. self.schedule.opts,
  122. now=run_time)
  123. self.assertIn('_skip_reason', ret)
  124. self.assertEqual('maxrunning', ret['_skip_reason'])
  125. self.assertEqual(False, ret['run'])