(maint) Update vm/hostname tag and lifetime specs

This commit is contained in:
Rick Bradley 2016-06-01 15:32:01 -05:00
parent 48149eda64
commit 09d7f9ca92
2 changed files with 80 additions and 60 deletions

View file

@ -11,6 +11,10 @@ module Vmpooler
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
@ -52,7 +56,7 @@ describe Vmpooler::API::V1 do
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":"rspec"}}'
expect_json(ok = true, http = 200)
# TODO: test for tag presence
expect has_set_tag?('testhost', 'tested_by', 'rspec')
end
it 'skips empty tags' do
@ -60,7 +64,7 @@ describe Vmpooler::API::V1 do
put "#{prefix}/vm/testhost", '{"tags":{"tested_by":""}}'
expect_json(ok = true, http = 200)
# TODO: test for tag presence
expect !has_set_tag?('testhost', 'tested_by', '')
end
it 'does not set tags if request body format is invalid' do
@ -68,7 +72,7 @@ describe Vmpooler::API::V1 do
put "#{prefix}/vm/testhost", '{"tags":{"tested"}}'
expect_json(ok = false, http = 400)
# TODO: ensure that tags were not set
expect !has_set_tag?('testhost', 'tested', '')
end
context '(allowed_tags configured)' do
@ -79,69 +83,83 @@ describe Vmpooler::API::V1 do
create_vm('testhost')
put "#{prefix}/vm/testhost", '{"tags":{"created_by":"rspec","tested_by":"rspec"}}'
expect_json(ok = false, http = 400)
# TODO: ensure that tag was not set
expect !has_set_tag?('testhost', 'tested_by', 'rspec')
end
end
# context '(tagfilter configured)' do
# let(:config) { {
# tagfilter: { 'url' => '(.*)\/' },
# } }
#
# it 'correctly filters tags' do
# app.settings.set :config, { :config => { tagfilter: { 'url' => '(.*)\/' } } }
#
# put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com/something.html"}}'
# expect_json(ok = true, http = 200)
#
# # expect(redis).to receive(:hset).with("vmpooler__vm__testhost", "tag:url", "foo.com")
#
# # TODO: ensure that filtered tags were set
# end
#
# it 'doesn\'t eat tags not matching filter' do
# app.settings.set :config, { :config => { tagfilter: { 'url' => '(.*)\/' } } }
#
# put "#{prefix}/vm/testhost", '{"tags":{"url":"foo.com"}}'
# expect_json(ok = true, http = 200)
#
# # expect(redis).to receive(:hset).with("vmpooler__vm__testhost", "tag:url", "foo.com")
# # TODO: ensure that filtered tags were set
# end
# end
context '(tagfilter configured)' do
let(:config) { {
tagfilter: { 'url' => '(.*)\/' },
} }
# context '(auth not configured)' do
# it 'allows VM lifetime to be modified without a token' do
# put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
# expect_json(ok = true, http = 200)
# end
#
# it 'does not allow a lifetime to be 0' do
# put "#{prefix}/vm/testhost", '{"lifetime":"0"}'
# expect_json(ok = false, http = 400)
# end
# end
it 'correctly filters tags' do
create_vm('testhost')
# context '(auth configured)' do
# let(:config) { { auth: true } }
#
# it 'allows VM lifetime to be modified with a token' do
# put "#{prefix}/vm/testhost", '{"lifetime":"1"}', {
# 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
# }
#
# expect_json(ok = true, http = 200)
# end
#
# it 'does not allows VM lifetime to be modified without a token' do
# put "#{prefix}/vm/testhost", '{"lifetime":"1"}'
#
# expect_json(ok = false, http = 401)
# end
# end
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

View file

@ -106,7 +106,7 @@ describe Vmpooler::API::V1 do
context '(auth not configured)' do
it 'does not extend VM lifetime if auth token is provided' do
app.settings.set :config, auth: true
app.settings.set :config, auth: false
create_ready_vm 'pool1', 'abcdefghijklmnop'
@ -123,6 +123,8 @@ describe Vmpooler::API::V1 do
}
expect(last_response.body).to eq(JSON.pretty_generate(expected))
vm = fetch_vm('abcdefghijklmnop')
expect(vm['lifetime']).to be_nil
end
end