mini_check 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmI4NmNjN2U5YTMyZDQzOTE2NmRmYmU4NGJmOWZkZjc4OWJkNmM2Nw==
4
+ MTNjYTk4MjlmODFmYzMwNjhhNTE5MzA5MGJjNTg5MTBlMmVkNzQ4Yw==
5
5
  data.tar.gz: !binary |-
6
- ZTE2NGIxNzQ4OGFlZTk1MTc5OTgwODljMGJmN2FlM2Q5NTcwOGE2Mw==
6
+ MGNjNDMwMmNmYTM2NWNhYWNhMDUxMmQ1NmM5ZGRiYWEwOTk2MzlhYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MGY3YTY5YTVmNTE1NjRmMGE1Y2U4MGJjMGQ4Njk5ZjZmMzY2MDM2MGU5Yzg4
10
- YTA1ZmZiMDIxZjk0ODFjYTc1NzlmMWI4MGNkYWVhMzEwODg5MTJmOGMwNjdm
11
- NWZlMGVkYjdhZDBiMzg4YzMzZjM3Y2VkNDA1ZWU5NGVhYWE1NmQ=
9
+ MTUwNmZjYTY3YmE0NmIxMzJjN2IyOTVjYjAwMzBkMGExOWMzYjU2Y2Y5NGU1
10
+ YTE1NDFmNGQ2MTRjZDgwOTVmYjkxOGRmMWFmMzk3YTc1YjI3MjQ1N2U0NjYx
11
+ NWVjZTA5MmRkYzA4ODFkOWU4N2NhMjE1ODg4YThjMmNjOTA5Njg=
12
12
  data.tar.gz: !binary |-
13
- OTJiNmUwNjFlOTNlNzM1ZGNiNzBkMjgzZTYxMTcxZTQ1YTczMTQ3YzA0ODZm
14
- MmFhOTY0ODg2NDgzMzViNGQzMDcyNTQ2YWY4MzFhZmIyMjcyMDA3NjVmMTgx
15
- NjIwMzFmZDM2ZGMzNjI2NDA4OTZkN2MzZGUzY2ZhNTAzMTJmNDY=
13
+ ODJmZmZlNWI3ODc0YjM1Y2E3NTE5NDg5MGU4NzNlMzlhOGNjZGMwYTg0NGU0
14
+ ZWRkNDAzYmZkNzAxMTk0ODZkNDVmMGRmNWY1YWMxN2M2YTllNTQ1NWIwZmQy
15
+ ODJlMTJlMTE3YTFjZjc4ODNiYmY1MTNlYWRlZjA5MDE2ODI3MDA=
data/Gemfile CHANGED
@@ -9,4 +9,3 @@ group :test, :development do
9
9
  gem 'rack-test'
10
10
  gem 'codeclimate-test-reporter', require: false
11
11
  end
12
-
data/README.md CHANGED
@@ -22,10 +22,8 @@ MyHealthCheck.register('health.db_connection'){ MyApp.db_connection.fetch('show
22
22
  Mount it in your `config.ru`:
23
23
 
24
24
  ```ruby
25
- run Rack::Cascade.new([
26
- MyHealthCheck,
27
- MyApp,
28
- ])
25
+ use MyHealthCheck
26
+ run MyApp
29
27
  ```
30
28
 
31
29
  If you now visit `http://localhost:XXXX/healthcheck` you should get something like:
@@ -8,7 +8,7 @@ module MiniCheck
8
8
  def initialize args = {}, &block
9
9
  args = {name: args} if !args.is_a?(Hash)
10
10
  args[:action] = block if block_given?
11
-
11
+
12
12
  set_attributes args
13
13
  end
14
14
 
@@ -29,12 +29,19 @@ module MiniCheck
29
29
  def to_hash
30
30
  {}.tap do |h|
31
31
  h[:healthy] = healthy?
32
- h[:error] = {message: exception.message, stack: exception.backtrace} if exception
32
+ h[:error] = error_hash if exception
33
33
  end
34
34
  end
35
35
 
36
36
  private
37
37
 
38
+ def error_hash
39
+ {
40
+ message: exception.message,
41
+ stack: exception.backtrace
42
+ }
43
+ end
44
+
38
45
  def set_attributes args = {}
39
46
  args.each do |k,v|
40
47
  send("#{k}=", v)
@@ -1,24 +1,19 @@
1
1
  module MiniCheck
2
2
  class ChecksCollection < Array
3
3
  def to_hash
4
- resp = {}
5
- each.map do |check|
6
- resp[check.name] = check.to_hash
7
- end
8
-
9
- resp
4
+ Hash[map { |check| [check.name, check.to_hash] }]
10
5
  end
11
6
 
12
7
  def healthy?
13
- !detect{|c| !c.healthy? }
8
+ all?(&:healthy?)
14
9
  end
15
10
 
16
11
  def run
17
- each{|c| c.run }
12
+ each(&:run)
18
13
  end
19
-
14
+
20
15
  def register name, &block
21
- self.<< Check.new(name, &block)
16
+ push(Check.new(name, &block))
22
17
  end
23
18
  end
24
19
  end
@@ -17,7 +17,7 @@ module MiniCheck
17
17
  checks.run
18
18
  [status, headers, [body]]
19
19
  else
20
- [404, headers, []]
20
+ host_app.call(env)
21
21
  end
22
22
  end
23
23
 
@@ -25,6 +25,12 @@ module MiniCheck
25
25
  checks.register name, &block
26
26
  end
27
27
 
28
+ def new(app)
29
+ copy = self.dup
30
+ copy.host_app = app
31
+ copy
32
+ end
33
+
28
34
  private
29
35
 
30
36
  def set_attributes args = {}
@@ -41,12 +47,16 @@ module MiniCheck
41
47
  checks.to_hash.to_json
42
48
  end
43
49
 
44
- def generally_healthy?
45
- checks.healthy?
50
+ def status
51
+ checks.healthy? ? 200 : 500
46
52
  end
47
53
 
48
- def status
49
- generally_healthy? ? 200 : 500
54
+ protected
55
+
56
+ attr_accessor :host_app
57
+
58
+ def host_app
59
+ @host_app ||= lambda{|env| [404, {}, []]}
50
60
  end
51
61
  end
52
62
  end
@@ -1,3 +1,3 @@
1
1
  module MiniCheck
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -34,10 +34,16 @@ describe MiniCheck::RackApp do
34
34
  end
35
35
 
36
36
  context 'unknown path' do
37
- it 'returns status 404' do
37
+ it 'returns status 404 when no host app is given' do
38
38
  get '/blahblah'
39
39
  expect(status).to eq(404)
40
40
  end
41
+
42
+ it 'delegates to the host app if given' do
43
+ app.send(:host_app=, lambda{|env| [999, {}, []] })
44
+ get '/blahblah'
45
+ expect(status).to eq(999)
46
+ end
41
47
  end
42
48
 
43
49
  context 'unknown verb' do
@@ -68,22 +74,98 @@ describe MiniCheck::RackApp do
68
74
  end
69
75
 
70
76
  it 'calls run on the checks' do
71
- expect(checks).to receive(:run).and_return(true)
77
+ expect(checks).to receive(:run)
72
78
  do_request
73
79
  end
74
80
 
75
- it 'returns each check' do
76
- expect(checks).to receive(:healthy?).and_return(true)
81
+ it 'has status 200 when checks are healthy' do
82
+ allow(checks).to receive(:healthy?).and_return(true)
77
83
  do_request
84
+ expect(status).to eq(200)
78
85
  end
79
86
 
80
- it 'has status 500 when one check is failing' do
81
- expect(checks).to receive(:healthy?).and_return(true)
87
+ it 'has status 500 when checks are failing' do
88
+ allow(checks).to receive(:healthy?).and_return(false)
82
89
  do_request
90
+ expect(status).to eq(500)
91
+ end
92
+ end
93
+
94
+ describe 'rack integration' do
95
+ describe 'mounted with cascade' do
96
+ let(:check_app){ MiniCheck::RackApp.new path: '/health'}
97
+ let(:host_app){ lambda{|env| [host_app_status, {}, host_app_body] } }
98
+ let(:host_app_status){ 201 }
99
+ let(:host_app_body){ 'Host app talking' }
100
+
101
+ let(:app) do
102
+ rack_app = Rack::Cascade.new([check_app, host_app])
103
+ Rack::Builder.new do
104
+ run rack_app
105
+ end
106
+ end
107
+
108
+ it 'mini_check replies if the request matches' do
109
+ get '/health'
110
+ expect(last_response.status).to eq(check_app.send(:status))
111
+ expect(last_response.body).to eq(check_app.send(:body))
112
+ end
113
+
114
+ it 'host_app replies if the request doesn\'t match' do
115
+ get '/somehting_else'
116
+ expect(last_response.status).to eq(host_app_status)
117
+ expect(last_response.body).to eq(host_app_body)
118
+ end
119
+ end
120
+
121
+ describe 'mounted as a middleware' do
122
+ let(:check_app){ MiniCheck::RackApp.new path: '/health'}
123
+ let(:host_app){ lambda{|env| [host_app_status, {}, host_app_body] } }
124
+ let(:host_app_status){ 201 }
125
+ let(:host_app_body){ 'Host app talking' }
126
+
127
+ let(:app) do
128
+ rack_app = Rack::Builder.new
129
+ rack_app.use check_app
130
+ rack_app.run host_app
131
+ rack_app
132
+ end
133
+
134
+ it 'mini_check replies if the request matches' do
135
+ get '/health'
136
+ expect(last_response.status).to eq(check_app.send(:status))
137
+ expect(last_response.body).to eq(check_app.send(:body))
138
+ end
139
+
140
+ it 'host_app replies if the request doesn\'t match' do
141
+ get '/somehting_else'
142
+ expect(last_response.status).to eq(host_app_status)
143
+ expect(last_response.body).to eq(host_app_body)
144
+ end
83
145
  end
84
146
  end
85
147
  end
86
148
 
149
+ describe 'new' do
150
+ let(:host_app){ double('host_app') }
151
+
152
+ it 'returns a copy of itself' do
153
+ copy = subject.new(host_app)
154
+ expect(copy).not_to be(subject)
155
+ expect(copy).to be_a(MiniCheck::RackApp)
156
+ end
157
+
158
+ it 'sets host_app as the argument on the copy, not itself' do
159
+ copy = subject.new(host_app)
160
+ expect(copy.send(:host_app)).to be(host_app)
161
+ expect(subject.send(:host_app)).not_to be(host_app)
162
+ end
163
+
164
+ it 'sets its checks to the copy' do
165
+ copy = subject.new(host_app)
166
+ expect(copy.checks).to be(subject.checks)
167
+ end
168
+ end
87
169
  describe 'register' do
88
170
  it 'forwards to checks' do
89
171
  name = 'asd'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Morales
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-22 00:00:00.000000000 Z
11
+ date: 2014-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler