From fa5ad97dc4f32d9c0b1e2094c783386342497827 Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Sat, 1 Oct 2016 15:24:28 -0700 Subject: [PATCH] move exception into Kubeclient namespace --- README.md | 6 +++- lib/kubeclient.rb | 2 +- lib/kubeclient/common.rb | 4 +-- .../{kube_exception.rb => http_exception.rb} | 8 ++++- lib/kubeclient/watch_stream.rb | 2 +- test/test_config.rb | 18 +++++------ test/test_guestbook_go.rb | 12 +++---- test/test_kubeclient.rb | 32 +++++++++++++------ test/test_missing_methods.rb | 4 +-- test/test_service.rb | 2 +- test/test_watch.rb | 2 +- 11 files changed, 57 insertions(+), 35 deletions(-) rename lib/kubeclient/{kube_exception.rb => http_exception.rb} (63%) diff --git a/README.md b/README.md index 4731a2e8..53f6ade5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/lib/kubeclient.rb b/lib/kubeclient.rb index 35209e92..e30b3265 100644 --- a/lib/kubeclient.rb +++ b/lib/kubeclient.rb @@ -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' diff --git a/lib/kubeclient/common.rb b/lib/kubeclient/common.rb index 3e188f8e..159f2437 100644 --- a/lib/kubeclient/common.rb +++ b/lib/kubeclient/common.rb @@ -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 @@ -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 diff --git a/lib/kubeclient/kube_exception.rb b/lib/kubeclient/http_exception.rb similarity index 63% rename from lib/kubeclient/kube_exception.rb rename to lib/kubeclient/http_exception.rb index 53305730..4f00cb02 100644 --- a/lib/kubeclient/kube_exception.rb +++ b/lib/kubeclient/http_exception.rb @@ -1,4 +1,4 @@ -# Kubernetes HTTP Exceptions +# TODO: remove this on next major version bump class KubeException < StandardError attr_reader :error_code, :message, :response @@ -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 diff --git a/lib/kubeclient/watch_stream.rb b/lib/kubeclient/watch_stream.rb index cbd450bb..f4b2a68e 100644 --- a/lib/kubeclient/watch_stream.rb +++ b/lib/kubeclient/watch_stream.rb @@ -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 = '' diff --git a/test/test_config.rb b/test/test_config.rb index e06410d1..391830e9 100644 --- a/test/test_config.rb +++ b/test/test_config.rb @@ -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') @@ -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') @@ -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 diff --git a/test/test_guestbook_go.rb b/test/test_guestbook_go.rb index 4ffc3ac4..bdf0012f 100644 --- a/test/test_guestbook_go.rb +++ b/test/test_guestbook_go.rb @@ -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 @@ -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 @@ -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 diff --git a/test/test_kubeclient.rb b/test/test_kubeclient.rb index 08513e16..28e7b64c 100644 --- a/test/test_kubeclient.rb +++ b/test/test_kubeclient.rb @@ -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')) @@ -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 @@ -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 @@ -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 @@ -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) @@ -457,7 +469,7 @@ 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: { @@ -465,7 +477,7 @@ def test_api_basic_auth_failure 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) diff --git a/test/test_missing_methods.rb b/test/test_missing_methods.rb index 17cf83a9..75212659 100644 --- a/test/test_missing_methods.rb +++ b/test/test_missing_methods.rb @@ -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 diff --git a/test/test_service.rb b/test/test_service.rb index 2c88e4da..47bbdd15 100644 --- a/test/test_service.rb +++ b/test/test_service.rb @@ -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) diff --git a/test/test_watch.rb b/test/test_watch.rb index a68fd4c4..c1fb2d02 100644 --- a/test/test_watch.rb +++ b/test/test_watch.rb @@ -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