From bac954e457893d090048b7ee4e25ab667adef4dc Mon Sep 17 00:00:00 2001 From: Anirvan BASU Date: Mon, 12 Dec 2016 15:19:27 +0100 Subject: [PATCH] Modified the mechanism for URI construction - for test & development environments. Added spec tests also. --- app/models/grid5000/job.rb | 19 +++++++++++++++++-- lib/grid5000/router.rb | 28 ++++++++++++++++++++-------- spec/lib/grid5000/router_spec.rb | 18 +++++++++++++++++- spec/models/grid5000/job_spec.rb | 16 ++++++++++++++++ 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/app/models/grid5000/job.rb b/app/models/grid5000/job.rb index 567a3703..135e7da3 100644 --- a/app/models/grid5000/job.rb +++ b/app/models/grid5000/job.rb @@ -25,15 +25,23 @@ class Job :scheduled_at, :walltime, :queue, :state, :mode, :command, :directory, :exit_code, :signal, :checkpoint, :anterior, :message, :stderr, :stdout] + # abasu bug ref. 7360 - added :job_key_from_file --- 29.11.2016 + # OAR expects these as import-job-key-from-file + READ_ONLY_UNDERSCORE_ATTRIBUTES = [:import_job_key_from_file] READ_WRITE_ATTRIBUTES = [:name, :project] attr_reader *READ_ONLY_ATTRIBUTES + attr_reader *READ_ONLY_UNDERSCORE_ATTRIBUTES attr_accessor *READ_WRITE_ATTRIBUTES def initialize(h = {}) @errors = [] h = h.symbolize_keys - (READ_ONLY_ATTRIBUTES+READ_WRITE_ATTRIBUTES).each do |attribute| - value = h[attribute] + (READ_ONLY_ATTRIBUTES+READ_WRITE_ATTRIBUTES+READ_ONLY_UNDERSCORE_ATTRIBUTES).each do |attribute| + if READ_ONLY_UNDERSCORE_ATTRIBUTES.include?(attribute) + value = h[attribute.to_s.gsub('_','-').to_sym] + else + value = h[attribute] + end value.symbolize_keys! if value.kind_of?(Hash) instance_variable_set "@#{attribute.to_s}", value end @@ -63,10 +71,16 @@ class Job h["reservation"] = Time.at(reservation).strftime("%Y-%m-%d %H:%M:%S") unless reservation.nil? h["property"] = properties unless properties.nil? || properties.empty? h["type"] = types unless types.nil? || types.empty? + %w{walltime queue directory name project signal checkpoint stderr stdout}.each do |prop| value = instance_variable_get "@#{prop}" h[prop] = value unless value.nil? end + # abasu bug ref. 7360 - added import_job_key_from_file --- 29.11.2016 + %w{import-job-key-from-file}.each do |prop| + value = instance_variable_get "@#{prop.gsub('-','_')}" + h[prop] = value unless value.nil? + end # --hold # --stdout= # --stderr= @@ -82,6 +96,7 @@ class Job h["links"] = link_proc.call(self) end end + h end diff --git a/lib/grid5000/router.rb b/lib/grid5000/router.rb index 67a2bc26..f4bd776c 100644 --- a/lib/grid5000/router.rb +++ b/lib/grid5000/router.rb @@ -33,31 +33,43 @@ module Grid5000 nil else File.join("/", (request.env['HTTP_X_API_VERSION'] || "")) - end + end # api_version = if request.env['HTTP_X_API_VERSION'].blank? + path_prefix = if request.env['HTTP_X_API_PATH_PREFIX'].blank? nil else File.join("/", (request.env['HTTP_X_API_PATH_PREFIX'] || "")) - end + end # path_prefix = if request.env['HTTP_X_API_PATH_PREFIX'].blank? + mount_path = if request.env['HTTP_X_API_MOUNT_PATH'].blank? nil else File.join("/", (request.env['HTTP_X_API_MOUNT_PATH'] || "")) - end + end # mount_path = if request.env['HTTP_X_API_MOUNT_PATH'].blank? + uri = File.join("/", *[api_version, path_prefix, path].compact) uri.gsub!(mount_path, '') unless mount_path.nil? uri = "/" if uri.blank? + # abasu / dmargery - bug ref 7360 - for correct URI construction if in_or_out == :out || relative_or_absolute == :absolute - uri = URI.join(base_uri(in_or_out), uri).to_s - end + root_uri=URI(base_uri(in_or_out)) + if root_uri.path.blank? + root_path='' + else + root_path=root_uri.path+'/' + end # if root_uri.path.blank? + + uri = URI.join(root_uri, root_path+uri).to_s + end # if in_or_out == :out || relative_or_absolute == :absolute uri - end + end # def uri_to() + # FIXME: move Rails.config to Grid5000.config def base_uri(in_or_out = :in) Rails.my_config("base_uri_#{in_or_out}".to_sym) - end + end # def base_uri() end end -end \ No newline at end of file +end diff --git a/spec/lib/grid5000/router_spec.rb b/spec/lib/grid5000/router_spec.rb index 08babd86..d2a43978 100644 --- a/spec/lib/grid5000/router_spec.rb +++ b/spec/lib/grid5000/router_spec.rb @@ -40,4 +40,20 @@ describe Grid5000::Router do }) Grid5000::Router.uri_to(request, "/sites/rennes/jobs").should == "/sid/grid5000/sites/rennes/jobs" end -end \ No newline at end of file + + it "should take into account the parameters of the config file with empty path" do + Rails.my_config("base_uri_out".to_sym).should == "http://api-out.local" + request = mock(Rack::MockRequest, :env => { + 'HTTP_X_API_VERSION' => 'sid' + }) + Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out).should == "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json" + end + + it "should take into account the parameters of the config file with path (for dev environment)" do + Api::Application::CONFIG["base_uri_out"] = "http://api-out.local/sid" + Rails.my_config("base_uri_out".to_sym).should == "http://api-out.local/sid" + request = mock(Rack::MockRequest, :env => {}) + Grid5000::Router.uri_to(request, "/sites/rennes/internal/oarapi/jobs/374172.json", :out).should == "http://api-out.local/sid/sites/rennes/internal/oarapi/jobs/374172.json" + end + +end diff --git a/spec/models/grid5000/job_spec.rb b/spec/models/grid5000/job_spec.rb index 14e8bd60..43c14611 100644 --- a/spec/models/grid5000/job_spec.rb +++ b/spec/models/grid5000/job_spec.rb @@ -109,6 +109,22 @@ describe Grid5000::Job do "resource"=>"/nodes=1" } end + + # abasu bug ref. 7360 - added test for import job_key_from_file --- 29.11.2016 + it "should copy import-job-key-from-file to a hash structure" do + reservation = Time.parse("2009-11-10 15:54:56Z") + job = Grid5000::Job.new(:resources => "/nodes=1", :reservation => reservation, :command => "id", :types => ["deploy", "idempotent"], :walltime => 3600, :checkpoint => 40, :'import-job-key-from-file' => "file://abcd") + job.should be_valid + job.to_hash(:destination => "oar-2.4-submission").should == { + "script"=>"id", + "checkpoint"=>40, + "walltime"=>3600, + "reservation"=>"2009-11-10 15:54:56", + "resource"=>"/nodes=1", + "type"=>["deploy", "idempotent"], + "import-job-key-from-file"=> "file://abcd" + } + end # it "should copy import-job-key-from-file to a hash structure" do end describe "Creating for future submission" do -- GitLab