spec/outputs/elasticsearch.rb in logstash-output-elasticsearch-0.1.1 vs spec/outputs/elasticsearch.rb in logstash-output-elasticsearch-0.1.5
- old
+ new
@@ -1,12 +1,11 @@
-require "test_utils"
+require "spec_helper"
require "ftw"
require "logstash/plugin"
require "logstash/json"
describe "outputs/elasticsearch" do
- extend LogStash::RSpec
it "should register" do
output = LogStash::Plugin.lookup("output", "elasticsearch").new("embedded" => "false", "protocol" => "transport", "manage_template" => "false")
# register will try to load jars and raise if it cannot find jars
@@ -342,8 +341,149 @@
# 'at' is a stopword, make sure stopwords are not ignored.
insist { terms }.include?("at")
end
end
+ end
+ end
+
+ describe "elasticsearch protocol" do
+ # ElasticSearch related jars
+#LogStash::Environment.load_elasticsearch_jars!
+ # Load elasticsearch protocol
+ require "logstash/outputs/elasticsearch/protocol"
+
+ describe "elasticsearch node client" do
+ # Test ElasticSearch Node Client
+ # Reference: http://www.elasticsearch.org/guide/reference/modules/discovery/zen/
+
+ it "should support hosts in both string and array" do
+ # Because we defined *hosts* method in NodeClient as private,
+ # we use *obj.send :method,[args...]* to call method *hosts*
+ client = LogStash::Outputs::Elasticsearch::Protocols::NodeClient.new
+
+ # Node client should support host in string
+ # Case 1: default :host in string
+ insist { client.send :hosts, :host => "host",:port => 9300 } == "host:9300"
+ # Case 2: :port =~ /^\d+_\d+$/
+ insist { client.send :hosts, :host => "host",:port => "9300-9302"} == "host:9300,host:9301,host:9302"
+ # Case 3: :host =~ /^.+:.+$/
+ insist { client.send :hosts, :host => "host:9303",:port => 9300 } == "host:9303"
+ # Case 4: :host =~ /^.+:.+$/ and :port =~ /^\d+_\d+$/
+ insist { client.send :hosts, :host => "host:9303",:port => "9300-9302"} == "host:9303"
+
+ # Node client should support host in array
+ # Case 5: :host in array with single item
+ insist { client.send :hosts, :host => ["host"],:port => 9300 } == ("host:9300")
+ # Case 6: :host in array with more than one items
+ insist { client.send :hosts, :host => ["host1","host2"],:port => 9300 } == "host1:9300,host2:9300"
+ # Case 7: :host in array with more than one items and :port =~ /^\d+_\d+$/
+ insist { client.send :hosts, :host => ["host1","host2"],:port => "9300-9302" } == "host1:9300,host1:9301,host1:9302,host2:9300,host2:9301,host2:9302"
+ # Case 8: :host in array with more than one items and some :host =~ /^.+:.+$/
+ insist { client.send :hosts, :host => ["host1","host2:9303"],:port => 9300 } == "host1:9300,host2:9303"
+ # Case 9: :host in array with more than one items, :port =~ /^\d+_\d+$/ and some :host =~ /^.+:.+$/
+ insist { client.send :hosts, :host => ["host1","host2:9303"],:port => "9300-9302" } == "host1:9300,host1:9301,host1:9302,host2:9303"
+ end
+ end
+ end
+
+ describe "Authentication option" do
+ ["node", "transport"].each do |protocol|
+ context "with protocol => #{protocol}" do
+ subject do
+ require "logstash/outputs/elasticsearch"
+ settings = {
+ "protocol" => protocol,
+ "node_name" => "logstash",
+ "cluster" => "elasticsearch",
+ "host" => "node01",
+ "user" => "test",
+ "password" => "test"
+ }
+ next LogStash::Outputs::ElasticSearch.new(settings)
+ end
+
+ it "should fail in register" do
+ expect {subject.register}.to raise_error
+ end
+ end
+ end
+ end
+
+ describe "SSL option" do
+ ["node", "transport"].each do |protocol|
+ context "with protocol => #{protocol}" do
+ subject do
+ require "logstash/outputs/elasticsearch"
+ settings = {
+ "protocol" => protocol,
+ "node_name" => "logstash",
+ "cluster" => "elasticsearch",
+ "host" => "node01",
+ "ssl" => true
+ }
+ next LogStash::Outputs::ElasticSearch.new(settings)
+ end
+
+ it "should fail in register" do
+ expect {subject.register}.to raise_error
+ end
+ end
+ end
+ end
+
+ describe "send messages to ElasticSearch using HTTPS", :elasticsearch_secure => true do
+ subject do
+ require "logstash/outputs/elasticsearch"
+ settings = {
+ "protocol" => "http",
+ "node_name" => "logstash",
+ "cluster" => "elasticsearch",
+ "host" => "node01",
+ "user" => "user",
+ "password" => "changeme",
+ "ssl" => true,
+ "cacert" => "/tmp/ca/certs/cacert.pem",
+ # or
+ #"truststore" => "/tmp/ca/truststore.jks",
+ #"truststore_password" => "testeteste"
+ }
+ next LogStash::Outputs::ElasticSearch.new(settings)
+ end
+
+ before :each do
+ subject.register
+ end
+
+ it "sends events to ES" do
+ expect {
+ subject.receive(LogStash::Event.new("message" => "sample message here"))
+ subject.buffer_flush(:final => true)
+ }.to_not raise_error
+ end
+ end
+
+ describe "connect using HTTP Authentication", :elasticsearch_secure => true do
+ subject do
+ require "logstash/outputs/elasticsearch"
+ settings = {
+ "protocol" => "http",
+ "cluster" => "elasticsearch",
+ "host" => "node01",
+ "user" => "user",
+ "password" => "changeme",
+ }
+ next LogStash::Outputs::ElasticSearch.new(settings)
+ end
+
+ before :each do
+ subject.register
+ end
+
+ it "sends events to ES" do
+ expect {
+ subject.receive(LogStash::Event.new("message" => "sample message here"))
+ subject.buffer_flush(:final => true)
+ }.to_not raise_error
end
end
end