1
0

test_maxrunning.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. def tearDown(self):
  41. self.schedule.reset()
  42. def test_maxrunning_minion(self):
  43. '''
  44. verify that scheduled job runs
  45. '''
  46. self.schedule.opts['__role'] = 'minion'
  47. job = {
  48. 'schedule': {
  49. 'maxrunning_minion': {
  50. 'function': 'test.ping',
  51. 'seconds': 10,
  52. 'maxrunning': 1
  53. }
  54. }
  55. }
  56. job_data = {'function': 'test.ping',
  57. 'run': True,
  58. 'name': 'maxrunning_minion',
  59. 'seconds': 10,
  60. '_seconds': 10,
  61. 'jid_include': True,
  62. 'maxrunning': 1}
  63. # Add the job to the scheduler
  64. self.schedule.opts.update(job)
  65. running_data = [{'fun_args': [],
  66. 'jid': '20181018165923360935',
  67. 'schedule': 'maxrunning_minion',
  68. 'pid': 15338,
  69. 'fun': 'test.ping',
  70. 'id': 'host'}]
  71. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  72. with patch('salt.utils.minion.running',
  73. MagicMock(return_value=running_data)):
  74. with patch('salt.utils.process.os_is_running',
  75. MagicMock(return_value=True)):
  76. ret = self.schedule._check_max_running('test.ping',
  77. job_data,
  78. self.schedule.opts,
  79. now=run_time)
  80. self.assertIn('_skip_reason', ret)
  81. self.assertEqual('maxrunning', ret['_skip_reason'])
  82. self.assertEqual(False, ret['run'])
  83. def test_maxrunning_master(self):
  84. '''
  85. verify that scheduled job runs
  86. '''
  87. self.schedule.opts['__role'] = 'master'
  88. job = {
  89. 'schedule': {
  90. 'maxrunning_master': {
  91. 'function': 'state.orch',
  92. 'args': ['test.orch_test'],
  93. 'minutes': 1,
  94. 'maxrunning': 1
  95. }
  96. }
  97. }
  98. job_data = {'function': 'state.orch',
  99. 'fun_args': ['test.orch_test'],
  100. 'run': True,
  101. 'name': 'maxrunning_master',
  102. 'minutes': 1,
  103. 'jid_include': True,
  104. 'maxrunning': 1}
  105. # Add the job to the scheduler
  106. self.schedule.opts.update(job)
  107. running_data = [{'fun_args': ['test.orch_test'],
  108. 'jid': '20181018165923360935',
  109. 'schedule': 'maxrunning_master',
  110. 'pid': 15338,
  111. 'fun': 'state.orch',
  112. 'id': 'host'}]
  113. run_time = dateutil_parser.parse('11/29/2017 4:00pm')
  114. with patch('salt.utils.master.get_running_jobs',
  115. MagicMock(return_value=running_data)):
  116. with patch('salt.utils.process.os_is_running',
  117. MagicMock(return_value=True)):
  118. ret = self.schedule._check_max_running('state.orch',
  119. job_data,
  120. self.schedule.opts,
  121. now=run_time)
  122. self.assertIn('_skip_reason', ret)
  123. self.assertEqual('maxrunning', ret['_skip_reason'])
  124. self.assertEqual(False, ret['run'])