(maint) Convert specs for vm snapshot operations

This commit is contained in:
Rick Bradley 2016-06-01 17:15:45 -05:00
parent 62928f4436
commit 0c3a269923
2 changed files with 112 additions and 73 deletions

View file

@ -50,3 +50,22 @@ end
def fetch_vm(vm) def fetch_vm(vm)
redis.hgetall("vmpooler__vm__#{vm}") redis.hgetall("vmpooler__vm__#{vm}")
end end
def snapshot_vm(vm, snapshot = '12345678901234567890123456789012')
redis.sadd('vmpooler__tasks__snapshot', "#{vm}:#{snapshot}")
redis.hset("vmpooler__vm__#{vm}", "snapshot:#{snapshot}", "1")
end
def has_vm_snapshot?(vm)
redis.smembers('vmpooler__tasks__snapshot').any? do |snapshot|
instance, sha = snapshot.split(':')
vm == instance
end
end
def vm_reverted_to_snapshot?(vm, snapshot = nil)
redis.smembers('vmpooler__tasks__snapshot-revert').any? do |action|
instance, sha = action.split(':')
instance == vm and (snapshot ? (sha == snapshot) : true)
end
end

View file

@ -220,78 +220,98 @@ describe Vmpooler::API::V1 do
end end
end end
# describe 'POST /vm/:hostname/snapshot' do describe 'POST /vm/:hostname/snapshot' do
# context '(auth not configured)' do context '(auth not configured)' do
# let(:config) { { auth: false } } it 'creates a snapshot' do
# create_vm('testhost')
# it 'creates a snapshot' do post "#{prefix}/vm/testhost/snapshot"
# expect(redis).to receive(:sadd) expect_json(ok = true, http = 202)
# expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
# post "#{prefix}/vm/testhost/snapshot" end
# end
# expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32)
# context '(auth configured)' do
# expect_json(ok = true, http = 202) before(:each) do
# end app.settings.set :config, auth: true
# end end
#
# context '(auth configured)' do it 'returns a 401 if not authed' do
# let(:config) { { auth: true } } post "#{prefix}/vm/testhost/snapshot"
# expect_json(ok = false, http = 401)
# it 'returns a 401 if not authed' do expect !has_vm_snapshot?('testhost')
# post "#{prefix}/vm/testhost/snapshot" end
#
# expect_json(ok = false, http = 401) it 'creates a snapshot if authed' do
# end create_vm('testhost')
# snapshot_vm('testhost', 'testsnapshot')
# it 'creates a snapshot if authed' do
# expect(redis).to receive(:sadd) post "#{prefix}/vm/testhost/snapshot", "", {
# 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
# 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')
# expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32) end
# end
# expect_json(ok = true, http = 202) end
# end
# end describe 'POST /vm/:hostname/snapshot/:snapshot' do
# end context '(auth not configured)' do
# it 'reverts to a snapshot' do
# describe 'POST /vm/:hostname/snapshot/:snapshot' do create_vm('testhost')
# context '(auth not configured)' do snapshot_vm('testhost', 'testsnapshot')
# let(:config) { { auth: false } }
# post "#{prefix}/vm/testhost/snapshot/testsnapshot"
# it 'reverts to a snapshot' do expect_json(ok = true, http = 202)
# expect(redis).to receive(:hget).with('vmpooler__vm__testhost', 'snapshot:testsnapshot').and_return(1) expect vm_reverted_to_snapshot?('testhost', 'testsnapshot')
# expect(redis).to receive(:sadd) end
#
# post "#{prefix}/vm/testhost/snapshot/testsnapshot" it 'fails if the specified snapshot does not exist' do
# create_vm('testhost')
# expect_json(ok = true, http = 202)
# end post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
# end 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345'
# }
# context '(auth configured)' do expect_json(ok = false, http = 404)
# let(:config) { { auth: true } } expect !vm_reverted_to_snapshot?('testhost', 'testsnapshot')
# end
# it 'returns a 401 if not authed' do end
# post "#{prefix}/vm/testhost/snapshot"
# context '(auth configured)' do
# expect_json(ok = false, http = 401) before(:each) do
# end app.settings.set :config, auth: true
# end
# it 'reverts to a snapshot if authed' do
# expect(redis).to receive(:hget).with('vmpooler__vm__testhost', 'snapshot:testsnapshot').and_return(1) it 'returns a 401 if not authed' do
# expect(redis).to receive(:sadd) create_vm('testhost')
# snapshot_vm('testhost', 'testsnapshot')
# post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", {
# 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345' post "#{prefix}/vm/testhost/snapshot/testsnapshot"
# } expect_json(ok = false, http = 401)
# expect !vm_reverted_to_snapshot?('testhost', 'testsnapshot')
# expect_json(ok = true, http = 202) end
# end
# end it 'fails if authed and the specified snapshot does not exist' do
# end 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
end end