Skip to content

Commit

Permalink
Merge pull request #1 from jhoblitt/check_rspec
Browse files Browse the repository at this point in the history
Check rspec
  • Loading branch information
jhoblitt committed Oct 8, 2013
2 parents 8d1140b + b9b1dd6 commit db912ca
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Gemfile.lock
*.orig
*.rej
*.patch
tmp/aruba/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ rvm:
- 1.9.2
- 1.9.3
- 2.0.0
script: "rake spec features"
notifications:
email: false
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ Install
Usage
-----

The formatter may be used by either passing the `-f|--format` flag to `rspec` or with the included `check_rspec` script. The later sets the appropriate exit status for a failing Nagios/Icinga plugin.

### `check_rspec`

Usage: check_rspec [options] [--] [passed to rspec]

Specific options:
-t, --timeout TIMEOUT default: 30.0 (seconds)
-h, --help Show this message

Any options to `check_rspec` not prefixed with `-` or `--` are passed directly
on to the `rspec` utility. If you need to pass an option flag you may
terminated `check_rspec`'s option parsing with a bare `--`.

check_rspec -- -e 'foo' trivial_spec.rb

### `rspec`

rspec -f RSpec::Nagios::Formatter

See the documentation on [rspec --format](https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/command-line/format-option)
Expand Down Expand Up @@ -55,3 +73,10 @@ Support
-------

Please log tickets and issues at [github](https://github.com/jhoblitt/rspec-nagios-formatter)

See Also
--------

* [cucumber-nagios](http://auxesis.github.io/cucumber-nagios/)
* [RSpec](https://github.com/rspec/rspec)
* [Icinga Plugins](http://docs.icinga.org/latest/en/plugins.html)
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ RSpec::Core::RakeTask.new(:spec)
task :default => [
:spec,
]

require 'cucumber'
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty -x"
t.fork = false
end
42 changes: 42 additions & 0 deletions bin/check_rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env ruby

require 'optparse'
require 'open3'
require 'timeout'

EXIT_CRITICAL = 2

options = { :timeout => 30.0 }
option_parser = OptionParser.new do |opts|
opts.banner = "Usage: check_rspec [options] [--] [passed to rspec]"
opts.separator ""
opts.separator "Specific options:"

opts.on("-t", "--timeout TIMEOUT", "default: 30.0 (seconds)", Float) do |t|
options[:timeout] = t
end

opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end

option_parser.parse!

cmd = ['rspec', '--format', 'RSpec::Nagios::Formatter', ARGV]
cmd_stdout, cmd_stderr, cmd_status = nil

begin
Timeout::timeout(options[:timeout]) {
cmd_stdout, cmd_stderr, cmd_status = Open3.capture3(cmd.join(' '))
}
rescue Timeout::Error
puts "RSPEC Critical - timeout after #{options[:timeout]} seconds"
exit EXIT_CRITICAL
end

puts cmd_stdout
unless cmd_status.exitstatus == 0
exit EXIT_CRITICAL
end
96 changes: 96 additions & 0 deletions features/check_rspec/options.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Feature: command line options
In order to modifiy the behavior of check_rspec
As an Icinga administrator
I want to be able to set command line options

Scenario: run a passing rspec test
Given a file named "trivial_spec.rb" with:
"""
describe do
it { true.should == true }
end
"""
When I successfully run `check_rspec trivial_spec.rb`
Then the stdout should contain:
"""
RSPEC OK
"""

Scenario: run a failing rspec test
Given a file named "trivial_spec.rb" with:
"""
describe do
it { true.should == false }
end
"""
When I run `check_rspec trivial_spec.rb`
Then the exit status should be 2
And the stdout should contain:
"""
RSPEC Critical
"""

Scenario: run a test with --timeout
Given a file named "trivial_spec.rb" with:
"""
describe do
it { true.should == true }
end
"""
When I successfully run `check_rspec --timeout 1 trivial_spec.rb`
Then the stdout should contain:
"""
RSPEC OK
"""

Scenario: run a test with --timeout that times out
Given a file named "trivial_spec.rb" with:
"""
describe do
it { sleep(1) }
end
"""
When I run `check_rspec --timeout 0.5 trivial_spec.rb`
Then the exit status should be 2
And the stdout should contain:
"""
RSPEC Critical - timeout after 0.5 seconds
"""

Scenario: view usage with -h
When I successfully run `check_rspec -h`
And the stdout should contain:
"""
Usage: check_rspec [options] [--] [passed to rspec]
Specific options:
-t, --timeout TIMEOUT default: 30.0 (seconds)
-h, --help Show this message
"""

Scenario: view usage with --help
When I successfully run `check_rspec --help`
And the stdout should contain:
"""
Usage: check_rspec [options] [--] [passed to rspec]
Specific options:
-t, --timeout TIMEOUT default: 30.0 (seconds)
-h, --help Show this message
"""

Scenario: pass options to rspec
Given a file named "trivial_spec.rb" with:
"""
describe 'foo' do
it { true.should == true }
end
describe 'bar' do
it { true.should == true }
end
"""
When I successfully run `check_rspec -- -e 'foo' trivial_spec.rb`
Then the stdout should contain:
"""
RSPEC OK - 1 example
"""
31 changes: 31 additions & 0 deletions features/check_rspec/run.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: run rspec tests
In order to monitor the enviroment
As an Icinga administrator
I want to get RSpec test results in Icinga plugin format

Scenario: run a passing rspec test
Given a file named "trivial_spec.rb" with:
"""
describe do
it { true.should == true }
end
"""
When I successfully run `check_rspec trivial_spec.rb`
Then it should pass with regexp:
"""
^RSPEC OK - 1 example, 0 failures, finished in 0.\d+ seconds | examples=1 passing=1 failures=0 pending=0 conformance=100% time=0.\d+s$
"""

Scenario: run a failing rspec test
Given a file named "trivial_spec.rb" with:
"""
describe do
it { true.should == false }
end
"""
When I run `check_rspec trivial_spec.rb`
Then the exit status should be 2
And it should fail with regexp:
"""
^RSPEC Critical - 1 example, 1 failures, finished in 0.\d+ seconds | examples=1 passing=0 failures=1 pending=0 conformance=0% time=0.\d+s$
"""
1 change: 1 addition & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'aruba/cucumber'
5 changes: 2 additions & 3 deletions lib/rspec/nagios/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ def dump_summary(duration, example_count, failure_count, pending_count)
def summary_line(duration, example_count, failure_count, pending_count)
passing_count = example_count - failure_count
# conformance is expressed as a percentage
# if example_count is zero we need to aviod div by 0
# if example_count is zero we need to avoid div by 0
if example_count > 0
conformance = passing_count / example_count.to_f
conformance *= 100
#conformance = "%.2f" % conformance
conformance = conformance.round(0)
else
conformance = 0
end
# limit duration precision to microseconds
time = duration.round(6)
time = duration.round(6)

summary = 'RSPEC'
if failure_count == 0
Expand Down
3 changes: 2 additions & 1 deletion rspec-nagios-formatter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ Gem::Specification.new do |s|
s.add_development_dependency("rspec-expectations", ">= 2.0.0")
s.add_development_dependency("rspec-mocks", ">= 2.0.0")
s.add_development_dependency("rake", ">= 10.0.0")
s.add_development_dependency('aruba', '~> 0.4.6')

s.rubygems_version = ">= 1.6.1"
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
# s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_path = "lib"
end

0 comments on commit db912ca

Please sign in to comment.