From 1fcda8612498d3e9c19df8590352fa3d5dfebbfa Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Thu, 5 Nov 2015 11:52:32 -0800 Subject: [PATCH] Spec tests for pool aliases, /vm/:template This PR adds spec testing for pool 'alias' functionality introduced in 17b24d6, as well as the following previously non-existant tests: - new tests for handling requests for a VM from a nonexistant pool - new tests for the `POST /vm/:template` endpoint --- spec/vmpooler/api/v1_spec.rb | 180 ++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 1 deletion(-) diff --git a/spec/vmpooler/api/v1_spec.rb b/spec/vmpooler/api/v1_spec.rb index 5f7643a..b5971fc 100644 --- a/spec/vmpooler/api/v1_spec.rb +++ b/spec/vmpooler/api/v1_spec.rb @@ -190,7 +190,8 @@ describe Vmpooler::API::V1 do pools: [ {'name' => 'pool1', 'size' => 5}, {'name' => 'pool2', 'size' => 10} - ] + ], + alias: { 'poolone' => 'pool1' } } } before do @@ -222,6 +223,31 @@ describe Vmpooler::API::V1 do expect_json(ok = true, http = 200) end + it 'returns a single VM for an alias' do + expect(redis).to receive(:exists).with("vmpooler__ready__poolone").and_return(false) + + post "#{prefix}/vm", '{"poolone":"1"}' + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + + it 'fails on nonexistant pools' do + expect(redis).to receive(:exists).with("vmpooler__ready__poolpoolpool").and_return(false) + + post "#{prefix}/vm", '{"poolpoolpool":"1"}' + + expect_json(ok = false, http = 404) + end + it 'returns multiple VMs' do post "#{prefix}/vm", '{"pool1":"1","pool2":"1"}' @@ -305,6 +331,158 @@ describe Vmpooler::API::V1 do end end + describe '/vm/:template' do + let(:redis) { double('redis') } + 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' } + } } + + before do + app.settings.set :config, config + app.settings.set :redis, redis + + allow(redis).to receive(:exists).and_return '1' + allow(redis).to receive(:hget).with('vmpooler__token__abcdefghijklmnopqrstuvwxyz012345', 'user').and_return 'jdoe' + allow(redis).to receive(:hset).and_return '1' + allow(redis).to receive(:sadd).and_return '1' + allow(redis).to receive(:scard).and_return '5' + allow(redis).to receive(:spop).with('vmpooler__ready__pool1').and_return 'abcdefghijklmnop' + allow(redis).to receive(:spop).with('vmpooler__ready__pool2').and_return 'qrstuvwxyz012345' + end + + describe 'POST /vm/:template' do + it 'returns a single VM' do + post "#{prefix}/vm/pool1", '' + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + + it 'returns a single VM for an alias' do + expect(redis).to receive(:exists).with("vmpooler__ready__poolone").and_return(false) + + post "#{prefix}/vm/poolone", '' + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + + it 'fails on nonexistant pools' do + expect(redis).to receive(:exists).with("vmpooler__ready__poolpoolpool").and_return(false) + + post "#{prefix}/vm/poolpoolpool", '' + + expect_json(ok = false, http = 404) + end + + it 'returns multiple VMs' do + post "#{prefix}/vm/pool1+pool2", '' + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + }, + pool2: { + hostname: 'qrstuvwxyz012345' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + + context '(auth not configured)' do + let(:config) { { auth: false } } + + it 'does not extend VM lifetime if auth token is provided' do + expect(redis).not_to receive(:hset).with("vmpooler__vm__abcdefghijklmnop", "lifetime", 2) + + post "#{prefix}/vm/pool1", '', { + 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345' + } + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + end + + context '(auth configured)' do + let(:config) { { auth: true } } + + it 'extends VM lifetime if auth token is provided' do + expect(redis).to receive(:hset).with("vmpooler__vm__abcdefghijklmnop", "lifetime", 2).once + + post "#{prefix}/vm/pool1", '', { + 'HTTP_X_AUTH_TOKEN' => 'abcdefghijklmnopqrstuvwxyz012345' + } + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + + it 'does not extend VM lifetime if auth token is not provided' do + expect(redis).not_to receive(:hset).with("vmpooler__vm__abcdefghijklmnop", "lifetime", 2) + + post "#{prefix}/vm/pool1", '' + + expected = { + ok: true, + pool1: { + hostname: 'abcdefghijklmnop' + } + } + + expect(last_response.body).to eq(JSON.pretty_generate(expected)) + + expect_json(ok = true, http = 200) + end + end + end + end + describe '/vm/:hostname' do let(:redis) { double('redis') } let(:prefix) { '/api/v1' }