diff --git a/spec/helpers.rb b/spec/helpers.rb index a9e29dc..4af3a29 100644 --- a/spec/helpers.rb +++ b/spec/helpers.rb @@ -50,3 +50,22 @@ end def fetch_vm(vm) redis.hgetall("vmpooler__vm__#{vm}") 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 diff --git a/spec/vmpooler/api/v1/vm_hostname_spec.rb b/spec/vmpooler/api/v1/vm_hostname_spec.rb index 7263785..f5dce5b 100644 --- a/spec/vmpooler/api/v1/vm_hostname_spec.rb +++ b/spec/vmpooler/api/v1/vm_hostname_spec.rb @@ -220,78 +220,98 @@ describe Vmpooler::API::V1 do end end - # describe 'POST /vm/:hostname/snapshot' do - # context '(auth not configured)' do - # let(:config) { { auth: false } } - # - # it 'creates a snapshot' do - # expect(redis).to receive(:sadd) - # - # post "#{prefix}/vm/testhost/snapshot" - # - # expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32) - # - # expect_json(ok = true, http = 202) - # end - # end - # - # context '(auth configured)' do - # let(:config) { { auth: true } } - # - # it 'returns a 401 if not authed' do - # post "#{prefix}/vm/testhost/snapshot" - # - # expect_json(ok = false, http = 401) - # end - # - # it 'creates a snapshot if authed' do - # expect(redis).to receive(:sadd) - # - # post "#{prefix}/vm/testhost/snapshot", "", { - # 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345' - # } - # - # expect(JSON.parse(last_response.body)['testhost']['snapshot'].length).to be(32) - # - # expect_json(ok = true, http = 202) - # end - # end - # end - # - # describe 'POST /vm/:hostname/snapshot/:snapshot' do - # context '(auth not configured)' do - # let(:config) { { auth: false } } - # - # it 'reverts to a snapshot' do - # expect(redis).to receive(:hget).with('vmpooler__vm__testhost', 'snapshot:testsnapshot').and_return(1) - # expect(redis).to receive(:sadd) - # - # post "#{prefix}/vm/testhost/snapshot/testsnapshot" - # - # expect_json(ok = true, http = 202) - # end - # end - # - # context '(auth configured)' do - # let(:config) { { auth: true } } - # - # it 'returns a 401 if not authed' do - # post "#{prefix}/vm/testhost/snapshot" - # - # expect_json(ok = false, http = 401) - # end - # - # it 'reverts to a snapshot if authed' do - # expect(redis).to receive(:hget).with('vmpooler__vm__testhost', 'snapshot:testsnapshot').and_return(1) - # expect(redis).to receive(:sadd) - # - # post "#{prefix}/vm/testhost/snapshot/testsnapshot", "", { - # 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345' - # } - # - # expect_json(ok = true, http = 202) - # 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