Created
August 19, 2015 08:51
-
-
Save guyboertje/20d0d3143c04fd784670 to your computer and use it in GitHub Desktop.
cabin mocking version 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogTracker | |
def filter(&block) | |
cache.detect(&block) | |
end | |
def cache() | |
@cache ||= [] | |
end | |
def <<(hash) | |
cache.push(hash) | |
end | |
end | |
module Cabin | |
def self.build(level, subscriber) | |
instance = Cabin::Channel.new | |
instance.level = level | |
instance.subscribe(subscriber) | |
instance | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context "when using a Json string, tags might be an ArrayList" do | |
let(:tcp) { double('tcpsocket') } | |
# LogTracker and Cabin.build defined in logstash-devutils | |
let(:mocklog) { LogTracker.new } | |
let(:tracker) { Cabin.build(:debug, mocklog) } | |
it "logs an error unless the ArrayList implements compact" do | |
allow(Cabin::Channel).to receive(:get).and_return(tracker) | |
#stub out riemann server | |
allow(TCPSocket).to receive(:new).and_return(tcp) | |
allow(UDPSocket).to receive(:new).and_return(tcp) | |
allow(tcp).to receive(:<<) | |
# small risk here, this is the riemann response in binary | |
# it is the TCP OK response to a send, it is unlikely to change. | |
allow(tcp).to receive(:read).with(4).and_return("\x00\x00\x00\x02") | |
allow(tcp).to receive(:read).with(2).and_return("\x10\x01") | |
json_string = '{"tags":["good_enough", "smart_enough", "doggone_it", "people_like_me", null], "message":"hello", "node_info":{"name":"node1", "status":"up"}, "@version":"1", "@timestamp":"2015-06-03T23:34:54.076Z", "host":"vagrant-ubuntu-trusty-64"}' | |
event = LogStash::Event.new(LogStash::Json.load(json_string)) | |
output = LogStash::Plugin.lookup("output", "riemann").new | |
expect {output.register}.not_to raise_error | |
output.receive(event) | |
expected = mocklog.filter {|h| !!(h[:error].inspect =~ %r|undefined method .compact. for ..Java..JavaUtil..ArrayList|)} | |
expect(expected).to be nil | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GOOD OUTCOME ------------------------------ | |
guy at Elastics-MacBook-Pro in ~/elastic/logstash-output-riemann on master* | |
$ be rspec ./spec/outputs/riemann_spec.rb:113 | |
Using Accessor#strict_set for specs | |
Run options: | |
include {:locations=>{"./spec/outputs/riemann_spec.rb"=>[113]}} | |
exclude {:redis=>true, :socket=>true, :performance=>true, :couchdb=>true, :elasticsearch=>true, :elasticsearch_secure=>true, :export_cypher=>true, :integration=>true, :windows=>true} | |
outputs/riemann | |
build_riemann_formatted_event | |
when using a Json string, tags might be an ArrayList | |
logs an error unless the ArrayList implements compact | |
Finished in 0.528 seconds (files took 0.618 seconds to load) | |
1 example, 0 failures | |
Randomized with seed 29023 | |
BAD OUTCOME ------------------------------- | |
guy at Elastics-MacBook-Pro in ~/elastic/logstash-output-riemann on master* | |
$ be rspec ./spec/outputs/riemann_spec.rb:113 | |
Using Accessor#strict_set for specs | |
Run options: | |
include {:locations=>{"./spec/outputs/riemann_spec.rb"=>[113]}} | |
exclude {:redis=>true, :socket=>true, :performance=>true, :couchdb=>true, :elasticsearch=>true, :elasticsearch_secure=>true, :export_cypher=>true, :integration=>true, :windows=>true} | |
outputs/riemann | |
build_riemann_formatted_event | |
when using a Json string, tags might be an ArrayList | |
logs an error unless the ArrayList implements compact (FAILED - 1) | |
Failures: | |
1) outputs/riemann build_riemann_formatted_event when using a Json string, tags might be an ArrayList logs an error unless the ArrayList implements compact | |
Failure/Error: expect(expected).to be nil | |
expected nil | |
got #<Hash:5590> => {:timestamp=>"2015-08-19T09:43:58.871000+0100", :message=>"Unhandled exception", :error=>#<NoMethodError: undefined method `compact' for #<Java::JavaUtil::ArrayList:0x366c4480>>, :level=>:debug, :file=>"logstash/outputs/riemann.rb", :line=>"163", :method=>"send_to_riemann"} | |
# ./spec/outputs/riemann_spec.rb:136:in `(root)' | |
Finished in 0.486 seconds (files took 0.559 seconds to load) | |
1 example, 1 failure | |
Failed examples: | |
rspec ./spec/outputs/riemann_spec.rb:119 # outputs/riemann build_riemann_formatted_event when using a Json string, tags might be an ArrayList logs an error unless the ArrayList implements compact | |
Randomized with seed 46154 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment