Skip to content

Commit

Permalink
move exception into Kubeclient namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Nov 4, 2016
1 parent 0e5e28b commit fa5ad97
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 35 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1',

You can find information about token in [this guide](http://kubernetes.io/docs/user-guide/accessing-the-cluster/) and in [this reference](http://kubernetes.io/docs/admin/authentication/).

You can also use kubeclient with non-blocking sockets such as Celluloid::IO, see [here](https://github.com/httprb/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)
You can also use kubeclient with non-blocking sockets such as Celluloid::IO, see [here](https://github.com/httprb/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)
for details. For example:

```ruby
Expand Down Expand Up @@ -393,6 +393,10 @@ client.process_template template
## Upgrading
#### past version 2.0
Replace `KubeException` with `KubeClient::HttpException`
#### past version 1.2.0
Replace Specific Entity class references:
```ruby
Expand Down
2 changes: 1 addition & 1 deletion lib/kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'json'
require 'rest-client'
require 'kubeclient/entity_list'
require 'kubeclient/kube_exception'
require 'kubeclient/http_exception'
require 'kubeclient/watch_notice'
require 'kubeclient/watch_stream'
require 'kubeclient/common'
Expand Down
4 changes: 2 additions & 2 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def handle_exception
{}
end
err_message = json_error_msg['message'] || e.message
raise KubeException.new(e.http_code, err_message, e.response)
raise Kubeclient::HttpException.new(e.http_code, err_message, e.response)
end

def discover
Expand Down Expand Up @@ -358,7 +358,7 @@ def all_entities
method_name = "get_#{entity.method_names[1]}"
begin
result_hash[entity.method_names[0]] = send(method_name)
rescue KubeException
rescue Kubeclient::HttpException
next # do not fail due to resources not supporting get
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Kubernetes HTTP Exceptions
# TODO: remove this on next major version bump
class KubeException < StandardError
attr_reader :error_code, :message, :response

Expand All @@ -12,3 +12,9 @@ def to_s
'HTTP status code ' + @error_code.to_s + ', ' + @message
end
end

module Kubeclient
# Exception that is raised when a http request fails
class HttpException < KubeException
end
end
2 changes: 1 addition & 1 deletion lib/kubeclient/watch_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def each
@http_client = build_client
response = @http_client.request(:get, @uri, build_client_options)
unless response.code < 300
fail KubeException.new(response.code, response.reason, response)
fail Kubeclient::HttpException.new(response.code, response.reason, response)
end

buffer = ''
Expand Down
18 changes: 9 additions & 9 deletions test/test_config.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
require 'test_helper'

def test_config_file(name)
File.new(File.join(File.dirname(__FILE__), 'config', name))
end

# Testing Kubernetes client configuration
class KubeClientConfigTest < MiniTest::Test
def test_allinone
config = Kubeclient::Config.read(test_config_file('allinone.kubeconfig'))
config = Kubeclient::Config.read(config_file('allinone.kubeconfig'))
assert_equal(['default/localhost:8443/system:admin'], config.contexts)
check_context(config.context, ssl: true)
end

def test_external
config = Kubeclient::Config.read(test_config_file('external.kubeconfig'))
config = Kubeclient::Config.read(config_file('external.kubeconfig'))
assert_equal(['default/localhost:8443/system:admin'], config.contexts)
check_context(config.context, ssl: true)
end

def test_nouser
config = Kubeclient::Config.read(test_config_file('nouser.kubeconfig'))
config = Kubeclient::Config.read(config_file('nouser.kubeconfig'))
assert_equal(['default/localhost:8443/nouser'], config.contexts)
check_context(config.context, ssl: false)
end

def test_user_token
config = Kubeclient::Config.read(test_config_file('userauth.kubeconfig'))
config = Kubeclient::Config.read(config_file('userauth.kubeconfig'))
assert_equal(['localhost/system:admin:token', 'localhost/system:admin:userpass'],
config.contexts)
context = config.context('localhost/system:admin:token')
Expand All @@ -34,7 +30,7 @@ def test_user_token
end

def test_user_password
config = Kubeclient::Config.read(test_config_file('userauth.kubeconfig'))
config = Kubeclient::Config.read(config_file('userauth.kubeconfig'))
assert_equal(['localhost/system:admin:token', 'localhost/system:admin:userpass'],
config.contexts)
context = config.context('localhost/system:admin:userpass')
Expand Down Expand Up @@ -69,4 +65,8 @@ def check_context(context, ssl: true)
assert_equal(OpenSSL::SSL::VERIFY_NONE, context.ssl_options[:verify_ssl])
end
end

def config_file(name)
File.new(File.join(File.dirname(__FILE__), 'config', name))
end
end
12 changes: 6 additions & 6 deletions test/test_guestbook_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_create_guestbook_entities

def delete_namespace(client, namespace_name)
client.delete_namespace namespace_name
rescue KubeException => exception
assert_instance_of(KubeException, exception)
rescue Kubeclient::HttpException => exception
assert_instance_of(Kubeclient::HttpException, exception)
assert_equal(404, exception.error_code)
end

Expand Down Expand Up @@ -85,8 +85,8 @@ def delete_services(client, namespace, services)
# it's just a string - service name
client.delete_service service, namespace
end
rescue KubeException => exception
assert_instance_of(KubeException, exception)
rescue Kubeclient::HttpException => exception
assert_instance_of(Kubeclient::HttpException, exception)
assert_equal(404, exception.error_code)
end
end
Expand All @@ -102,8 +102,8 @@ def delete_replication_controllers(client, namespace, replication_controllers)
# it's just a string - rc name
client.delete_replication_controller rc, namespace
end
rescue KubeException => exception
assert_instance_of(KubeException, exception)
rescue Kubeclient::HttpException => exception
assert_instance_of(Kubeclient::HttpException, exception)
assert_equal(404, exception.error_code)
end
end
Expand Down
32 changes: 22 additions & 10 deletions test/test_kubeclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,28 @@ def test_exception

client = Kubeclient::Client.new 'http://localhost:8080/api/'

exception = assert_raises(KubeException) do
exception = assert_raises(Kubeclient::HttpException) do
service = client.create_service service
end

assert_instance_of(KubeException, exception)
assert_instance_of(Kubeclient::HttpException, exception)
assert_equal("converting to : type names don't match (Pod, Namespace)",
exception.message)
assert_equal(409, exception.error_code)
end

def test_deprecated_exception
error_message = 'certificate verify failed'

stub_request(:get, 'http://localhost:8080/api')
.to_raise(OpenSSL::SSL::SSLError.new(error_message))

client = Kubeclient::Client.new 'http://localhost:8080/api/'

exception = assert_raises(KubeException) { client.api }
assert_equal(error_message, exception.message)
end

def test_api
stub_request(:get, 'http://localhost:8080/api')
.to_return(status: 200, body: open_test_file('versions_list.json'))
Expand All @@ -99,7 +111,7 @@ def test_api_ssl_failure

client = Kubeclient::Client.new 'http://localhost:8080/api/'

exception = assert_raises(KubeException) { client.api }
exception = assert_raises(Kubeclient::HttpException) { client.api }
assert_equal(error_message, exception.message)
end

Expand Down Expand Up @@ -144,7 +156,7 @@ def test_api_valid_with_bad_endpoint
.to_return(status: [404, 'Resource Not Found'])

client = Kubeclient::Client.new 'http://localhost:8080/api/'
assert_raises(KubeException) { client.api_valid? }
assert_raises(Kubeclient::HttpException) { client.api_valid? }
end

def test_api_valid_with_non_json
Expand All @@ -165,11 +177,11 @@ def test_nonjson_exception

client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'

exception = assert_raises(KubeException) do
exception = assert_raises(Kubeclient::HttpException) do
client.get_services
end

assert_instance_of(KubeException, exception)
assert_instance_of(Kubeclient::HttpException, exception)
assert(exception.message.include?('Not Found'))
assert_equal(404, exception.error_code)
end
Expand Down Expand Up @@ -393,14 +405,14 @@ def test_api_bearer_token_failure

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

client = Kubeclient::Client.new 'http://localhost:8080/api/',
auth_options: {
bearer_token: 'invalid_token'
}

exception = assert_raises(KubeException) { client.get_pods }
exception = assert_raises(Kubeclient::HttpException) { client.get_pods }
assert_equal(403, exception.error_code)
assert_equal(error_message, exception.message)
assert_equal(response, exception.response)
Expand Down Expand Up @@ -457,15 +469,15 @@ def test_api_basic_auth_failure
response = OpenStruct.new(code: 401, message: '401 Unauthorized')

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

client = Kubeclient::Client.new 'http://localhost:8080/api/',
auth_options: {
username: 'username',
password: 'password'
}

exception = assert_raises(KubeException) { client.get_pods }
exception = assert_raises(Kubeclient::HttpException) { client.get_pods }
assert_equal(401, exception.error_code)
assert_equal(error_message, exception.message)
assert_equal(response, exception.response)
Expand Down
4 changes: 2 additions & 2 deletions test/test_missing_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def test_missing
status: 404
) # If discovery fails we expect the below raise an exception
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
assert_raises(KubeException) do
assert_raises(Kubeclient::HttpException) do
client.method(:get_pods)
end
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
assert_raises(KubeException) do
assert_raises(Kubeclient::HttpException) do
client.respond_to?(:get_pods)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def test_get_service_no_ns

client = Kubeclient::Client.new 'http://localhost:8080/api/'

exception = assert_raises(KubeException) do
exception = assert_raises(Kubeclient::HttpException) do
client.get_service 'redis-slave'
end
assert_equal(404, exception.error_code)
Expand Down
2 changes: 1 addition & 1 deletion test/test_watch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_watch_pod_failure
stub_request(:get, %r{.*\/watch/pods}).to_return(status: 404)

client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1'
assert_raises KubeException do
assert_raises Kubeclient::HttpException do
client.watch_pods.each do
end
end
Expand Down

0 comments on commit fa5ad97

Please sign in to comment.