Skip to content

Commit

Permalink
Merge pull request ManageIQ#195 from grosser/grosser/exception
Browse files Browse the repository at this point in the history
move exception into Kubeclient namespace
# Conflicts:
#	README.md
#	lib/kubeclient/common.rb
  • Loading branch information
cben committed Jan 22, 2018
1 parent b35476c commit 5d3e5df
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 37 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ You can find information about tokens in [this guide](http://kubernetes.io/docs/

### Non-blocking IO

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 @@ -460,6 +460,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:

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_error'
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 @@ -114,7 +114,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::HttpError.new(e.http_code, err_message, e.response)
end

def discover
Expand Down Expand Up @@ -370,7 +370,7 @@ def all_entities(options = {})
method_name = "get_#{entity.method_names[1]}"
begin
result_hash[entity.method_names[0]] = send(method_name, options)
rescue KubeException
rescue Kubeclient::HttpError
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,5 @@
# Kubernetes HTTP Exceptions
# TODO: remove this on next major version bump
# Deprected http exception
class KubeException < StandardError
attr_reader :error_code, :message, :response

Expand All @@ -16,3 +17,9 @@ def to_s
string
end
end

module Kubeclient
# Exception that is raised when a http request fails
class HttpError < 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
raise KubeException.new(response.code, response.reason, response)
raise Kubeclient::HttpError.new(response.code, response.reason, response)
end

buffer = ''
Expand Down
20 changes: 10 additions & 10 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
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 @@ -45,8 +45,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::HttpError => exception
assert_instance_of(Kubeclient::HttpError, exception)
assert_equal(404, exception.error_code)
end

Expand Down Expand Up @@ -89,8 +89,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::HttpError => exception
assert_instance_of(Kubeclient::HttpError, exception)
assert_equal(404, exception.error_code)
end
end
Expand All @@ -106,8 +106,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::HttpError => exception
assert_instance_of(Kubeclient::HttpError, exception)
assert_equal(404, exception.error_code)
end
end
Expand Down
34 changes: 23 additions & 11 deletions test/test_kubeclient.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'

# Kubernetes client entity tests
class KubeClientTest < MiniTest::Test
class KubeclientTest < MiniTest::Test
def test_json
our_object = Kubeclient::Resource.new
our_object.foo = 'bar'
Expand Down Expand Up @@ -71,18 +71,30 @@ def test_exception

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

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

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

assert_includes(exception.to_s, ' for POST http://localhost:8080/api')
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 @@ -100,7 +112,7 @@ def test_api_ssl_failure

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

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

Expand Down Expand Up @@ -154,7 +166,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::HttpError) { client.api_valid? }
end

def test_api_valid_with_non_json
Expand All @@ -173,11 +185,11 @@ def test_nonjson_exception

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

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

assert_instance_of(KubeException, exception)
assert_instance_of(Kubeclient::HttpError, exception)
assert(exception.message.include?('Not Found'))
assert_equal(404, exception.error_code)
end
Expand Down Expand Up @@ -491,14 +503,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::HttpError.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::HttpError) { client.get_pods }
assert_equal(403, exception.error_code)
assert_equal(error_message, exception.message)
assert_equal(response, exception.response)
Expand Down Expand Up @@ -570,14 +582,14 @@ 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::HttpError.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::HttpError) { 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::HttpError) do
client.method(:get_pods)
end
client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
assert_raises(KubeException) do
assert_raises(Kubeclient::HttpError) 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 @@ -185,7 +185,7 @@ def test_get_service_no_ns

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

exception = assert_raises(KubeException) do
exception = assert_raises(Kubeclient::HttpError) 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::HttpError) do
client.watch_pods.each do
end
end
Expand Down

0 comments on commit 5d3e5df

Please sign in to comment.