Catch HTTP 403 response

Also catch not authorized requests with HTTP 403 response in order to
handle it further.

Change-Id: Ib922bd5892f0204566656303e6a484daa8d2d5e7
This commit is contained in:
Michael Polenchuk 2015-11-30 20:11:33 +03:00
parent 21c5f820c7
commit 92b92aa4c9
2 changed files with 25 additions and 1 deletions

View File

@ -50,7 +50,7 @@ class Puppet::Provider::Openstack < Puppet::Provider
end
break
rescue Puppet::ExecutionFailure => e
if e.message =~ /HTTP 401/
if e.message =~ /HTTP 40[13]/
raise(Puppet::Error::OpenstackUnauthorizedError, 'Could not authenticate.')
elsif e.message =~ /Unable to establish connection/
current_time = Time.now.to_i

View File

@ -53,6 +53,30 @@ describe Puppet::Provider::Openstack do
Puppet::Provider::Openstack.request('project', 'list', ['--long'])
end
end
context 'catch unauthorized errors' do
it 'should raise an error with non-existent user' do
ENV['OS_USERNAME'] = 'test'
ENV['OS_PASSWORD'] = 'abc123'
ENV['OS_PROJECT_NAME'] = 'test'
ENV['OS_AUTH_URL'] = 'http://127.0.0.1:5000'
provider.class.stubs(:openstack)
.with('project', 'list', '--quiet', '--format', 'csv', ['--long'])
.raises(Puppet::ExecutionFailure, 'Could not find user: test (HTTP 401)')
expect do
Puppet::Provider::Openstack.request('project', 'list', ['--long'])
end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/)
end
it 'should raise an error with not authorized to perform' do
provider.class.stubs(:openstack)
.with('role', 'list', '--quiet', '--format', 'csv', ['--long'])
.raises(Puppet::ExecutionFailure, 'You are not authorized to perform the requested action: identity:list_grants (HTTP 403)')
expect do
Puppet::Provider::Openstack.request('role', 'list', ['--long'])
end.to raise_error(Puppet::Error::OpenstackUnauthorizedError, /Could not authenticate/)
end
end
end
describe 'parse_csv' do