elasticsearch_check_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Admin::SystemCheck::ElasticsearchCheck do
  4. subject(:check) { described_class.new(user) }
  5. let(:user) { Fabricate(:user) }
  6. it_behaves_like 'a check available to devops users'
  7. describe 'pass?' do
  8. context 'when chewy is enabled' do
  9. before do
  10. allow(Chewy).to receive(:enabled?).and_return(true)
  11. allow(Chewy.client.cluster).to receive(:health).and_return({ 'status' => 'green', 'number_of_nodes' => 1 })
  12. allow(Chewy.client.indices).to receive_messages(get_mapping: {
  13. AccountsIndex.index_name => AccountsIndex.mappings_hash.deep_stringify_keys,
  14. StatusesIndex.index_name => StatusesIndex.mappings_hash.deep_stringify_keys,
  15. PublicStatusesIndex.index_name => PublicStatusesIndex.mappings_hash.deep_stringify_keys,
  16. InstancesIndex.index_name => InstancesIndex.mappings_hash.deep_stringify_keys,
  17. TagsIndex.index_name => TagsIndex.mappings_hash.deep_stringify_keys,
  18. }, get_settings: {
  19. 'chewy_specifications' => {
  20. 'settings' => {
  21. 'index' => {
  22. 'number_of_replicas' => 0,
  23. },
  24. },
  25. },
  26. })
  27. end
  28. context 'when running version is present and high enough' do
  29. before do
  30. allow(Chewy.client).to receive(:info)
  31. .and_return({ 'version' => { 'number' => '999.99.9' } })
  32. end
  33. it 'returns true' do
  34. expect(check.pass?).to be true
  35. end
  36. end
  37. context 'when running version is present and too low' do
  38. context 'when compatible version is too low' do
  39. before do
  40. allow(Chewy.client).to receive(:info)
  41. .and_return({ 'version' => { 'number' => '1.2.3', 'minimum_wire_compatibility_version' => '1.0' } })
  42. end
  43. it 'returns false' do
  44. expect(check.pass?).to be false
  45. end
  46. end
  47. context 'when compatible version is high enough' do
  48. before do
  49. allow(Chewy.client).to receive(:info)
  50. .and_return({ 'version' => { 'number' => '1.2.3', 'minimum_wire_compatibility_version' => '99.9' } })
  51. end
  52. it 'returns true' do
  53. expect(check.pass?).to be true
  54. end
  55. end
  56. end
  57. context 'when running version is missing' do
  58. before { stub_elasticsearch_error }
  59. it 'returns false' do
  60. expect(check.pass?).to be false
  61. end
  62. end
  63. end
  64. context 'when chewy is not enabled' do
  65. before { allow(Chewy).to receive(:enabled?).and_return(false) }
  66. it 'returns true' do
  67. expect(check.pass?).to be true
  68. end
  69. end
  70. end
  71. describe 'message' do
  72. before do
  73. allow(Chewy).to receive(:enabled?).and_return(true)
  74. allow(Chewy.client.cluster).to receive(:health).and_return({ 'status' => 'green', 'number_of_nodes' => 1 })
  75. allow(Chewy.client.indices).to receive(:get_mapping).and_return({
  76. AccountsIndex.index_name => AccountsIndex.mappings_hash.deep_stringify_keys,
  77. StatusesIndex.index_name => StatusesIndex.mappings_hash.deep_stringify_keys,
  78. PublicStatusesIndex.index_name => PublicStatusesIndex.mappings_hash.deep_stringify_keys,
  79. InstancesIndex.index_name => InstancesIndex.mappings_hash.deep_stringify_keys,
  80. TagsIndex.index_name => TagsIndex.mappings_hash.deep_stringify_keys,
  81. })
  82. end
  83. context 'when running version is present' do
  84. before { allow(Chewy.client).to receive(:info).and_return({ 'version' => { 'number' => '1.2.3' } }) }
  85. it 'sends class name symbol to message instance' do
  86. allow(Admin::SystemCheck::Message).to receive(:new)
  87. .with(:elasticsearch_version_check, anything)
  88. check.message
  89. expect(Admin::SystemCheck::Message).to have_received(:new)
  90. .with(:elasticsearch_version_check, 'Elasticsearch 1.2.3 is running while 7.x is required')
  91. end
  92. end
  93. context 'when running version is missing' do
  94. before { stub_elasticsearch_error }
  95. it 'sends class name symbol to message instance' do
  96. allow(Admin::SystemCheck::Message).to receive(:new)
  97. .with(:elasticsearch_running_check)
  98. check.message
  99. expect(Admin::SystemCheck::Message).to have_received(:new)
  100. .with(:elasticsearch_running_check)
  101. end
  102. end
  103. end
  104. def stub_elasticsearch_error
  105. client = instance_double(Elasticsearch::Client)
  106. allow(client).to receive(:info).and_raise(Elasticsearch::Transport::Transport::Error)
  107. allow(Chewy).to receive(:client).and_return(client)
  108. end
  109. end