diff --git a/lib/scalarm/service_core/information_service.rb b/lib/scalarm/service_core/information_service.rb index c905b18..9d181b2 100644 --- a/lib/scalarm/service_core/information_service.rb +++ b/lib/scalarm/service_core/information_service.rb @@ -19,6 +19,11 @@ def initialize(service_url, username, password, development=false) @username = username @password = password @development = development + + + scheme = @development ? 'http' : 'https' + + @uri = URI("#{scheme}://#{username}:#{password}@#{service_url}") end def register_service(service, host, port) @@ -64,37 +69,36 @@ def get_list_of(service) end def send_request(request, data = nil, opts = {}) - @host, @port = @service_url.split(':') - @port, @prefix = @port.split('/') - @prefix = @prefix.nil? ? '/' : "/#{@prefix}/" - - Logger.info("InformationService: sending #{request} request to the Information Service at '#{@host}:#{@port}'") + Logger.info("[InformationService]: sending #{request} request at '#{@uri}") + resource = @uri.path + '/' + request req = if data.nil? - Net::HTTP::Get.new(@prefix + request) + Net::HTTP::Get.new(resource) else if opts.include?(:method) and opts[:method] == 'DELETE' - Net::HTTP::Delete.new(@prefix + request) + Net::HTTP::Delete.new(resource) else - Net::HTTP::Post.new(@prefix + request) + Net::HTTP::Post.new(resource) end end - req.basic_auth(@username, @password) - req.set_form_data(data) unless data.nil? + if not req.is_a?(Net::HTTP::Get) + req.basic_auth(@username, @password) + req.set_form_data(data) unless data.nil? + end - if @development + if @uri.scheme == 'http' ssl_options = {} else ssl_options = { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE } end begin - response = Net::HTTP.start(@host, @port, ssl_options) { |http| http.request(req) } + response = Net::HTTP.start(@uri.host, @uri.port, ssl_options) { |http| http.request(req) } #puts "#{Time.now} --- response from Information Service is #{response.code} #{response.body}" return response.code, response.body rescue Exception => e - Logger.error("Exception occurred on request to Information Service: #{e.to_s}") + Logger.error("[InformationService] Exception occurred: #{e.to_s}") Logger.error(e.backtrace.join("\n\t")) raise diff --git a/scalarm-service_core.gemspec b/scalarm-service_core.gemspec index d915411..f89801f 100644 --- a/scalarm-service_core.gemspec +++ b/scalarm-service_core.gemspec @@ -24,6 +24,7 @@ InformationService client, parameters validator and other utils. spec.add_development_dependency 'bundler', '~> 1.7' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'mocha' + spec.add_development_dependency 'webmock' spec.add_dependency 'activesupport', '~> 4.1' spec.add_dependency 'actionpack', '~> 4.1' spec.add_dependency 'rest-client', '~> 1.8' diff --git a/test/information_service_test.rb b/test/information_service_test.rb new file mode 100644 index 0000000..398ba14 --- /dev/null +++ b/test/information_service_test.rb @@ -0,0 +1,45 @@ +require 'minitest/autorun' +require 'webmock/minitest' + +require '../lib/scalarm/service_core/information_service' +require '../lib/scalarm/service_core/logger' + +class InformationServiceTest < Minitest::Test + + def setup + @host = 'system.scalarm.com' + + @is = Scalarm::ServiceCore::InformationService.new("#{@host}:11300", 'scalarm', 'scalarm') + @is2 = Scalarm::ServiceCore::InformationService.new("#{@host}/information", 'scalarm', 'scalarm') + end + + def test_get_experiment_manager_host_port + stub_request(:any, "https://#{@host}:11300/experiment_managers").to_return( + body: "[\"system.scalarm.com\"]", + headers: {'Content-Type' => 'application/json'} + ) + + em_list = @is.get_list_of('experiment_managers') + assert (not em_list.empty?) + end + + def test_get_experiment_manager_host_path + stub_request(:any, "https://#{@host}/information/experiment_managers").to_return( + body: "[\"system.scalarm.com\"]", + headers: {'Content-Type' => 'application/json'} + ) + + stub_request(:any, "https://#{@host}/information/storage_managers").to_return( + body: '[]', + headers: {'Content-Type' => 'application/json'} + ) + + em_list = @is2.get_list_of('experiment_managers') + assert (not em_list.empty?) + + sm_list = @is2.get_list_of('storage_managers') + assert (not sm_list.nil?) + assert_empty sm_list + end + +end \ No newline at end of file