Last active
August 29, 2015 14:27
-
-
Save guyboertje/12b32a34c08940e69630 to your computer and use it in GitHub Desktop.
Cabin mocking - it can be done
This file contains 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 cache() | |
@cache ||= Hash.new{|h,k| h[k] = []} | |
end | |
def <<(hash) | |
hash.each{|k,v| cache[k].push(v) } | |
end | |
end | |
context "when using a Json string, tags might be an ArrayList" do | |
let(:tcp) { double('tcpsocket') } | |
let(:mocklog) { LogTracker.new } | |
let(:tracker) do | |
instance = Cabin::Channel.new | |
instance.level = :debug | |
instance.subscribe(mocklog) | |
instance | |
end | |
it "#receive will fail unless the ArrayList implements compact" do | |
allow(Cabin::Channel).to receive(:get).and_return(tracker) | |
allow(TCPSocket).to receive(:new).and_return(tcp) | |
allow(UDPSocket).to receive(:new).and_return(tcp) | |
allow(tcp).to receive(:<<) | |
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) | |
expect(mocklog.cache[:error]).to eq([]) | |
end | |
end |
This file contains 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
uy at Elastics-MacBook-Pro in ~/elastic/logstash-output-riemann on master* | |
$ be rspec ./spec/outputs/riemann_spec.rb:123 | |
Using Accessor#strict_set for specs | |
Run options: | |
include {:locations=>{"./spec/outputs/riemann_spec.rb"=>[123]}} | |
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 | |
#receive will fail 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 #receive will fail unless the ArrayList implements compact | |
Failure/Error: expect(mocklog.cache[:error]).to eq([]) | |
expected: [] | |
got: [#<NoMethodError: undefined method `compact' for #<Java::JavaUtil::ArrayList:0x8c11eee>>] | |
(compared using ==) | |
Diff: | |
@@ -1,2 +1,2 @@ | |
-[] | |
+[#<NoMethodError: undefined method `compact' for #<Java::JavaUtil::ArrayList:0x8c11eee>>] | |
# ./spec/outputs/riemann_spec.rb:146:in `(root)' | |
Finished in 0.474 seconds (files took 0.52 seconds to load) | |
1 example, 1 failure | |
Failed examples: | |
rspec ./spec/outputs/riemann_spec.rb:133 # outputs/riemann build_riemann_formatted_event when using a Json string, tags might be an ArrayList #receive will fail unless the ArrayList implements compact | |
Randomized with seed 41588 |
This file contains 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
guy at Elastics-MacBook-Pro in ~/elastic/logstash-output-riemann on master* | |
$ be rspec ./spec/outputs/riemann_spec.rb:123 | |
Using Accessor#strict_set for specs | |
Run options: | |
include {:locations=>{"./spec/outputs/riemann_spec.rb"=>[123]}} | |
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 | |
#receive will fail unless the ArrayList implements compact | |
Finished in 0.502 seconds (files took 0.534 seconds to load) | |
1 example, 0 failures | |
Randomized with seed 8914 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment