Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding healthcheck endpoint #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -34,5 +47,6 @@ PLATFORMS

DEPENDENCIES
fleet-api (= 0.6.1)
rspec (~> 3.0)
sinatra
sinatra-contrib
6 changes: 6 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -18,6 +23,7 @@ class App < Sinatra::Application
disable :static
end

use FleetAdapter::Routes::Healthcheck
use FleetAdapter::Routes::Services
end
end
Expand Down
1 change: 1 addition & 0 deletions app/routes.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions app/routes/healthcheck.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions spec/api/healthcheck_spec.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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