File: sniffer_spec.rb

package info (click to toggle)
ruby-elasticsearch 7.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,712 kB
  • sloc: ruby: 44,120; sh: 16; makefile: 2
file content (275 lines) | stat: -rw-r--r-- 7,332 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require 'spec_helper'

describe Elasticsearch::Transport::Transport::Sniffer do
  let(:transport) do
    double('transport').tap do |t|
      allow(t).to receive(:perform_request).and_return(response)
      allow(t).to receive(:options).and_return(sniffer_timeout: 2)
    end
  end

  let(:sniffer) do
    described_class.new(transport)
  end

  let(:response) do
    double('response').tap do |r|
      allow(r).to receive(:body).and_return(raw_response)
    end
  end

  let(:raw_response) do
    { 'nodes' => { 'n1' => { 'http' => { 'publish_address' => publish_address } } } }
  end

  let(:publish_address) do
    '127.0.0.1:9250'
  end

  describe '#initialize' do
    it 'has a transport instance' do
      expect(sniffer.transport).to be(transport)
    end

    it 'inherits the sniffer timeout from the transport object' do
      expect(sniffer.timeout).to eq(2)
    end
  end

  describe '#timeout' do
    let(:sniffer) do
      described_class.new(double('transport', options: {}))
    end

    before do
      sniffer.timeout = 3
    end

    it 'allows the timeout to be configured' do
      expect(sniffer.timeout).to eq(3)
    end
  end

  describe '#hosts' do
    let(:hosts) do
      sniffer.hosts
    end

    context 'when the entire response is parsed' do
      let(:raw_response) do
        {
          "cluster_name" => "elasticsearch_test",
          "nodes" => {
              "N1" => {
                "name" => "Node 1",
                "transport_address" => "127.0.0.1:9300",
                "host" => "testhost1",
                "ip"   => "127.0.0.1",
                "version" => "7.0.0",
                "roles" => [
                  "master",
                  "data",
                  "ingest"
                ],
                "attributes" => {
                  "testattr" => "test"
                },
                "http" => {
                  "bound_address" => [
                    "[fe80::1]:9250",
                    "[::1]:9250",
                    "127.0.0.1:9250"
                  ],
                "publish_address" => "127.0.0.1:9250",
                "max_content_length_in_bytes" => 104857600
                }
              }
          }
        }
      end

      it 'parses the id' do
        expect(sniffer.hosts[0][:id]).to eq('N1')
      end

      it 'parses the name' do
        expect(sniffer.hosts[0][:name]).to eq('Node 1')
      end

      it 'parses the version' do
        expect(sniffer.hosts[0][:version]).to eq('7.0.0')
      end

      it 'parses the host' do
        expect(sniffer.hosts[0][:host]).to eq('127.0.0.1')
      end

      it 'parses the port' do
        expect(sniffer.hosts[0][:port]).to eq('9250')
      end

      it 'parses the roles' do
        expect(sniffer.hosts[0][:roles]).to eq(['master',
                                                'data',
                                                'ingest'])
      end

      it 'parses the attributes' do
        expect(sniffer.hosts[0][:attributes]).to eq('testattr' => 'test')
      end
    end

    context 'when the transport protocol does not match' do
      let(:raw_response) do
        { 'nodes' => { 'n1' => { 'foo' => { 'publish_address' => '127.0.0.1:9250' } } } }
      end

      it 'does not parse the addresses' do
        expect(hosts).to eq([])
      end
    end

    context 'when a list of nodes is returned' do
      let(:raw_response) do
        { 'nodes' => { 'n1' => { 'http' => { 'publish_address' => '127.0.0.1:9250' } },
                       'n2' => { 'http' => { 'publish_address' => '127.0.0.1:9251' } } } }
      end

      it 'parses the response' do
        expect(hosts.size).to eq(2)
      end

      it 'correctly parses the hosts' do
        expect(hosts[0][:host]).to eq('127.0.0.1')
        expect(hosts[1][:host]).to eq('127.0.0.1')
      end

      it 'correctly parses the ports' do
        expect(hosts[0][:port]).to eq('9250')
        expect(hosts[1][:port]).to eq('9251')
      end
    end

    context 'when the host and port are an ip address and port' do
      it 'parses the response' do
        expect(hosts.size).to eq(1)
      end

      it 'correctly parses the host' do
        expect(hosts[0][:host]).to eq('127.0.0.1')
      end

      it 'correctly parses the port' do
        expect(hosts[0][:port]).to eq('9250')
      end
    end

    context 'when the host and port are a hostname and port' do
      let(:publish_address) do
        'testhost1.com:9250'
      end

      let(:hosts) do
        sniffer.hosts
      end

      it 'parses the response' do
        expect(hosts.size).to eq(1)
      end

      it 'correctly parses the host' do
        expect(hosts[0][:host]).to eq('testhost1.com')
      end

      it 'correctly parses the port' do
        expect(hosts[0][:port]).to eq('9250')
      end
    end

    context 'when the host and port are in the format: hostname/ip:port' do
      let(:publish_address) do
        'example.com/127.0.0.1:9250'
      end

      it 'parses the response' do
        expect(hosts.size).to eq(1)
      end

      it 'uses the hostname' do
        expect(hosts[0][:host]).to eq('example.com')
      end

      it 'correctly parses the port' do
        expect(hosts[0][:port]).to eq('9250')
      end

      context 'when the address is IPv6' do
        let(:publish_address) do
          'example.com/[::1]:9250'
        end

        it 'parses the response' do
          expect(hosts.size).to eq(1)
        end

        it 'uses the hostname' do
          expect(hosts[0][:host]).to eq('example.com')
        end

        it 'correctly parses the port' do
          expect(hosts[0][:port]).to eq('9250')
        end
      end
    end

    context 'when the address is IPv6' do
      let(:publish_address) do
        '[::1]:9250'
      end

      it 'parses the response' do
        expect(hosts.size).to eq(1)
      end

      it 'correctly parses the host' do
        expect(hosts[0][:host]).to eq('::1')
      end

      it 'correctly parses the port' do
        expect(hosts[0][:port]).to eq('9250')
      end
    end

    context 'when the transport has :randomize_hosts option' do
      let(:raw_response) do
        { 'nodes' => { 'n1' => { 'http' => { 'publish_address' => '127.0.0.1:9250' } },
                       'n2' => { 'http' => { 'publish_address' => '127.0.0.1:9251' } } } }
      end

      before do
        allow(transport).to receive(:options).and_return(randomize_hosts: true)
      end

      it 'shuffles the list' do
        expect(hosts.size).to eq(2)
      end
    end
  end
end