mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 10:08:40 -05:00
Add healthcheck endpoint, spec testing
This commit is contained in:
parent
4d06c01d21
commit
08132f75bd
4 changed files with 76 additions and 2 deletions
2
Gemfile
2
Gemfile
|
|
@ -6,7 +6,7 @@ gem 'puma', '~> 4.3'
|
||||||
gem 'rack', '~> 2.2'
|
gem 'rack', '~> 2.2'
|
||||||
gem 'rake', '~> 13.0'
|
gem 'rake', '~> 13.0'
|
||||||
gem 'redis', '~> 4.1'
|
gem 'redis', '~> 4.1'
|
||||||
gem 'rbvmomi', '~> 2.1'
|
gem 'rbvmomi', '~> 2.4', '>= 2.4.1'
|
||||||
gem 'sinatra', '~> 2.0'
|
gem 'sinatra', '~> 2.0'
|
||||||
gem 'prometheus-client', '~> 2.0'
|
gem 'prometheus-client', '~> 2.0'
|
||||||
gem 'net-ldap', '~> 0.16'
|
gem 'net-ldap', '~> 0.16'
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
module Vmpooler
|
module Vmpooler
|
||||||
class API < Sinatra::Base
|
class API < Sinatra::Base
|
||||||
# Load API components
|
# Load API components
|
||||||
%w[helpers dashboard reroute v1 request_logger].each do |lib|
|
%w[helpers dashboard reroute v1 request_logger healthcheck].each do |lib|
|
||||||
require "vmpooler/api/#{lib}"
|
require "vmpooler/api/#{lib}"
|
||||||
end
|
end
|
||||||
# Load dashboard components
|
# Load dashboard components
|
||||||
|
|
@ -40,6 +40,10 @@ module Vmpooler
|
||||||
require 'prometheus/middleware/exporter'
|
require 'prometheus/middleware/exporter'
|
||||||
use Vmpooler::Metrics::Promstats::CollectorMiddleware, metrics_prefix: "#{metrics.prometheus_prefix}_http"
|
use Vmpooler::Metrics::Promstats::CollectorMiddleware, metrics_prefix: "#{metrics.prometheus_prefix}_http"
|
||||||
use Prometheus::Middleware::Exporter, path: metrics.prometheus_endpoint
|
use Prometheus::Middleware::Exporter, path: metrics.prometheus_endpoint
|
||||||
|
# Note that a user may want to use this check without prometheus
|
||||||
|
# However, prometheus setup includes the web server which is required for this check
|
||||||
|
# At this time prometheus is a requirement of using the health check on manager
|
||||||
|
use Vmpooler::API::Healthcheck
|
||||||
end
|
end
|
||||||
|
|
||||||
if torun.include? :api
|
if torun.include? :api
|
||||||
|
|
|
||||||
14
lib/vmpooler/api/healthcheck.rb
Normal file
14
lib/vmpooler/api/healthcheck.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Vmpooler
|
||||||
|
class API
|
||||||
|
class Healthcheck < Sinatra::Base
|
||||||
|
get '/healthcheck/?' do
|
||||||
|
content_type :json
|
||||||
|
|
||||||
|
status 200
|
||||||
|
JSON.pretty_generate({ 'ok' => true })
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
56
spec/integration/api/healthcheck_spec.rb
Normal file
56
spec/integration/api/healthcheck_spec.rb
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
require 'rack/test'
|
||||||
|
|
||||||
|
describe Vmpooler::API::Healthcheck do
|
||||||
|
include Rack::Test::Methods
|
||||||
|
|
||||||
|
def app()
|
||||||
|
Vmpooler::API
|
||||||
|
end
|
||||||
|
|
||||||
|
# Added to ensure no leakage in rack state from previous tests.
|
||||||
|
# Removes all routes, filters, middleware and extension hooks from the current class
|
||||||
|
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
|
||||||
|
before(:each) do
|
||||||
|
app.reset!
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:config) {
|
||||||
|
{
|
||||||
|
config: {
|
||||||
|
'site_name' => 'test pooler',
|
||||||
|
'vm_lifetime_auth' => 2,
|
||||||
|
},
|
||||||
|
pools: [
|
||||||
|
{'name' => 'pool1', 'size' => 5, 'alias' => ['poolone', 'poolun']},
|
||||||
|
{'name' => 'pool2', 'size' => 10},
|
||||||
|
{'name' => 'pool3', 'size' => 10, 'alias' => 'NotArray'}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let(:current_time) { Time.now }
|
||||||
|
|
||||||
|
let(:metrics) {
|
||||||
|
double("metrics")
|
||||||
|
}
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
expect(app).to receive(:run!).once
|
||||||
|
expect(metrics).to receive(:setup_prometheus_metrics)
|
||||||
|
expect(metrics).to receive(:prometheus_prefix)
|
||||||
|
expect(metrics).to receive(:prometheus_endpoint)
|
||||||
|
app.execute([:api], config, redis, metrics, nil)
|
||||||
|
app.settings.set :config, auth: false
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '/healthcheck' do
|
||||||
|
it 'returns OK' do
|
||||||
|
get "/healthcheck"
|
||||||
|
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||||
|
expect(last_response.status).to eq(200)
|
||||||
|
result = JSON.parse(last_response.body)
|
||||||
|
expect(result).to eq({'ok' => true})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue