mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
(POOLER-160) Add Prometheus to pooler startup
This is a re-architect of the vmpooler initialisation code to: 1. Allow an API service for both manager and the api 2. Add the Prometheus endpoints to the web service. Needed to change the way the Rack Service is started as instantiating using ".New" leads to a failure to initialise the http Stats collection. 3. Selectively load the pooler api and/or Prometheus endpoints. 4. Rework API Spec tests for revised API loading. Needed to tidy up the initialisation and perform a reset! after each test to avoid "leaks" and dependencies between the tests.
This commit is contained in:
parent
ffab7def9e
commit
bbd76bde4c
15 changed files with 166 additions and 88 deletions
|
|
@ -8,6 +8,13 @@ describe Vmpooler::API::V1 do
|
|||
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: {
|
||||
|
|
@ -32,9 +39,8 @@ describe Vmpooler::API::V1 do
|
|||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,7 +5,14 @@ describe Vmpooler::API::V1 do
|
|||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API end
|
||||
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
|
||||
|
||||
describe '/ondemandvm' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
|
|
@ -32,13 +39,11 @@ describe Vmpooler::API::V1 do
|
|||
let(:current_time) { Time.now }
|
||||
let(:vmname) { 'abcdefghijkl' }
|
||||
let(:checkoutlock) { Mutex.new }
|
||||
let(:redis) { MockRedis.new }
|
||||
let(:uuid) { SecureRandom.uuid }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
app.settings.set :config, auth: false
|
||||
app.settings.set :checkoutlock, checkoutlock
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,10 @@ describe Vmpooler::API::V1 do
|
|||
Vmpooler::API
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
Vmpooler::API.reset!
|
||||
end
|
||||
|
||||
let(:config) {
|
||||
{
|
||||
config: {
|
||||
|
|
@ -32,9 +36,8 @@ describe Vmpooler::API::V1 do
|
|||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,6 +12,13 @@ describe Vmpooler::API::V1 do
|
|||
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
|
||||
|
||||
describe 'status and metrics endpoints' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
|
||||
|
|
@ -32,8 +39,8 @@ describe Vmpooler::API::V1 do
|
|||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, nil)
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -8,14 +8,21 @@ describe Vmpooler::API::V1 do
|
|||
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
|
||||
|
||||
describe '/token' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:current_time) { Time.now }
|
||||
let(:config) { { } }
|
||||
|
||||
before do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
before(:each) do
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, nil)
|
||||
end
|
||||
|
||||
describe 'GET /token' do
|
||||
|
|
@ -97,7 +104,9 @@ describe Vmpooler::API::V1 do
|
|||
let(:prefix) { '/api/v1' }
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before do
|
||||
before(:each) do
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, nil)
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
end
|
||||
|
|
|
|||
|
|
@ -12,8 +12,16 @@ describe Vmpooler::API::V1 do
|
|||
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
|
||||
|
||||
describe '/vm/:hostname' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:metrics) { Vmpooler::DummyStatsd.new }
|
||||
|
||||
let(:config) {
|
||||
{
|
||||
|
|
@ -33,11 +41,9 @@ describe Vmpooler::API::V1 do
|
|||
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
let(:redis) { MockRedis.new }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
||||
|
|
@ -81,9 +87,9 @@ describe Vmpooler::API::V1 do
|
|||
end
|
||||
|
||||
context '(tagfilter configured)' do
|
||||
let(:config) { {
|
||||
tagfilter: { 'url' => '(.*)\/' },
|
||||
} }
|
||||
before(:each) do
|
||||
app.settings.set :config, tagfilter: { 'url' => '(.*)\/' }
|
||||
end
|
||||
|
||||
it 'correctly filters tags' do
|
||||
create_vm('testhost', redis)
|
||||
|
|
@ -104,7 +110,9 @@ describe Vmpooler::API::V1 do
|
|||
end
|
||||
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
before(:each) do
|
||||
app.settings.set :config, auth: false
|
||||
end
|
||||
|
||||
it 'allows VM lifetime to be modified without a token' do
|
||||
create_vm('testhost', redis)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ describe Vmpooler::API::V1 do
|
|||
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
|
||||
|
||||
describe '/vm' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:metrics) { Vmpooler::DummyStatsd.new }
|
||||
|
|
@ -32,9 +39,8 @@ describe Vmpooler::API::V1 do
|
|||
let(:checkoutlock) { Mutex.new }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
app.settings.set :config, auth: false
|
||||
app.settings.set :checkoutlock, checkoutlock
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
|
|
@ -104,8 +110,8 @@ describe Vmpooler::API::V1 do
|
|||
end
|
||||
|
||||
it 'returns 503 for empty pool when aliases are not defined' do
|
||||
Vmpooler::API.settings.config.delete(:alias)
|
||||
Vmpooler::API.settings.config[:pool_names] = ['pool1', 'pool2']
|
||||
app.settings.config.delete(:alias)
|
||||
app.settings.config[:pool_names] = ['pool1', 'pool2']
|
||||
|
||||
create_ready_vm 'pool1', vmname, redis
|
||||
post "#{prefix}/vm/pool1"
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ describe Vmpooler::API::V1 do
|
|||
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
|
||||
|
||||
describe '/vm/:template' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:metrics) { Vmpooler::DummyStatsd.new }
|
||||
|
|
@ -33,9 +40,8 @@ describe Vmpooler::API::V1 do
|
|||
let(:checkoutlock) { Mutex.new }
|
||||
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
expect(app).to receive(:run!).once
|
||||
app.execute(['api'], config, redis, metrics)
|
||||
app.settings.set :config, auth: false
|
||||
app.settings.set :checkoutlock, checkoutlock
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
|
|
@ -84,8 +90,8 @@ describe Vmpooler::API::V1 do
|
|||
end
|
||||
|
||||
it 'returns 503 for empty pool when aliases are not defined' do
|
||||
Vmpooler::API.settings.config.delete(:alias)
|
||||
Vmpooler::API.settings.config[:pool_names] = ['pool1', 'pool2']
|
||||
app.settings.config.delete(:alias)
|
||||
app.settings.config[:pool_names] = ['pool1', 'pool2']
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop', redis
|
||||
post "#{prefix}/vm/pool1"
|
||||
|
|
|
|||
|
|
@ -5,13 +5,35 @@ describe Vmpooler::API do
|
|||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
described_class
|
||||
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
|
||||
|
||||
describe 'Dashboard' do
|
||||
|
||||
let(:config) { {
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5}
|
||||
],
|
||||
graphite: {}
|
||||
} }
|
||||
|
||||
before(:each) do
|
||||
expect(app).to receive(:run!)
|
||||
app.execute(['api'], config, redis, nil)
|
||||
app.settings.set :config, auth: false
|
||||
end
|
||||
|
||||
context '/' do
|
||||
before { get '/' }
|
||||
before(:each) do
|
||||
get '/'
|
||||
end
|
||||
|
||||
it { expect(last_response.status).to eq(302) }
|
||||
it { expect(last_response.location).to eq('http://example.org/dashboard/') }
|
||||
|
|
@ -21,7 +43,7 @@ describe Vmpooler::API do
|
|||
ENV['SITE_NAME'] = 'test pooler'
|
||||
ENV['VMPOOLER_CONFIG'] = 'thing'
|
||||
|
||||
before do
|
||||
before(:each) do
|
||||
get '/dashboard/'
|
||||
end
|
||||
|
||||
|
|
@ -31,10 +53,12 @@ describe Vmpooler::API do
|
|||
end
|
||||
|
||||
context 'unknown route' do
|
||||
before { get '/doesnotexist' }
|
||||
before(:each) do
|
||||
get '/doesnotexist'
|
||||
end
|
||||
|
||||
it { expect(last_response.status).to eq(404) }
|
||||
it { expect(last_response.header['Content-Type']).to eq('application/json') }
|
||||
it { expect(last_response.status).to eq(404) }
|
||||
it { expect(last_response.body).to eq(JSON.pretty_generate({ok: false})) }
|
||||
end
|
||||
|
||||
|
|
@ -47,10 +71,8 @@ describe Vmpooler::API do
|
|||
graphite: {}
|
||||
} }
|
||||
|
||||
before do
|
||||
$config = config
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :environment, :test
|
||||
end
|
||||
|
||||
|
|
@ -120,10 +142,8 @@ describe Vmpooler::API do
|
|||
graphite: {}
|
||||
} }
|
||||
|
||||
before do
|
||||
$config = config
|
||||
before(:each) do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :environment, :test
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ describe 'Pool Manager' do
|
|||
timeout: 5
|
||||
) { MockRedis.new }
|
||||
}
|
||||
let(:redis) { MockRedis.new }
|
||||
|
||||
let(:provider) { Vmpooler::PoolManager::Provider::Base.new(config, logger, metrics, redis_connection_pool, 'mock_provider', provider_options) }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue