mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 01:58:41 -05:00
(POOLER-73) Restructure tests to unit and integration directories
Previously all of the spec tests for VM Pooler were all together in the specs directory. However some tests require a working local Redis server to operate and other instead mock all external dependencies. This commit splits the test files between unit and integration, where integration tests require a working Redis instance, and unit tests do not. This commit also removes the root `vmpooler` directory as it is not required. The tests rake test still operates correctly. This commit also adds the mock_redis library for testing for the pool_manager.
This commit is contained in:
parent
eb67ccad5a
commit
8bcf74872a
9 changed files with 1 additions and 0 deletions
111
spec/integration/api/v1/status_spec.rb
Normal file
111
spec/integration/api/v1/status_spec.rb
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
module Vmpooler
|
||||
class API
|
||||
module Helpers
|
||||
def authenticate(auth, username_str, password_str)
|
||||
username_str == 'admin' and password_str == 's3cr3t'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_set_tag?(vm, tag, value)
|
||||
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
|
||||
end
|
||||
|
||||
describe Vmpooler::API::V1 do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API
|
||||
end
|
||||
|
||||
describe '/status' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
|
||||
let(:config) {
|
||||
{
|
||||
config: {
|
||||
'site_name' => 'test pooler',
|
||||
'vm_lifetime_auth' => 2,
|
||||
},
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5},
|
||||
{'name' => 'pool2', 'size' => 10}
|
||||
],
|
||||
alias: { 'poolone' => 'pool1' },
|
||||
}
|
||||
}
|
||||
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
redis.flushdb
|
||||
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
||||
describe 'GET /status' do
|
||||
it 'returns the configured maximum size for each pool' do
|
||||
get "#{prefix}/status/"
|
||||
|
||||
# of course /status doesn't conform to the weird standard everything else uses...
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result["pools"]["pool1"]["max"]).to be(5)
|
||||
expect(result["pools"]["pool2"]["max"]).to be(10)
|
||||
end
|
||||
|
||||
it 'returns the number of ready vms for each pool' do
|
||||
3.times {|i| create_ready_vm("pool1", "vm-#{i}") }
|
||||
get "#{prefix}/status/"
|
||||
|
||||
# of course /status doesn't conform to the weird standard everything else uses...
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result["pools"]["pool1"]["ready"]).to be(3)
|
||||
expect(result["pools"]["pool2"]["ready"]).to be(0)
|
||||
end
|
||||
|
||||
it 'returns the number of running vms for each pool' do
|
||||
3.times {|i| create_running_vm("pool1", "vm-#{i}") }
|
||||
4.times {|i| create_running_vm("pool2", "vm-#{i}") }
|
||||
|
||||
get "#{prefix}/status/"
|
||||
|
||||
# of course /status doesn't conform to the weird standard everything else uses...
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result["pools"]["pool1"]["running"]).to be(3)
|
||||
expect(result["pools"]["pool2"]["running"]).to be(4)
|
||||
end
|
||||
|
||||
it 'returns the number of pending vms for each pool' do
|
||||
3.times {|i| create_pending_vm("pool1", "vm-#{i}") }
|
||||
4.times {|i| create_pending_vm("pool2", "vm-#{i}") }
|
||||
|
||||
get "#{prefix}/status/"
|
||||
|
||||
# of course /status doesn't conform to the weird standard everything else uses...
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result["pools"]["pool1"]["pending"]).to be(3)
|
||||
expect(result["pools"]["pool2"]["pending"]).to be(4)
|
||||
end
|
||||
|
||||
it '(for v1 backwards compatibility) lists any empty pools in the status section' do
|
||||
get "#{prefix}/status/"
|
||||
|
||||
# of course /status doesn't conform to the weird standard everything else uses...
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result["status"]["empty"].sort).to eq(["pool1", "pool2"])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
173
spec/integration/api/v1/token_spec.rb
Normal file
173
spec/integration/api/v1/token_spec.rb
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
module Vmpooler
|
||||
class API
|
||||
module Helpers
|
||||
def authenticate(auth, username_str, password_str)
|
||||
username_str == 'admin' and password_str == 's3cr3t'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Vmpooler::API::V1 do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API
|
||||
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
|
||||
end
|
||||
|
||||
describe 'GET /token' do
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
|
||||
it 'returns a 404' do
|
||||
get "#{prefix}/token"
|
||||
expect_json(ok = false, http = 404)
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
let(:config) { { auth: true } }
|
||||
|
||||
it 'returns a 401 if not authed' do
|
||||
get "#{prefix}/token"
|
||||
expect_json(ok = false, http = 401)
|
||||
end
|
||||
|
||||
it 'returns a list of tokens if authed' do
|
||||
create_token "abc", "admin", current_time
|
||||
|
||||
authorize 'admin', 's3cr3t'
|
||||
get "#{prefix}/token"
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect(JSON.parse(last_response.body)['abc']['created']).to eq(current_time.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /token' do
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
|
||||
it 'returns a 404' do
|
||||
post "#{prefix}/token"
|
||||
expect_json(ok = false, http = 404)
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
let(:config) { { auth: true } }
|
||||
|
||||
it 'returns a 401 if not authed' do
|
||||
post "#{prefix}/token"
|
||||
expect_json(ok = false, http = 401)
|
||||
end
|
||||
|
||||
it 'returns a newly created token if authed' do
|
||||
authorize 'admin', 's3cr3t'
|
||||
post "#{prefix}/token"
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
returned_token = JSON.parse(last_response.body)['token']
|
||||
expect(returned_token.length).to be(32)
|
||||
expect(get_token_data(returned_token)['user']).to eq("admin")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '/token/:token' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before do
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
end
|
||||
|
||||
def create_vm_for_token(token, pool, vm)
|
||||
redis.sadd("vmpooler__running__#{pool}", vm)
|
||||
redis.hset("vmpooler__vm__#{vm}", "token:token", token)
|
||||
end
|
||||
|
||||
describe 'GET /token/:token' do
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
|
||||
it 'returns a 404' do
|
||||
get "#{prefix}/token/this"
|
||||
expect_json(ok = false, http = 404)
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
let(:config) { {
|
||||
auth: true,
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5}
|
||||
]
|
||||
} }
|
||||
|
||||
it 'returns a token' do
|
||||
create_token "mytoken", "admin", current_time
|
||||
create_vm_for_token "mytoken", "pool1", "vmhostname"
|
||||
|
||||
get "#{prefix}/token/mytoken"
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect(JSON.parse(last_response.body)['ok']).to eq(true)
|
||||
expect(JSON.parse(last_response.body)['mytoken']['user']).to eq('admin')
|
||||
expect(JSON.parse(last_response.body)['mytoken']['vms']['running']).to include('vmhostname')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /token/:token' do
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
|
||||
it 'returns a 404' do
|
||||
delete "#{prefix}/token/this"
|
||||
expect_json(ok = false, http = 404)
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
let(:config) { { auth: true } }
|
||||
|
||||
it 'returns a 401 if not authed' do
|
||||
delete "#{prefix}/token/this"
|
||||
expect_json(ok = false, http = 401)
|
||||
end
|
||||
|
||||
it 'deletes a token if authed' do
|
||||
create_token("mytoken", "admin", current_time)
|
||||
authorize 'admin', 's3cr3t'
|
||||
|
||||
delete "#{prefix}/token/mytoken"
|
||||
expect_json(ok = true, http = 200)
|
||||
end
|
||||
|
||||
it 'fails if token does not exist' do
|
||||
authorize 'admin', 's3cr3t'
|
||||
|
||||
delete "#{prefix}/token/missingtoken"
|
||||
expect_json(ok = false, http = 401) # TODO: should this be 404?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
317
spec/integration/api/v1/vm_hostname_spec.rb
Normal file
317
spec/integration/api/v1/vm_hostname_spec.rb
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
module Vmpooler
|
||||
class API
|
||||
module Helpers
|
||||
def authenticate(auth, username_str, password_str)
|
||||
username_str == 'admin' and password_str == 's3cr3t'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_set_tag?(vm, tag, value)
|
||||
value == redis.hget("vmpooler__vm__#{vm}", "tag:#{tag}")
|
||||
end
|
||||
|
||||
describe Vmpooler::API::V1 do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API
|
||||
end
|
||||
|
||||
describe '/vm/:hostname' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
|
||||
let(:config) {
|
||||
{
|
||||
config: {
|
||||
'site_name' => 'test pooler',
|
||||
'vm_lifetime_auth' => 2,
|
||||
},
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5},
|
||||
{'name' => 'pool2', 'size' => 10}
|
||||
],
|
||||
alias: { 'poolone' => 'pool1' },
|
||||
}
|
||||
}
|
||||
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
redis.flushdb
|
||||
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
||||
describe 'PUT /vm/:hostname' do
|
||||
it 'allows tags to be set' do
|
||||
create_vm('testhost')
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":"rspec"}}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect has_set_tag?('testhost', 'tested_by', 'rspec')
|
||||
end
|
||||
|
||||
it 'skips empty tags' do
|
||||
create_vm('testhost')
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":""}}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect !has_set_tag?('testhost', 'tested_by', '')
|
||||
end
|
||||
|
||||
it 'does not set tags if request body format is invalid' do
|
||||
create_vm('testhost')
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"tested"}}'
|
||||
expect_json(ok = false, http = 400)
|
||||
|
||||
expect !has_set_tag?('testhost', 'tested', '')
|
||||
end
|
||||
|
||||
context '(allowed_tags configured)' do
|
||||
it 'fails if specified tag is not in allowed_tags array' do
|
||||
app.settings.set :config,
|
||||
{ :config => { 'allowed_tags' => ['created_by', 'project', 'url'] } }
|
||||
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"created_by":"rspec","tested_by":"rspec"}}'
|
||||
expect_json(ok = false, http = 400)
|
||||
|
||||
expect !has_set_tag?('testhost', 'tested_by', 'rspec')
|
||||
end
|
||||
end
|
||||
|
||||
context '(tagfilter configured)' do
|
||||
let(:config) { {
|
||||
tagfilter: { 'url' => '(.*)\/' },
|
||||
} }
|
||||
|
||||
it 'correctly filters tags' do
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com/something.html"}}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect has_set_tag?('testhost', 'url', 'foo.com')
|
||||
end
|
||||
|
||||
it "doesn't eat tags not matching filter" do
|
||||
create_vm('testhost')
|
||||
put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com"}}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect has_set_tag?('testhost', 'url', 'foo.com')
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth not configured)' do
|
||||
let(:config) { { auth: false } }
|
||||
|
||||
it 'allows VM lifetime to be modified without a token' do
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
vm = fetch_vm('testhost')
|
||||
expect(vm['lifetime'].to_i).to eq(1)
|
||||
end
|
||||
|
||||
it 'does not allow a lifetime to be 0' do
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"lifetime":"0"}'
|
||||
expect_json(ok = false, http = 400)
|
||||
|
||||
vm = fetch_vm('testhost')
|
||||
expect(vm['lifetime']).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
before(:each) do
|
||||
app.settings.set :config, auth: true
|
||||
end
|
||||
|
||||
it 'allows VM lifetime to be modified with a token' do
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"lifetime":"1"}', {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
vm = fetch_vm('testhost')
|
||||
expect(vm['lifetime'].to_i).to eq(1)
|
||||
end
|
||||
|
||||
it 'does not allows VM lifetime to be modified without a token' do
|
||||
create_vm('testhost')
|
||||
|
||||
put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
|
||||
expect_json(ok = false, http = 401)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE /vm/:hostname' do
|
||||
context '(auth not configured)' do
|
||||
it 'does not delete a non-existant VM' do
|
||||
delete "#{prefix}/vm/testhost"
|
||||
expect_json(ok = false, http = 404)
|
||||
end
|
||||
|
||||
it 'deletes an existing VM' do
|
||||
create_running_vm('pool1', 'testhost')
|
||||
expect fetch_vm('testhost')
|
||||
|
||||
delete "#{prefix}/vm/testhost"
|
||||
expect_json(ok = true, http = 200)
|
||||
expect !fetch_vm('testhost')
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
before(:each) do
|
||||
app.settings.set :config, auth: true
|
||||
end
|
||||
|
||||
context '(checked-out without token)' do
|
||||
it 'deletes a VM without supplying a token' do
|
||||
create_running_vm('pool1', 'testhost')
|
||||
expect fetch_vm('testhost')
|
||||
|
||||
delete "#{prefix}/vm/testhost"
|
||||
expect_json(ok = true, http = 200)
|
||||
expect !fetch_vm('testhost')
|
||||
end
|
||||
end
|
||||
|
||||
context '(checked-out with token)' do
|
||||
it 'fails to delete a VM without supplying a token' do
|
||||
create_running_vm('pool1', 'testhost', 'abcdefghijklmnopqrstuvwxyz012345')
|
||||
expect fetch_vm('testhost')
|
||||
|
||||
delete "#{prefix}/vm/testhost"
|
||||
expect_json(ok = false, http = 401)
|
||||
expect fetch_vm('testhost')
|
||||
end
|
||||
|
||||
it 'deletes a VM when token is supplied' do
|
||||
create_running_vm('pool1', 'testhost', 'abcdefghijklmnopqrstuvwxyz012345')
|
||||
expect fetch_vm('testhost')
|
||||
|
||||
delete "#{prefix}/vm/testhost", "", {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect !fetch_vm('testhost')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /vm/:hostname/snapshot' do
|
||||
context '(auth not configured)' do
|
||||
it 'creates a snapshot' do
|
||||
create_vm('testhost')
|
||||
post "#{prefix}/vm/testhost/snapshot"
|
||||
expect_json(ok = true, http = 202)
|
||||
expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
before(:each) do
|
||||
app.settings.set :config, auth: true
|
||||
end
|
||||
|
||||
it 'returns a 401 if not authed' do
|
||||
post "#{prefix}/vm/testhost/snapshot"
|
||||
expect_json(ok = false, http = 401)
|
||||
expect !has_vm_snapshot?('testhost')
|
||||
end
|
||||
|
||||
it 'creates a snapshot if authed' do
|
||||
create_vm('testhost')
|
||||
snapshot_vm('testhost', 'testsnapshot')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot", "", {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 202)
|
||||
expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
|
||||
expect has_vm_snapshot?('testhost')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /vm/:hostname/snapshot/:snapshot' do
|
||||
context '(auth not configured)' do
|
||||
it 'reverts to a snapshot' do
|
||||
create_vm('testhost')
|
||||
snapshot_vm('testhost', 'testsnapshot')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot/testsnapshot"
|
||||
expect_json(ok = true, http = 202)
|
||||
expect vm_reverted_to_snapshot?('testhost', 'testsnapshot')
|
||||
end
|
||||
|
||||
it 'fails if the specified snapshot does not exist' do
|
||||
create_vm('testhost')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = false, http = 404)
|
||||
expect !vm_reverted_to_snapshot?('testhost', 'testsnapshot')
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
before(:each) do
|
||||
app.settings.set :config, auth: true
|
||||
end
|
||||
|
||||
it 'returns a 401 if not authed' do
|
||||
create_vm('testhost')
|
||||
snapshot_vm('testhost', 'testsnapshot')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot/testsnapshot"
|
||||
expect_json(ok = false, http = 401)
|
||||
expect !vm_reverted_to_snapshot?('testhost', 'testsnapshot')
|
||||
end
|
||||
|
||||
it 'fails if authed and the specified snapshot does not exist' do
|
||||
create_vm('testhost')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = false, http = 404)
|
||||
expect !vm_reverted_to_snapshot?('testhost', 'testsnapshot')
|
||||
end
|
||||
|
||||
it 'reverts to a snapshot if authed' do
|
||||
create_vm('testhost')
|
||||
snapshot_vm('testhost', 'testsnapshot')
|
||||
|
||||
post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 202)
|
||||
expect vm_reverted_to_snapshot?('testhost', 'testsnapshot')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
329
spec/integration/api/v1/vm_spec.rb
Normal file
329
spec/integration/api/v1/vm_spec.rb
Normal file
|
|
@ -0,0 +1,329 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
module Vmpooler
|
||||
class API
|
||||
module Helpers
|
||||
def authenticate(auth, username_str, password_str)
|
||||
username_str == 'admin' and password_str == 's3cr3t'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Vmpooler::API::V1 do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API
|
||||
end
|
||||
|
||||
describe '/vm' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:metrics) { Vmpooler::DummyStatsd.new }
|
||||
let(:config) {
|
||||
{
|
||||
config: {
|
||||
'site_name' => 'test pooler',
|
||||
'vm_lifetime_auth' => 2,
|
||||
},
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5},
|
||||
{'name' => 'pool2', 'size' => 10}
|
||||
],
|
||||
statsd: { 'prefix' => 'stats_prefix'},
|
||||
alias: { 'poolone' => 'pool1' },
|
||||
pool_names: [ 'pool1', 'pool2', 'poolone' ]
|
||||
}
|
||||
}
|
||||
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
redis.flushdb
|
||||
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
||||
describe 'POST /vm' do
|
||||
it 'returns a single VM' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1"}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'returns a single VM for an alias' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"poolone":"1"}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'fails on nonexistant pools' do
|
||||
post "#{prefix}/vm", '{"poolpoolpool":"1"}'
|
||||
expect_json(ok = false, http = 404)
|
||||
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']
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
post "#{prefix}/vm/pool1"
|
||||
post "#{prefix}/vm/pool1"
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns 503 for empty pool referenced by alias' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
post "#{prefix}/vm/poolone"
|
||||
post "#{prefix}/vm/poolone"
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns multiple VMs' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', 'qrstuvwxyz012345'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
},
|
||||
pool2: {
|
||||
hostname: 'qrstuvwxyz012345'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'returns multiple VMs even when multiple instances from the same pool are requested' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '2abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', 'qrstuvwxyz012345'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
|
||||
},
|
||||
pool2: {
|
||||
hostname: 'qrstuvwxyz012345'
|
||||
}
|
||||
}
|
||||
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result['ok']).to eq(true)
|
||||
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
|
||||
expect(result['pool2']['hostname']).to eq('qrstuvwxyz012345')
|
||||
|
||||
expect_json(ok = true, http = 200)
|
||||
end
|
||||
|
||||
it 'returns multiple VMs even when multiple instances from multiple pools are requested' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '2abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', '1qrstuvwxyz012345'
|
||||
create_ready_vm 'pool2', '2qrstuvwxyz012345'
|
||||
create_ready_vm 'pool2', '3qrstuvwxyz012345'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
|
||||
},
|
||||
pool2: {
|
||||
hostname: [ '1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345' ]
|
||||
}
|
||||
}
|
||||
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result['ok']).to eq(true)
|
||||
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
|
||||
expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345')
|
||||
|
||||
expect_json(ok = true, http = 200)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop')).to eq(true)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated, when requesting multiple instances from a pool' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from a pool' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"1"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop')).to eq(true)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '2abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"2","pool2":"3"}'
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', '1abcdefghijklmnop')).to eq(true)
|
||||
expect(pool_has_ready_vm?('pool1', '2abcdefghijklmnop')).to eq(true)
|
||||
end
|
||||
|
||||
context '(auth not configured)' do
|
||||
it 'does not extend VM lifetime if auth token is provided' do
|
||||
app.settings.set :config, auth: false
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1"}', {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime']).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
it 'extends VM lifetime if auth token is provided' do
|
||||
app.settings.set :config, auth: true
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1"}', {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime'].to_i).to eq(2)
|
||||
end
|
||||
|
||||
it 'does not extend VM lifetime if auth token is not provided' do
|
||||
app.settings.set :config, auth: true
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm", '{"pool1":"1"}'
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime']).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
309
spec/integration/api/v1/vm_template_spec.rb
Normal file
309
spec/integration/api/v1/vm_template_spec.rb
Normal file
|
|
@ -0,0 +1,309 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
module Vmpooler
|
||||
class API
|
||||
module Helpers
|
||||
def authenticate(auth, username_str, password_str)
|
||||
username_str == 'admin' and password_str == 's3cr3t'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe Vmpooler::API::V1 do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
Vmpooler::API
|
||||
end
|
||||
|
||||
describe '/vm/:template' do
|
||||
let(:prefix) { '/api/v1' }
|
||||
let(:metrics) { Vmpooler::DummyStatsd.new }
|
||||
let(:config) {
|
||||
{
|
||||
config: {
|
||||
'site_name' => 'test pooler',
|
||||
'vm_lifetime_auth' => 2,
|
||||
},
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5},
|
||||
{'name' => 'pool2', 'size' => 10}
|
||||
],
|
||||
statsd: { 'prefix' => 'stats_prefix'},
|
||||
alias: { 'poolone' => 'pool1' },
|
||||
pool_names: [ 'pool1', 'pool2', 'poolone' ]
|
||||
}
|
||||
}
|
||||
|
||||
let(:current_time) { Time.now }
|
||||
|
||||
before(:each) do
|
||||
redis.flushdb
|
||||
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :metrics, metrics
|
||||
app.settings.set :config, auth: false
|
||||
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
|
||||
end
|
||||
|
||||
describe 'POST /vm/:template' do
|
||||
it 'returns a single VM' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1", ''
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'returns a single VM for an alias' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/poolone", ''
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'fails on nonexistant pools' do
|
||||
post "#{prefix}/vm/poolpoolpool", ''
|
||||
expect_json(ok = false, http = 404)
|
||||
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']
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
post "#{prefix}/vm/pool1"
|
||||
post "#{prefix}/vm/pool1"
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns 503 for empty pool referenced by alias' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
post "#{prefix}/vm/poolone"
|
||||
post "#{prefix}/vm/poolone"
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns multiple VMs' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', 'qrstuvwxyz012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool2", ''
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
},
|
||||
pool2: {
|
||||
hostname: 'qrstuvwxyz012345'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
end
|
||||
|
||||
it 'returns multiple VMs even when multiple instances from multiple pools are requested' do
|
||||
create_ready_vm 'pool1', '1abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '2abcdefghijklmnop'
|
||||
|
||||
create_ready_vm 'pool2', '1qrstuvwxyz012345'
|
||||
create_ready_vm 'pool2', '2qrstuvwxyz012345'
|
||||
create_ready_vm 'pool2', '3qrstuvwxyz012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: [ '1abcdefghijklmnop', '2abcdefghijklmnop' ]
|
||||
},
|
||||
pool2: {
|
||||
hostname: [ '1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345' ]
|
||||
}
|
||||
}
|
||||
|
||||
result = JSON.parse(last_response.body)
|
||||
expect(result['ok']).to eq(true)
|
||||
expect(result['pool1']['hostname']).to include('1abcdefghijklmnop', '2abcdefghijklmnop')
|
||||
expect(result['pool2']['hostname']).to include('1qrstuvwxyz012345', '2qrstuvwxyz012345', '3qrstuvwxyz012345')
|
||||
expect_json(ok = true, http = 200)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop')).to eq(true)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated, when requesting multiple instances from a pool' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '0123456789012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool1+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from a pool' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool1', '0123456789012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool1+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop')).to eq(true)
|
||||
expect(pool_has_ready_vm?('pool1', '0123456789012345')).to eq(true)
|
||||
end
|
||||
|
||||
it 'fails when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', '0123456789012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
end
|
||||
|
||||
it 'returns any checked out vms to their pools when not all requested vms can be allocated, when requesting multiple instances from multiple pools' do
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
create_ready_vm 'pool2', '0123456789012345'
|
||||
|
||||
post "#{prefix}/vm/pool1+pool1+pool2+pool2+pool2", ''
|
||||
|
||||
expected = { ok: false }
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
expect_json(ok = false, http = 503)
|
||||
|
||||
expect(pool_has_ready_vm?('pool1', 'abcdefghijklmnop')).to eq(true)
|
||||
expect(pool_has_ready_vm?('pool2', '0123456789012345')).to eq(true)
|
||||
end
|
||||
|
||||
context '(auth not configured)' do
|
||||
it 'does not extend VM lifetime if auth token is provided' do
|
||||
app.settings.set :config, auth: false
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1", '', {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime']).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context '(auth configured)' do
|
||||
it 'extends VM lifetime if auth token is provided' do
|
||||
app.settings.set :config, auth: true
|
||||
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1", '', {
|
||||
'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime'].to_i).to eq(2)
|
||||
end
|
||||
|
||||
it 'does not extend VM lifetime if auth token is not provided' do
|
||||
app.settings.set :config, auth: true
|
||||
create_ready_vm 'pool1', 'abcdefghijklmnop'
|
||||
|
||||
post "#{prefix}/vm/pool1", ''
|
||||
|
||||
expected = {
|
||||
ok: true,
|
||||
pool1: {
|
||||
hostname: 'abcdefghijklmnop'
|
||||
}
|
||||
}
|
||||
expect_json(ok = true, http = 200)
|
||||
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(expected))
|
||||
|
||||
vm = fetch_vm('abcdefghijklmnop')
|
||||
expect(vm['lifetime']).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
173
spec/integration/dashboard_spec.rb
Normal file
173
spec/integration/dashboard_spec.rb
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
require 'spec_helper'
|
||||
require 'rack/test'
|
||||
|
||||
describe Vmpooler::API do
|
||||
include Rack::Test::Methods
|
||||
|
||||
def app()
|
||||
described_class
|
||||
end
|
||||
|
||||
describe 'Dashboard' do
|
||||
|
||||
before(:each) do
|
||||
redis.flushdb
|
||||
end
|
||||
|
||||
context '/' do
|
||||
before { get '/' }
|
||||
|
||||
it { expect(last_response.status).to eq(302) }
|
||||
it { expect(last_response.location).to eq('http://example.org/dashboard/') }
|
||||
end
|
||||
|
||||
context '/dashboard/' do
|
||||
let(:config) { {
|
||||
config: {'site_name' => 'test pooler'}
|
||||
} }
|
||||
|
||||
before do
|
||||
$config = config
|
||||
get '/dashboard/'
|
||||
end
|
||||
|
||||
it { expect(last_response).to be_ok }
|
||||
it { expect(last_response.body).to match(/test pooler/) }
|
||||
it { expect(last_response.body).not_to match(/<b>vmpooler<\/b>/) }
|
||||
end
|
||||
|
||||
context 'unknown route' do
|
||||
before { get '/doesnotexist' }
|
||||
|
||||
it { expect(last_response.status).to eq(404) }
|
||||
it { expect(last_response.header['Content-Type']).to eq('application/json') }
|
||||
it { expect(last_response.body).to eq(JSON.pretty_generate({ok: false})) }
|
||||
end
|
||||
|
||||
describe '/dashboard/stats/vmpooler/pool' do
|
||||
let(:config) { {
|
||||
pools: [
|
||||
{'name' => 'pool1', 'size' => 5},
|
||||
{'name' => 'pool2', 'size' => 1}
|
||||
],
|
||||
graphite: {}
|
||||
} }
|
||||
|
||||
before do
|
||||
$config = config
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :environment, :test
|
||||
end
|
||||
|
||||
context 'without history param' do
|
||||
it 'returns basic JSON' do
|
||||
create_ready_vm('pool1', 'vm1')
|
||||
create_ready_vm('pool1', 'vm2')
|
||||
create_ready_vm('pool1', 'vm3')
|
||||
create_ready_vm('pool2', 'vm4')
|
||||
create_ready_vm('pool2', 'vm5')
|
||||
|
||||
get '/dashboard/stats/vmpooler/pool'
|
||||
|
||||
json_hash = {
|
||||
pool1: {size: 5, ready: 3},
|
||||
pool2: {size: 1, ready: 2}
|
||||
}
|
||||
|
||||
expect(last_response).to be_ok
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(json_hash))
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with history param' do
|
||||
it 'returns JSON with zeroed history when redis does not have values' do
|
||||
get '/dashboard/stats/vmpooler/pool', :history => true
|
||||
|
||||
json_hash = {
|
||||
pool1: {size: 5, ready: 0, history: [0]},
|
||||
pool2: {size: 1, ready: 0, history: [0]}
|
||||
}
|
||||
|
||||
expect(last_response).to be_ok
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(json_hash))
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
end
|
||||
|
||||
it 'returns JSON with history when redis has values' do
|
||||
create_ready_vm('pool1', 'vm1')
|
||||
create_ready_vm('pool1', 'vm2')
|
||||
create_ready_vm('pool1', 'vm3')
|
||||
create_ready_vm('pool2', 'vm4')
|
||||
create_ready_vm('pool2', 'vm5')
|
||||
|
||||
get '/dashboard/stats/vmpooler/pool', :history => true
|
||||
|
||||
json_hash = {
|
||||
pool1: {size: 5, ready: 3, history: [3]},
|
||||
pool2: {size: 1, ready: 2, history: [2]}
|
||||
}
|
||||
|
||||
expect(last_response).to be_ok
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(json_hash))
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '/dashboard/stats/vmpooler/running' do
|
||||
let(:config) { {
|
||||
pools: [
|
||||
{'name' => 'pool-1', 'size' => 5},
|
||||
{'name' => 'pool-2', 'size' => 1},
|
||||
{'name' => 'diffpool-1', 'size' => 3}
|
||||
],
|
||||
graphite: {}
|
||||
} }
|
||||
|
||||
before do
|
||||
$config = config
|
||||
app.settings.set :config, config
|
||||
app.settings.set :redis, redis
|
||||
app.settings.set :environment, :test
|
||||
end
|
||||
|
||||
context 'without history param' do
|
||||
|
||||
it 'returns basic JSON' do
|
||||
get '/dashboard/stats/vmpooler/running'
|
||||
|
||||
json_hash = {pool: {running: 0}, diffpool: {running: 0}}
|
||||
|
||||
expect(last_response).to be_ok
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(json_hash))
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
end
|
||||
|
||||
it 'adds major correctly' do
|
||||
create_running_vm('pool-1', 'vm1')
|
||||
create_running_vm('pool-1', 'vm2')
|
||||
create_running_vm('pool-1', 'vm3')
|
||||
|
||||
create_running_vm('pool-2', 'vm4')
|
||||
create_running_vm('pool-2', 'vm5')
|
||||
create_running_vm('pool-2', 'vm6')
|
||||
create_running_vm('pool-2', 'vm7')
|
||||
create_running_vm('pool-2', 'vm8')
|
||||
|
||||
create_running_vm('diffpool-1', 'vm9')
|
||||
create_running_vm('diffpool-1', 'vm10')
|
||||
|
||||
get '/dashboard/stats/vmpooler/running'
|
||||
|
||||
json_hash = {pool: {running: 8}, diffpool: {running: 2}}
|
||||
|
||||
expect(last_response).to be_ok
|
||||
expect(last_response.body).to eq(JSON.pretty_generate(json_hash))
|
||||
expect(last_response.header['Content-Type']).to eq('application/json')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue