health_checker_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. class Sample1 < MonitoringHelper::HealthChecker::Backend
  4. def check_health
  5. response = MonitoringHelper::HealthChecker::Response.new
  6. response.issues << 'issue1'
  7. response
  8. end
  9. end
  10. class Sample2 < MonitoringHelper::HealthChecker::Backend
  11. def check_health
  12. response = MonitoringHelper::HealthChecker::Response.new
  13. response.issues << 'issue2'
  14. response
  15. end
  16. end
  17. RSpec.describe MonitoringHelper::HealthChecker do
  18. let(:instance) { described_class.new }
  19. describe '#check_health' do
  20. it 'returns merged responses' do
  21. allow(instance).to receive(:backends).and_return([Sample1, Sample2])
  22. expect(instance.check_health.issues).to match_array(%w[issue1 issue2])
  23. end
  24. end
  25. describe '#healthy?' do
  26. it 'returns true if response has no issues' do
  27. response = build_response
  28. allow(instance).to receive(:response).and_return(response)
  29. expect(instance).to be_healthy
  30. end
  31. it 'returns false if response has issues' do
  32. response = build_response(%w[issue])
  33. allow(instance).to receive(:response).and_return(response)
  34. expect(instance).not_to be_healthy
  35. end
  36. end
  37. describe '#message' do
  38. it 'returns success if healthy' do
  39. allow(instance).to receive(:healthy?).and_return(true)
  40. expect(instance.message).to eq 'success'
  41. end
  42. it 'returns joined issues if any' do
  43. response = build_response(%w[issue1 issue2])
  44. allow(instance).to receive_messages(healthy?: false, response: response)
  45. expect(instance.message).to eq 'issue1;issue2'
  46. end
  47. end
  48. def build_response(issues = [])
  49. response = MonitoringHelper::HealthChecker::Response.new
  50. response.issues.concat(issues)
  51. response
  52. end
  53. end