~cytrogen/masto-fe

ref: 296b3d1560eb47f969320f056395b53a49416e52 masto-fe/spec/lib/admin/system_check/elasticsearch_check_spec.rb -rw-r--r-- 4.4 KiB
296b3d15 — Claire Merge commit 'dc09c10fa8cc9230bf14e48d790c8f0c26043f8f' into glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# frozen_string_literal: true

require 'rails_helper'

describe Admin::SystemCheck::ElasticsearchCheck do
  subject(:check) { described_class.new(user) }

  let(:user) { Fabricate(:user) }

  it_behaves_like 'a check available to devops users'

  describe 'pass?' do
    context 'when chewy is enabled' do
      before do
        allow(Chewy).to receive(:enabled?).and_return(true)
        allow(Chewy.client.cluster).to receive(:health).and_return({ 'status' => 'green', 'number_of_nodes' => 1 })
        allow(Chewy.client.indices).to receive_messages(get_mapping: {
          AccountsIndex.index_name => AccountsIndex.mappings_hash.deep_stringify_keys,
          StatusesIndex.index_name => StatusesIndex.mappings_hash.deep_stringify_keys,
          InstancesIndex.index_name => InstancesIndex.mappings_hash.deep_stringify_keys,
          TagsIndex.index_name => TagsIndex.mappings_hash.deep_stringify_keys,
        }, get_settings: {
          'chewy_specifications' => {
            'settings' => {
              'index' => {
                'number_of_replicas' => 0,
              },
            },
          },
        })
      end

      context 'when running version is present and high enough' do
        before do
          allow(Chewy.client).to receive(:info)
            .and_return({ 'version' => { 'number' => '999.99.9' } })
        end

        it 'returns true' do
          expect(check.pass?).to be true
        end
      end

      context 'when running version is present and too low' do
        context 'when compatible version is too low' do
          before do
            allow(Chewy.client).to receive(:info)
              .and_return({ 'version' => { 'number' => '1.2.3', 'minimum_wire_compatibility_version' => '1.0' } })
          end

          it 'returns false' do
            expect(check.pass?).to be false
          end
        end

        context 'when compatible version is high enough' do
          before do
            allow(Chewy.client).to receive(:info)
              .and_return({ 'version' => { 'number' => '1.2.3', 'minimum_wire_compatibility_version' => '99.9' } })
          end

          it 'returns true' do
            expect(check.pass?).to be true
          end
        end
      end

      context 'when running version is missing' do
        before { stub_elasticsearch_error }

        it 'returns false' do
          expect(check.pass?).to be false
        end
      end
    end

    context 'when chewy is not enabled' do
      before { allow(Chewy).to receive(:enabled?).and_return(false) }

      it 'returns true' do
        expect(check.pass?).to be true
      end
    end
  end

  describe 'message' do
    before do
      allow(Chewy).to receive(:enabled?).and_return(true)
      allow(Chewy.client.cluster).to receive(:health).and_return({ 'status' => 'green', 'number_of_nodes' => 1 })
      allow(Chewy.client.indices).to receive(:get_mapping).and_return({
        AccountsIndex.index_name => AccountsIndex.mappings_hash.deep_stringify_keys,
        StatusesIndex.index_name => StatusesIndex.mappings_hash.deep_stringify_keys,
        InstancesIndex.index_name => InstancesIndex.mappings_hash.deep_stringify_keys,
        TagsIndex.index_name => TagsIndex.mappings_hash.deep_stringify_keys,
      })
    end

    context 'when running version is present' do
      before { allow(Chewy.client).to receive(:info).and_return({ 'version' => { 'number' => '1.2.3' } }) }

      it 'sends class name symbol to message instance' do
        allow(Admin::SystemCheck::Message).to receive(:new)
          .with(:elasticsearch_version_check, anything)

        check.message

        expect(Admin::SystemCheck::Message).to have_received(:new)
          .with(:elasticsearch_version_check, 'Elasticsearch 1.2.3 is running while 7.x is required')
      end
    end

    context 'when running version is missing' do
      before { stub_elasticsearch_error }

      it 'sends class name symbol to message instance' do
        allow(Admin::SystemCheck::Message).to receive(:new)
          .with(:elasticsearch_running_check)

        check.message

        expect(Admin::SystemCheck::Message).to have_received(:new)
          .with(:elasticsearch_running_check)
      end
    end
  end

  def stub_elasticsearch_error
    client = instance_double(Elasticsearch::Transport::Client)
    allow(client).to receive(:info).and_raise(Elasticsearch::Transport::Transport::Error)
    allow(Chewy).to receive(:client).and_return(client)
  end
end