Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some initial tests #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions logstash-output-opentsdb.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"

s.add_development_dependency 'logstash-devutils'
s.add_development_dependency 'logstash-codec-plain'
end

63 changes: 62 additions & 1 deletion spec/outputs/opentsdb_spec.rb
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
require "logstash/devutils/rspec/spec_helper"
require 'logstash/devutils/rspec/spec_helper'
require 'logstash/outputs/opentsdb'

require_relative '../spec_helper'

describe LogStash::Outputs::Opentsdb do

before do
subject.register
subject.receive event
end

subject { LogStash::Outputs::Opentsdb.new(config) }

context 'single metric, no tags' do
let(:config) do
{
'metrics' => ['metric_1', '%{val}']
}
end

let(:event) { LogStash::Event.new('val' => 123) }

it 'sends the correct opentsdb format' do
expect(subject.socket.pop).to match(/^put metric_1 \d+ 123 \n/)
end
end

context 'single metric, multiple tags' do
let(:config) do
{
'metrics' => ['metric_1', '%{val}', 'tag1', '%{tag1}', 'tag2', '%{tag2}']
}
end

let(:event) { LogStash::Event.new('val' => 123, 'tag1' => 'foo', 'tag2' => 'bar') }

it 'sends the correct opentsdb format' do
expect(subject.socket.pop).to match(/^put metric_1 \d+ 123 tag1=foo tag2=bar\n/)
end
end

context 'socket.puts fails with ECONNRESET' do
let(:config) do
{
'metrics' => ['metric_1', '%{val}']
}
end
let(:event) { LogStash::Event.new('val' => 123) }

before do
subject.register
end

it 'tries to reconnect' do
allow(subject.socket).to receive(:puts).and_raise(Errno::ECONNRESET)
expect(subject).to receive(:connect)

subject.receive event
end
end
end
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative 'support/server'

class LogStash::Outputs::Opentsdb
attr_reader :socket

# Mock connect to return a fake server
def connect
@socket = Mocks::Server.new
end
end
29 changes: 29 additions & 0 deletions spec/support/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Mocks
class Server

def initialize
@queue = Array.new
end

def size
@queue.length
end

def pop
@queue.pop
end

def stop
end

def empty?
@queue.empty?
end

def puts(data)
data.split("\n").each do |line|
@queue << "#{line}\n"
end
end
end
end