spec/inputs/elasticsearch_spec.rb in logstash-input-elasticsearch-4.11.0 vs spec/inputs/elasticsearch_spec.rb in logstash-input-elasticsearch-4.12.0
- old
+ new
@@ -16,10 +16,38 @@
describe LogStash::Inputs::Elasticsearch, :ecs_compatibility_support do
let(:plugin) { described_class.new(config) }
let(:queue) { Queue.new }
+ before(:each) do
+ Elasticsearch::Client.send(:define_method, :ping) { } # define no-action ping method
+ end
+
+ context "register" do
+ let(:config) do
+ {
+ "schedule" => "* * * * * UTC"
+ }
+ end
+
+ context "against authentic Elasticsearch" do
+ it "should not raise an exception" do
+ expect { plugin.register }.to_not raise_error
+ end
+ end
+
+ context "against not authentic Elasticsearch" do
+ before(:each) do
+ Elasticsearch::Client.send(:define_method, :ping) { raise Elasticsearch::UnsupportedProductError.new("Fake error") } # define error ping method
+ end
+
+ it "should raise ConfigurationError" do
+ expect { plugin.register }.to raise_error(LogStash::ConfigurationError)
+ end
+ end
+ end
+
it_behaves_like "an interruptible input plugin" do
let(:esclient) { double("elasticsearch-client") }
let(:config) do
{
"schedule" => "* * * * * UTC"
@@ -36,10 +64,11 @@
"_source" => { "message" => ["ohayo"] }
}
allow(esclient).to receive(:search) { { "hits" => { "hits" => [hit] } } }
allow(esclient).to receive(:scroll) { { "hits" => { "hits" => [hit] } } }
allow(esclient).to receive(:clear_scroll).and_return(nil)
+ allow(esclient).to receive(:ping)
end
end
ecs_compatibility_matrix(:disabled, :v1, :v8) do |ecs_select|
@@ -94,10 +123,11 @@
client = Elasticsearch::Client.new
expect(Elasticsearch::Client).to receive(:new).with(any_args).and_return(client)
expect(client).to receive(:search).with(any_args).and_return(mock_response)
expect(client).to receive(:scroll).with({ :body => { :scroll_id => "cXVlcnlUaGVuRmV0Y2g" }, :scroll=> "1m" }).and_return(mock_scroll_response)
expect(client).to receive(:clear_scroll).and_return(nil)
+ expect(client).to receive(:ping)
end
it 'creates the events from the hits' do
event = input(config) do |pipeline, queue|
queue.pop
@@ -309,10 +339,11 @@
# SLICE0 is a three-page scroll in which the last page is empty
slice0_query = LogStash::Json.dump(query.merge('slice' => { 'id' => 0, 'max' => 2}))
expect(client).to receive(:search).with(hash_including(:body => slice0_query)).and_return(slice0_response0)
expect(client).to receive(:scroll).with(hash_including(:body => { :scroll_id => slice0_scroll1 })).and_return(slice0_response1)
expect(client).to receive(:scroll).with(hash_including(:body => { :scroll_id => slice0_scroll2 })).and_return(slice0_response2)
+ allow(client).to receive(:ping)
# SLICE1 is a two-page scroll in which the last page has no next scroll id
slice1_query = LogStash::Json.dump(query.merge('slice' => { 'id' => 1, 'max' => 2}))
expect(client).to receive(:search).with(hash_including(:body => slice1_query)).and_return(slice1_response0)
expect(client).to receive(:scroll).with(hash_including(:body => { :scroll_id => slice1_scroll1 })).and_return(slice1_response1)
@@ -408,10 +439,11 @@
before do
expect(Elasticsearch::Client).to receive(:new).with(any_args).and_return(client)
expect(client).to receive(:search).with(any_args).and_return(response)
allow(client).to receive(:scroll).with({ :body => {:scroll_id => "cXVlcnlUaGVuRmV0Y2g"}, :scroll => "1m" }).and_return(scroll_reponse)
allow(client).to receive(:clear_scroll).and_return(nil)
+ allow(client).to receive(:ping).and_return(nil)
end
ecs_compatibility_matrix(:disabled, :v1, :v8) do |ecs_select|
before(:each) do
@@ -584,17 +616,18 @@
let(:config) { super().merge({ 'cloud_id' => valid_cloud_id }) }
it "should set host(s)" do
plugin.register
client = plugin.send(:client)
- expect( extract_transport(client).hosts ).to eql [{
- :scheme => "https",
- :host => "ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io",
- :port => 9243,
- :path => "",
- :protocol => "https"
- }]
+
+ expect( client.transport.instance_variable_get(:@seeds) ).to eql [{
+ :scheme => "https",
+ :host => "ac31ebb90241773157043c34fd26fd46.us-central1.gcp.cloud.es.io",
+ :port => 9243,
+ :path => "",
+ :protocol => "https"
+ }]
end
context 'invalid' do
let(:config) { super().merge({ 'cloud_id' => 'invalid:dXMtY2VudHJhbDEuZ2NwLmNsb3VkLmVzLmlv' }) }
@@ -841,11 +874,11 @@
expect(request.header['user-agent'][0]).to match(/logstash\/\d*\.\d*\.\d* \(OS=.*; JVM=.*\) logstash-input-elasticsearch\/\d*\.\d*\.\d*/)
end
end
end
- shared_examples'configurable timeout' do |config_name, manticore_transport_option|
+ shared_examples 'configurable timeout' do |config_name, manticore_transport_option|
let(:config_value) { fail NotImplementedError }
let(:config) { super().merge(config_name => config_value) }
{
:string => 'banana',
:negative => -123,
@@ -872,9 +905,12 @@
expect(new_elasticsearch_client_params).to include(:transport_options)
transport_options = new_elasticsearch_client_params[:transport_options]
expect(transport_options).to include(manticore_transport_option)
expect(transport_options[manticore_transport_option]).to eq(config_value.to_i)
+ mock_client = double("fake_client")
+ allow(mock_client).to receive(:ping)
+ mock_client
end
plugin.register
end
end