spec/inputs/gelf_spec.rb in logstash-input-gelf-3.3.0 vs spec/inputs/gelf_spec.rb in logstash-input-gelf-3.3.1
- old
+ new
@@ -1,7 +1,8 @@
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
+require "logstash/devutils/rspec/shared_examples"
require "logstash/inputs/gelf"
require_relative "../support/helpers"
require "gelf"
require "flores/random"
@@ -17,10 +18,23 @@
after { producer.stop }
it_behaves_like "an interruptible input plugin"
end
+ def client_bootstrap(gelfclient, queue)
+ while queue.size <= 0
+ gelfclient.notify!("short_message" => "prime")
+ sleep(0.1)
+ end
+ gelfclient.notify!("short_message" => "start")
+
+ e = queue.pop
+ while (e.get("message") != "start")
+ e = queue.pop
+ end
+ end
+
describe "chunked gelf messages" do
let(:port) { 12209 }
let(:host) { "127.0.0.1" }
let(:chunksize) { 1420 }
let(:gelfclient) { GELF::Notifier.new(host, port, chunksize) }
@@ -38,25 +52,22 @@
"we survived gelf!"
] }
before(:each) do
subject.register
- Thread.new { subject.run(queue) }
+ @runner = Thread.new { subject.run(queue) }
+
+ client_bootstrap(gelfclient, queue)
end
- it "processes them" do
- while queue.size <= 0
- gelfclient.notify!("short_message" => "prime")
- sleep(0.1)
- end
- gelfclient.notify!("short_message" => "start")
+ after(:each) do
+ subject.do_stop
+ @runner.kill
+ @runner.join
+ end
- e = queue.pop
- while (e.get("message") != "start")
- e = queue.pop
- end
-
+ it "processes them" do
messages.each do |m|
gelfclient.notify!("short_message" => m)
end
events = messages.map{queue.pop}
@@ -66,28 +77,90 @@
expect(e.get("host")).to eq(Socket.gethostname)
end
end
end
+ describe "sending _@timestamp as bigdecimal" do
+ let(:host) { "127.0.0.1" }
+ let(:chunksize) { 1420 }
+ let(:gelfclient) { GELF::Notifier.new(host, port, chunksize) }
+
+ let(:config) { { "port" => port, "host" => host } }
+ let(:queue) { Queue.new }
+
+ subject(:gelf_input) { described_class.new(config) }
+
+ before(:each) do
+ subject.register
+ @runner = Thread.new { subject.run(queue) }
+
+ client_bootstrap(gelfclient, queue)
+ end
+
+ after(:each) do
+ subject.do_stop
+ @runner.kill
+ @runner.join
+ end
+
+ context "with valid value" do
+ let(:port) { 12210 }
+
+ it "should be correctly processed" do
+ gelfclient.notify!("short_message" => "msg1", "_@timestamp" => BigDecimal.new("946702800.1"))
+ gelfclient.notify!("short_message" => "msg2")
+
+ e = queue.pop
+ expect(e.get("message")).to eq("msg1")
+ expect(e.timestamp).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.100Z")
+ expect(e.get("host")).to eq(Socket.gethostname)
+
+ e = queue.pop
+ expect(e.get("message")).to eq("msg2")
+ end
+ end
+
+ context "with invalid value" do
+ let(:port) { 12211 }
+
+ it "should create an error tagged event" do
+ gelfclient.notify!("short_message" => "msg1", "_@timestamp" => "foo")
+ gelfclient.notify!("short_message" => "msg2")
+
+ e = queue.pop
+ expect(e.get("message")).to eq("msg1")
+ event_tags = e.to_hash['tags']
+ aggregate_failures "tag and leave bad value in place" do
+ expect(event_tags).to include("_gelf_move_field_failure")
+ expect(e.get('_@timestamp')).to eq("foo")
+ end
+ expect(e.get("host")).to eq(Socket.gethostname)
+
+ e = queue.pop
+ expect(e.get("message")).to eq("msg2")
+ end
+ end
+ end
+
context "timestamp coercion" do
# these test private methods, this is advisable for now until we roll out this coercion in the Timestamp class
# and remove this
context "integer numeric values" do
it "should coerce" do
- expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800).to_iso8601).to eq("2000-01-01T05:00:00.000Z")
+ expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800)).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.000Z")
expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800).usec).to eq(0)
end
end
context "float numeric values" do
# using explicit and certainly useless to_f here just to leave no doubt about the numeric type involved
it "should coerce and preserve millisec precision in iso8601" do
- expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.1.to_f).to_iso8601).to eq("2000-01-01T05:00:00.100Z")
- expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.12.to_f).to_iso8601).to eq("2000-01-01T05:00:00.120Z")
- expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.123.to_f).to_iso8601).to eq("2000-01-01T05:00:00.123Z")
+ expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.1.to_f)).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.100Z")
+ expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.12.to_f)).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.120Z")
+ expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.123.to_f)).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.123Z")
end
it "should coerce and preserve usec precision" do
expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.1.to_f).usec).to eq(100000)
expect(LogStash::Inputs::Gelf.coerce_timestamp(946702800.12.to_f).usec).to eq(120000)
@@ -126,10 +199,10 @@
# these test private methods, this is advisable for now until we roll out this coercion in the Timestamp class
# and remove this
it "should coerce integer numeric json timestamp input" do
event = LogStash::Inputs::Gelf.new_event("{\"timestamp\":946702800}", "dummy")
- expect(event.timestamp.to_iso8601).to eq("2000-01-01T05:00:00.000Z")
+ expect(event.timestamp).to be_a_logstash_timestamp_equivalent_to("2000-01-01T05:00:00.000Z")
end
it "should coerce float numeric value and preserve milliseconds precision in iso8601" do
event = LogStash::Inputs::Gelf.new_event("{\"timestamp\":946702800.123}", "dummy")
expect(event.timestamp.to_iso8601).to eq("2000-01-01T05:00:00.123Z")