[ARVADOS] updated: 6b17ef224b600b3ce889546d648df43d8aea81f4

git at public.curoverse.com git at public.curoverse.com
Tue Sep 30 17:00:59 EDT 2014


Summary of changes:
 services/api/app/models/job.rb     | 28 ++++++++++++++++++--------
 services/api/test/unit/job_test.rb | 41 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 9 deletions(-)

       via  6b17ef224b600b3ce889546d648df43d8aea81f4 (commit)
      from  2873926cdbfc8012b276db11d24cea3ad6a4bdd4 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit 6b17ef224b600b3ce889546d648df43d8aea81f4
Author: Peter Amstutz <peter.amstutz at curoverse.com>
Date:   Tue Sep 30 16:56:52 2014 -0400

    3859: Cleaned up validate_state_change.  Added unit test for job locking.

diff --git a/services/api/app/models/job.rb b/services/api/app/models/job.rb
index 19a8a6c..72cd381 100644
--- a/services/api/app/models/job.rb
+++ b/services/api/app/models/job.rb
@@ -330,17 +330,29 @@ class Job < ArvadosModel
   end
 
   def validate_state_change
+    ok = true
     if self.state_changed?
-      if self.state_was.in? [Complete, Failed, Cancelled]
-        # Once in a finished state, don't permit any changes
+      ok = case self.state_was
+           when nil
+             # state isn't set yet
+             true
+           when Queued
+             # Permit going from queued to any state
+             true
+           when Running
+             # From running, may only transition to a finished state
+             [Complete, Failed, Cancelled].include? self.state
+           when Complete, Failed, Cancelled
+             # Once in a finished state, don't permit any more state changes
+             false
+           else
+             # Any other state transition is also invalid
+             false
+           end
+      if not ok
         errors.add :state, "invalid change from #{self.state_was} to #{self.state}"
-        return false
-      elsif self.state_was == Running and not self.state.in? [Complete, Failed, Cancelled]
-        # From running, can only transition to a finished state
-        errors.add :state, "invalid change from #{self.state_was} to #{self.state}"
-        return false
       end
     end
-    true
+    ok
   end
 end
diff --git a/services/api/test/unit/job_test.rb b/services/api/test/unit/job_test.rb
index a70c56f..2fce928 100644
--- a/services/api/test/unit/job_test.rb
+++ b/services/api/test/unit/job_test.rb
@@ -169,7 +169,6 @@ class JobTest < ActiveSupport::TestCase
     [['running', true, [['state', 'Running']]], ['success', false, [['state', 'Failed']]]],
     [['running', true, [['state', 'Running']]], ['state', 'Complete', [['success', true],['finished_at', 'not_nil']]]],
     [['running', true, [['state', 'Running']]], ['state', 'Failed', [['success', false],['finished_at', 'not_nil']]]],
-    [['running', true, [['state', 'Running']]], ['running', false, [['state', 'Queued']]]],
     [['cancelled_at', Time.now, [['state', 'Cancelled']]], ['success', false, [['state', 'Cancelled'],['finished_at', 'nil'], ['cancelled_at', 'not_nil']]]],
     [['cancelled_at', Time.now, [['state', 'Cancelled'],['running', false]]], ['success', true, [['state', 'Cancelled'],['running', false],['finished_at', 'nil'],['cancelled_at', 'not_nil']]]],
     # potential migration cases
@@ -207,4 +206,44 @@ class JobTest < ActiveSupport::TestCase
     end
   end
 
+  test "Test job locking" do
+    set_user_from_auth :active_trustedclient
+    job = Job.create! job_attrs
+
+    assert_equal "Queued", job.state
+
+    # Should be able to lock successfully
+    job.lock current_user.uuid
+    assert_equal "Running", job.state
+
+    assert_raises ArvadosModel::ConflictError do
+      # Can't lock it again
+      job.lock current_user.uuid
+    end
+    job.reload
+    assert_equal "Running", job.state
+
+    set_user_from_auth :project_viewer
+    assert_raises ArvadosModel::ConflictError do
+      # Can't lock it as a different user either
+      job.lock current_user.uuid
+    end
+    job.reload
+    assert_equal "Running", job.state
+
+    assert_raises ArvadosModel::PermissionDeniedError do
+      # Can't update fields as a different user
+      job.update_attributes(state: "Failed")
+    end
+    job.reload
+    assert_equal "Running", job.state
+
+
+    set_user_from_auth :active_trustedclient
+
+    # Can update fields as the locked_by user
+    job.update_attributes(state: "Failed")
+    assert_equal "Failed", job.state
+  end
+
 end

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


hooks/post-receive
-- 




More information about the arvados-commits mailing list