(#32) Handle vmpooler responses when token is invalid

This commit adds an errors class to handle when pooler tokens are
invalid. The retrieve method in Pooler will raise an AuthError if it
gets an HTTP 401 from the vmpooler.
This commit is contained in:
Brian Cain 2016-09-24 10:22:48 -07:00
parent 86e1792775
commit 05e9d5a0cc
3 changed files with 20 additions and 1 deletions

5
lib/vmfloaty/errors.rb Normal file
View file

@ -0,0 +1,5 @@
class AuthError < StandardError
def initialize(msg="Could not authenticate to pooler")
super
end
end

View file

@ -1,6 +1,7 @@
require 'faraday'
require 'vmfloaty/http'
require 'json'
require 'vmfloaty/errors'
class Pooler
def self.list(verbose, url, os_filter=nil)
@ -40,10 +41,13 @@ class Pooler
response = conn.post "vm/#{os_string}"
res_body = JSON.parse(response.body)
if res_body["ok"]
res_body
elsif response.status == 401
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
else
raise "Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/vm/#{os_string}. #{res_body}"
end
end

View file

@ -44,6 +44,16 @@ describe Pooler do
@retrieve_response_body_double = "{\"ok\":true,\"debian-7-i386\":{\"hostname\":[\"sc0o4xqtodlul5w\",\"4m4dkhqiufnjmxy\"]},\"centos-7-x86_64\":{\"hostname\":\"zb91y9qbrbf6d3q\"}}"
end
it "raises an AuthError if the token is invalid" do
stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).
to_return(:status => 401, :body => "{\"ok\":false}", :headers => {})
vm_hash = {}
vm_hash['debian-7-i386'] = 1
expect{ Pooler.retrieve(false, vm_hash, 'mytokenfile', @vmpooler_url) }.to raise_error(AuthError)
end
it "retrieves a single vm with a token" do
stub_request(:post, "#{@vmpooler_url}/vm/debian-7-i386").
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}).