Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bugs in RSpec 2.6 and needing to maintain compatibility with 1.8.5 me…

…ans Mocha is back in.
  • Loading branch information...
commit 573e45a9df428741e79f013aaa0b673fa5e39807 1 parent e8952bd
@ohookins authored
View
1  Gemfile
@@ -16,3 +16,4 @@ gem 'eventmachine', '0.12.10'
gem 'daemons', '1.1.4'
gem 'diff-lcs', '1.1.2'
gem 'fpm', '0.3.11'
+gem 'mocha', '0.10.0'
View
4 Gemfile.lock
@@ -7,6 +7,9 @@ GEM
fpm (0.3.11)
json
json (1.5.3)
+ metaclass (0.0.1)
+ mocha (0.10.0)
+ metaclass (~> 0.0.1)
rack (1.2.4)
rack-protection (1.1.4)
rack
@@ -40,6 +43,7 @@ DEPENDENCIES
eventmachine (= 0.12.10)
fpm (= 0.3.11)
json (= 1.5.3)
+ mocha (= 0.10.0)
rack (= 1.2.4)
rack-protection (= 1.1.4)
rack-test (= 0.6.1)
View
16 spec/nagios_spec.rb
@@ -1,4 +1,6 @@
require 'rspec'
+require 'spec_helper'
+require 'mocha'
require 'tempfile'
require 'fileutils'
require 'nagios'
@@ -80,8 +82,8 @@
nagios = Nagios.new(@cmd_file, @status_file)
# Set up mocks to catch the commands that will be sent
- nagios.should_receive(:send_command).with('DEL_HOST_DOWNTIME;1')
- nagios.should_receive(:send_command).with('DEL_SVC_DOWNTIME;2')
+ nagios.expects(:send_command).with('DEL_HOST_DOWNTIME;1')
+ nagios.expects(:send_command).with('DEL_SVC_DOWNTIME;2')
nagios.delete_all_downtime_for_host('example1')
end
@@ -92,8 +94,8 @@
nagios = Nagios.new(@cmd_file, @status_file)
# Mock out time and send_command
- nagios.should_receive(:get_seconds_since_epoch).and_return(1234567890)
- nagios.should_receive(:send_command).with('SCHEDULE_HOST_DOWNTIME;localhost;1234567890;1234567899;1;0;0;Test Dude;Test Downtime')
+ nagios.expects(:get_seconds_since_epoch).returns(1234567890)
+ nagios.expects(:send_command).with('SCHEDULE_HOST_DOWNTIME;localhost;1234567890;1234567899;1;0;0;Test Dude;Test Downtime')
nagios.schedule_host_downtime('localhost', '9', 'Test Dude', 'Test Downtime')
end
@@ -104,8 +106,8 @@
nagios = Nagios.new(@cmd_file, @status_file)
# Mock out time and send_command
- nagios.should_receive(:get_seconds_since_epoch).and_return(1234567890)
- nagios.should_receive(:send_command).with('SCHEDULE_HOST_SVC_DOWNTIME;localhost;1234567890;1234567899;1;0;0;Test Dude;Test Downtime')
+ nagios.expects(:get_seconds_since_epoch).returns(1234567890)
+ nagios.expects(:send_command).with('SCHEDULE_HOST_SVC_DOWNTIME;localhost;1234567890;1234567899;1;0;0;Test Dude;Test Downtime')
nagios.schedule_services_downtime('localhost', '9', 'Test Dude', 'Test Downtime')
end
@@ -126,7 +128,7 @@
mocktime = 1234567890
# Mock the call to get the current time so we have predictable output
- nagios.should_receive(:get_seconds_since_epoch).and_return(mocktime)
+ nagios.expects(:get_seconds_since_epoch).returns(mocktime)
nagios.send(:send_command, mockcommand)
# Verify the output written to the command file
View
21 spec/sinagios_client_spec.rb
@@ -1,5 +1,6 @@
require 'sinagios_client'
require 'rspec'
+require 'spec_helper'
require 'stringio'
describe SinagiosClient do
@@ -60,7 +61,7 @@
describe '#parse_config_file' do
it 'sets options based on the config file if it exists' do
fakeconfig = File.join(File.dirname(__FILE__), 'test_data/fakeconfig.conf')
- SinagiosClient.any_instance.should_receive(:config_file_path).twice.and_return(fakeconfig)
+ SinagiosClient.any_instance.expects(:config_file_path).twice.returns(fakeconfig)
expect do
sc = SinagiosClient.new(['--operation', 'delete', '--hosts', 'host1'])
sc.instance_eval do
@@ -72,4 +73,22 @@
end.to_not raise_error
end
end
+
+ describe '#finish_connection' do
+ it 'closes the http connection if it is open' do
+ sc = SinagiosClient.new(['--operation', 'delete', '--hosts', 'host1', '--uri', 'http://api.example.com/'])
+
+ # mock out the connection
+ http = mock("Net::HTTP")
+ http.expects(:started?).returns(true)
+ http.expects(:finish)
+
+ # inject the mock
+ sc.instance_eval do
+ @http = http
+ end
+
+ sc.send(:finish_connection)
+ end
+ end
end
View
37 spec/sinagios_spec.rb
@@ -1,5 +1,6 @@
require 'sinagios'
require 'rspec'
+require 'spec_helper'
require 'rack/test'
describe 'the Sinagios app' do
@@ -12,11 +13,11 @@ def app
it 'returns all downtime correctly when downtime exists' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
+ Nagios.expects(:new).returns(@fakenagios)
# generate some fake downtime data
downtime = {'localhost' => {:host => [1], :service => [2]}}
- @fakenagios.should_receive(:get_all_downtime).and_return(downtime)
+ @fakenagios.expects(:get_all_downtime).returns(downtime)
get '/v1/downtime'
last_response.body.should == downtime.to_json
@@ -26,8 +27,8 @@ def app
it 'returns an empty hash when there is no downtime' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
- @fakenagios.should_receive(:get_all_downtime).and_return({})
+ Nagios.expects(:new).returns(@fakenagios)
+ @fakenagios.expects(:get_all_downtime).returns({})
get '/v1/downtime'
last_response.body.should == {}.to_json
@@ -37,11 +38,11 @@ def app
it 'returns just the downtime for a particular host' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
+ Nagios.expects(:new).returns(@fakenagios)
# generate some fake downtime data
downtime = {'localhost' => {:host => [1], :service => [2]}}
- @fakenagios.should_receive(:get_all_downtime).and_return(downtime)
+ @fakenagios.expects(:get_all_downtime).returns(downtime)
get '/v1/downtime/localhost'
last_response.body.should == downtime['localhost'].to_json
@@ -51,8 +52,8 @@ def app
it 'returns an error when a specific host has no downtime' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
- @fakenagios.should_receive(:get_all_downtime).and_return({})
+ Nagios.expects(:new).returns(@fakenagios)
+ @fakenagios.expects(:get_all_downtime).returns({})
get '/v1/downtime/localhost'
last_response.body.should == ''
@@ -62,14 +63,14 @@ def app
it 'successfully deletes all host downtime if present' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
+ Nagios.expects(:new).returns(@fakenagios)
# generate some fake downtime data
downtime = {'localhost' => {:host => [1], :service => [2]}}
- @fakenagios.should_receive(:get_all_downtime).and_return(downtime)
+ @fakenagios.expects(:get_all_downtime).returns(downtime)
# mock the deletion
- @fakenagios.should_receive(:delete_all_downtime_for_host).with('localhost').and_return('All downtime for localhost deleted!')
+ @fakenagios.expects(:delete_all_downtime_for_host).with('localhost').returns('All downtime for localhost deleted!')
delete '/v1/downtime/localhost'
last_response.body.should == 'All downtime for localhost deleted!'
@@ -79,8 +80,8 @@ def app
it 'returns an error when trying to delete non-existent downtime' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
- @fakenagios.should_receive(:get_all_downtime).and_return({})
+ Nagios.expects(:new).returns(@fakenagios)
+ @fakenagios.expects(:get_all_downtime).returns({})
delete '/v1/downtime/localhost'
last_response.body.should == ''
@@ -90,7 +91,7 @@ def app
it 'creates new downtime successfully when provided valid input' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
+ Nagios.expects(:new).returns(@fakenagios)
host = 'localhost'
duration = '5'
@@ -98,8 +99,8 @@ def app
comment = 'Test downtime'
# Mock calls to schedule the downtime
- @fakenagios.should_receive(:schedule_host_downtime).with(host, duration, author, comment)
- @fakenagios.should_receive(:schedule_services_downtime).with(host, duration, author, comment)
+ @fakenagios.expects(:schedule_host_downtime).with(host, duration, author, comment)
+ @fakenagios.expects(:schedule_services_downtime).with(host, duration, author, comment)
post "/v1/downtime/#{host}", params = {:duration => duration, :author => author, :comment => comment}
last_response.body.should == ''
@@ -127,7 +128,7 @@ def app
it 'passes the health check when the nagios object is created successfully' do
# Mock the Nagios class so we don't have to deal with real data here
@fakenagios = mock('nagios')
- Nagios.should_receive(:new).and_return(@fakenagios)
+ Nagios.expects(:new).returns(@fakenagios)
get '/v1/health'
last_response.body.should == 'OK'
@@ -136,7 +137,7 @@ def app
it 'fails the health check when the nagios object throws an exception during creation' do
# Mock the Nagios class so we don't have to deal with real data here
- Nagios.should_receive(:new).and_raise(Exception)
+ Nagios.expects(:new).raises(Exception)
get '/v1/health'
last_response.body.should_not == 'OK'
last_response.status.should == 500
View
3  spec/spec_helper.rb
@@ -0,0 +1,3 @@
+RSpec.configure do |config|
+ config.mock_framework = :mocha
+end
Please sign in to comment.
Something went wrong with that request. Please try again.