health_check 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,56 +3,35 @@
3
3
  # Any failure causes exit
4
4
  set -e
5
5
 
6
- rehash=''
7
- if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
8
- echo "Detected user installed rvm"
9
- elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
10
- echo "Detected root installed rvm"
11
- elif [[ -d "$HOME/.rbenv" ]] ; then
12
- echo "Detected rbenv ..."
13
- rehash='rbenv rehash'
14
- else
15
- printf "Note: Neither rvm nor rbenv was not found.\n"
16
- fi
17
-
18
- echo "Checking required commands exist:"
19
- for cmd in bash gem egrep ls tail kill find cpio ruby gem rake bundle
20
- do
21
- echo -n " "
22
- which $cmd || ( echo "Aborting test: Missing $cmd command!" && exit 2 )
23
- done
24
- if ! bundle check ; then
25
- echo "Test aborted (missing required gems)"
26
- exit 2
27
- fi
6
+ case "$1" in
7
+ redo)
8
+ . test/init_variables
9
+ cd $railsapp
10
+ echo Dropping database ...
11
+ $rake db:drop
12
+ echo Removing migrations ...
13
+ rm -f db/migrate/* db/schema.rb
14
+ case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in
15
+ [0-9]*)
16
+ echo 'Jruby requires the database to be created before the server is started: running rake db:migrate'
17
+ $rake db:migrate
18
+ ;;
19
+ esac
20
+ actual_rails_version=`$rails -v`
21
+ ;;
22
+ *)
23
+ . test/setup_railsapp
24
+ ;;
25
+ esac
28
26
 
29
- base_dir=`pwd`
27
+ cd $railsapp
28
+ date > $custom_file
30
29
 
31
30
  testurl="$base_dir/test/testurl"
32
31
  fake_smtp_server="$base_dir/test/fake_smtp_server"
33
32
 
34
- test_dir=`pwd`/tmp
35
- rm -rf tmp/Gemfile* tmp/railsapp tmp/bin tmp/gems
36
- mkdir -p tmp/gems
37
-
38
- [ -d lib/health_check ] || exec echo test MUST be executed in the base of the health_check gem/clone of git repository
39
-
40
- echo Installing health_check as a gem into tmp/gems
41
- env GEM_HOME=$test_dir/gems rake install
42
-
43
- export GEM_PATH="$test_dir/gems:`gem environment gempath`"
44
- echo Set GEM_PATH=$GEM_PATH
45
-
46
- echo Gems in tmp/gems:
47
- ls tmp/gems
48
-
49
- echo GEM LIST
50
- gem list
51
-
52
- echo Environment:
53
- env | egrep 'TRAVIS|RAILS|RUBY|_ENV|GEM|BUNDLE'
54
-
55
33
  server_pid=''
34
+ fake_smtp_pid=''
56
35
 
57
36
  finish()
58
37
  {
@@ -60,24 +39,31 @@ finish()
60
39
  echo ========================================================
61
40
  echo TEST ${1:-FAILED}
62
41
  echo ========================================================
63
- echo Result of: ls -lR $test_dir/railsapp/log $test_dir/railsapp/db
64
- ls -lR $test_dir/railsapp/log $test_dir/railsapp/db
42
+ echo Result of: ls -lR $railsapp/log $railsapp/db
43
+ ls -lR $railsapp/log $railsapp/db
65
44
 
66
- if [ -s $test_dir/railsapp/log/test.log ]
45
+ if [ -s $railsapp/log/test.log ]
67
46
  then
68
47
  echo ========================================================
69
- echo Last 300 lines of test log
70
- tail -300 $test_dir/railsapp/log/test.log
48
+ echo Last 50 lines of test log
49
+ tail -50 $railsapp/log/test.log
71
50
  fi
72
51
 
52
+ case "$fake_smtp_pid" in
53
+ [0-9]*)
54
+ echo ========================================================
55
+ echo "Killing fake smtp server [pid: $fake_smtp_pid]"
56
+ kill -9 $fake_smtp_pid
57
+ ;;
58
+ esac
73
59
  case "$server_pid" in
74
60
  [0-9]*)
75
61
  echo ========================================================
76
62
  echo "Killing rails server [pid: $server_pid]"
77
63
  kill -9 $server_pid
78
- wait
79
64
  ;;
80
65
  esac
66
+ wait
81
67
  trap "" 0
82
68
  echo ========================================================
83
69
  echo TEST ${1:-FAILED}
@@ -87,126 +73,24 @@ finish()
87
73
 
88
74
  trap "finish FAILED 1" 0
89
75
 
90
- rails='rails'
91
- if [ -x bin/rails ]
92
- then
93
- rails="`pwd`/bin/rails"
94
- echo "Using binstub: $rails for rails command"
95
- fi
96
- rake='rake'
97
- if [ -x bin/rake ]
98
- then
99
- rake="`pwd`/bin/rake"
100
- echo "Using binstub: $rake for rake command"
101
- fi
102
-
103
- cd $test_dir
104
-
105
- actual_rails_version=`$rails -v`
106
-
107
- case `ruby -e 'puts JRUBY_VERSION' 2> /dev/null` in
108
- [0-9]*)
109
- db=jdbcsqlite3
110
- # Appears to need a bit extra time
111
- ;;
112
- *)
113
- db=sqlite3
114
- ;;
115
- esac
116
-
117
- echo "Creating $actual_rails_version app in $test_dir/railsapp using adapter $db"
118
- case "$actual_rails_version" in
119
- *' '[12].*)
120
- $rails railsapp -d $db
121
- ;;
122
- *' '[34].*)
123
- $rails new railsapp -d $db
124
- ;;
125
- *)
126
- echo "Unknown rails version"
127
- ;;
128
- esac
129
-
130
- cd railsapp
131
-
132
- echo "Changed current directory to railsapp root: `pwd`"
133
-
134
- echo "Fixing rdoc require in Rakefile if needed"
135
- ruby -p -i.bak -e '$_.gsub!(/rake.rdoctask/, "rdoc/task")' Rakefile
136
-
137
- echo "Configuring mailer to point to fake_smtp_server port 3555"
138
- cat >> config/environment.rb <<'!EOF!'
139
-
140
- ActionMailer::Base.delivery_method = :smtp
141
- ActionMailer::Base.smtp_settings = { :address => "localhost", :port => 3555 }
142
-
143
- !EOF!
144
-
145
- echo "Last ten lines of config/environment.rb:"
146
- tail -10 config/environment.rb
147
- echo
148
-
149
- if [ -s Gemfile ]
150
- then
151
- echo Adding health_check as gem to Gemfile...
152
- echo "gem 'health_check', :path => '$base_dir'" >> Gemfile
153
-
154
- case "$BUNDLE_GEMFILE" in
155
- ?*)
156
- echo Unsetting BUNDLE_GEMFILE '(so Gemfile for rails application will be used)'
157
- unset BUNDLE_GEMFILE
158
- ;;
159
- esac
160
- echo
161
- echo Gemfile contents:
162
- cat Gemfile
163
- echo
164
- echo running bundle install --binstubs
165
- bundle install --binstubs
166
- echo "Using binstubs in `pwd`/bin for rails and rake commands"
167
- rails="`pwd`/bin/rails"
168
- rake="`pwd`/bin/rake"
169
- # Fix for rvm, otherwise bundle run from rails create fails
170
- export PATH="`pwd`/bin:$PATH"
171
- else
172
- dest=`pwd`/vendor/plugins/health_check
173
- echo Installing health_check as plugin in $dest ...
174
- mkdir -p $dest
175
- (
176
- cd $base_dir
177
-
178
- find . | egrep -v '/tmp|/\.git|\.gem$' | cpio -pdl $dest
179
-
180
- cd $dest
181
-
182
- echo Files installed as plugin:
183
- find . -type f
184
- echo
185
- )
186
- fi
187
-
188
- echo Setting RAILS_ENV=test RACK_ENV=test
189
- export RAILS_ENV=test RACK_ENV=test
190
-
191
- case $db in
192
- jdbcsqlite3)
193
- echo
194
- echo 'Jruby requires the dastabase to be created before the server is started: running rake db:migrate'
195
- $rake db:migrate
196
- echo
197
- ;;
198
- esac
199
-
200
76
  port=3456
201
- echo Starting server on port $port ...
202
77
 
203
78
  if [ -x script/server ]
204
79
  then
205
- bundle exec ./script/server webrick -p $port &
80
+ if [ -f Gemfile ]
81
+ then
82
+ echo Starting server on port $port using bundle exec ./script/server ...
83
+ bundle exec ./script/server webrick -p $port &
84
+ else
85
+ echo Starting server on port $port using ./script/server ...
86
+ ./script/server webrick -p $port &
87
+ fi
206
88
  else
89
+ echo Starting server on port $port using $rails s ...
207
90
  $rails s webrick -p $port &
208
91
  fi
209
92
  server_pid=$!
93
+ sleep 17
210
94
 
211
95
  echo STATIC-FILE > public/static.txt
212
96
 
@@ -215,80 +99,119 @@ host=http://localhost:${port}
215
99
  # get a static file
216
100
  echo
217
101
  echo 'TESTING can get a static file ...'
218
- $testurl ${host}/static.txt STATIC-FILE
102
+ $testurl ${host}/static.txt 200 text/plain STATIC-FILE
219
103
  echo
220
104
 
221
105
  echo 'TESTING health_check/migration should pass with no database migrations ...'
222
106
  mkdir -p db/migrate
223
107
  ls db/migrate
224
- $testurl ${host}/health_check/migration success
108
+ $testurl ${host}/health_check/migration 200 text/plain $success
225
109
  echo
226
110
 
227
111
  echo 'TESTING health_check/migration should fail without initial database migration ...'
228
112
  cp $base_dir/test/migrate/nine/* db/migrate
229
113
  ls db/migrate
230
- $testurl ${host}/health_check/migration failed
114
+ $testurl ${host}/health_check/migration 550 text/plain failed
231
115
  echo
232
116
 
233
117
  echo 'TESTING health_check/database should pass without initial database migration (since it ignores the difference) ...'
234
- $testurl ${host}/health_check/database success
118
+ $testurl ${host}/health_check/database 200 text/plain $success
235
119
  echo
236
120
 
237
121
  echo 'TESTING health_check/site should pass ...'
238
- $testurl ${host}/health_check/site success
122
+ $testurl ${host}/health_check/site 200 text/plain $success
239
123
  echo
240
124
 
241
125
  echo 'TESTING health_check/migration should pass after initial database migration ...'
242
126
  $rake db:migrate
243
- $testurl ${host}/health_check/migration success
127
+ $testurl ${host}/health_check/migration 200 text/plain $success
244
128
  echo
245
129
 
246
130
  echo 'TESTING health_check/migration should fail without all migrations ...'
247
131
  cp $base_dir/test/migrate/twelve/* db/migrate
248
132
  ls db/migrate
249
- $testurl ${host}/health_check/migration failed
133
+ $testurl ${host}/health_check/migration 550 text/plain failed
250
134
  echo
251
135
 
252
136
  echo 'TESTING health_check/migration should pass after both database migrations ...'
253
137
  $rake db:migrate
254
- $testurl ${host}/health_check/migration success
138
+ $testurl ${host}/health_check/migration 200 text/plain $success
255
139
  echo
256
140
 
257
141
  echo 'TESTING health_check/migration should pass after both database migrations ...'
258
142
  $rake db:migrate
259
- $testurl ${host}/health_check/migration success
143
+ $testurl ${host}/health_check/migration 200 text/plain $success
260
144
  echo
261
145
 
262
146
  echo 'TESTING health_check/email should fail without smtp available ...'
263
- $testurl ${host}/health_check/email failed
147
+ $testurl ${host}/health_check/email 550 text/plain failed
264
148
  echo
265
149
 
266
150
  echo 'TESTING health_check/email should pass with smtp available ...'
267
151
  $fake_smtp_server &
152
+ fake_smtp_pid=$!
268
153
  sleep 5
269
- $testurl ${host}/health_check/email success
154
+ $testurl ${host}/health_check/email 200 text/plain $success
270
155
  echo
271
156
 
272
157
  echo 'TESTING health_check (all) should fail without smtp available ...'
273
- $testurl ${host}/health_check failed
158
+ $testurl ${host}/health_check 550 text/plain failed
274
159
  echo
275
160
 
276
161
  echo 'TESTING health_check/all should fail without smtp available ...'
277
- $testurl ${host}/health_check failed
162
+ $testurl ${host}/health_check 550 text/plain failed
278
163
  echo
279
164
 
165
+ kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected
280
166
  echo 'TESTING health_check (all) should pass with smtp available ...'
281
167
  $fake_smtp_server &
168
+ fake_smtp_pid=$!
282
169
  sleep 5
283
- $testurl ${host}/health_check success
170
+ $testurl ${host}/health_check 200 text/plain $success
284
171
  echo
285
172
 
173
+ kill -9 $fake_smtp_pid || echo fake_smtp_server had finished as expected
286
174
  echo 'TESTING health_check/all should pass with smtp available ...'
287
175
  $fake_smtp_server &
176
+ fake_smtp_pid=$!
288
177
  sleep 5
289
- $testurl ${host}/health_check/all success
178
+ $testurl ${host}/health_check/all 200 text/plain $success
179
+ echo
180
+
181
+ echo 'TESTING health_check/custom should pass ...'
182
+ $testurl ${host}/health_check/custom 200 text/plain $success
183
+ echo
184
+
185
+ echo 'TESTING health_check/custom.json should pass ...'
186
+ $testurl ${host}/health_check/custom.json 200 application/json '"healthy":true'
187
+ $testurl ${host}/health_check/custom.json 200 application/json "\"message\":\"$success\""
188
+ echo
189
+
190
+ echo 'TESTING health_check/custom.xml should pass ...'
191
+ $testurl ${host}/health_check/custom.xml 200 application/xml '<healthy type="boolean">true</healthy>'
192
+ $testurl ${host}/health_check/custom.xml 200 application/xml "<message>$success</message>"
193
+ echo
194
+
195
+ echo 'TESTING health_check/custom should fail when custom returns string ...'
196
+ rm -f $custom_file
197
+ $testurl ${host}/health_check/custom 550 text/plain failed
198
+ echo
199
+
200
+ echo 'TESTING health_check (all) should fail when custom check fails ...'
201
+ $testurl ${host}/health_check 550 text/plain "$custom_file is missing!"
202
+ echo
203
+
204
+ echo 'TESTING health_check.json (all) should fail when custom check fails ...'
205
+ $testurl ${host}/health_check.json 555 application/json '"healthy":false'
206
+ $testurl ${host}/health_check.json 555 application/json "$custom_file is missing!"
207
+ echo
208
+
209
+ echo 'TESTING health_check.xml (all) should fail when custom check fails ...'
210
+ $testurl ${host}/health_check.xml 555 application/xml '<healthy type="boolean">false</healthy>'
290
211
  echo
291
212
 
213
+ date > $custom_file
214
+
292
215
  finish PASSED 0
293
216
  exit 0
294
217
 
data/test/testurl CHANGED
@@ -3,31 +3,58 @@ require 'net/http'
3
3
  require 'uri'
4
4
 
5
5
  def open(url)
6
- Net::HTTP.get(URI.parse(url))
6
+ Net::HTTP.get_response(URI.parse(url))
7
7
  end
8
8
 
9
- page_content = open(ARGV[0]) rescue nil
10
- unless page_content
9
+ response = open(ARGV[0]) rescue nil
10
+ unless response
11
11
  i=0
12
12
  print "waiting.."
13
- while i < 120 and not page_content
13
+ while i < 120 and not response
14
14
  print "."
15
15
  STDOUT.flush
16
16
  i += 1
17
17
  sleep(1)
18
- page_content = open(ARGV[0]) rescue nil
18
+ response = open(ARGV[0]) rescue nil
19
19
  end
20
- puts "\n #{page_content ? 'got url content' : 'timed out'} after waiting #{i} seconds"
20
+ unless response
21
+ puts "\nFAIL: timed out after waiting #{i} seconds"
22
+ exit 9
23
+ end
24
+ puts "\n got url content after waiting #{i} seconds"
21
25
  end
22
26
 
23
- puts page_content
27
+ page_content = response.body
28
+
29
+ puts " response code: #{response.code} #{response.message}"
30
+ puts " content_type: #{response.content_type}"
31
+ puts " body: #{page_content}"
32
+
24
33
  if ARGV[1]
25
- if page_content.to_s.include? ARGV[1]
26
- puts "PASS (found #{ARGV[1]})"
27
- exit 0
34
+ if response.code == ARGV[1]
35
+ puts "PASS (response code was #{ARGV[1]})"
36
+ else
37
+ puts "FAIL (response code was #{response.code}, expected #{ARGV[1]})"
38
+ exit 1
39
+ end
40
+ end
41
+
42
+ if ARGV[2]
43
+ if response.content_type == ARGV[2]
44
+ puts "PASS (content type was #{response.content_type})"
28
45
  else
29
- puts "FAIL (could not find #{ARGV[1]})"
46
+ puts "FAIL (content type was #{response.content_type}, expected #{ARGV[2]})"
30
47
  exit 2
31
48
  end
32
49
  end
33
- exit(page_content.nil? ? 1 : 0)
50
+
51
+ if ARGV[3]
52
+ if page_content.to_s.include? ARGV[3]
53
+ puts "PASS (found #{ARGV[3]})"
54
+ else
55
+ puts "FAIL (could not find #{ARGV[3]})"
56
+ exit 3
57
+ end
58
+ end
59
+
60
+ exit 0