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

View file

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