is_it_working 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe IsItWorking::Filter do
4
+
5
+ it "should have a name" do
6
+ filter = IsItWorking::Filter.new(:test, lambda{})
7
+ filter.name.should == :test
8
+ end
9
+
10
+ it "should run a check and return a thread" do
11
+ check = lambda do |status|
12
+ status.ok("success")
13
+ end
14
+
15
+ filter = IsItWorking::Filter.new(:test, check)
16
+ runner = filter.run
17
+ status = runner.filter_status
18
+ runner.join
19
+ status.should be_success
20
+ status.messages.first.message.should == "success"
21
+ status.time.should_not be_nil
22
+ end
23
+
24
+ it "should run a check and recue an errors" do
25
+ check = lambda do |status|
26
+ raise "boom!"
27
+ end
28
+
29
+ filter = IsItWorking::Filter.new(:test, check)
30
+ runner = filter.run
31
+ status = runner.filter_status
32
+ runner.join
33
+ status.should_not be_success
34
+ status.messages.first.message.should include("boom")
35
+ status.time.should_not be_nil
36
+ end
37
+
38
+ it "should run multiple filters and return their statuses" do
39
+ filter_1 = IsItWorking::Filter.new(:test, lambda{|status| status.ok("OK")})
40
+ filter_2 = IsItWorking::Filter.new(:test, lambda{|status| status.fail("FAIL")})
41
+ statuses = IsItWorking::Filter.run_filters([filter_1, filter_2])
42
+ statuses.first.should be_success
43
+ statuses.last.should_not be_success
44
+ end
45
+
46
+ end
@@ -0,0 +1,140 @@
1
+ require 'spec_helper'
2
+
3
+ describe IsItWorking::Handler do
4
+
5
+ it "should lookup filters from the pre-defined checks" do
6
+ handler = IsItWorking::Handler.new do |h|
7
+ h.check :directory, :path => ".", :permissions => :read
8
+ end
9
+ response = handler.call({})
10
+ response.first.should == 200
11
+ response.last.flatten.join("").should include("OK")
12
+ response.last.flatten.join("").should include("directory")
13
+ end
14
+
15
+ it "should use blocks as filters" do
16
+ handler = IsItWorking::Handler.new do |h|
17
+ h.check :block do |status|
18
+ status.ok("Okey dokey")
19
+ end
20
+ end
21
+ response = handler.call({})
22
+ response.first.should == 200
23
+ response.last.flatten.join("").should include("OK")
24
+ response.last.flatten.join("").should include("block - Okey dokey")
25
+ end
26
+
27
+ it "should use object as filters" do
28
+ handler = IsItWorking::Handler.new do |h|
29
+ h.check :lambda, lambda{|status| status.ok("A-okay")}
30
+ end
31
+ response = handler.call({})
32
+ response.first.should == 200
33
+ response.last.flatten.join("").should include("OK")
34
+ response.last.flatten.join("").should include("lambda - A-okay")
35
+ end
36
+
37
+ it "should create asynchronous filters by default" do
38
+ handler = IsItWorking::Handler.new do |h|
39
+ h.check :block do |status|
40
+ status.ok("Okey dokey")
41
+ end
42
+ end
43
+ runner = IsItWorking::Filter::AsyncRunner.new{}
44
+ IsItWorking::Filter::AsyncRunner.should_receive(:new).and_return(runner)
45
+ response = handler.call({})
46
+ end
47
+
48
+ it "should be able to create synchronous filters" do
49
+ handler = IsItWorking::Handler.new do |h|
50
+ h.check :block, :async => false do |status|
51
+ status.ok("Okey dokey")
52
+ end
53
+ end
54
+ runner = IsItWorking::Filter::SyncRunner.new{}
55
+ IsItWorking::Filter::SyncRunner.should_receive(:new).and_return(runner)
56
+ response = handler.call({})
57
+ end
58
+
59
+ it "should work with synchronous checks" do
60
+ handler = IsItWorking::Handler.new do |h|
61
+ h.check :block, :async => false do |status|
62
+ status.ok("Okey dokey")
63
+ end
64
+ end
65
+ response = handler.call({})
66
+ response.first.should == 200
67
+ response.last.flatten.join("").should include("OK")
68
+ response.last.flatten.join("").should include("block - Okey dokey")
69
+ end
70
+
71
+ it "should return a success response if all checks pass" do
72
+ handler = IsItWorking::Handler.new do |h|
73
+ h.check :block do |status|
74
+ status.ok("success")
75
+ end
76
+ h.check :block do |status|
77
+ status.ok("worked")
78
+ end
79
+ end
80
+ response = handler.call({})
81
+ response.first.should == 200
82
+ response.last.flatten.join("").should include("block - success")
83
+ response.last.flatten.join("").should include("block - worked")
84
+ end
85
+
86
+ it "should return an error response if any check fails" do
87
+ handler = IsItWorking::Handler.new do |h|
88
+ h.check :block do |status|
89
+ status.ok("success")
90
+ end
91
+ h.check :block do |status|
92
+ status.fail("down")
93
+ end
94
+ end
95
+ response = handler.call({})
96
+ response.first.should == 500
97
+ response.last.flatten.join("").should include("block - success")
98
+ response.last.flatten.join("").should include("block - down")
99
+ end
100
+
101
+ it "should be able to be used in a middleware stack with the route /is_it_working" do
102
+ app_response = [200, {"Content-Type" => "text/plain"}, ["OK"]]
103
+ app = lambda{|env| app_response}
104
+ check_called = false
105
+ stack = IsItWorking::Handler.new(app) do |h|
106
+ h.check(:test){|status| check_called = true; status.ok("Woot!")}
107
+ end
108
+
109
+ stack.call("PATH_INFO" => "/").should == app_response
110
+ check_called.should == false
111
+ stack.call("PATH_INFO" => "/is_it_working").last.flatten.join("").should include("Woot!")
112
+ check_called.should == true
113
+ end
114
+
115
+ it "should be able to be used in a middleware stack with a custom route" do
116
+ app_response = [200, {"Content-Type" => "text/plain"}, ["OK"]]
117
+ app = lambda{|env| app_response}
118
+ check_called = false
119
+ stack = IsItWorking::Handler.new(app, "/woot") do |h|
120
+ h.check(:test){|status| check_called = true; status.ok("Woot!")}
121
+ end
122
+
123
+ stack.call("PATH_INFO" => "/is_it_working").should == app_response
124
+ check_called.should == false
125
+ stack.call("PATH_INFO" => "/woot").last.flatten.join("").should include("Woot!")
126
+ check_called.should == true
127
+ end
128
+
129
+ it "should be able to synchronize access to a block" do
130
+ handler = IsItWorking::Handler.new
131
+ handler.synchronize{1}.should == 1
132
+ handler.synchronize{2}.should == 2
133
+ end
134
+
135
+ it "should be able to set the host name reported in the output" do
136
+ handler = IsItWorking::Handler.new
137
+ handler.hostname = "woot"
138
+ handler.call("PATH_INFO" => "/is_it_working").last.join("").should include("woot")
139
+ end
140
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe IsItWorking::MemcacheCheck do
4
+
5
+ let(:status){ IsItWorking::Status.new(:memcache) }
6
+ let(:memcache){ MemCache.new(['cache-1.example.com', 'cache-2.example.com']) }
7
+ let(:servers){ memcache.servers }
8
+
9
+ it "should succeed if all servers are responding" do
10
+ check = IsItWorking::MemcacheCheck.new(:cache => memcache)
11
+ servers.first.should_receive(:socket).and_return(mock(:socket))
12
+ servers.last.should_receive(:socket).and_return(mock(:socket))
13
+ check.call(status)
14
+ status.should be_success
15
+ status.messages.first.message.should == "cache-1.example.com:11211 is available"
16
+ status.messages.last.message.should == "cache-2.example.com:11211 is available"
17
+ end
18
+
19
+ it "should fail if any server is not responding" do
20
+ check = IsItWorking::MemcacheCheck.new(:cache => memcache)
21
+ servers.first.should_receive(:socket).and_return(mock(:socket))
22
+ servers.last.should_receive(:socket).and_return(nil)
23
+ check.call(status)
24
+ status.should_not be_success
25
+ status.messages.first.message.should == "cache-1.example.com:11211 is available"
26
+ status.messages.last.message.should == "cache-2.example.com:11211 is not available"
27
+ end
28
+
29
+ it "should be able to get the MemCache object from an ActiveSupport::Cache" do
30
+ require 'active_support/cache'
31
+ rails_cache = ActiveSupport::Cache::MemCacheStore.new(memcache)
32
+ check = IsItWorking::MemcacheCheck.new(:cache => rails_cache)
33
+ servers.first.should_receive(:socket).and_return(mock(:socket))
34
+ servers.last.should_receive(:socket).and_return(mock(:socket))
35
+ check.call(status)
36
+ status.should be_success
37
+ status.messages.first.message.should == "cache-1.example.com:11211 is available"
38
+ status.messages.last.message.should == "cache-2.example.com:11211 is available"
39
+ end
40
+
41
+ it "should be able to alias the memcache host names in the output" do
42
+ check = IsItWorking::MemcacheCheck.new(:cache => memcache, :alias => "memcache")
43
+ servers.first.should_receive(:socket).and_return(mock(:socket))
44
+ servers.last.should_receive(:socket).and_return(mock(:socket))
45
+ check.call(status)
46
+ status.should be_success
47
+ status.messages.first.message.should == "memcache 1 is available"
48
+ status.messages.last.message.should == "memcache 2 is available"
49
+ end
50
+
51
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe IsItWorking::PingCheck do
4
+
5
+ let(:status){ IsItWorking::Status.new(:ping) }
6
+
7
+ it "should succeed if the host is accepting connections on the specified port" do
8
+ server = TCPServer.new(51123)
9
+ begin
10
+ check = IsItWorking::PingCheck.new(:host => "localhost", :port => 51123)
11
+ check.call(status)
12
+ status.should be_success
13
+ status.messages.first.message.should == "localhost is accepting connections on port 51123"
14
+ ensure
15
+ server.close
16
+ end
17
+ end
18
+
19
+ it "should fail if the host is not accepting connections on the specified port" do
20
+ check = IsItWorking::PingCheck.new(:host => "localhost", :port => 51123)
21
+ check.call(status)
22
+ status.should_not be_success
23
+ status.messages.first.message.should == "localhost is not accepting connections on port 51123"
24
+ end
25
+
26
+ it "should fail if the host cannot be found" do
27
+ check = IsItWorking::PingCheck.new(:host => "127.0.0.256", :port => 51123)
28
+ check.call(status)
29
+ status.should_not be_success
30
+ status.messages.first.message.should include("failed")
31
+ end
32
+
33
+ it "should fail if the server takes too long to respond" do
34
+ check = IsItWorking::PingCheck.new(:host => "127.0.0.255", :port => 51123, :timeout => 0.01)
35
+ check.call(status)
36
+ status.should_not be_success
37
+ status.messages.first.message.should == "127.0.0.255 did not respond on port 51123 within 0.01 seconds"
38
+ end
39
+
40
+ it "should be able to alias the host name in the output" do
41
+ check = IsItWorking::PingCheck.new(:host => "localhost", :port => 51123, :alias => "secret server")
42
+ check.call(status)
43
+ status.should_not be_success
44
+ status.messages.first.message.should == "secret server is not accepting connections on port 51123"
45
+ end
46
+
47
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ begin
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ add_filter "/spec/"
6
+ end
7
+ rescue LoadError
8
+ # simplecov not installed
9
+ end
10
+
11
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'is_it_working'))
@@ -0,0 +1,44 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe IsItWorking::Status do
4
+
5
+ let(:status){ IsItWorking::Status.new(:test) }
6
+
7
+ it "should have a name" do
8
+ status.name.should == :test
9
+ end
10
+
11
+ it "should have errors" do
12
+ status.fail("boom")
13
+ status.should_not be_success
14
+ status.messages.size.should == 1
15
+ status.messages.first.should_not be_ok
16
+ status.messages.first.message.should == "boom"
17
+ end
18
+
19
+ it "should have successes" do
20
+ status.ok("wow")
21
+ status.should be_success
22
+ status.messages.size.should == 1
23
+ status.messages.first.should be_ok
24
+ status.messages.first.message.should == "wow"
25
+ end
26
+
27
+ it "should have both errors and successes" do
28
+ status.fail("boom")
29
+ status.ok("wow")
30
+ status.should_not be_success
31
+ status.messages.size.should == 2
32
+ status.messages.first.should_not be_ok
33
+ status.messages.first.message.should == "boom"
34
+ status.messages.last.should be_ok
35
+ status.messages.last.message.should == "wow"
36
+ end
37
+
38
+ it "should have a time" do
39
+ status.time.should == nil
40
+ status.time = 0.1
41
+ status.time.should == 0.1
42
+ end
43
+
44
+ end
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+ require 'webmock/rspec'
3
+
4
+ describe IsItWorking::UrlCheck do
5
+
6
+ before :all do
7
+ WebMock.disable_net_connect!
8
+ end
9
+
10
+ after :all do
11
+ WebMock.allow_net_connect!
12
+ end
13
+
14
+ after :each do
15
+ WebMock.reset!
16
+ end
17
+
18
+ let(:status){ IsItWorking::Status.new(:url) }
19
+
20
+ it "should succeed if the URL returns a 2xx response" do
21
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [200, "Success"])
22
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1")
23
+ check.call(status)
24
+ status.should be_success
25
+ status.messages.first.message.should == "GET http://example.com/test?a=1 responded with response '200 Success'"
26
+ end
27
+
28
+ it "should fail if the URL returns a 1xx response" do
29
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [150, "Continue"])
30
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1")
31
+ check.call(status)
32
+ status.should_not be_success
33
+ status.messages.first.message.should == "GET http://example.com/test?a=1 failed with response '150 Continue'"
34
+ end
35
+
36
+ it "should fail if the URL returns a 3xx response" do
37
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [302, "Found"])
38
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1")
39
+ check.call(status)
40
+ status.should_not be_success
41
+ status.messages.first.message.should == "GET http://example.com/test?a=1 failed with response '302 Found'"
42
+ end
43
+
44
+ it "should fail if the URL returns a 4xx response" do
45
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [404, "Not Found"])
46
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1")
47
+ check.call(status)
48
+ status.should_not be_success
49
+ status.messages.first.message.should == "GET http://example.com/test?a=1 failed with response '404 Not Found'"
50
+ end
51
+
52
+ it "should fail if the URL returns a 5xx response" do
53
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [503, "Service Unavailable"])
54
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1")
55
+ check.call(status)
56
+ status.should_not be_success
57
+ status.messages.first.message.should == "GET http://example.com/test?a=1 failed with response '503 Service Unavailable'"
58
+ end
59
+
60
+ it "should send basic authentication" do
61
+ stub_request(:get, "user:passwd@example.com/test?a=1").to_return(:status => [200, "Success"])
62
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1", :username => "user", :password => "passwd")
63
+ check.call(status)
64
+ status.should be_success
65
+ status.messages.first.message.should == "GET http://example.com/test?a=1 responded with response '200 Success'"
66
+ end
67
+
68
+ it "should send headers with the request" do
69
+ stub_request(:get, "example.com/test?a=1").to_return(:status => [200, "Success"])
70
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test?a=1", :headers => {"Accept-Encoding" => "gzip"})
71
+ check.call(status)
72
+ status.should be_success
73
+ status.messages.first.message.should == "GET http://example.com/test?a=1 responded with response '200 Success'"
74
+ WebMock.should have_requested(:get, "example.com/test?a=1").with(:headers => {"Accept-Encoding" => "gzip"})
75
+ end
76
+
77
+ it "should use SSL connection if URL is HTTPS" do
78
+ http = Net::HTTP.new('localhost')
79
+ Net::HTTP.should_receive(:new).with('localhost', 443).and_return(http)
80
+ request = Net::HTTP::Get.new("/test?a=1")
81
+ Net::HTTP::Get.should_receive(:new).with("/test?a=1", {}).and_return(request)
82
+ http.should_receive(:start).and_yield
83
+ http.should_receive(:request).with(request).and_return(Net::HTTPSuccess.new(nil, "200", "Success"))
84
+
85
+ check = IsItWorking::UrlCheck.new(:get => "https://localhost/test?a=1")
86
+ check.call(status)
87
+ status.should be_success
88
+ status.messages.first.message.should == "GET https://localhost/test?a=1 responded with response '200 Success'"
89
+ http.use_ssl?.should == true
90
+ http.verify_mode.should == OpenSSL::SSL::VERIFY_PEER
91
+ end
92
+
93
+ it "should be able to alias the URL in the output" do
94
+ stub_request(:get, "example.com/test").to_return(:status => [200, "Success"])
95
+ check = IsItWorking::UrlCheck.new(:get => "http://example.com/test", :alias => "service ping URL")
96
+ check.call(status)
97
+ status.should be_success
98
+ status.messages.first.message.should == "GET service ping URL responded with response '200 Success'"
99
+ end
100
+
101
+ it "should try to get the URL through a proxy" do
102
+ http = Net::HTTP.new('localhost')
103
+ Net::HTTP.should_receive(:Proxy).with('localhost', 8080, "user", "passwd").and_return(Net::HTTP)
104
+ Net::HTTP.should_receive(:new).with('localhost', 80).and_return(http)
105
+ request = Net::HTTP::Get.new("/test?a=1")
106
+ Net::HTTP::Get.should_receive(:new).with("/test?a=1", {}).and_return(request)
107
+ http.should_receive(:start).and_yield
108
+ http.should_receive(:request).with(request).and_return(Net::HTTPSuccess.new(nil, "200", "Success"))
109
+
110
+ check = IsItWorking::UrlCheck.new(:get => "http://localhost/test?a=1", :proxy => {:host => "localhost", :port => 8080, :username => "user", :password => "passwd"})
111
+ check.call(status)
112
+ status.should be_success
113
+ status.messages.first.message.should == "GET http://localhost/test?a=1 responded with response '200 Success'"
114
+ end
115
+
116
+ it "should set open and read timeouts" do
117
+ http = Net::HTTP.new('localhost')
118
+ Net::HTTP.should_receive(:new).with('localhost', 80).and_return(http)
119
+ request = Net::HTTP::Get.new("/test?a=1")
120
+ Net::HTTP::Get.should_receive(:new).with("/test?a=1", {}).and_return(request)
121
+ http.should_receive(:start).and_yield
122
+ http.should_receive(:request).with(request).and_return(Net::HTTPSuccess.new(nil, "200", "Success"))
123
+
124
+ check = IsItWorking::UrlCheck.new(:get => "http://localhost/test?a=1", :open_timeout => 1, :read_timeout => 2)
125
+ check.call(status)
126
+ status.should be_success
127
+ status.messages.first.message.should == "GET http://localhost/test?a=1 responded with response '200 Success'"
128
+ http.open_timeout.should == 1
129
+ http.read_timeout.should == 2
130
+ end
131
+
132
+ it "should fail on a timeout" do
133
+ http = Net::HTTP.new('localhost')
134
+ Net::HTTP.should_receive(:new).with('localhost', 80).and_return(http)
135
+ request = Net::HTTP::Get.new("/test?a=1")
136
+ Net::HTTP::Get.should_receive(:new).with("/test?a=1", {}).and_return(request)
137
+ http.should_receive(:start).and_raise(TimeoutError)
138
+
139
+ check = IsItWorking::UrlCheck.new(:get => "http://localhost/test?a=1", :open_timeout => 1, :read_timeout => 2)
140
+ check.call(status)
141
+ status.should_not be_success
142
+ status.messages.first.message.should match(/GET http:\/\/localhost\/test\?a=1 timed out after .* seconds/)
143
+ end
144
+ end