From 01e837091bfb3f686b172eb0dfedd3a12117568e Mon Sep 17 00:00:00 2001 From: Brian DeHamer Date: Fri, 31 Oct 2014 08:03:35 -0700 Subject: [PATCH] Adding healthcheck endpoint --- Gemfile | 4 ++++ Gemfile.lock | 14 ++++++++++++ app.rb | 6 ++++++ app/routes.rb | 1 + app/routes/healthcheck.rb | 18 ++++++++++++++++ spec/api/healthcheck_spec.rb | 41 ++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 17 +++++++++++++++ 7 files changed, 101 insertions(+) create mode 100644 app/routes/healthcheck.rb create mode 100644 spec/api/healthcheck_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile index 12bf45d..bbd7505 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,7 @@ ruby '2.1.2' gem 'sinatra', require: 'sinatra/base' gem 'sinatra-contrib' gem 'fleet-api', '0.6.1', require: 'fleet' + +group :test do + gem 'rspec', '~> 3.0' +end diff --git a/Gemfile.lock b/Gemfile.lock index 480b115..e083281 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ GEM remote: https://rubygems.org/ specs: backports (3.6.0) + diff-lcs (1.2.5) faraday (0.8.9) multipart-post (~> 1.2.0) faraday_middleware (0.9.0) @@ -16,6 +17,18 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) + rspec-expectations (3.1.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) + rspec-support (3.1.2) sinatra (1.4.5) rack (~> 1.4) rack-protection (~> 1.4) @@ -34,5 +47,6 @@ PLATFORMS DEPENDENCIES fleet-api (= 0.6.1) + rspec (~> 3.0) sinatra sinatra-contrib diff --git a/app.rb b/app.rb index 560156d..568b7fb 100644 --- a/app.rb +++ b/app.rb @@ -1,10 +1,15 @@ require 'rubygems' require 'bundler' +require 'fleet' # Setup load paths Bundler.require $: << File.expand_path('../', __FILE__) +Fleet.configure do |config| + config.fleet_api_url = ENV['FLEETCTL_ENDPOINT'] +end + # Require base require 'sinatra/base' @@ -18,6 +23,7 @@ class App < Sinatra::Application disable :static end + use FleetAdapter::Routes::Healthcheck use FleetAdapter::Routes::Services end end diff --git a/app/routes.rb b/app/routes.rb index 5e0d915..721f8e0 100644 --- a/app/routes.rb +++ b/app/routes.rb @@ -1,6 +1,7 @@ module FleetAdapter module Routes autoload :Base, 'app/routes/base' + autoload :Healthcheck, 'app/routes/healthcheck' autoload :Services, 'app/routes/services' end end diff --git a/app/routes/healthcheck.rb b/app/routes/healthcheck.rb new file mode 100644 index 0000000..32933f2 --- /dev/null +++ b/app/routes/healthcheck.rb @@ -0,0 +1,18 @@ +module FleetAdapter + module Routes + class Healthcheck < Base + + get '/healthcheck' do + headers 'Content-Type' => 'text/plain' + + begin + Fleet.new.list_machines + 'true' + rescue + 'false' + end + end + + end + end +end diff --git a/spec/api/healthcheck_spec.rb b/spec/api/healthcheck_spec.rb new file mode 100644 index 0000000..4cafb6a --- /dev/null +++ b/spec/api/healthcheck_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe FleetAdapter::Routes::Healthcheck do + + describe 'GET /healthcheck' do + + before do + allow_any_instance_of(Fleet::Client).to receive(:list_machines) + end + + it 'has a text/plain Content-Type' do + get '/healthcheck' + expect(last_response.headers['Content-Type']).to eq 'text/plain' + end + + it 'returns a 200 status' do + get '/healthcheck' + expect(last_response.status).to eq 200 + end + + context 'when Fleet is healthy' do + it 'returns true' do + get '/healthcheck' + expect(last_response.body).to eq 'true' + end + end + + context 'when Fleet is NOT healthy' do + + before do + allow_any_instance_of(Fleet::Client).to receive(:list_machines) + .and_raise('oops') + end + + it 'returns false' do + get '/healthcheck' + expect(last_response.body).to eq 'false' + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..0687504 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,17 @@ +require 'rack/test' + +require File.expand_path '../../app.rb', __FILE__ + +ENV['RACK_ENV'] = 'test' + +module ApiType + include Rack::Test::Methods + + def app + FleetAdapter::App + end +end + +RSpec.configure do |c| + c.include ApiType, type: :api, file_path: %r(spec/api) +end