Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCAL-1142: support for service URLs without port #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions lib/scalarm/service_core/information_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions scalarm-service_core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
45 changes: 45 additions & 0 deletions test/information_service_test.rb
Original file line number Diff line number Diff line change
@@ -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