1
0

test_maxrunning.py 4.9 KB

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