From b352b40550a502fbec805554fa092ce9d6db8c44 Mon Sep 17 00:00:00 2001 From: Ameya Varade Date: Thu, 29 May 2014 16:13:35 +0530 Subject: [PATCH] Implemented review comments. --- lib/chef/knife/cloud/exceptions.rb | 2 +- lib/chef/knife/cloud/fog/service.rb | 40 ++++++++--------- spec/unit/fog_service_spec.rb | 68 ++++++++++++++++++----------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/lib/chef/knife/cloud/exceptions.rb b/lib/chef/knife/cloud/exceptions.rb index 4ea1717..3c4bbb5 100644 --- a/lib/chef/knife/cloud/exceptions.rb +++ b/lib/chef/knife/cloud/exceptions.rb @@ -30,7 +30,7 @@ class ServerCreateDependenciesError < KnifeCloudError; end class BootstrapError < KnifeCloudError; end class ServerShowError < KnifeCloudError; end class ChefServerError < KnifeCloudError; end - class ServiceNetworkError < KnifeCloudError; end + class NetworkNotFoundError < KnifeCloudError; end end end end diff --git a/lib/chef/knife/cloud/fog/service.rb b/lib/chef/knife/cloud/fog/service.rb index 949a8dc..9d5735d 100644 --- a/lib/chef/knife/cloud/fog/service.rb +++ b/lib/chef/knife/cloud/fog/service.rb @@ -32,7 +32,7 @@ def load_fog_gem def connection add_api_endpoint @connection ||= begin - connection = Fog::Compute.new(@auth_params) + connection = Fog::Compute.new(@auth_params) rescue Excon::Errors::Unauthorized => e error_message = "Connection failure, please check your username and password." ui.fatal(error_message) @@ -46,20 +46,20 @@ def connection def network @network ||= begin - network = Fog::Network.new(@auth_params) - rescue Excon::Errors::Unauthorized => e - error_message = "Connection failure, please check your username and password." - ui.fatal(error_message) - raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}" - rescue Excon::Errors::SocketError => e - error_message = "Connection failure, please check your authentication URL." - ui.fatal(error_message) - raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}" - rescue Fog::Errors::NotFound => e - error_message = "No Network service found. This command is unavailable with current cloud." - ui.fatal(error_message) - raise CloudExceptions::ServiceNetworkError, "#{e.message}. #{error_message}" - end + network = Fog::Network.new(@auth_params) + rescue Excon::Errors::Unauthorized => e + error_message = "Connection failure, please check your username and password." + ui.fatal(error_message) + raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}" + rescue Excon::Errors::SocketError => e + error_message = "Connection failure, please check your authentication URL." + ui.fatal(error_message) + raise CloudExceptions::ServiceConnectionError, "#{e.message}. #{error_message}" + rescue Fog::Errors::NotFound => e + error_message = "No Network service found. This command is unavailable with current cloud." + ui.fatal(error_message) + raise CloudExceptions::NetworkNotFoundError, "#{e.message}. #{error_message}" + end end # cloud server specific implementation methods for commands. @@ -111,14 +111,14 @@ def delete_server(server_name) end end - ["servers", "images", "networks"].each do |iterator| - define_method("list_#{iterator}") do + ["servers", "images", "networks"].each do |resource_type| + define_method("list_#{resource_type}") do begin - case iterator + case resource_type when "networks" - network.method(iterator).call.all + network.method(resource_type).call.all else - connection.method(iterator).call.all + connection.method(resource_type).call.all end rescue Excon::Errors::BadRequest => e handle_excon_exception(CloudExceptions::CloudAPIException, e) diff --git a/spec/unit/fog_service_spec.rb b/spec/unit/fog_service_spec.rb index 573f31a..4d0f607 100644 --- a/spec/unit/fog_service_spec.rb +++ b/spec/unit/fog_service_spec.rb @@ -27,6 +27,30 @@ Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}) instance.network end + + context "connection to fog" do + before do + instance.stub(:exit) + instance.stub(:ui).and_return(Object.new) + instance.ui.should_receive(:fatal) + end + + it "handles Unauthorized exception." do + Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise Excon::Errors::Unauthorized.new("Unauthorized") + expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServiceConnectionError) + end + + it "handles SocketError or any other connection exception." do + socket_error = Excon::Errors::SocketError.new(Exception.new "Mock Error") + Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise socket_error + expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::ServiceConnectionError) + end + + it "handles NetworkNotFoundError exception." do + Fog::Network.should_receive(:new).with({:provider => 'Any Cloud Provider'}).and_raise Fog::Errors::NotFound.new("NotFound") + expect {instance.network}.to raise_error(Chef::Knife::Cloud::CloudExceptions::NetworkNotFoundError) + end + end end context "add_custom_attributes" do @@ -45,33 +69,25 @@ end end - ["servers", "images", "networks"].each do |iterator| - context "list #{iterator}" do - case iterator - when "networks" - it "lists #{iterator}." do - instance.stub_chain(:network, "#{iterator}".to_sym, :all) - instance.method("list_#{iterator}").call - end - - it "handles Excon::Errors::BadRequest exception." do - instance.stub(:ui).and_return(Object.new) - instance.ui.should_receive(:fatal) - instance.stub_chain(:network, "#{iterator}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Network") - expect {instance.method("list_#{iterator}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException) - end - else - it "lists #{iterator}." do - instance.stub_chain(:connection, "#{iterator}".to_sym, :all) - instance.method("list_#{iterator}").call - end + ["servers", "images", "networks"].each do |resource_type| + resource = case resource_type + when "networks" + :network + else + :connection + end + context "list #{resource_type}" do + + it "lists #{resource_type} of the current cloud service provider account." do + instance.stub_chain(resource.to_sym, "#{resource_type}".to_sym, :all) + instance.method("list_#{resource_type}").call + end - it "handles Excon::Errors::BadRequest exception." do - instance.stub(:ui).and_return(Object.new) - instance.ui.should_receive(:fatal) - instance.stub_chain(:connection, "#{iterator}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Server") - expect {instance.method("list_#{iterator}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException) - end + it "handles Excon::Errors::BadRequest exception." do + instance.stub(:ui).and_return(Object.new) + instance.ui.should_receive(:fatal) + instance.stub_chain(resource.to_sym, "#{resource_type}".to_sym, :all).and_raise Excon::Errors::BadRequest.new("Invalid Resource") + expect {instance.method("list_#{resource_type}").call}.to raise_error(Chef::Knife::Cloud::CloudExceptions::CloudAPIException) end end end