Skip to content

Commit aa6123b

Browse files
authored
Merge pull request #2 from aboutsource/feature/tests
Integrate test env
2 parents ec6558e + f617546 commit aa6123b

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ Style/Documentation:
33

44
Metrics:
55
Enabled: false
6+
7+
FileName:
8+
Enabled: false

sensu-plugins-trello.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency 'rake', '~> 10.0'
2525
spec.add_development_dependency 'rspec', '~> 3.7'
2626
spec.add_development_dependency 'rubocop', '~> 0.54'
27+
spec.add_development_dependency 'webmock', '~> 3.3'
2728
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'json'
2+
require 'webmock/rspec'
3+
4+
require_relative './plugin_stub.rb'
5+
require_relative '../bin/check-trello-incidents.rb'
6+
7+
describe CheckTrelloIncidents do
8+
before(:each) do
9+
@api = stub_request(
10+
:get,
11+
'https://api.trello.com//1/lists/abcde/cards/?key=12345&token=12345'
12+
)
13+
14+
@check = CheckTrelloIncidents.new
15+
@check.config[:api_key] = '12345'
16+
@check.config[:api_token] = '12345'
17+
@check.config[:list] = 'abcde'
18+
19+
allow(@check).to receive(:output)
20+
end
21+
22+
it 'should run' do
23+
@api.to_return(body: '[]', status: 200)
24+
25+
expect { @check.run }.to raise_error do |error|
26+
expect(error).to be_a SystemExit
27+
expect(error.status).to eq 0
28+
end
29+
expect(@check).to have_received(:output).with('No new incidents')
30+
expect(@api).to have_been_requested
31+
end
32+
33+
it 'should be critical if trello cards' do
34+
cards = [{ id: '1a2b3c', name: 'foo' }, { id: '1x2y3z', name: 'bar' }]
35+
@api.to_return(body: JSON.generate(cards), status: 200)
36+
37+
expect { @check.run }.to raise_error do |error|
38+
expect(error).to be_a SystemExit
39+
expect(error.status).to eq 2
40+
end
41+
expect(@check).to have_received(:output).with('foo; bar')
42+
expect(@api).to have_been_requested
43+
end
44+
end

test/plugin_stub.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
RSpec.configure do |c|
2+
c.before { allow($stdout).to receive(:puts) }
3+
c.before { allow($stderr).to receive(:puts) }
4+
5+
# XXX: Sensu plugins run in the context of an at_exit handler. This prevents
6+
# XXX: code-under-test from being run at the end of the rspec suite.
7+
c.before(:each) do
8+
Sensu::Plugin::CLI.class_eval do
9+
# PluginStub
10+
class PluginStub
11+
def run; end
12+
13+
def ok(*); end
14+
15+
def warning(*); end
16+
17+
def critical(*); end
18+
19+
def unknown(*); end
20+
end
21+
class_variable_set(:@@autorun, PluginStub)
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)