[ARVADOS] created: 6caf9c476d842c59883ca7087182fde2445bd712

git at public.curoverse.com git at public.curoverse.com
Fri Jul 4 13:22:42 EDT 2014


        at  6caf9c476d842c59883ca7087182fde2445bd712 (commit)


commit 6caf9c476d842c59883ca7087182fde2445bd712
Author: Tom Clegg <tom at curoverse.com>
Date:   Fri Jul 4 13:01:28 2014 -0400

    3185: Fix job validation failure with no errors given.
    
    * Do not fail docker validation if runtime_constraints==nil.
    
    * Reposition "check docker image" and "check script version" as
      validations, rather than before_validation and before_create/update
      filters respectively.
    
    * Use valid values for the non-docker attributes in docker test cases.
    
    * Add tests to ensure job validation failures provide error messages.
    
    Move test/functional/arvados/v1/git_setup.rb to
    test/helpers/git_test_helper.rb and fix bugs:
    
    * Use "setup do" form instead of "def setup" to avoid clobbering other
      setup tasks
    
    * Restore git_repositories_dir to its original value during teardown,
      to avoid polluting other tests

diff --git a/services/api/app/models/job.rb b/services/api/app/models/job.rb
index 654778a..fc445ae 100644
--- a/services/api/app/models/job.rb
+++ b/services/api/app/models/job.rb
@@ -6,11 +6,10 @@ class Job < ArvadosModel
   serialize :script_parameters, Hash
   serialize :runtime_constraints, Hash
   serialize :tasks_summary, Hash
-  before_validation :find_docker_image_locator
   before_create :ensure_unique_submit_id
-  before_create :ensure_script_version_is_commit
-  before_update :ensure_script_version_is_commit
   after_commit :trigger_crunch_dispatch_if_cancelled, :on => :update
+  validate :ensure_script_version_is_commit
+  validate :find_docker_image_locator
 
   has_many :commit_ancestors, :foreign_key => :descendant, :primary_key => :script_version
 
@@ -87,7 +86,8 @@ class Job < ArvadosModel
         self.supplied_script_version = self.script_version if self.supplied_script_version.nil? or self.supplied_script_version.empty?
         self.script_version = sha1
       else
-        raise ArgumentError.new("Specified script_version does not resolve to a commit")
+        self.errors.add :script_version, "#{self.script_version} does not resolve to a commit"
+        return false
       end
     end
   end
@@ -104,16 +104,22 @@ class Job < ArvadosModel
   def find_docker_image_locator
     # Find the Collection that holds the Docker image specified in the
     # runtime constraints, and store its locator in docker_image_locator.
-    if runtime_constraints.nil? then
+    unless runtime_constraints.is_a? Hash
+      # We're still in validation stage, so we can't assume
+      # runtime_constraints isn't something horrible like an array or
+      # a string. Treat those cases as "no docker image supplied";
+      # other validations will fail anyway.
       self.docker_image_locator = nil
-      return false
+      return true
     end
     image_search = runtime_constraints['docker_image']
     image_tag = runtime_constraints['docker_image_tag']
     if image_search.nil?
       self.docker_image_locator = nil
+      true
     elsif coll = Collection.for_latest_docker_image(image_search, image_tag)
       self.docker_image_locator = coll.uuid
+      true
     else
       errors.add(:docker_image_locator, "not found for #{image_search}")
       false
diff --git a/services/api/test/functional/arvados/v1/commits_controller_test.rb b/services/api/test/functional/arvados/v1/commits_controller_test.rb
index 788cd83..f7f99d1 100644
--- a/services/api/test/functional/arvados/v1/commits_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/commits_controller_test.rb
@@ -1,5 +1,5 @@
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 # NOTE: calling Commit.find_commit_range(user, nil, nil, 'rev') will produce
 # an error message "fatal: bad object 'rev'" on stderr if 'rev' does not exist
@@ -13,7 +13,7 @@ class Arvados::V1::CommitsControllerTest < ActionController::TestCase
   fixtures :repositories, :users
 
   # See git_setup.rb for the commit log for test.git.tar
-  include GitSetup
+  include GitTestHelper
 
   test "test_find_commit_range" do
     authorize_with :active
diff --git a/services/api/test/functional/arvados/v1/git_setup.rb b/services/api/test/functional/arvados/v1/git_setup.rb
deleted file mode 100644
index 46f5f70..0000000
--- a/services/api/test/functional/arvados/v1/git_setup.rb
+++ /dev/null
@@ -1,27 +0,0 @@
-require 'fileutils'
-require 'tmpdir'
-
-# Commit log for test.git.tar
-# master is the main branch
-# b1 is a branch off of master
-# tag1 is a tag
-#
-# 1de84a8 * b1
-# 077ba2a * master
-# 4fe459a * tag1
-# 31ce37f * foo
-
-module GitSetup
-  def setup
-    @tmpdir = Dir.mktmpdir()
-    #puts "setup #{@tmpdir}"
-    `cp test/test.git.tar #{@tmpdir} && cd #{@tmpdir} && tar xf test.git.tar`
-    Rails.configuration.git_repositories_dir = "#{@tmpdir}/test"
-    Commit.refresh_repositories
-  end
-
-  def teardown
-    #puts "teardown #{@tmpdir}"
-    FileUtils.remove_entry @tmpdir, true
-  end
-end
diff --git a/services/api/test/functional/arvados/v1/job_reuse_controller_test.rb b/services/api/test/functional/arvados/v1/job_reuse_controller_test.rb
index b00fbf1..62bc866 100644
--- a/services/api/test/functional/arvados/v1/job_reuse_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/job_reuse_controller_test.rb
@@ -1,11 +1,11 @@
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 class Arvados::V1::JobReuseControllerTest < ActionController::TestCase
   fixtures :repositories, :users, :jobs, :links, :collections
 
   # See git_setup.rb for the commit log for test.git.tar
-  include GitSetup
+  include GitTestHelper
 
   setup do
     @controller = Arvados::V1::JobsController.new
diff --git a/services/api/test/functional/arvados/v1/jobs_controller_test.rb b/services/api/test/functional/arvados/v1/jobs_controller_test.rb
index 0188bd4..86b4595 100644
--- a/services/api/test/functional/arvados/v1/jobs_controller_test.rb
+++ b/services/api/test/functional/arvados/v1/jobs_controller_test.rb
@@ -1,9 +1,9 @@
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 class Arvados::V1::JobsControllerTest < ActionController::TestCase
 
-  include GitSetup
+  include GitTestHelper
 
   test "submit a job" do
     authorize_with :active
diff --git a/services/api/test/helpers/git_test_helper.rb b/services/api/test/helpers/git_test_helper.rb
new file mode 100644
index 0000000..39e506f
--- /dev/null
+++ b/services/api/test/helpers/git_test_helper.rb
@@ -0,0 +1,30 @@
+require 'fileutils'
+require 'tmpdir'
+
+# Commit log for "foo" repository in test.git.tar
+# master is the main branch
+# b1 is a branch off of master
+# tag1 is a tag
+#
+# 1de84a8 * b1
+# 077ba2a * master
+# 4fe459a * tag1
+# 31ce37f * foo
+
+module GitTestHelper
+  def self.included base
+    base.setup do
+      @tmpdir = Dir.mktmpdir()
+      `cp test/test.git.tar #{@tmpdir} && cd #{@tmpdir} && tar xf test.git.tar`
+      @orig_git_repositories_dir = Rails.configuration.git_repositories_dir
+      Rails.configuration.git_repositories_dir = "#{@tmpdir}/test"
+      Commit.refresh_repositories
+    end
+
+    base.teardown do
+      FileUtils.remove_entry @tmpdir, true
+      Rails.configuration.git_repositories_dir = @orig_git_repositories_dir
+      Commit.refresh_repositories
+    end
+  end
+end
diff --git a/services/api/test/integration/crunch_dispatch_test.rb b/services/api/test/integration/crunch_dispatch_test.rb
index ee1bd9f..81767af 100644
--- a/services/api/test/integration/crunch_dispatch_test.rb
+++ b/services/api/test/integration/crunch_dispatch_test.rb
@@ -1,8 +1,8 @@
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 class CrunchDispatchTest < ActionDispatch::IntegrationTest
-  include GitSetup
+  include GitTestHelper
 
   fixtures :all
 
diff --git a/services/api/test/integration/serialized_encoding_test.rb b/services/api/test/integration/serialized_encoding_test.rb
index 269018d..8a1cb10 100644
--- a/services/api/test/integration/serialized_encoding_test.rb
+++ b/services/api/test/integration/serialized_encoding_test.rb
@@ -1,8 +1,8 @@
 require 'test_helper'
-load 'test/functional/arvados/v1/git_setup.rb'
+require 'helpers/git_test_helper'
 
 class SerializedEncodingTest < ActionDispatch::IntegrationTest
-  include GitSetup
+  include GitTestHelper
 
   fixtures :all
 
diff --git a/services/api/test/unit/job_test.rb b/services/api/test/unit/job_test.rb
index 5f53b2a..e1ca7c5 100644
--- a/services/api/test/unit/job_test.rb
+++ b/services/api/test/unit/job_test.rb
@@ -1,15 +1,27 @@
 require 'test_helper'
+require 'helpers/git_test_helper'
 
 class JobTest < ActiveSupport::TestCase
+  include GitTestHelper
+
   BAD_COLLECTION = "#{'f' * 32}+0"
 
   setup do
     set_user_from_auth :active
   end
 
+  def job_attrs merge_me={}
+    # Default (valid) set of attributes, with given overrides
+    {
+      script: "hash",
+      script_version: "master",
+      repository: "foo",
+    }.merge(merge_me)
+  end
+
   test "Job without Docker image doesn't get locator" do
-    job = Job.new
-    assert job.valid?
+    job = Job.new job_attrs
+    assert job.valid?, job.errors.full_messages.to_s
     assert_nil job.docker_image_locator
   end
 
@@ -19,55 +31,58 @@ class JobTest < ActiveSupport::TestCase
   }.each_pair do |spec_type, (fixture_type, fixture_name, fixture_attr)|
     test "Job initialized with Docker image #{spec_type} gets locator" do
       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
-      job = Job.new(runtime_constraints: {'docker_image' => image_spec})
-      assert(job.valid?, "Docker image #{spec_type} was invalid")
+      job = Job.new job_attrs(runtime_constraints:
+                              {'docker_image' => image_spec})
+      assert job.valid?, job.errors.full_messages.to_s
       assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
     end
 
     test "Job modified with Docker image #{spec_type} gets locator" do
-      job = Job.new
-      assert job.valid?
+      job = Job.new job_attrs
+      assert job.valid?, job.errors.full_messages.to_s
       assert_nil job.docker_image_locator
       image_spec = send(fixture_type, fixture_name).send(fixture_attr)
       job.runtime_constraints['docker_image'] = image_spec
-      assert(job.valid?, "modified Docker image #{spec_type} was invalid")
+      assert job.valid?, job.errors.full_messages.to_s
       assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
     end
   end
 
   test "removing a Docker runtime constraint removes the locator" do
     image_locator = collections(:docker_image).uuid
-    job = Job.new(runtime_constraints: {'docker_image' => image_locator})
-    assert job.valid?
+    job = Job.new job_attrs(runtime_constraints:
+                            {'docker_image' => image_locator})
+    assert job.valid?, job.errors.full_messages.to_s
     assert_equal(image_locator, job.docker_image_locator)
     job.runtime_constraints = {}
-    assert(job.valid?, "clearing runtime constraints made the Job invalid")
+    assert job.valid?, job.errors.full_messages.to_s + "after clearing runtime constraints"
     assert_nil job.docker_image_locator
   end
 
   test "locate a Docker image with a repository + tag" do
     image_repo, image_tag =
       links(:docker_image_collection_tag2).name.split(':', 2)
-    job = Job.new(runtime_constraints:
-                  {'docker_image' => image_repo,
-                    'docker_image_tag' => image_tag})
-    assert(job.valid?, "Job with Docker tag search invalid")
+    job = Job.new job_attrs(runtime_constraints:
+                            {'docker_image' => image_repo,
+                              'docker_image_tag' => image_tag})
+    assert job.valid?, job.errors.full_messages.to_s
     assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
   end
 
   test "can't locate a Docker image with a nonexistent tag" do
     image_repo = links(:docker_image_collection_repository).name
     image_tag = '__nonexistent tag__'
-    job = Job.new(runtime_constraints:
-                  {'docker_image' => image_repo,
-                    'docker_image_tag' => image_tag})
+    job = Job.new job_attrs(runtime_constraints:
+                            {'docker_image' => image_repo,
+                              'docker_image_tag' => image_tag})
     assert(job.invalid?, "Job with bad Docker tag valid")
   end
 
   test "locate a Docker image with a partial hash" do
     image_hash = links(:docker_image_collection_hash).name[0..24]
-    job = Job.new(runtime_constraints: {'docker_image' => image_hash})
-    assert(job.valid?, "Job with partial Docker image hash failed")
+    job = Job.new job_attrs(runtime_constraints:
+                            {'docker_image' => image_hash})
+    assert job.valid?, job.errors.full_messages.to_s + " with partial hash #{image_hash}"
     assert_equal(collections(:docker_image).uuid, job.docker_image_locator)
   end
 
@@ -76,20 +91,21 @@ class JobTest < ActiveSupport::TestCase
     'locator' => BAD_COLLECTION,
   }.each_pair do |spec_type, image_spec|
     test "Job validation fails with nonexistent Docker image #{spec_type}" do
-      job = Job.new(runtime_constraints: {'docker_image' => image_spec})
+      job = Job.new job_attrs(runtime_constraints:
+                              {'docker_image' => image_spec})
       assert(job.invalid?, "nonexistent Docker image #{spec_type} was valid")
     end
   end
 
   test "Job validation fails with non-Docker Collection constraint" do
-    job = Job.new(runtime_constraints:
-                  {'docker_image' => collections(:foo_file).uuid})
+    job = Job.new job_attrs(runtime_constraints:
+                            {'docker_image' => collections(:foo_file).uuid})
     assert(job.invalid?, "non-Docker Collection constraint was valid")
   end
 
   test "can't create Job with Docker image locator" do
     begin
-      job = Job.new(docker_image_locator: BAD_COLLECTION)
+      job = Job.new job_attrs(docker_image_locator: BAD_COLLECTION)
     rescue ActiveModel::MassAssignmentSecurity::Error
       # Test passes - expected attribute protection
     else
@@ -98,7 +114,7 @@ class JobTest < ActiveSupport::TestCase
   end
 
   test "can't assign Docker image locator to Job" do
-    job = Job.new
+    job = Job.new job_attrs
     begin
       Job.docker_image_locator = BAD_COLLECTION
     rescue NoMethodError
@@ -106,4 +122,29 @@ class JobTest < ActiveSupport::TestCase
     end
     assert_nil job.docker_image_locator
   end
+
+  [
+   {script_parameters: ""},
+   {script_parameters: []},
+   {script_parameters: {symbols: :are_not_allowed_here}},
+   {runtime_constraints: ""},
+   {runtime_constraints: []},
+   {tasks_summary: ""},
+   {tasks_summary: []},
+   {script_version: "no/branch/could/ever/possibly/have/this/name"},
+  ].each do |invalid_attrs|
+    test "validation failures set error messages: #{invalid_attrs.to_json}" do
+      # Ensure valid_attrs doesn't produce errors -- otherwise we will
+      # not know whether errors reported below are actually caused by
+      # invalid_attrs.
+      dummy = Job.create! job_attrs
+
+      job = Job.create job_attrs(invalid_attrs)
+      assert_raises(ActiveRecord::RecordInvalid, ArgumentError,
+                    "save! did not raise the expected exception") do
+        job.save!
+      end
+      assert_not_empty job.errors, "validation failure did not provide errors"
+    end
+  end
 end

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list