Skip to content

Instantly share code, notes, and snippets.

@guyboertje
Last active October 12, 2015 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyboertje/12f7ec9e9ef1942145d7 to your computer and use it in GitHub Desktop.
Save guyboertje/12f7ec9e9ef1942145d7 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
require "logstash/filters/translate"
describe LogStash::Filters::Translate do
let(:config) { Hash.new }
subject { described_class.new(config) }
describe "exact translation" do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => [ "200", "OK",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error" ],
"exact" => true,
"regex" => false
}
end
let(:event) { LogStash::Event.new("status" => 200) }
it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("OK")
end
end
describe "multi translation" do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => [ "200", "OK",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error" ],
"exact" => false,
"regex" => false
}
end
let(:event) { LogStash::Event.new("status" => "200 & 500") }
it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("OK & Server Error")
end
end
describe "regex translation" do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => [ "^2[0-9][0-9]$", "OK",
"^3[0-9][0-9]$", "Redirect",
"^4[0-9][0-9]$", "Client Error",
"^5[0-9][0-9]$", "Server Error" ],
"exact" => true,
"regex" => true
}
end
let(:event) { LogStash::Event.new("status" => "200") }
it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("OK")
end
end
describe "fallback value" do
context "static configuration" do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"fallback" => "no match"
}
end
let(:event) { LogStash::Event.new("status" => "200") }
it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("no match")
end
end
context "allow sprintf" do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"fallback" => "%{missing_translation}"
}
end
let(:event) { LogStash::Event.new("status" => "200", "missing_translation" => "missing no match") }
it "return the exact translation" do
subject.register
subject.filter(event)
expect(event["translation"]).to eq("missing no match")
end
end
end
end
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
require "logstash/filters/translate"
describe LogStash::Filters::Translate do
let(:config) do
{
"field" => "status",
"destination" => "translation",
"dictionary" => dictionary,
"exact" => exact,
"regex" => regex
}
end
let(:event) { LogStash::Event.new(event_hash) }
subject { described_class.new(config) }
describe "when translations exist" do
before do
subject.register
subject.filter(event)
end
context "for exact translation" do
let(:exact) { true }
let(:regex) { false }
let(:dictionary) do
[ "200", "OK",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error" ]
end
let(:event_hash) { { "status" => 200 } }
it "return the translated value" do
expect(event["translation"]).to eq("OK")
end
end
context "for multi translation" do
let(:exact) { false }
let(:regex) { false }
let(:dictionary) do
[ "200", "OK",
"300", "Redirect",
"400", "Client Error",
"500", "Server Error" ]
end
let(:event_hash) { { "status" => "200 & 500" } }
it "return the translated value" do
expect(event["translation"]).to eq("OK & Server Error")
end
end
describe "for regex translations" do
let(:exact) { true }
let(:regex) { true }
let(:dictionary) do
[ "^2[0-9][0-9]$", "OK",
"^3[0-9][0-9]$", "Redirect",
"^4[0-9][0-9]$", "Client Error",
"^5[0-9][0-9]$", "Server Error" ]
end
let(:event) { LogStash::Event.new("status" => "300") }
it "return the translated value" do
expect(event["translation"]).to eq("Redirect")
end
end
end
describe "when using a fallback value" do
let(:dictionary) { ["200", "OK", "300", "Redirect"] }
before do
config.update("fallback" => fallback)
subject.register
subject.filter(event)
end
context "for static configuration" do
let(:fallback) { "no match" }
let(:event_hash) { { "status" => "234" } }
it "return the fallback translation" do
expect(event["translation"]).to eq("no match")
end
end
context "for interpolation" do
let(:fallback) { "%{missing_translation}" }
let(:event_hash) { { "status" => "234", "missing_translation" => "missing no match" } }
it "return the fallback translation" do
expect(event["translation"]).to eq("missing no match")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment