Skip to content

Commit

Permalink
specs for /collectd and /publish/:name
Browse files Browse the repository at this point in the history
  • Loading branch information
gorsuch committed Aug 28, 2012
1 parent bc3dfad commit a53d4bb
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 38 deletions.
1 change: 1 addition & 0 deletions lib/backstop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ def self.deploy; env!("DEPLOY"); end
def self.port; env!("PORT").to_i; end
def self.carbon_urls; env!("CARBON_URLS").split(","); end
def self.prefixes; env!("PREFIXES").split(","); end
def self.test?; ENV.key?('TEST'); end
end
end
28 changes: 6 additions & 22 deletions lib/backstop/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,15 @@

module Backstop
class Application < Sinatra::Base

configure do
enable :logging
require 'newrelic_rpm'
@@sockets = []
@@publisher = nil
end

helpers do
def sockets
if !@@sockets.empty?
return @@sockets
else
@@sockets = []
Config.carbon_urls.each do |c|
if (c =~ /^carbon:\/\//)
c.gsub!(/carbon:\/\//, "")
host, port = c.split(":")
s = TCPSocket.new host, port
@@sockets.push s
end
end
end
def publisher
@@publisher ||= Backstop::Publisher.new(Config.carbon_urls)
end
end

Expand All @@ -47,9 +34,8 @@ def sockets
results.each do |r|
r["source"] = "collectd"
halt 400, "missing fields" unless (r[:cloud] && r[:slot] && r[:id] && r[:metric] && r[:value] && r[:measure_time])
s = sockets.sample
r[:cloud].gsub!(/\./, "-")
s.puts "mitt.#{r[:cloud]}.#{r[:slot]}.#{r[:id]}.#{r[:metric]} #{r[:value]} #{r[:measure_time]}" if s
publisher.publish("mitt.#{r[:cloud]}.#{r[:slot]}.#{r[:id]}.#{r[:metric]}", r[:value], r[:measure_time])

This comment has been minimized.

Copy link
@tomkersten

tomkersten Sep 12, 2012

This may be a silly question, but, why the "mitt" prefix?

This comment has been minimized.

Copy link
@obfuscurity

obfuscurity Sep 12, 2012

Owner

Mitt was the predecessor to Backstop.

This comment has been minimized.

Copy link
@tomkersten

tomkersten Sep 12, 2012

Ah. Yea...sorry...I did see that in the README but didn't connect the dots. Thanks.

end
end
"ok"
Expand Down Expand Up @@ -85,14 +71,12 @@ def sockets
data.each do |item|
item["source"] = params[:name]
halt 400, "missing fields" unless (item['metric'] && item['value'] && item['measure_time'])
s = sockets.sample
s.puts "#{item['source']}.#{item['metric']} #{item['value']} #{item['measure_time']}"
publisher.publish("#{item['source']}.#{item['metric']}", item['value'], item['measure_time'])
end
else
data["source"] = params[:name]
halt 400, "missing fields" unless (data['metric'] && data['value'] && data['measure_time'])
s = sockets.sample
s.puts "#{data['source']}.#{data['metric']} #{data['value']} #{data['measure_time']}"
publisher.publish("#{data['source']}.#{data['metric']}", data['value'], data['measure_time'])
end
"ok"
else
Expand Down
13 changes: 13 additions & 0 deletions spec/backstop/bad_collectd_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"values": [1901474177],
"dstypes": ["counter"],
"dsnames": ["value"],
"interval": 10,
"host": "leeloo.octo.it",
"plugin": "cpu",
"plugin_instance": "0",
"type": "cpu",
"type_instance": "idle"
}
]
14 changes: 14 additions & 0 deletions spec/backstop/good_collectd_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"values": [1901474177],
"dstypes": ["counter"],
"dsnames": ["value"],
"time": 1280959128,
"interval": 10,
"host": "leeloo.octo.it",
"plugin": "cpu",
"plugin_instance": "0",
"type": "cpu",
"type_instance": "idle"
}
]
67 changes: 62 additions & 5 deletions spec/backstop/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,74 @@
require 'backstop/web'
require 'rack/test'

set :environment, :test

describe Backstop::Application do
include Rack::Test::Methods

def app
Backstop::Application
end

it 'responds to /health' do
get '/health'
last_response.should be_ok
before(:each) do
app.class_variable_set :@@publisher, nil
end

context 'GET /health' do
it 'should handle GET /health' do
get '/health'
last_response.should be_ok
end
end

context 'POST /publish/:name' do
it 'should require JSON' do
post '/publish/foo', 'foo'
last_response.should_not be_ok
last_response.status.should eq(400)
end

it 'should handle a single metric' do
p = double('publisher')
Backstop::Publisher.should_receive(:new) { p }
p.should_receive(:publish).with('test.bar', 12345, 1)
post '/publish/test', { :metric => 'bar', :value => 12345, :measure_time => 1 }.to_json
last_response.should be_ok
end

it 'should handle an array of metrics' do
p = double('publisher')
Backstop::Publisher.should_receive(:new) { p }
p.should_receive(:publish).with('test.bar', 12345, 1)
p.should_receive(:publish).with('test.bar', 12344, 2)
post '/publish/test', [{ :metric => 'bar', :value => 12345, :measure_time => 1 }, { :metric => 'bar', :value => 12344, :measure_time => 2} ].to_json
last_response.should be_ok
end
end

context 'POST /collectd' do
let(:collectd_data) { File.open(File.dirname(__FILE__) + '/good_collectd_data.json').read }
let(:bad_collectd_data) { File.open(File.dirname(__FILE__) + '/bad_collectd_data.json').read }

it 'should require JSON' do
post '/collectd', 'foo'
last_response.should_not be_ok
last_response.status.should eq(400)
end

it 'should handle a collectd metric' do
p = double('publisher')
Backstop::Publisher.should_receive(:new) { p }
p.should_receive(:publish).with('mitt.leeloo.octo.it.cpu.0.idle', 1901474177, 1280959128)
post '/collectd', collectd_data
last_response.body.should eq('ok')
last_response.status.should eq(200)
end

it 'should complain if missing fields' do
post '/collectd', bad_collectd_data
last_response.status.should eq(400)
last_response.body.should eq('missing fields')
end
end
end


14 changes: 3 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'backstop'

ENV['CARBON_URLS'] = 'carbon://1.1.1.1:5000'
ENV['PREFIXES'] = 'test'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
end

0 comments on commit a53d4bb

Please sign in to comment.