test_maxrunning.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import logging
  4. import dateutil.parser as dateutil_parser
  5. from tests.support.mock import MagicMock, patch
  6. from tests.unit.utils.scheduler.base import SchedulerTestsBase
  7. log = logging.getLogger(__name__)
  8. class SchedulerMaxRunningTest(SchedulerTestsBase):
  9. """
  10. Validate the pkg module
  11. """
  12. def setUp(self):
  13. super(SchedulerMaxRunningTest, self).setUp()
  14. self.schedule.opts["loop_interval"] = 1
  15. def test_maxrunning_minion(self):
  16. """
  17. verify that scheduled job runs
  18. """
  19. self.schedule.opts["__role"] = "minion"
  20. job = {
  21. "schedule": {
  22. "maxrunning_minion": {
  23. "function": "test.ping",
  24. "seconds": 10,
  25. "maxrunning": 1,
  26. }
  27. }
  28. }
  29. job_data = {
  30. "function": "test.ping",
  31. "run": True,
  32. "name": "maxrunning_minion",
  33. "seconds": 10,
  34. "_seconds": 10,
  35. "jid_include": True,
  36. "maxrunning": 1,
  37. }
  38. # Add the job to the scheduler
  39. self.schedule.opts.update(job)
  40. running_data = [
  41. {
  42. "fun_args": [],
  43. "jid": "20181018165923360935",
  44. "schedule": "maxrunning_minion",
  45. "pid": 15338,
  46. "fun": "test.ping",
  47. "id": "host",
  48. }
  49. ]
  50. run_time = dateutil_parser.parse("11/29/2017 4:00pm")
  51. with patch("salt.utils.minion.running", MagicMock(return_value=running_data)):
  52. with patch(
  53. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  54. ):
  55. ret = self.schedule._check_max_running(
  56. "test.ping", job_data, self.schedule.opts, now=run_time
  57. )
  58. self.assertIn("_skip_reason", ret)
  59. self.assertEqual("maxrunning", ret["_skip_reason"])
  60. self.assertEqual(False, ret["run"])
  61. def test_maxrunning_master(self):
  62. """
  63. verify that scheduled job runs
  64. """
  65. self.schedule.opts["__role"] = "master"
  66. job = {
  67. "schedule": {
  68. "maxrunning_master": {
  69. "function": "state.orch",
  70. "args": ["test.orch_test"],
  71. "minutes": 1,
  72. "maxrunning": 1,
  73. }
  74. }
  75. }
  76. job_data = {
  77. "function": "state.orch",
  78. "fun_args": ["test.orch_test"],
  79. "run": True,
  80. "name": "maxrunning_master",
  81. "minutes": 1,
  82. "jid_include": True,
  83. "maxrunning": 1,
  84. }
  85. # Add the job to the scheduler
  86. self.schedule.opts.update(job)
  87. running_data = [
  88. {
  89. "fun_args": ["test.orch_test"],
  90. "jid": "20181018165923360935",
  91. "schedule": "maxrunning_master",
  92. "pid": 15338,
  93. "fun": "state.orch",
  94. "id": "host",
  95. }
  96. ]
  97. run_time = dateutil_parser.parse("11/29/2017 4:00pm")
  98. with patch(
  99. "salt.utils.master.get_running_jobs", MagicMock(return_value=running_data)
  100. ):
  101. with patch(
  102. "salt.utils.process.os_is_running", MagicMock(return_value=True)
  103. ):
  104. ret = self.schedule._check_max_running(
  105. "state.orch", job_data, self.schedule.opts, now=run_time
  106. )
  107. self.assertIn("_skip_reason", ret)
  108. self.assertEqual("maxrunning", ret["_skip_reason"])
  109. self.assertEqual(False, ret["run"])