|
8 | 8 |
|
9 | 9 | context "registration" do
|
10 | 10 |
|
11 |
| - let(:plugin) { LogStash::Plugin.lookup("filter", "elasticsearch").new({}) } |
12 |
| - before do |
13 |
| - allow(plugin).to receive(:test_connection!) |
| 11 | + let(:plugin_class) { LogStash::Plugin.lookup("filter", "elasticsearch") } |
| 12 | + let(:plugin) { plugin_class.new(config) } |
| 13 | + let(:config) { Hash.new } |
| 14 | + |
| 15 | + context 'with defaults' do |
| 16 | + before do |
| 17 | + allow(plugin).to receive(:test_connection!) |
| 18 | + end |
| 19 | + |
| 20 | + it "should not raise an exception" do |
| 21 | + expect {plugin.register}.to_not raise_error |
| 22 | + end |
14 | 23 | end
|
15 | 24 |
|
16 |
| - it "should not raise an exception" do |
17 |
| - expect {plugin.register}.to_not raise_error |
| 25 | + context 'hosts' do |
| 26 | + let(:config) do |
| 27 | + super().merge( |
| 28 | + 'hosts' => hosts |
| 29 | + ) |
| 30 | + end |
| 31 | + let(:hosts) do |
| 32 | + fail NotImplementedError, 'spec or spec group must define `hosts`.' |
| 33 | + end |
| 34 | + |
| 35 | + let(:client_stub) { double(:client).as_null_object } |
| 36 | + let(:logger_stub) { double(:logger).as_null_object } |
| 37 | + |
| 38 | + before(:each) do |
| 39 | + allow(plugin).to receive(:logger).and_return(logger_stub) |
| 40 | + end |
| 41 | + |
| 42 | + context 'with schema://hostname' do |
| 43 | + let(:hosts) { ['http://foo.local', 'http://bar.local'] } |
| 44 | + |
| 45 | + it 'creates client with URIs that do not include a port' do |
| 46 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 47 | + expect(options).to include :hosts |
| 48 | + expect(options[:hosts]).to be_an Array |
| 49 | + expect(options[:hosts]).to include(having_attributes(host: 'foo.local', scheme: 'http', port: nil)) |
| 50 | + expect(options[:hosts]).to include(having_attributes(host: 'bar.local', scheme: 'http', port: nil)) |
| 51 | + end.and_return(client_stub) |
| 52 | + |
| 53 | + plugin.register |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'with `ssl => true`' do |
| 58 | + let(:config) { super().merge('ssl' => 'true') } |
| 59 | + context 'and one or more explicitly-http hosts' do |
| 60 | + let(:hosts) { ['https://foo.local', 'http://bar.local'] } |
| 61 | + |
| 62 | + it 'raises an exception' do |
| 63 | + expect { plugin.register }.to raise_error(LogStash::ConfigurationError) |
| 64 | + end |
| 65 | + |
| 66 | + it 'emits a helpful log message' do |
| 67 | + plugin.register rescue nil |
| 68 | + expect(plugin.logger).to have_received(:error).with(match(/force SSL/)) |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'and all explicitly-https hosts' do |
| 73 | + let(:hosts) { ['https://foo.local', 'https://bar.local'] } |
| 74 | + |
| 75 | + it 'sets the schemas on all to https' do |
| 76 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 77 | + expect(options).to include :hosts |
| 78 | + expect(options[:hosts]).to be_an Array |
| 79 | + options[:hosts].each do |host| |
| 80 | + expect(host).to be_an URI |
| 81 | + expect(host.scheme).to eq 'https' |
| 82 | + end |
| 83 | + end.and_return(client_stub) |
| 84 | + |
| 85 | + plugin.register |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + context 'and one or more schemaless hosts' do |
| 90 | + let(:hosts) { ['https://foo.local', 'bar.local'] } |
| 91 | + |
| 92 | + it 'sets the schemas on all to https' do |
| 93 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 94 | + expect(options).to include :hosts |
| 95 | + expect(options[:hosts]).to be_an Array |
| 96 | + options[:hosts].each do |host| |
| 97 | + expect(host).to be_an URI |
| 98 | + expect(host.scheme).to eq 'https' |
| 99 | + end |
| 100 | + end.and_return(client_stub) |
| 101 | + |
| 102 | + plugin.register |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context 'with one or more ipv6 hostnames' do |
| 107 | + let(:hosts) { ['[::1]', '[::2]:9201', 'https://[::3]:9202', '::4'] } |
| 108 | + it 'defaults to the http protocol' do |
| 109 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 110 | + expect(options).to include :hosts |
| 111 | + expect(options[:hosts]).to be_an Array |
| 112 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: '[::1]', port: nil)) |
| 113 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: '[::2]', port: 9201)) |
| 114 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: '[::3]', port: 9202)) |
| 115 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: '[::4]', port: nil)) |
| 116 | + end.and_return(client_stub) |
| 117 | + |
| 118 | + plugin.register |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + { |
| 124 | + 'with `ssl => false' => {'ssl' => 'false'}, |
| 125 | + 'without `ssl` directive' => {} |
| 126 | + }.each do |context_string, config_override| |
| 127 | + context(context_string) do |
| 128 | + let(:config) { super().merge(config_override) } |
| 129 | + |
| 130 | + context 'with a mix of http and https hosts' do |
| 131 | + let(:hosts) { ['https://foo.local', 'http://bar.local'] } |
| 132 | + it 'does not modify the protocol' do |
| 133 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 134 | + expect(options).to include :hosts |
| 135 | + expect(options[:hosts]).to be_an Array |
| 136 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: 'foo.local', port: nil)) |
| 137 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: 'bar.local', port: nil)) |
| 138 | + end.and_return(client_stub) |
| 139 | + |
| 140 | + plugin.register |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + context 'with https-only hosts' do |
| 145 | + let(:hosts) { ['https://foo.local', 'https://bar.local'] } |
| 146 | + it 'does not modify the protocol' do |
| 147 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 148 | + expect(options).to include :hosts |
| 149 | + expect(options[:hosts]).to be_an Array |
| 150 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: 'foo.local', port: nil)) |
| 151 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: 'bar.local', port: nil)) |
| 152 | + end.and_return(client_stub) |
| 153 | + |
| 154 | + plugin.register |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + context 'with http-only hosts' do |
| 159 | + let(:hosts) { ['http://foo.local', 'http://bar.local'] } |
| 160 | + it 'does not modify the protocol' do |
| 161 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 162 | + expect(options).to include :hosts |
| 163 | + expect(options[:hosts]).to be_an Array |
| 164 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: 'foo.local', port: nil)) |
| 165 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: 'bar.local', port: nil)) |
| 166 | + end.and_return(client_stub) |
| 167 | + |
| 168 | + plugin.register |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + context 'with one or more schemaless hosts' do |
| 173 | + let(:hosts) { ['foo.local', 'bar.local' ] } |
| 174 | + it 'defaults to the http protocol' do |
| 175 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 176 | + expect(options).to include :hosts |
| 177 | + expect(options[:hosts]).to be_an Array |
| 178 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: 'foo.local', port: nil)) |
| 179 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: 'bar.local', port: nil)) |
| 180 | + end.and_return(client_stub) |
| 181 | + |
| 182 | + plugin.register |
| 183 | + end |
| 184 | + end |
| 185 | + |
| 186 | + context 'with one or more square-bracketed ipv6 literals' do |
| 187 | + let(:hosts) { ['[::1]', '[::2]:9201', 'http://[::3]','https://[::4]:9202', '::5'] } |
| 188 | + it 'defaults to the http protocol' do |
| 189 | + expect(::Elasticsearch::Client).to receive(:new) do |options| |
| 190 | + expect(options).to include :hosts |
| 191 | + expect(options[:hosts]).to be_an Array |
| 192 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: '[::1]', port: nil)) |
| 193 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: '[::2]', port: 9201)) |
| 194 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: '[::3]', port: nil)) |
| 195 | + expect(options[:hosts]).to include(having_attributes(scheme: 'https', host: '[::4]', port: 9202)) |
| 196 | + expect(options[:hosts]).to include(having_attributes(scheme: 'http', host: '[::5]', port: nil)) |
| 197 | + end.and_return(client_stub) |
| 198 | + |
| 199 | + plugin.register |
| 200 | + end |
| 201 | + end |
| 202 | + end |
| 203 | + end |
18 | 204 | end
|
19 | 205 | end
|
20 | 206 |
|
|
0 commit comments