scheduler_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe MonitoringHelper::HealthChecker::Scheduler do
  4. let(:instance) { described_class.new }
  5. describe '#check_health' do
  6. before do
  7. allow(instance).to receive(:last_execution)
  8. allow(instance).to receive(:none_running)
  9. allow(instance).to receive(:failed_jobs)
  10. instance.check_health
  11. end
  12. it 'checks last_execution' do
  13. expect(instance).to have_received(:last_execution)
  14. end
  15. it 'checks none_running' do
  16. expect(instance).to have_received(:none_running)
  17. end
  18. it 'checks failed_jobs' do
  19. expect(instance).to have_received(:failed_jobs)
  20. end
  21. end
  22. describe '#last_execution' do
  23. it 'does nothing if no schedulers active' do
  24. allow(instance).to receive(:last_execution_scope).and_return([])
  25. instance.send(:last_execution)
  26. expect(instance.response.issues).to be_blank
  27. end
  28. it 'does nothing if schedulers are on time' do
  29. scheduler = create(:scheduler, last_run: 5.minutes.ago)
  30. allow(instance).to receive(:last_execution_scope).and_return([scheduler])
  31. instance.send(:last_execution)
  32. expect(instance.response.issues).to be_blank
  33. end
  34. it 'adds issue if scheduler is late' do
  35. scheduler = create(:scheduler, last_run: 50.minutes.ago)
  36. allow(instance).to receive(:last_execution_scope).and_return([scheduler])
  37. instance.send(:last_execution)
  38. expect(instance.response.issues.first).to start_with('scheduler may not run')
  39. end
  40. it 'adds single issue if multiple schedulers are late' do
  41. scheduler = create(:scheduler, last_run: 50.minutes.ago)
  42. scheduler2 = create(:scheduler, last_run: 30.minutes.ago)
  43. allow(instance).to receive(:last_execution_scope).and_return([scheduler, scheduler2])
  44. instance.send(:last_execution)
  45. expect(instance.response.issues.count).to be 1
  46. end
  47. end
  48. describe '#last_execution_scope' do
  49. it 'returns active schedulers with last run timestamp' do
  50. Scheduler.destroy_all
  51. _scheduler = create(:scheduler, last_run: nil)
  52. scheduler2 = create(:scheduler, last_run: 30.minutes.ago)
  53. _scheduler3 = create(:scheduler, last_run: 30.minutes.ago, active: false)
  54. expect(instance.send(:last_execution_scope)).to eq [scheduler2]
  55. end
  56. end
  57. describe '#last_execution_on_time?' do
  58. it 'returns true if scheduler is within execution tolerance' do
  59. scheduler = create(:scheduler, last_run: 5.minutes.ago)
  60. expect(instance.send(:last_execution_on_time?, scheduler)).to be_truthy
  61. end
  62. it 'returns true if last run is beyond execution tolerance but period moves it within' do
  63. scheduler = create(:scheduler, last_run: 15.minutes.ago)
  64. expect(instance.send(:last_execution_on_time?, scheduler)).to be_truthy
  65. end
  66. it 'returns false if scheduler is beyond execution tolerance' do
  67. scheduler = create(:scheduler, last_run: 50.minutes.ago)
  68. expect(instance.send(:last_execution_on_time?, scheduler)).to be_falsey
  69. end
  70. context 'with timeplan' do
  71. it 'returns true if timeplan scheduler was not skipped' do
  72. travel_to Time.current.beginning_of_day
  73. scheduler = create(:scheduler, :timeplan, last_run: 55.minutes.ago)
  74. expect(instance.send(:last_execution_on_time?, scheduler)).to be_truthy
  75. end
  76. # https://github.com/zammad/zammad/issues/4079
  77. it 'returns true if timeplan scheduler was slightly late only' do
  78. travel_to Time.current.beginning_of_day - 10.minutes
  79. scheduler = create(:scheduler, :timeplan, last_run: 6.hours.ago)
  80. expect(instance.send(:last_execution_on_time?, scheduler)).to be_truthy
  81. end
  82. it 'returns true if timeplan scheduler was skipped once' do
  83. travel_to Time.current.noon
  84. scheduler = create(:scheduler, :timeplan, last_run: 1.day.ago)
  85. expect(instance.send(:last_execution_on_time?, scheduler)).to be_truthy
  86. end
  87. it 'returns false if timeplan scheduler was skipped twice' do
  88. travel_to Time.current.noon
  89. scheduler = create(:scheduler, :timeplan, last_run: 2.days.ago)
  90. expect(instance.send(:last_execution_on_time?, scheduler)).to be_falsey
  91. end
  92. end
  93. end
  94. describe '#none_running' do
  95. it 'does nothing if any schedulers were run' do
  96. Scheduler.all.sample.update! last_run: Time.current
  97. instance.send(:none_running)
  98. expect(instance.response.issues).to be_blank
  99. end
  100. it 'adds issue if no schedulers were run' do
  101. Scheduler.update_all last_run: nil
  102. instance.send(:none_running)
  103. expect(instance.response.issues.first).to eq 'scheduler not running'
  104. end
  105. end
  106. describe '#failed_jobs' do
  107. it 'does nothing if no failed jobs' do
  108. allow(Scheduler).to receive(:failed_jobs).and_return([])
  109. instance.send(:failed_jobs)
  110. expect(instance.response.issues).to be_blank
  111. end
  112. context 'with a failed job' do
  113. let(:scheduler) { create(:scheduler) }
  114. before do
  115. allow(Scheduler).to receive(:failed_jobs).and_return([scheduler])
  116. instance.send(:failed_jobs)
  117. end
  118. it 'adds issue' do
  119. expect(instance.response.issues.first).to start_with('Failed to run')
  120. end
  121. it 'adds action' do
  122. expect(instance.response.actions.first).to eq :restart_failed_jobs
  123. end
  124. end
  125. end
  126. end