Skip to content

Commit

Permalink
Raise KubeException instead of Signet::AuthorizationError
Browse files Browse the repository at this point in the history
Fix tests to test JSON error parsing rather than stubbing around that
  • Loading branch information
jeremywadsack committed Nov 21, 2016
1 parent 57edb67 commit 1a5cb7e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
19 changes: 13 additions & 6 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,7 @@ def discovery_needed?(method_sym)
def handle_exception
yield
rescue RestClient::Exception => e
json_error_msg = begin
JSON.parse(e.response || '') || {}
rescue JSON::ParserError
{}
end
err_message = json_error_msg['message'] || e.message
err_message = json_error_message(e.response) || e.message
raise KubeException.new(e.http_code, err_message, e.response)
end

Expand Down Expand Up @@ -496,6 +491,18 @@ def use_default_gcp_token
authorization.apply({})

authorization.access_token
rescue Signet::AuthorizationError => e
err_message = json_error_message(e.response.body) || e.message
raise KubeException.new(e.response.status, err_message, e.response.body)
end

def json_error_message(body)
json_error_msg = begin
JSON.parse(body || '') || {}
rescue JSON::ParserError
{}
end
json_error_msg['message']
end
end
end
27 changes: 20 additions & 7 deletions test/test_kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

# Kubernetes client entity tests
class KubeClientTest < MiniTest::Test
# Used to stub responses from RestClient in exceptions
class RestClientResponseStub < String
attr_reader :code

def initialize(body, code)
@code = code
super(body)
end
end

def test_json
our_object = Kubeclient::Resource.new
our_object.foo = 'bar'
Expand Down Expand Up @@ -390,11 +400,13 @@ def test_api_bearer_token_success
def test_api_bearer_token_failure
error_message = '"/api/v1" is forbidden because ' \
'system:anonymous cannot list on pods in'
response = OpenStruct.new(code: 401, message: error_message)
response = RestClientResponseStub.new('', 403)
exception = RestClient::Exception.new(response)
exception.message = error_message

stub_request(:get, 'http://localhost:8080/api/v1')
.with(headers: { Authorization: 'Bearer invalid_token' })
.to_raise(KubeException.new(403, error_message, response))
.to_raise(exception)

client = Kubeclient::Client.new('http://localhost:8080/api/',
auth_options: {
Expand Down Expand Up @@ -455,10 +467,12 @@ def test_api_basic_auth_back_comp_success

def test_api_basic_auth_failure
error_message = 'HTTP status code 401, 401 Unauthorized'
response = OpenStruct.new(code: 401, message: '401 Unauthorized')
response = RestClientResponseStub.new('401 Unauthorized', 401)
exception = RestClient::Exception.new(response)
exception.message = error_message

stub_request(:get, 'http://username:password@localhost:8080/api/v1')
.to_raise(KubeException.new(401, error_message, response))
.to_raise(exception)

client = Kubeclient::Client.new('http://localhost:8080/api/',
auth_options: {
Expand Down Expand Up @@ -645,15 +659,14 @@ def test_auth_use_default_gcp_failure
body: '',
headers: { 'Content-Type' => 'application/json; charset=UTF-8' })

# TODO: What do we want to do here?
# Should we raise our own exception or just let this pass through?
expected_msg = 'Authorization failed.'
exception = assert_raises(Signet::AuthorizationError) do
exception = assert_raises(KubeException) do
Kubeclient::Client.new('http://localhost:8080',
auth_options: {
use_default_gcp: true
})
end
assert_equal(401, exception.error_code)
assert_equal(expected_msg, exception.message)
end

Expand Down

0 comments on commit 1a5cb7e

Please sign in to comment.