diff --git a/.rubocop.yml b/.rubocop.yml index dd16d44..813b2c4 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,3 +3,6 @@ Style/Documentation: Metrics: Enabled: false + +FileName: + Enabled: false diff --git a/sensu-plugins-trello.gemspec b/sensu-plugins-trello.gemspec index 1d98f7d..037fceb 100644 --- a/sensu-plugins-trello.gemspec +++ b/sensu-plugins-trello.gemspec @@ -24,4 +24,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.7' spec.add_development_dependency 'rubocop', '~> 0.54' + spec.add_development_dependency 'webmock', '~> 3.3' end diff --git a/test/check-trello-incidents_spec.rb b/test/check-trello-incidents_spec.rb new file mode 100644 index 0000000..8abad12 --- /dev/null +++ b/test/check-trello-incidents_spec.rb @@ -0,0 +1,44 @@ +require 'json' +require 'webmock/rspec' + +require_relative './plugin_stub.rb' +require_relative '../bin/check-trello-incidents.rb' + +describe CheckTrelloIncidents do + before(:each) do + @api = stub_request( + :get, + 'https://api.trello.com//1/lists/abcde/cards/?key=12345&token=12345' + ) + + @check = CheckTrelloIncidents.new + @check.config[:api_key] = '12345' + @check.config[:api_token] = '12345' + @check.config[:list] = 'abcde' + + allow(@check).to receive(:output) + end + + it 'should run' do + @api.to_return(body: '[]', status: 200) + + expect { @check.run }.to raise_error do |error| + expect(error).to be_a SystemExit + expect(error.status).to eq 0 + end + expect(@check).to have_received(:output).with('No new incidents') + expect(@api).to have_been_requested + end + + it 'should be critical if trello cards' do + cards = [{ id: '1a2b3c', name: 'foo' }, { id: '1x2y3z', name: 'bar' }] + @api.to_return(body: JSON.generate(cards), status: 200) + + expect { @check.run }.to raise_error do |error| + expect(error).to be_a SystemExit + expect(error.status).to eq 2 + end + expect(@check).to have_received(:output).with('foo; bar') + expect(@api).to have_been_requested + end +end diff --git a/test/plugin_stub.rb b/test/plugin_stub.rb new file mode 100644 index 0000000..379e0b5 --- /dev/null +++ b/test/plugin_stub.rb @@ -0,0 +1,24 @@ +RSpec.configure do |c| + c.before { allow($stdout).to receive(:puts) } + c.before { allow($stderr).to receive(:puts) } + + # XXX: Sensu plugins run in the context of an at_exit handler. This prevents + # XXX: code-under-test from being run at the end of the rspec suite. + c.before(:each) do + Sensu::Plugin::CLI.class_eval do + # PluginStub + class PluginStub + def run; end + + def ok(*); end + + def warning(*); end + + def critical(*); end + + def unknown(*); end + end + class_variable_set(:@@autorun, PluginStub) + end + end +end