spec/outputs/graphite_spec.rb in logstash-output-graphite-0.1.1 vs spec/outputs/graphite_spec.rb in logstash-output-graphite-0.1.2
- old
+ new
@@ -1,16 +1,12 @@
-require "logstash/devutils/rspec/spec_helper"
-require "logstash/outputs/graphite"
-require "mocha/api"
+require_relative '../spec_helper'
-describe LogStash::Outputs::Graphite, :socket => true do
-
+describe LogStash::Outputs::Graphite do
- describe "defaults should include all metrics" do
- port = 4939
- config <<-CONFIG
- input {
+ let(:port) { 4939 }
+ let(:config) do <<-CONFIG
+ input {
generator {
message => "foo=fancy bar=42"
count => 1
type => "generator"
}
@@ -25,40 +21,43 @@
host => "localhost"
port => #{port}
metrics => [ "hurray.%{foo}", "%{bar}" ]
}
}
- CONFIG
+ CONFIG
+ end
- let(:queue) { Queue.new }
- before :each do
- server = TCPServer.new("127.0.0.1", port)
- Thread.new do
- client = server.accept
- p client
- while true
- p :read
- line = client.readline
- p :done
- queue << line
- p line
- end
- end
- end
+ let(:pipeline) { LogStash::Pipeline.new(config) }
+ let(:server) { Mocks::Server.new(port) }
- agent do
- lines = queue.pop
+ before do
+ server.start
+ pipeline.run
+ end
- insist { lines.size } == 1
- insist { lines }.any? { |l| l =~ /^hurray.fancy 42.0 \d{10,}\n$/ }
+ after do
+ server.stop
+ end
+
+ context "with a default run" do
+
+ it "generate one element" do
+ expect(server.size).to eq(1)
end
+
+ it "include all metrics" do
+ lines = server.pop
+ expect(lines).to match(/^hurray.fancy 42.0 \d{10,}\n$/)
+ end
+
end
- describe "fields_are_metrics => true" do
- describe "metrics_format => ..." do
- describe "match one key" do
- config <<-CONFIG
+ context "if fields_are_metrics => true" do
+ context "when metrics_format => ..." do
+
+ context "match one key" do
+ let(:config) do <<-CONFIG
input {
generator {
message => "foo=123"
count => 1
type => "generator"
@@ -70,29 +69,34 @@
}
output {
graphite {
host => "localhost"
- port => 2003
+ port => #{port}
fields_are_metrics => true
include_metrics => ["foo"]
metrics_format => "foo.bar.sys.data.*"
debug => true
}
}
CONFIG
+ end
- agent do
- @mock.rewind
- lines = @mock.readlines
- insist { lines.size } == 1
- insist { lines[0] } =~ /^foo.bar.sys.data.foo 123.0 \d{10,}\n$/
+ it "generate one element" do
+ expect(server.size).to eq(1)
end
+
+ it "match the generated key" do
+ lines = server.pop
+ expect(lines).to match(/^foo.bar.sys.data.foo 123.0 \d{10,}\n$/)
+ end
+
end
- describe "match all keys" do
- config <<-CONFIG
+ context "match all keys" do
+
+ let(:config) do <<-CONFIG
input {
generator {
message => "foo=123 bar=42"
count => 1
type => "generator"
@@ -104,31 +108,43 @@
}
output {
graphite {
host => "localhost"
- port => 2003
+ port => #{port}
fields_are_metrics => true
include_metrics => [".*"]
metrics_format => "foo.bar.sys.data.*"
debug => true
}
}
CONFIG
+ end
- agent do
- @mock.rewind
- lines = @mock.readlines.delete_if { |l| l =~ /\.sequence \d+/ }
+ let(:lines) do
+ dict = {}
+ while(!server.empty?)
+ line = server.pop
+ key = line.split(' ')[0]
+ dict[key] = line
+ end
+ dict
+ end
- insist { lines.size } == 2
- insist { lines }.any? { |l| l =~ /^foo.bar.sys.data.foo 123.0 \d{10,}\n$/ }
- insist { lines }.any? { |l| l =~ /^foo.bar.sys.data.bar 42.0 \d{10,}\n$/ }
+ it "match the generated foo key" do
+ expect(lines['foo.bar.sys.data.foo']).to match(/^foo.bar.sys.data.foo 123.0 \d{10,}\n$/)
end
+
+ it "match the generated bar key" do
+ expect(lines['foo.bar.sys.data.bar']).to match(/^foo.bar.sys.data.bar 42.0 \d{10,}\n$/)
+ end
+
end
- describe "no match" do
- config <<-CONFIG
+ context "no match" do
+
+ let(:config) do <<-CONFIG
input {
generator {
message => "foo=123 bar=42"
count => 1
type => "generator"
@@ -140,28 +156,28 @@
}
output {
graphite {
host => "localhost"
- port => 2003
+ port => #{port}
fields_are_metrics => true
include_metrics => ["notmatchinganything"]
metrics_format => "foo.bar.sys.data.*"
debug => true
}
}
CONFIG
+ end
- agent do
- @mock.rewind
- lines = @mock.readlines
- insist { lines.size } == 0
+ it "generate no event" do
+ expect(server.empty?).to be_true
end
end
- describe "match one key with invalid metric_format" do
- config <<-CONFIG
+ context "match a key with invalid metric_format" do
+
+ let(:config) do <<-CONFIG
input {
generator {
message => "foo=123"
count => 1
type => "generator"
@@ -173,33 +189,33 @@
}
output {
graphite {
host => "localhost"
- port => 2003
+ port => #{port}
fields_are_metrics => true
include_metrics => ["foo"]
metrics_format => "invalidformat"
debug => true
}
}
CONFIG
+ end
- agent do
- @mock.rewind
- lines = @mock.readlines
- insist { lines.size } == 1
- insist { lines[0] } =~ /^foo 123.0 \d{10,}\n$/
+ it "match the foo key" do
+ lines = server.pop
+ expect(lines).to match(/^foo 123.0 \d{10,}\n$/)
end
end
end
end
- describe "fields are metrics = false" do
- describe "metrics_format not set" do
- describe "match one key with metrics list" do
- config <<-CONFIG
+ context "fields are metrics = false" do
+ context "metrics_format not set" do
+ context "match one key with metrics list" do
+
+ let(:config) do <<-CONFIG
input {
generator {
message => "foo=123"
count => 1
type => "generator"
@@ -211,26 +227,25 @@
}
output {
graphite {
host => "localhost"
- port => 2003
+ port => #{port}
fields_are_metrics => false
include_metrics => ["foo"]
metrics => [ "custom.foo", "%{foo}" ]
debug => true
}
}
CONFIG
+ end
- agent do
- @mock.rewind
- lines = @mock.readlines
-
- insist { lines.size } == 1
- insist { lines[0] } =~ /^custom.foo 123.0 \d{10,}\n$/
+ it "match the custom.foo key" do
+ lines = server.pop
+ expect(lines).to match(/^custom.foo 123.0 \d{10,}\n$/)
end
- end
+ end
end
end
+
end